<commit_before># --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from __future__ import absolute_import, division, print_function, unicode_literals

from prompt_toolkit.completion import Completer, Completion

import azclishell.configuration
from azclishell.argfinder import ArgsFinder
from azclishell.command_tree import in_tree
from azclishell.layout import get_scope
from azclishell.util import parse_quotes

from azure.cli.core.parser import AzCliCommandParser
from azure.cli.core._util import CLIError


SELECT_SYMBOL = azclishell.configuration.SELECT_SYMBOL

GLOBAL_PARAM_DESCRIPTIONS = {
    '--verbose' : 'Increase logging verbosity. Use --debug for full debug logs.',
    '--debug' : 'Increase logging verbosity to show all debug logs.',
    '--output' : 'Output format',
    '-o' : 'Output format'
}
OUTPUT_CHOICES = ['json', 'tsv', 'table', 'jsonc']
OUTPUT_OPTIONS = ['--output', '-o']
GLOBAL_PARAM = GLOBAL_PARAM_DESCRIPTIONS.keys()


def dynamic_param_logic(text):
    """ validates parameter values for dynamic completion """
    is_param = False
    started_param = False
    prefix = ""
    param = ""
    txtspt = text.split()
    if txtspt:
        param = txtspt[-1]
        if param.startswith("-"):
            is_param = True
        elif len(txtspt) > 2 and txtspt[-2]\
                and txtspt[-2].startswith('-'):
            is_param = True
            param = txtspt[-2]
            started_param = True
            prefix = txtspt[-1]
    return is_param, started_param, prefix, param


def reformat_cmd(text):
    """ reformat the text to be stripped of noise """
    # remove az if there
    text = text.replace('az', '')
    # disregard defaulting symbols
    if SELECT_SYMBOL['scope'] in text:
        text = text.replace(SELECT_SYMBOL['scope'], "")

    if SELECT_SYMBOL['unscope'] in text:
        text = text.replace(SELECT_SYMBOL['unscope'], "")

    if get_scope():
        text = get_scope() + ' ' + text
    return text


def gen_dyn_completion(comp, started_param, prefix, text):
    """ how to validate and generate completion for dynamic params """
    if len(comp.split()) > 1:
        completion = '\"' + comp + '\"'
    else:
        completion = comp
    if started_param:
        if comp.lower().startswith(prefix.lower()) and comp not in text.split():
            yield Completion(completion, -len(prefix))
    else:
        yield Completion(completion, -len(prefix))


def sort_completions(gen):
    """ sorts the completions """
    return sorted(list(gen), key=lambda a: a.text)


# pylint: disable=too-many-instance-attributes
class AzCompleter(Completer):
    """ Completes Azure CLI commands """<commit_msg>Lexicographically sort the parameters with priority to required ones (#68)<commit_after># --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from __future__ import absolute_import, division, print_function, unicode_literals

from prompt_toolkit.completion import Completer, Completion

import azclishell.configuration
from azclishell.argfinder import ArgsFinder
from azclishell.command_tree import in_tree
from azclishell.layout import get_scope
from azclishell.util import parse_quotes

from azure.cli.core.parser import AzCliCommandParser
from azure.cli.core._util import CLIError


SELECT_SYMBOL = azclishell.configuration.SELECT_SYMBOL

GLOBAL_PARAM_DESCRIPTIONS = {
    '--verbose' : 'Increase logging verbosity. Use --debug for full debug logs.',
    '--debug' : 'Increase logging verbosity to show all debug logs.',
    '--output' : 'Output format',
    '-o' : 'Output format'
}
OUTPUT_CHOICES = ['json', 'tsv', 'table', 'jsonc']
OUTPUT_OPTIONS = ['--output', '-o']
GLOBAL_PARAM = GLOBAL_PARAM_DESCRIPTIONS.keys()


def dynamic_param_logic(text):
    """ validates parameter values for dynamic completion """
    is_param = False
    started_param = False
    prefix = ""
    param = ""
    txtspt = text.split()
    if txtspt:
        param = txtspt[-1]
        if param.startswith("-"):
            is_param = True
        elif len(txtspt) > 2 and txtspt[-2]\
                and txtspt[-2].startswith('-'):
            is_param = True
            param = txtspt[-2]
            started_param = True
            prefix = txtspt[-1]
    return is_param, started_param, prefix, param


def reformat_cmd(text):
    """ reformat the text to be stripped of noise """
    # remove az if there
    text = text.replace('az', '')
    # disregard defaulting symbols
    if SELECT_SYMBOL['scope'] in text:
        text = text.replace(SELECT_SYMBOL['scope'], "")

    if SELECT_SYMBOL['unscope'] in text:
        text = text.replace(SELECT_SYMBOL['unscope'], "")

    if get_scope():
        text = get_scope() + ' ' + text
    return text


def gen_dyn_completion(comp, started_param, prefix, text):
    """ how to validate and generate completion for dynamic params """
    if len(comp.split()) > 1:
        completion = '\"' + comp + '\"'
    else:
        completion = comp
    if started_param:
        if comp.lower().startswith(prefix.lower()) and comp not in text.split():
            yield Completion(completion, -len(prefix))
    else:
        yield Completion(completion, -len(prefix))


def sort_completions(gen):
    """ sorts the completions """

    def _get_weight(val):
        """ weights the completions with required things first the lexicographically"""
        priority = ''
        if val.display_meta and val.display_meta.startswith('[REQUIRED]'):
            priority = ' '  # a space has the lowest ordinance
        return priority + val.text

    return sorted(list(gen), key=_get_weight)


# pylint: disable=too-many-instance-attributes
class AzCompleter(Completer):
    """ Completes Azure CLI commands """
{'fraction_non_alphanumeric': 0.16404136833731106, 'fraction_numerical': 0.002545743834526651, 'mean_word_length': 2.5354330708661417, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3595478', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1718, 'n_words': 505}