1
|
|
|
"""This module is about the skin/rainmeter section handling.""" |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
import yaml |
5
|
|
|
|
6
|
|
|
import sublime |
7
|
|
|
|
8
|
|
|
# import own libs |
9
|
|
|
from ... import logger |
10
|
|
|
from ..levenshtein import levenshtein |
11
|
|
|
from ..compiler import compile_keys, compile_values |
12
|
|
|
from ..yaml_content_reader import YamlContentReader |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class SkinRainmeterSectionAutoComplete(YamlContentReader): # pylint: disable=R0903; only provide one method |
16
|
|
|
"""This uses the provided YAML files to extract the possible completions.""" |
17
|
|
|
|
18
|
|
|
def __get_completions(self): |
19
|
|
|
try: |
20
|
|
|
rainmeter_section_content = self._get_yaml_content( |
21
|
|
|
"completion/skin/", |
22
|
|
|
"rainmeter_section.yaml" |
23
|
|
|
) |
24
|
|
|
skin_rainmeter_section = yaml.load(rainmeter_section_content) |
25
|
|
|
|
26
|
|
|
general_image_options_content = self._get_yaml_content( |
27
|
|
|
"completion/meter/", |
28
|
|
|
"general_image_options.yaml" |
29
|
|
|
) |
30
|
|
|
meters_general_image_options = yaml.load(general_image_options_content) |
31
|
|
|
|
32
|
|
|
skin_rainmeter_section.extend(meters_general_image_options) |
33
|
|
|
|
34
|
|
|
return skin_rainmeter_section |
35
|
|
|
|
36
|
|
|
except yaml.YAMLError as error: |
37
|
|
|
logger.error(error) |
38
|
|
|
return [] |
39
|
|
|
|
40
|
|
|
# only show our completion list because nothing else makes sense in this context |
41
|
|
|
flags = sublime.INHIBIT_EXPLICIT_COMPLETIONS | sublime.INHIBIT_WORD_COMPLETIONS |
42
|
|
|
|
43
|
|
|
all_completions = None |
44
|
|
|
all_key_completions = None |
45
|
|
|
all_value_completions = None |
46
|
|
|
|
47
|
|
|
def get_key_context_completion(self, prefix, line_content, section, keyvalues): |
48
|
|
|
"""Get a list of keys for the current context.""" |
49
|
|
|
if section.casefold() != "Rainmeter".casefold(): |
50
|
|
|
return None |
51
|
|
|
|
52
|
|
|
# use lazy initialization because else the API is not available yet |
53
|
|
|
if not self.all_completions: |
54
|
|
|
self.all_completions = self.__get_completions() |
55
|
|
|
self.all_key_completions = compile_keys(self.all_completions) |
56
|
|
|
|
57
|
|
|
# filter by already existing keys |
58
|
|
|
completions = [] |
59
|
|
|
|
60
|
|
|
for completion in self.all_key_completions: |
61
|
|
|
dummy_key, display, content, dummy_unique = completion |
62
|
|
|
|
63
|
|
|
contained = 0 |
64
|
|
|
# value not needed |
65
|
|
|
for key, _ in keyvalues: |
66
|
|
|
if key.casefold() == content.casefold(): |
67
|
|
|
contained = 1 |
68
|
|
|
break |
69
|
|
|
|
70
|
|
|
if contained == 0: |
71
|
|
|
completions.append((display, content)) |
72
|
|
|
|
73
|
|
|
# no results, means all keys are used up |
74
|
|
|
if not completions: |
75
|
|
|
return None |
76
|
|
|
|
77
|
|
|
# only show sorted by distance if something was already typed |
78
|
|
|
# because distance to empty string makes no sense |
79
|
|
|
if line_content != "": |
80
|
|
|
# sort by levenshtein distance |
81
|
|
|
sorted_completions = sorted( |
82
|
|
|
completions, key=lambda c: levenshtein(c[1], prefix) |
83
|
|
|
) |
84
|
|
|
|
85
|
|
|
return sorted_completions, self.flags |
86
|
|
|
else: |
87
|
|
|
return completions, self.flags |
88
|
|
|
|
89
|
|
|
def get_value_context_completion(self, section, key_match): |
90
|
|
|
"""Get context completion for a specified found key.""" |
91
|
|
|
if section != "Rainmeter": |
92
|
|
|
return None |
93
|
|
|
|
94
|
|
|
# use lazy initialization because else the API is not available yet |
95
|
|
|
if not self.all_completions: |
96
|
|
|
self.all_completions = self.__get_completions() |
97
|
|
|
|
98
|
|
|
if not self.all_value_completions: |
99
|
|
|
self.all_value_completions = compile_values(self.all_completions) |
100
|
|
|
|
101
|
|
|
value_completions = self.all_value_completions[key_match] |
102
|
|
|
|
103
|
|
|
if not value_completions: |
104
|
|
|
logger.info("found no Rainmeter value completions for key '" + key_match + "' thus returning None") |
105
|
|
|
return None |
106
|
|
|
else: |
107
|
|
|
return value_completions, self.flags |
108
|
|
|
|