<commit_before>"""This module contains a class for clang binary based completion.

Attributes:
    log (logging.Logger): logger for this module
"""
import re
import sublime
import time
import logging

from os import path

from ..tools import Tools
from ..tools import SublBridge
from .base_complete import BaseCompleter
from .compiler_variant import ClangCompilerVariant
from .compiler_variant import ClangClCompilerVariant

log = logging.getLogger(__name__)
log.debug(" reloading module")


class Completer(BaseCompleter):
    """Encapsulates completions based on the output from clang_binary.

    Attributes:

        clang_binary (str): e.g. "clang++" or "clang++-3.6"
        flags_dict (dict): compilation flags lists for each view
        std_flag (TYPE): std flag, e.g. "std=c++11"

        completions (list): current completions
        compl_regex (regex): regex to parse raw completion for name and content
        compl_content_regex (regex): regex to parse the content of the
        completion opts_regex (regex): regex to detect optional parameters
        triggers

        group_params (str): string for a group to capture function parameters
        group_types (str): string for a group to capture type names
        group_opts (str): string for a group to capture optional parameters

        PARAM_CHARS (str): chars allowed to be part of function or type
        PARAM_TAG (str): function params tag for convenience
        TYPE_TAG (str): type name tag for convenience

    """
    name = "bin"
    clang_binary = None

    PARAM_TAG = "param"
    TYPE_TAG = "type"
    PARAM_CHARS = "\w\s\*\&\<\>:,\(\)\$\{\}!_\."
    TYPE_CHARS = "\w\s\*\&\<\>:,\(\)\$\{\}\[\]!"
    group_params = "(?P<{param_tag}>[{param_chars}]+)".format(
        param_chars=PARAM_CHARS,
        param_tag=PARAM_TAG)
    group_types = "(?P<{type_tag}>[{type_chars}]+)".format(
        type_tag=TYPE_TAG,
        type_chars=TYPE_CHARS)

    compl_str_mask = "{complete_flag}={file}:{row}:{col}"

    compl_regex = re.compile("COMPLETION:\s(?P<name>.*)\s:\s(?P<content>.*)")
    compl_content_regex = re.compile(
        "\<#{group_params}#\>|\[#{group_types}#\]".format(
            group_params=group_params, group_types=group_types))

    opts_regex = re.compile("{#|#}")

    def __init__(self, clang_binary, version_str):
        """Initialize the Completer.

        Args:
            clang_binary (str): string for clang binary e.g. 'clang-3.8++'
            version_str (str): string for clang version e.g. '3.8.0'

        """
        # init common completer interface
        super().__init__(clang_binary, version_str)
        self.clang_binary = clang_binary

        # Create compiler options of specific variant of the compiler.
        filename = path.splitext(path.basename(clang_binary))[0]
        if filename.startswith('clang-cl'):
            self.compiler_variant = ClangClCompilerVariant()
        else:
            self.compiler_variant = ClangCompilerVariant()

    def complete(self, completion_request):
        """Called asynchronously to create a list of autocompletions.

        It builds up a clang command that is then executed
        as a subprocess. The output is parsed for completions """
        log.debug(" completing with cmd command")
        view = completion_request.get_view()
        start = time.time()
        output_text = self.run_clang_command(
            view, "complete", completion_request.get_trigger_position())
        raw_complete = output_text.splitlines()
        end = time.time()
        log.debug(" code complete done in %s seconds", end - start)

        completions = Completer._parse_completions(raw_complete)
        log.debug(' completions: %s' % completions)
        return (completion_request, completions)

    def info(self, tooltip_request):
        """Provide information about object in given location.

        Using the current translation unit it queries libclang for available
        information about cursor.

        Args:
            tooltip_request (tools.ActionRequest): A request for action
                from the plugin.

        Returns:
            (tools.ActionRequest, str): completion request along with the
                info details read from the translation unit.
        """
        # This is a dummy implementation.
        msg = """
        EasyClangComplete:
        "use_libclang" is false
        "show_type_info" is true.

        Unfortunately, there is no way to show type info
        if you are not using libclang.

        Please use libclang or set "show_type_info" to false.
        """
        sublime.error_message(msg)

    def update(self, view, settings):
        """Update build for current view.

        Args:
            view (sublime.View): this view
            show_errors (TYPE): do we need to show errors? If not this is a
                dummy function as we gain nothing from building it with binary.

        """
        show_errors = settings.errors_on_save
        if not show_errors:
            # in this class there is no need to rebuild the file. It brings no
            # benefits. We only want to do it if we need to show errors.
            return False

        start = time.time()
        output_text = self.run_clang_command(view, "update")
        end = time.time()
        log.debug(" rebuilding done in %s seconds", end - start)

        if show_errors:
            self.show_errors(view, output_text)

    def run_clang_command(self, view, task_type, cursor_pos=0):
        """Construct and run clang command based on task.
<commit_msg>Fix test error<commit_after>"""This module contains a class for clang binary based completion.

Attributes:
    log (logging.Logger): logger for this module
