|
1
|
|
|
# python libs |
|
|
|
|
|
|
2
|
|
|
import re |
|
3
|
|
|
|
|
4
|
|
|
# st libs |
|
5
|
|
|
import sublime |
|
6
|
|
|
|
|
7
|
|
|
# own libs |
|
8
|
|
|
from .. import logger |
|
|
|
|
|
|
9
|
|
|
from ..completion.skin.rainmeter_section import SkinRainmeterSectionAutoComplete |
|
|
|
|
|
|
10
|
|
|
from ..completion.skin.metadata_section import SkinMetadataSectionAutoComplete |
|
|
|
|
|
|
11
|
|
|
from ..completion.section import SkinSectionAutoCompleter |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
class ContextSensAutoCompletion(object): |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
# only show our completion list because nothing else makes sense in this context |
|
17
|
|
|
flags = sublime.INHIBIT_EXPLICIT_COMPLETIONS | sublime.INHIBIT_WORD_COMPLETIONS |
|
18
|
|
|
|
|
19
|
|
|
section = None |
|
20
|
|
|
skin_rainmeter_section = None |
|
21
|
|
|
skin_metadata_section = None |
|
22
|
|
|
|
|
23
|
|
|
scope = "source.rainmeter" |
|
24
|
|
|
|
|
25
|
|
|
# comments are specified by ';' |
|
26
|
|
|
comment_exp = re.compile(r'^\s*;.*') |
|
27
|
|
|
|
|
28
|
|
|
# enable searching for [] in multiline environment |
|
29
|
|
|
bracket_expression = re.compile(r'^\s*\[.+\]\s*$', re.MULTILINE) |
|
30
|
|
|
section_expression = re.compile(r'^\s*\[(.+)\]\s*$', re.I) |
|
31
|
|
|
key_expression = re.compile(r'^\s*(.+)\s*\=?\s*(.*?)\s*$', re.MULTILINE) |
|
32
|
|
|
key_value_expression = re.compile(r'^\s*(.+?)\s*\=\s*(.*?)\s*$', re.MULTILINE) |
|
33
|
|
|
|
|
34
|
|
|
def __init__(self): |
|
35
|
|
|
self.section = SkinSectionAutoCompleter() |
|
36
|
|
|
self.skin_rainmeter_section = SkinRainmeterSectionAutoComplete() |
|
37
|
|
|
self.skin_metadata_section = SkinMetadataSectionAutoComplete() |
|
38
|
|
|
|
|
39
|
|
|
def get_lines_of_section_on_cursor(self, view, location): |
|
|
|
|
|
|
40
|
|
|
size = view.size() |
|
41
|
|
|
start_content = view.substr(sublime.Region(0, location)) |
|
42
|
|
|
end_content = view.substr(sublime.Region(location, size)) |
|
43
|
|
|
|
|
44
|
|
|
start_index = self.get_current_section_content_start_index(start_content) |
|
45
|
|
|
end_index = self.get_current_section_content_end_index(end_content, location, size) |
|
46
|
|
|
|
|
47
|
|
|
section = view.substr(sublime.Region(start_index, end_index)) |
|
48
|
|
|
lines = section.splitlines() |
|
49
|
|
|
|
|
50
|
|
|
return lines |
|
51
|
|
|
|
|
52
|
|
|
def get_current_section_content_start_index(self, start_content): |
|
|
|
|
|
|
53
|
|
|
matches = list(self.bracket_expression.finditer(start_content)) |
|
54
|
|
|
|
|
55
|
|
|
if len(matches) > 0: |
|
56
|
|
|
last_match = matches[-1] |
|
57
|
|
|
return last_match.start() |
|
58
|
|
|
|
|
59
|
|
|
# no previous section found, hardly legal but who cares |
|
60
|
|
|
else: |
|
61
|
|
|
return 0 |
|
62
|
|
|
|
|
63
|
|
|
def get_current_section_content_end_index(self, end_content, offset, end_index): |
|
|
|
|
|
|
64
|
|
|
matches = list(self.bracket_expression.finditer(end_content)) |
|
65
|
|
|
if len(matches) > 0: |
|
66
|
|
|
first_match = matches[0] |
|
67
|
|
|
return first_match.start() + offset |
|
68
|
|
|
|
|
69
|
|
|
# no next section found |
|
70
|
|
|
else: |
|
71
|
|
|
return end_index |
|
72
|
|
|
|
|
73
|
|
|
def get_key_value(self, line_content): |
|
|
|
|
|
|
74
|
|
|
key_value_match = self.key_value_expression.search(line_content) |
|
75
|
|
|
if key_value_match: |
|
76
|
|
|
key_match = key_value_match.group(1) |
|
77
|
|
|
value_match = key_value_match.group(2) |
|
78
|
|
|
logger.info(__file__, "on_query_completions", "key/value found in '" + line_content + "' with ('" + key_match + "', '" + value_match + "')") |
|
79
|
|
|
|
|
80
|
|
|
return key_match, value_match |
|
81
|
|
|
|
|
82
|
|
|
key_only_match = self.key_expression.search(line_content) |
|
83
|
|
|
if key_only_match: |
|
84
|
|
|
logger.info(__file__, "on_query_completions", "potential key found in '" + line_content + "'") |
|
85
|
|
|
return key_only_match.group(1), None |
|
86
|
|
|
|
|
87
|
|
|
return None, None |
|
88
|
|
|
|
|
89
|
|
|
def get_key_values(self, lines): |
|
|
|
|
|
|
90
|
|
|
key_values = [] |
|
91
|
|
|
|
|
92
|
|
|
for line in lines: |
|
93
|
|
|
key, value = self.get_key_value(line) |
|
94
|
|
|
if key: |
|
95
|
|
|
key_values.append((key, value)) |
|
96
|
|
|
|
|
97
|
|
|
return key_values |
|
98
|
|
|
|
|
99
|
|
|
def on_query_completions(self, view, prefix, locations): |
|
|
|
|
|
|
100
|
|
|
for location in locations: |
|
101
|
|
|
# ignore non scope |
|
102
|
|
|
if not view.match_selector(location, self.scope): |
|
103
|
|
|
return None |
|
104
|
|
|
|
|
105
|
|
|
# ignore on comment lines |
|
106
|
|
|
cursor_line = view.line(location) |
|
107
|
|
|
line_content = view.substr(cursor_line) |
|
108
|
|
|
if self.comment_exp.search(line_content): |
|
109
|
|
|
logger.info(__file__, "on_query_completions", "found comment") |
|
110
|
|
|
return None |
|
111
|
|
|
|
|
112
|
|
|
# find last occurance of the [] to determine the ini sections |
|
113
|
|
|
lines = self.get_lines_of_section_on_cursor(view, location) |
|
114
|
|
|
# filter empty lines |
|
115
|
|
|
lines = list(filter(None, lines)) |
|
116
|
|
|
# filter comments |
|
117
|
|
|
lines = list(filter(lambda l: not self.comment_exp.search(l), lines)) |
|
118
|
|
|
|
|
119
|
|
|
if not lines: |
|
120
|
|
|
logger.info(__file__, "bootstrap.on_query_completions", "section is empty") |
|
121
|
|
|
size = view.size() |
|
122
|
|
|
content = view.substr(sublime.Region(0, size)) |
|
123
|
|
|
sections = self.bracket_expression.findall(content) |
|
124
|
|
|
|
|
125
|
|
|
return self.section.get_key_context_completion(prefix, line_content, sections) |
|
126
|
|
|
|
|
127
|
|
|
# extract section |
|
128
|
|
|
first_line = lines[0] |
|
129
|
|
|
match = self.section_expression.search(first_line) |
|
130
|
|
|
|
|
131
|
|
|
# no section defined |
|
132
|
|
|
# TODO section suggestion |
|
|
|
|
|
|
133
|
|
|
# if not match: |
|
134
|
|
|
# logger.info(__file__, "on_query_completions", "no section found") |
|
135
|
|
|
# size = view.size() |
|
136
|
|
|
# content = view.substr(sublime.Region(0, size)) |
|
137
|
|
|
# sections = self.bracket_expression.findall(content) |
|
138
|
|
|
|
|
139
|
|
|
# return self.section.get_key_context_completion(prefix, line_content, sections) |
|
140
|
|
|
section = match.group(1) |
|
141
|
|
|
|
|
142
|
|
|
key_match, value_match = self.get_key_value(line_content) |
|
143
|
|
|
key_values = self.get_key_values(lines) |
|
144
|
|
|
|
|
145
|
|
|
if value_match == "": |
|
146
|
|
|
logger.info(__file__, "on_query_completions", "after equal trigger in '" + line_content + "'") |
|
147
|
|
|
# value trigger |
|
148
|
|
|
value_result = self.skin_rainmeter_section.get_value_context_completion(view, prefix, location, line_content, section, key_match, key_values) |
|
149
|
|
|
if value_result: |
|
150
|
|
|
return value_result |
|
151
|
|
|
|
|
152
|
|
|
# only do key completion if we are in the key are |
|
153
|
|
|
# that means in front of the equal or no equal at all |
|
154
|
|
|
else: |
|
155
|
|
|
logger.info(__file__, "on_query_completions", "before equal trigger in '" + line_content + "'") |
|
156
|
|
|
key_result = self.skin_rainmeter_section.get_key_context_completion(view, prefix, location, line_content, section, key_values) |
|
157
|
|
|
if key_result: |
|
158
|
|
|
return key_result |
|
159
|
|
|
|
|
160
|
|
|
key_result = self.skin_metadata_section.get_key_context_completion(prefix, line_content, section, key_values) |
|
161
|
|
|
if key_result: |
|
162
|
|
|
return key_result |
|
163
|
|
|
|
|
164
|
|
|
return None |
|
165
|
|
|
|
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.