GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b7b9ec...0f5343 )
by thatsIch
57s
created

get_key_context_completion()   B

Complexity

Conditions 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
c 3
b 0
f 0
dl 0
loc 22
rs 8.9197
1
"""This module handles the skin section auto completion.
2
3
This provides the means to automatically add the base rainmeter sections
4
upon auto complete request with:
5
6
* [Rainmeter]
7
* [Metadata]
8
* [Variables]
9
* [Measure]
10
* [Meter]
11
* [MeterStyle]
12
13
This only activates if the file is empty or at least 2 new lines above the current caret.
14
"""
15
16
import yaml
17
18
import sublime
19
20
from .. import logger
0 ignored issues
show
Bug introduced by
The name logger does not seem to exist in module completion.
Loading history...
21
from .levenshtein import levenshtein
22
from .yaml_content_reader import YamlContentReader
23
24
25
class SkinSectionAutoCompleter(YamlContentReader): # pylint: disable=R0903; only provide one method
26
    """Ths class is the logical state holder for the auto completion suggestions.
27
28
    Upon the request the respective yaml file is parsed and converted into a logical
29
    representation of the completions. Depending on the prior information the completions
30
    can be filtered containing less entries.
31
    """
32
33
    def __get_completions(self):
34
        """IO access to the yaml file.
35
36
        Uses a yaml loader to parse it into a python object.
37
        """
38
        try:
39
            section_content = self._get_yaml_content("completion/", "section.yaml")
40
            section = yaml.load(section_content)
41
42
            return section
43
44
        except yaml.YAMLError as error:
45
            logger.error(error)
46
            return []
47
48
    @staticmethod
49
    def __get_compiled_key_completions(options):
50
        """Completion can contain lots of duplicate information.
51
52
        For example the trigger is most of the time also the result.
53
        Only in case of a value attribute is that returned.
54
        It also takes hints into consideration for compilation.
55
        """
56
        keys = []
57
        for option in options:
58
            section = option['title']
59
            display = option['title'] + "\t" + option['hint']
60
61
            if 'value' in option:
62
                result = option['value']
63
            else:
64
                result = option['title']
65
            if 'unique' in option:
66
                unique = option['unique']
67
            else:
68
                unique = False
69
70
            pair = (section, display, result, unique)
71
            keys.append(pair)
72
73
        return keys
74
75
    def __lazy_initialize_completions(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
76
        # use lazy initialization because else the API is not available yet
77
        if not self.all_completions:
78
            self.all_completions = self.__get_completions()
79
            self.all_key_completions = self.__get_compiled_key_completions(self.all_completions)
80
81
    def __filter_completions_by_sec(self, sections):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
82
        # filter by already existing keys
83
        completions = []
84
85
        settings = sublime.load_settings("Rainmeter.sublime-settings")
86
        allow_duplicates = settings.get("allow_completion_section_duplicates", False)
87
88
        for completion in self.all_key_completions:
89
            # trigger is not used here
90
            section_id, display, content, unique = completion
91
92
            # we only need to search for duplicates
93
            # if we are having a unique section like [Rainmeter]
94
            # and not allow duplicates
95
            if unique and not allow_duplicates:
96
                contained = 0
97
                # value not used here
98
                for section in sections:
99
                    if section.casefold() == section_id.casefold():
100
                        contained = 1
101
                        break
102
103
                if contained == 0:
104
                    completions.append((display, content))
105
            else:
106
                completions.append((display, content))
107
108
        return completions
109
110
    # only show our completion list because nothing else makes sense in this context
111
    flags = sublime.INHIBIT_EXPLICIT_COMPLETIONS | sublime.INHIBIT_WORD_COMPLETIONS
112
113
    all_completions = None
114
    all_key_completions = None
115
116
    def get_key_context_completion(self, prefix, line_content, sections):
117
        """Provide all possible sections without duplicates of unique ones."""
118
        # if section.casefold() != "Metadata".casefold():
119
        #     return None
120
121
        self.__lazy_initialize_completions()
122
        completions = self.__filter_completions_by_sec(sections)
123
        # no results, means all keys are used up
124
        if not completions:
125
            return None
126
127
        # only show sorted by distance if something was already typed
128
        # because distance to empty string makes no sense
129
        if line_content != "":
130
            # sort by levenshtein distance
131
            sorted_completions = sorted(
132
                completions,
133
                key=lambda completion: levenshtein(completion[1], prefix)
134
            )
135
            return sorted_completions, self.flags
136
        else:
137
            return completions, self.flags
138