-
-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathcompletions.py
More file actions
493 lines (424 loc) · 17.4 KB
/
completions.py
File metadata and controls
493 lines (424 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import logging
import re
from collections import namedtuple
from fnmatch import fnmatch
from sublime_lib.resource_path import ResourcePath
import sublime
import sublime_plugin
from ..lib.scope_data import (
COMMIT_SCOPE_COMPLETION_CMD,
COMPILED_HEADS,
create_scope_suffix_completion
)
from ..lib import syntax_paths
from ..lib import inhibit_word_completions
__all__ = (
'SyntaxDefCompletionsListener',
'PackagedevCommitScopeCompletionCommand'
)
logger = logging.getLogger(__name__)
CompletionTemplate = namedtuple('CompletionTemplate', ['kind', 'format', 'suffix'])
Completion = namedtuple('Completion', ['trigger', 'template', 'details'])
# a list of kinds used to denote the different kinds of completions
TPL_HEADER_BASE = CompletionTemplate(
format=sublime.COMPLETION_FORMAT_TEXT,
kind=(sublime.KIND_ID_NAMESPACE, 'K', 'Header Key'),
suffix=": ",
)
TPL_HEADER_TRUE = CompletionTemplate(
format=sublime.COMPLETION_FORMAT_SNIPPET,
kind=(sublime.KIND_ID_NAMESPACE, 'K', 'Header Key'),
suffix=": ${1:true}",
)
TPL_HEADER_DICT = CompletionTemplate(
format=sublime.COMPLETION_FORMAT_SNIPPET,
kind=(sublime.KIND_ID_NAMESPACE, 'D', 'Header Dict'),
suffix=":\n ",
)
TPL_HEADER_LIST = CompletionTemplate(
format=sublime.COMPLETION_FORMAT_SNIPPET,
kind=(sublime.KIND_ID_NAMESPACE, 'L', 'Header List'),
suffix=":\n - ",
)
TPL_BRANCH = CompletionTemplate(
format=sublime.COMPLETION_FORMAT_SNIPPET,
kind=(sublime.KIND_ID_NAVIGATION, 'b', 'Branch Point'),
suffix=": ",
)
TPL_CONTEXT = CompletionTemplate(
format=sublime.COMPLETION_FORMAT_SNIPPET,
kind=(sublime.KIND_ID_KEYWORD, 'c', 'Context'),
suffix=":\n ",
)
TPL_FUNCTION = CompletionTemplate(
format=sublime.COMPLETION_FORMAT_SNIPPET,
kind=(sublime.KIND_ID_FUNCTION, 'f', 'Function'),
suffix=": ",
)
TPL_FUNCTION_TRUE = CompletionTemplate(
format=sublime.COMPLETION_FORMAT_SNIPPET,
kind=(sublime.KIND_ID_FUNCTION, 'f', 'Function'),
suffix=": ${1:true}",
)
TPL_FUNCTION_FALSE = CompletionTemplate(
format=sublime.COMPLETION_FORMAT_SNIPPET,
kind=(sublime.KIND_ID_FUNCTION, 'f', 'Function'),
suffix=": ${1:false}",
)
TPL_FUNCTION_NUMERIC = CompletionTemplate(
format=sublime.COMPLETION_FORMAT_SNIPPET,
kind=(sublime.KIND_ID_FUNCTION, 'f', 'Function'),
suffix=": ${1:1}",
)
TPL_CAPTURUES = CompletionTemplate(
format=sublime.COMPLETION_FORMAT_SNIPPET,
kind=(sublime.KIND_ID_FUNCTION, 'c', 'Captures'),
suffix=":\n ",
)
TPL_VARIABLE = CompletionTemplate(
format=sublime.COMPLETION_FORMAT_SNIPPET,
kind=(sublime.KIND_ID_VARIABLE, 'v', 'Variable'),
suffix=": ",
)
PACKAGE_NAME = __package__.split('.')[0]
def status(msg, window=None, console=False):
msg = "[%s] %s" % (PACKAGE_NAME, msg)
(window or sublime).status_message(msg)
if console:
print(msg)
def format_static_completion(completion):
return sublime.CompletionItem(
trigger=completion.trigger,
kind=completion.template.kind,
details=completion.details,
completion=completion.trigger + completion.template.suffix,
completion_format=completion.template.format,
)
def format_static_completions(templates):
return list(map(format_static_completion, templates))
def format_completions(items, annotation="", kind=sublime.KIND_AMBIGUOUS):
format_string = "Defined at line <a href='subl:goto_line {{\"line\": \"{0}\"}}'>{0}</a>"
return [
sublime.CompletionItem(
trigger=trigger,
annotation=annotation,
kind=kind,
details=format_string.format(row) if row is not None else "",
)
for trigger, row in items
]
class SyntaxDefCompletionsListener(sublime_plugin.ViewEventListener):
base_completions_root = format_static_completions([
# base keys
Completion('name', TPL_HEADER_BASE, "The display name of the syntax."),
Completion('scope', TPL_HEADER_BASE, "The main scope of the syntax."),
Completion('version', TPL_HEADER_BASE, "The sublime-syntax version."),
Completion('extends', TPL_HEADER_BASE, "The syntax which is to be extended."),
Completion('name', TPL_HEADER_BASE, "The display name of the syntax."),
Completion(
"first_line_match",
TPL_HEADER_BASE,
"The pattern to identify a file by content.",
),
Completion('hidden', TPL_HEADER_TRUE, "Hide this syntax from the menu."),
# dict keys
Completion('variables', TPL_HEADER_DICT, 'The variables definitions.'),
Completion('contexts', TPL_HEADER_DICT, 'The syntax contexts.'),
# list keys
Completion('file_extensions', TPL_HEADER_LIST, "The list of file extensions."),
Completion(
'hidden_file_extensions',
TPL_HEADER_LIST,
"The list of hidden file extensions.",
),
])
base_completions_contexts = format_static_completions([
# meta functions
Completion(
'meta_append',
TPL_FUNCTION_TRUE,
"Add rules to the end of the inherit context.",
),
Completion(
'meta_content_scope',
TPL_FUNCTION,
"A scope to apply to the content of a context.",
),
Completion(
'meta_include_prototype',
TPL_FUNCTION_FALSE,
"Flag to in-/exclude `prototype`",
),
Completion(
'meta_prepend',
TPL_FUNCTION_TRUE,
"Add rules to the beginning of the inherit context.",
),
Completion('meta_scope', TPL_FUNCTION, "A scope to apply to the full context."),
Completion('clear_scopes', TPL_FUNCTION, "Clear meta scopes."),
# matching tokens
Completion('match', TPL_FUNCTION, "Pattern to match tokens."),
# scoping
Completion('scope', TPL_FUNCTION, "The scope to apply if a token matches"),
Completion('captures', TPL_CAPTURUES, "Assigns scopes to the capture groups."),
# contexts
Completion('push', TPL_FUNCTION, "Push a context onto the stack."),
Completion('set', TPL_FUNCTION, "Set a context onto the stack."),
Completion('with_prototype', TPL_FUNCTION, "Rules to prepend to each context."),
# branching
Completion(
'branch_point',
TPL_FUNCTION,
"Name of the point to rewind to if a branch fails.",
),
Completion('branch', TPL_FUNCTION, "Push branches onto the stack."),
Completion('fail', TPL_FUNCTION, "Fail the current branch."),
# embedding
Completion('embed', TPL_FUNCTION, "A context or syntax to embed."),
Completion('embed_scope', TPL_FUNCTION, "A scope to apply to the embedded syntax."),
Completion('escape', TPL_FUNCTION, "A pattern to denote the end of the embedded syntax."),
Completion('escape_captures', TPL_CAPTURUES, "Assigns scopes to the capture groups."),
# including
Completion('include', TPL_FUNCTION, "Includes a context."),
Completion('apply_prototype', TPL_FUNCTION_TRUE, "Apply prototype of included syntax."),
])
base_completions_contexts_version_1 = (
base_completions_contexts
+ format_static_completions([
Completion('pop', TPL_FUNCTION_TRUE, 'Pop context(s) from the stack.'),
])
)
base_completions_contexts_version_2 = (
base_completions_contexts
+ format_static_completions([
Completion('pop', TPL_FUNCTION_NUMERIC, 'Pop context(s) from the stack.'),
])
)
@classmethod
def applies_to_primary_view_only(cls):
return False
@classmethod
def is_applicable(cls, settings):
return settings.get('syntax') == syntax_paths.SYNTAX_DEF
@inhibit_word_completions
def on_query_completions(self, prefix, locations):
def match_selector(selector, offset=0):
"""Verify scope for each location."""
return all(self.view.match_selector(point + offset, selector)
for point in locations)
result = None
# None of our business
if not match_selector("- comment - (source.regexp - keyword.other.variable)"):
result = None
# Scope name completions based on our scope_data database
elif match_selector("meta.expect-scope, meta.scope", -1):
result = self._complete_scope(prefix, locations)
# Auto-completion for include values using the 'contexts' keys and for
elif match_selector(
"meta.expect-context-list-or-content | meta.context-list-or-content",
-1,
):
result = (
(self._complete_keyword(prefix, locations) or [])
+ self._complete_context(prefix, locations)
)
# Auto-completion for include values using the 'contexts' keys
elif match_selector(
"meta.expect-context-list | meta.expect-context | meta.include | meta.context-list",
-1,
):
result = self._complete_context(prefix, locations) or None
# Auto-completion for branch points with 'fail' key
elif match_selector(
"meta.expect-branch-point-reference | meta.branch-point-reference",
-1,
):
result = self._complete_branch_point()
elif match_selector(
"meta.extends",
-1,
):
result = self._complete_syntax_file()
# Auto-completion for variables in match patterns using 'variables' keys
elif match_selector("keyword.other.variable"):
result = self._complete_variable()
else:
# Standard completions for unmatched regions
result = self._complete_keyword(prefix, locations)
return result
def _line_prefix(self, point):
_, col = self.view.rowcol(point)
line = self.view.substr(self.view.line(point))
return line[:col]
def _complete_context(self, prefix, locations):
# Verify that we're not looking for an external include
for point in locations:
line_prefix = self._line_prefix(point)
real_prefix = re.search(r"[^,\[ ]*$", line_prefix).group(0)
if real_prefix.startswith("scope:") or "/" in real_prefix:
return [] # Don't show any completions here
elif real_prefix != prefix:
# print("Unexpected prefix mismatch: {} vs {}".format(real_prefix, prefix))
return []
return format_completions(
[(self.view.substr(r), self.view.rowcol(r.begin())[0] + 1)
for r in self.view.find_by_selector("entity.name.function.context")],
annotation="",
kind=TPL_CONTEXT.kind,
)
def _complete_syntax_file(self):
completions = []
kind = (sublime.KIND_ID_VARIABLE, 's', 'Syntax')
settings = sublime.load_settings("PackageDev.sublime-settings")
excludes = settings.get("settings.exclude_syntax_patterns", [])
if not isinstance(excludes, list):
excludes = []
try:
my_folder = str(ResourcePath.from_file_path(self.view.file_name()).parent)
except (TypeError, ValueError):
my_folder = ""
for syntax in sublime.list_syntaxes():
if any(fnmatch(syntax.path, pattern) for pattern in excludes):
continue
# add relative resource path completion (file name of siblings)
if my_folder:
folder, file = syntax.path.rsplit("/", 1)
if folder == my_folder:
completions.append(
sublime.CompletionItem(
trigger=file,
kind=kind,
annotation="hidden" if syntax.hidden else ""
)
)
# add full resource path
completions.append(
sublime.CompletionItem(
trigger=syntax.path,
kind=kind,
annotation="hidden" if syntax.hidden else ""
)
)
return completions
def _complete_keyword(self, prefix, locations):
def match_selector(selector, offset=0):
"""Verify scope for each location."""
return all(self.view.match_selector(point + offset, selector)
for point in locations)
prefixes = set()
for point in locations:
# Ensure that we are completing a key name everywhere
line_prefix = self._line_prefix(point)
real_prefix = re.sub(r"^ +(- +)*", " ", line_prefix) # collapse leading whitespace
prefixes.add(real_prefix)
if len(prefixes) != 1:
return None
else:
real_prefix = next(iter(prefixes))
# (Supposedly) all keys start their own line
match = re.match(r"^(\s*)[\w-]*$", real_prefix)
if not match:
return None
elif not match.group(1):
return self.base_completions_root
elif match_selector("meta.block.contexts"):
if self._determine_version() == 1:
return self.base_completions_contexts_version_1
else:
return self.base_completions_contexts_version_2
else:
return None
def _complete_scope(self, prefix, locations):
# Determine entire prefix
window = self.view.window()
prefixes = set()
for point in locations:
*_, real_prefix = self._line_prefix(point).rpartition(" ")
prefixes.add(real_prefix)
if len(prefixes) > 1:
return None
else:
real_prefix = next(iter(prefixes))
# Tokenize the current selector
tokens = real_prefix.split(".")
if len(tokens) <= 1:
# No work to be done here, just return the heads
return COMPILED_HEADS.to_completion()
base_scope_completion = self._complete_base_scope(tokens[-1])
# Browse the nodes and their children
nodes = COMPILED_HEADS
for i, token in enumerate(tokens[:-1]):
node = nodes.find(token)
if not node:
status(
"`%s` not found in scope naming conventions" % '.'.join(tokens[:i + 1]),
window
)
break
nodes = node.children
if not nodes:
status("No nodes available in scope naming conventions after `%s`"
% '.'.join(tokens[:-1]), window)
break
else:
# Offer to complete from conventions or base scope
return nodes.to_completion() + base_scope_completion
# Since we don't have anything to offer,
# just complete the base scope appendix/suffix.
return base_scope_completion
def _complete_base_scope(self, last_token):
regions = self.view.find_by_selector("meta.scope string - meta.block")
if len(regions) != 1:
status(
"Warning: Could not determine base scope uniquely",
console=True
)
return []
# TODO some syntaxes use a different suffix than the last segment of the base scope
base_scope = self.view.substr(regions[0])
*_, base_suffix = base_scope.rpartition(".")
# Only useful when the base scope suffix is not already the last one
# In this case it is even useful to inhibit other completions completely
if last_token == base_suffix:
return []
return [create_scope_suffix_completion(base_suffix)]
def _complete_variable(self):
return format_completions(
[(self.view.substr(r), self.view.rowcol(r.begin())[0] + 1)
for r in self.view.find_by_selector("entity.name.constant")],
annotation="",
kind=TPL_VARIABLE.kind,
)
def _complete_branch_point(self):
return format_completions(
[(self.view.substr(r), self.view.rowcol(r.begin())[0] + 1)
for r in self.view.find_by_selector("entity.name.label.branch-point")],
annotation="",
kind=TPL_BRANCH.kind,
)
def _determine_version(self):
version_regions = self.view.find_by_selector('storage.type.version.sublime-syntax')
if version_regions:
if len(version_regions) > 1:
logger.debug("Found multiple versions (%d), using last", len(version_regions))
version_line = self.view.substr(self.view.line(version_regions[-1]))
*_, version_str = version_line.partition(": ")
if version_str:
try:
return int(version_str)
except ValueError:
logger.debug("Unable to parse version string '%s'", version_str)
return 1
class PackagedevCommitScopeCompletionCommand(sublime_plugin.TextCommand):
"""Insert a scope segment and re-open the completions popup when sensible."""
def name(self):
return COMMIT_SCOPE_COMPLETION_CMD
def run(self, edit, text, is_base_suffix=False):
self.view.run_command("insert", {"characters": text})
if is_base_suffix:
return
self.view.run_command('insert', {'characters': "."})
# Allow ST to process our inserts (and work around a crash).
sublime.set_timeout(
lambda: self.view.run_command('auto_complete', {'disable_auto_insert': True}),
)