"""
import re
import sublime
import time
import logging

from os import path

from ..tools import Tools
from ..tools import SublBridge
from .base_complete import BaseCompleter
from .compiler_variant import ClangCompilerVariant
from .compiler_variant import ClangClCompilerVariant

log = logging.getLogger(__name__)
log.debug(" reloading module")


class Completer(BaseCompleter):
    """Encapsulates completions based on the output from clang_binary.

    Attributes:

        clang_binary (str): e.g. "clang++" or "clang++-3.6"
        flags_dict (dict): compilation flags lists for each view
        std_flag (TYPE): std flag, e.g. "std=c++11"

        completions (list): current completions
        compl_regex (regex): regex to parse raw completion for name and content
        compl_content_regex (regex): regex to parse the content of the
        completion opts_regex (regex): regex to detect optional parameters
        triggers

        group_params (str): string for a group to capture function parameters
        group_types (str): string for a group to capture type names
        group_opts (str): string for a group to capture optional parameters

        PARAM_CHARS (str): chars allowed to be part of function or type
        PARAM_TAG (str): function params tag for convenience
        TYPE_TAG (str): type name tag for convenience

    """
    name = "bin"
    clang_binary = None

    PARAM_TAG = "param"
    TYPE_TAG = "type"
    PARAM_CHARS = "\w\s\*\&\<\>:,\(\)\$\{\}!_\."
    TYPE_CHARS = "\w\s\*\&\<\>:,\(\)\$\{\}\[\]!"
    group_params = "(?P<{param_tag}>[{param_chars}]+)".format(
        param_chars=PARAM_CHARS,
        param_tag=PARAM_TAG)
    group_types = "(?P<{type_tag}>[{type_chars}]+)".format(
        type_tag=TYPE_TAG,
        type_chars=TYPE_CHARS)

    compl_str_mask = "{complete_flag}={file}:{row}:{col}"

    compl_regex = re.compile("COMPLETION:\s(?P<name>.*)\s:\s(?P<content>.*)")
    compl_content_regex = re.compile(
        "\<#{group_params}#\>|\[#{group_types}#\]".format(
            group_params=group_params, group_types=group_types))

    opts_regex = re.compile("{#|#}")

    def __init__(self, clang_binary, version_str):
        """Initialize the Completer.

        Args:
            clang_binary (str): string for clang binary e.g. 'clang-3.8++'
            version_str (str): string for clang version e.g. '3.8.0'

        """
        # init common completer interface
        super().__init__(clang_binary, version_str)
        self.clang_binary = clang_binary

        # Create compiler options of specific variant of the compiler.
        filename = path.splitext(path.basename(clang_binary))[0]
        if filename.startswith('clang-cl'):
            self.compiler_variant = ClangClCompilerVariant()
        else:
            self.compiler_variant = ClangCompilerVariant()

    def complete(self, completion_request):
        """Called asynchronously to create a list of autocompletions.

        It builds up a clang command that is then executed
        as a subprocess. The output is parsed for completions """
        log.debug(" completing with cmd command")
        view = completion_request.get_view()
        start = time.time()
        output_text = self.run_clang_command(
            view, "complete", completion_request.get_trigger_position())
        raw_complete = output_text.splitlines()
        end = time.time()
        log.debug(" code complete done in %s seconds", end - start)

        completions = Completer._parse_completions(raw_complete)
        log.debug(' completions: %s' % completions)
        return (completion_request, completions)

    def info(self, tooltip_request):
        """Provide information about object in given location.

        Using the current translation unit it queries libclang for available
        information about cursor.

        Args:
            tooltip_request (tools.ActionRequest): A request for action
                from the plugin.

        Returns:
            (tools.ActionRequest, str): completion request along with the
                info details read from the translation unit.
        """
        # This is a dummy implementation.
        msg = """
        EasyClangComplete:
        "use_libclang" is false
        "show_type_info" is true.

        Unfortunately, there is no way to show type info
        if you are not using libclang.

        Please use libclang or set "show_type_info" to false.
        """
        sublime.error_message(msg)

    def update(self, view, settings):
        """Update build for current view.

        Args:
            view (sublime.View): this view
            show_errors (TYPE): do we need to show errors? If not this is a
                dummy function as we gain nothing from building it with binary.

        """
        show_errors = settings.errors_on_save
        if not show_errors:
            # in this class there is no need to rebuild the file. It brings no
            # benefits. We only want to do it if we need to show errors.
            return False

        start = time.time()
        output_text = self.run_clang_command(view, "update")
        end = time.time()
        log.debug(" rebuilding done in %s seconds", end - start)

        if show_errors:
            self.show_errors(view, output_text,
                             settings.show_phantoms_for_errors)

    def run_clang_command(self, view, task_type, cursor_pos=0):
        """Construct and run clang command based on task.

{'fraction_non_alphanumeric': 0.09696105516442385, 'fraction_numerical': 0.0019606095713394526, 'mean_word_length': 2.5693384223918576, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '344110', 'n_tokens_mistral': 3187, 'n_tokens_neox': 3092, 'n_words': 1021}