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 |
21
|
|
|
from .levenshtein import levenshtein |
22
|
|
|
from .compiler import compile_keys |
23
|
|
|
from .yaml_content_reader import YamlContentReader |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
def str_equal_case_ignore(str1, str2): |
27
|
|
|
"""Compare two strings ignoring the case.""" |
28
|
|
|
return str1.casefold() == str2.casefold() |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
def sections_contain_section_id(sections, section_id): |
32
|
|
|
"""Iterates through the sections container and checks if the section_id is in that container.""" |
33
|
|
|
|
34
|
|
|
# value not used here |
35
|
|
|
for section in sections: |
36
|
|
|
if str_equal_case_ignore(section, section_id): |
37
|
|
|
return True |
38
|
|
|
|
39
|
|
|
return False |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
class SkinSectionAutoCompleter(YamlContentReader): # pylint: disable=R0903; only provide one method |
43
|
|
|
|
44
|
|
|
""" |
45
|
|
|
Ths class is the logical state holder for the auto completion suggestions. |
46
|
|
|
|
47
|
|
|
Upon the request the respective yaml file is parsed and converted into a logical |
48
|
|
|
representation of the completions. Depending on the prior information the completions |
49
|
|
|
can be filtered containing less entries. |
50
|
|
|
""" |
51
|
|
|
|
52
|
|
|
def __get_completions(self): |
53
|
|
|
"""IO access to the yaml file. |
54
|
|
|
|
55
|
|
|
Uses a yaml loader to parse it into a python object. |
56
|
|
|
""" |
57
|
|
|
try: |
58
|
|
|
section_content = self._get_yaml_content("completion/", "section.yaml") |
59
|
|
|
section = yaml.load(section_content) |
60
|
|
|
|
61
|
|
|
return section |
62
|
|
|
|
63
|
|
|
except yaml.YAMLError as error: |
64
|
|
|
logger.error(error) |
65
|
|
|
return [] |
66
|
|
|
|
67
|
|
|
def __lazy_initialize_completions(self): |
68
|
|
|
# use lazy initialization because else the API is not available yet |
69
|
|
|
if not self.all_completions: |
70
|
|
|
self.all_completions = self.__get_completions() |
71
|
|
|
self.all_key_completions = compile_keys(self.all_completions) |
72
|
|
|
|
73
|
|
|
def __filter_completions_by_sec(self, sections): |
74
|
|
|
# filter by already existing keys |
75
|
|
|
completions = [] |
76
|
|
|
|
77
|
|
|
settings = sublime.load_settings("Rainmeter.sublime-settings") |
78
|
|
|
allow_duplicates = settings.get("allow_completion_section_duplicates", False) |
79
|
|
|
|
80
|
|
|
for completion in self.all_key_completions: |
81
|
|
|
# trigger is not used here |
82
|
|
|
section_id, display, content, unique = completion |
83
|
|
|
|
84
|
|
|
# we only need to search for duplicates |
85
|
|
|
# if we are having a unique section like [Rainmeter] |
86
|
|
|
# and not allow duplicates |
87
|
|
|
if unique and not allow_duplicates: |
88
|
|
|
contained = sections_contain_section_id(sections, section_id) |
89
|
|
|
|
90
|
|
|
if not contained: |
91
|
|
|
completions.append((display, content)) |
92
|
|
|
else: |
93
|
|
|
completions.append((display, content)) |
94
|
|
|
|
95
|
|
|
return completions |
96
|
|
|
|
97
|
|
|
# only show our completion list because nothing else makes sense in this context |
98
|
|
|
flags = sublime.INHIBIT_EXPLICIT_COMPLETIONS | sublime.INHIBIT_WORD_COMPLETIONS |
99
|
|
|
|
100
|
|
|
all_completions = None |
101
|
|
|
all_key_completions = None |
102
|
|
|
|
103
|
|
|
def get_key_context_completion(self, prefix, line_content, sections): |
104
|
|
|
"""Provide all possible sections without duplicates of unique ones.""" |
105
|
|
|
# if section.casefold() != "Metadata".casefold(): |
106
|
|
|
# return None |
107
|
|
|
|
108
|
|
|
self.__lazy_initialize_completions() |
109
|
|
|
completions = self.__filter_completions_by_sec(sections) |
110
|
|
|
# no results, means all keys are used up |
111
|
|
|
if not completions: |
112
|
|
|
return None |
113
|
|
|
|
114
|
|
|
# only show sorted by distance if something was already typed |
115
|
|
|
# because distance to empty string makes no sense |
116
|
|
|
if line_content != "": |
117
|
|
|
# sort by levenshtein distance |
118
|
|
|
sorted_completions = sorted( |
119
|
|
|
completions, |
120
|
|
|
key=lambda completion: levenshtein(completion[1], prefix) |
121
|
|
|
) |
122
|
|
|
return sorted_completions, self.flags |
123
|
|
|
else: |
124
|
|
|
return completions, self.flags |
125
|
|
|
|