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 .yaml_content_reader import YamlContentReader |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
class SkinSectionAutoCompleter(YamlContentReader): |
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(__file__, "__get_completions(self)", error) |
46
|
|
|
return [] |
47
|
|
|
|
48
|
|
|
def __get_compiled_key_completions(self, options): |
|
|
|
|
49
|
|
|
"""Completion can contain lots of duplicate information. |
50
|
|
|
|
51
|
|
|
For example the trigger is most of the time also the result. |
52
|
|
|
Only in case of a value attribute is that returned. |
53
|
|
|
It also takes hints into consideration for compilation. |
54
|
|
|
""" |
55
|
|
|
keys = [] |
56
|
|
|
for option in options: |
57
|
|
|
title = option['title'] + "\t" + option['hint'] |
58
|
|
|
|
59
|
|
|
if 'value' in option: |
60
|
|
|
result = option['value'] |
61
|
|
|
else: |
62
|
|
|
result = option['title'] |
63
|
|
|
|
64
|
|
|
pair = (title, result) |
65
|
|
|
keys.append(pair) |
66
|
|
|
|
67
|
|
|
return keys |
68
|
|
|
|
69
|
|
|
def __lazy_initialize_completions(self): |
|
|
|
|
70
|
|
|
# use lazy initialization because else the API is not available yet |
71
|
|
|
if not self.all_completions: |
72
|
|
|
self.all_completions = self.__get_completions() |
73
|
|
|
self.all_key_completions = self.__get_compiled_key_completions(self.all_completions) |
74
|
|
|
|
75
|
|
|
def __filter_completions_by_already_defined_sections(self, sections): |
|
|
|
|
76
|
|
|
# filter by already existing keys |
77
|
|
|
completions = [] |
78
|
|
|
|
79
|
|
|
for completion in self.all_key_completions: |
80
|
|
|
# trigger is not used here |
81
|
|
|
_, content = completion |
82
|
|
|
|
83
|
|
|
contained = 0 |
84
|
|
|
# value not used here |
85
|
|
|
for section in sections: |
86
|
|
|
if section.casefold() == content.casefold(): |
87
|
|
|
contained = 1 |
88
|
|
|
break |
89
|
|
|
|
90
|
|
|
if contained == 0: |
91
|
|
|
completions.append(completion) |
92
|
|
|
|
93
|
|
|
return completions |
94
|
|
|
|
95
|
|
|
# only show our completion list because nothing else makes sense in this context |
96
|
|
|
flags = sublime.INHIBIT_EXPLICIT_COMPLETIONS | sublime.INHIBIT_WORD_COMPLETIONS |
97
|
|
|
|
98
|
|
|
all_completions = None |
99
|
|
|
all_key_completions = None |
100
|
|
|
|
101
|
|
|
def get_key_context_completion(self, prefix, line_content, sections): |
102
|
|
|
"""Provide all possible sections without duplicates of unique ones.""" |
103
|
|
|
# if section.casefold() != "Metadata".casefold(): |
104
|
|
|
# return None |
105
|
|
|
|
106
|
|
|
self.__lazy_initialize_completions() |
107
|
|
|
completions = self.__filter_completions_by_already_defined_sections(sections) |
108
|
|
|
|
109
|
|
|
# no results, means all keys are used up |
110
|
|
|
if not completions: |
111
|
|
|
return None |
112
|
|
|
|
113
|
|
|
# only show sorted by distance if something was already typed |
114
|
|
|
# because distance to empty string makes no sense |
115
|
|
|
if line_content != "": |
116
|
|
|
# sort by levenshtein distance |
117
|
|
|
sorted_completions = sorted( |
118
|
|
|
completions, |
119
|
|
|
key=lambda completion: levenshtein(completion[1], prefix) |
120
|
|
|
) |
121
|
|
|
return sorted_completions, self.flags |
122
|
|
|
else: |
123
|
|
|
return completions, self.flags |
124
|
|
|
|