1
|
|
|
import yaml |
|
|
|
|
2
|
|
|
|
3
|
|
|
import sublime |
|
|
|
|
4
|
|
|
|
5
|
|
|
from .. import logger |
|
|
|
|
6
|
|
|
from .levenshtein import levenshtein |
7
|
|
|
from .yaml_content_reader import YamlContentReader |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class SkinSectionAutoCompleter(YamlContentReader): |
|
|
|
|
11
|
|
|
|
12
|
|
|
def __get_completions(self): |
|
|
|
|
13
|
|
|
try: |
14
|
|
|
section_content = self._get_yaml_content("completion/", "section.yaml") |
|
|
|
|
15
|
|
|
section = yaml.load(section_content) |
16
|
|
|
|
17
|
|
|
return section |
18
|
|
|
|
19
|
|
|
except yaml.YAMLError as error: |
20
|
|
|
logger.error(__file__, "__get_completions(self)", error) |
21
|
|
|
return [] |
22
|
|
|
|
23
|
|
|
def __get_compiled_key_completions(self, options): |
|
|
|
|
24
|
|
|
keys = [] |
25
|
|
|
for option in options: |
26
|
|
|
title = option['title'] + "\t" + option['hint'] |
27
|
|
|
|
28
|
|
|
if 'value' in option: |
29
|
|
|
result = option['value'] |
30
|
|
|
else: |
31
|
|
|
result = option['title'] |
32
|
|
|
|
33
|
|
|
pair = (title, result) |
34
|
|
|
keys.append(pair) |
35
|
|
|
|
36
|
|
|
return keys |
37
|
|
|
|
38
|
|
|
# only show our completion list because nothing else makes sense in this context |
|
|
|
|
39
|
|
|
flags = sublime.INHIBIT_EXPLICIT_COMPLETIONS | sublime.INHIBIT_WORD_COMPLETIONS |
|
|
|
|
40
|
|
|
|
41
|
|
|
all_completions = None |
42
|
|
|
all_key_completions = None |
43
|
|
|
|
44
|
|
View Code Duplication |
def get_key_context_completion(self, prefix, line_content, sections): |
|
|
|
|
45
|
|
|
# if section.casefold() != "Metadata".casefold(): |
46
|
|
|
# return None |
47
|
|
|
|
48
|
|
|
# use lazy initialization because else the API is not available yet |
49
|
|
|
if not self.all_completions: |
50
|
|
|
self.all_completions = self.__get_completions() |
51
|
|
|
self.all_key_completions = self.__get_compiled_key_completions(self.all_completions) |
|
|
|
|
52
|
|
|
|
53
|
|
|
# filter by already existing keys |
54
|
|
|
completions = [] |
55
|
|
|
|
56
|
|
|
for completion in self.all_key_completions: |
57
|
|
|
# trigger is not used here |
58
|
|
|
_, content = completion |
59
|
|
|
|
60
|
|
|
contained = 0 |
61
|
|
|
# value not used here |
62
|
|
|
for section in sections: |
63
|
|
|
if section.casefold() == content.casefold(): |
64
|
|
|
contained = 1 |
65
|
|
|
break |
66
|
|
|
|
67
|
|
|
if contained == 0: |
68
|
|
|
completions.append(completion) |
69
|
|
|
|
70
|
|
|
# no results, means all keys are used up |
71
|
|
|
if not completions: |
72
|
|
|
return None |
73
|
|
|
|
74
|
|
|
# only show sorted by distance if something was already typed because distance to empty string makes no sense |
|
|
|
|
75
|
|
|
if line_content != "": |
76
|
|
|
# sort by levenshtein distance |
77
|
|
|
sorted_completions = sorted(completions, key=lambda completion: levenshtein(completion[1], prefix)) |
|
|
|
|
78
|
|
|
return sorted_completions, self.flags |
79
|
|
|
else: |
80
|
|
|
return completions, self.flags |
81
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.