-
Notifications
You must be signed in to change notification settings - Fork 27
/
build-flavored-queries.py
executable file
·418 lines (388 loc) · 11.3 KB
/
build-flavored-queries.py
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
#!/usr/bin/env python3
"""Generate NeoVim queries.
Everything in `queries/` uses tree-sitter syntax, as defined at
<https://tree-sitter.github.io/tree-sitter/syntax-highlighting#theme>. However, each
editor has a slightly different syntax.
This file performs conversions so two sets of files don't need to be maintained.
<https://docs.helix-editor.com/master/themes.html#syntax-highlighting> has a bit better
guide for the standard parameters than tree-sitter does.
"""
import re
from glob import glob
from pathlib import Path
REPLACEMENTS_HELIX = []
REPLACEMENTS_ZED = []
REPLACEMENTS_LAPCE = []
ALLOWED_CAPS_ALL = ["@error"]
ALLOWED_PREDICATES_ALL = [
"#eq?",
"#match?",
]
ALLOWED_CAPS_NVIM = {
"highlights.scm": [
"@variable",
"@variable.builtin",
"@variable.parameter",
"@variable.parameter.builtin",
"@variable.member",
"@constant",
"@constant.builtin",
"@constant.macro",
"@module",
"@module.builtin",
"@label",
"@string",
"@string.documentation",
"@string.regexp",
"@string.escape",
"@string.special",
"@string.special.symbol",
"@string.special.url",
"@string.special.path",
"@character",
"@character.special",
"@boolean",
"@number",
"@number.float",
"@type",
"@type.builtin",
"@type.definition",
"@type.qualifier",
"@attribute",
"@attribute.builtin",
"@property",
"@function",
"@function.builtin",
"@function.call",
"@function.macro",
"@function.method",
"@function.method.call",
"@constructor",
"@operator",
"@keyword",
"@keyword.coroutine",
"@keyword.function",
"@keyword.operator",
"@keyword.import",
"@keyword.storage",
"@keyword.type",
"@keyword.repeat",
"@keyword.return",
"@keyword.debug",
"@keyword.exception",
"@keyword.conditional",
"@keyword.conditional.ternary",
"@keyword.directive",
"@keyword.directive.define",
"@punctuation.delimiter",
"@punctuation.bracket",
"@punctuation.special",
"@comment",
"@comment.documentation",
"@comment.error",
"@comment.warning",
"@comment.todo",
"@comment.note",
"@none",
"@conceal",
"@spell",
"@nospell",
],
"injections.scm": [
"@injection.language",
"@injection.content",
"@comment",
],
"locals.scm": [
"@local.definition",
"@local.definition.constant",
"@local.definition.function",
"@local.definition.method",
"@local.definition.var",
"@local.definition.parameter",
"@local.definition.macro",
"@local.definition.type",
"@local.definition.field",
"@local.definition.enum",
"@local.definition.namespace",
"@local.definition.import",
"@local.definition.associated",
"@local.scope",
"@local.reference",
],
"folds.scm": [
"@fold",
],
"indents.scm": [
"@indent.begin",
"@indent.end",
"@indent.align",
"@indent.dedent",
"@indent.branch",
"@indent.ignore",
"@indent.auto",
"@indent.zero",
],
"textobjects.scm": [
"@attribute.inner",
"@attribute.outer",
"@function.inner",
"@function.outer",
"@class.inner",
"@class.outer",
"@conditional.inner",
"@conditional.outer",
"@loop.inner",
"@loop.outer",
"@call.inner",
"@call.outer",
"@block.inner",
"@block.outer",
"@parameter.inner",
"@parameter.outer",
"@regex.inner",
"@regex.outer",
"@comment.inner",
"@comment.outer",
"@assignment.inner",
"@assignment.outer",
"@return.inner",
"@return.outer",
"@frame.inner",
"@frame.outer",
],
}
ALLOWED_SETTINGS_NVIM = {
"injections.scm": [
"injection.combined",
"injection.language",
"injection.include-children",
]
}
# Old nvim-treesitter before updates
REPLACEMENTS_NVIM = [
# Changes to highlight queries
(r"@comment.line", r"@comment"),
(r"@constant.builtin.boolean", r"@boolean"),
(r"@constant.character.escape", r"@string.escape"),
(r"@keyword.module", r"@module"),
(r"@keyword.control.", r"@keyword."),
(r"@namespace", r"@module"),
# Changes to indent queries
(r"@indent\s+@extend", r"@indent.begin"),
# Changes to textobject queries
(r"(@[\w.]+\.)inside", r"\1inner"),
(r"(@[\w.]+\.)around", r"\1outer"),
# nvim uses `var` rather than `variable`
(r"(@[\w.]+)\.variable", r"\1.var"),
# nothing more specific than reference
(r"(@local.reference)[\w.]+", r"\1"),
]
ALLOWED_CAPS_HELIX = {
"highlights.scm": [
"@attribute",
"@type.builtin",
"@type.parameter",
"@type.enum.variant",
"@constructor",
"@constant.builtin",
"@constant.builtin.boolean",
"@constant.character",
"@constant.character.escape",
"@constant.numeric",
"@constant.numeric.integer",
"@constant.numeric.float",
"@string",
"@string.regexp",
"@string.special.path",
"@string.special.url",
"@string.special.symbol",
"@comment",
"@comment.line",
"@comment.block",
"@comment.block.documentation",
"@variable",
"@variable.builtin",
"@variable.parameter",
"@variable.other",
"@variable.other.member",
"@label",
"@punctuation",
"@punctuation.delimiter",
"@punctuation.bracket",
"@punctuation.special",
"@keyword",
"@keyword.control",
"@keyword.control.conditional",
"@keyword.control.repeat",
"@keyword.control.import",
"@keyword.control.return",
"@keyword.control.exception",
"@keyword.operator",
"@keyword.directive",
"@keyword.function",
"@keyword.storage",
"@keyword.storage.type",
"@keyword.storage.modifier",
"@operator",
"@function",
"@function.builtin",
"@function.method",
"@function.macro",
"@function.special",
"@namespace",
"@special",
],
"injections.scm": [
"@injection.language",
"@injection.content",
"@injection.filename",
"@injection.shebang",
"@comment",
],
# unspecified in helix
"locals.scm": [
c.replace("var", "variable") for c in ALLOWED_CAPS_NVIM["locals.scm"]
],
"folds.scm": ALLOWED_CAPS_NVIM["folds.scm"],
"indents.scm": [
"@indent",
"@outdent",
"@indent.always",
"@outdent.always",
"@align",
"@extend",
"@extend.prevent-once",
],
"textobjects.scm": [
"@function.inside",
"@function.around",
"@class.inside",
"@class.around",
"@test.inside",
"@test.around",
"@parameter.inside",
"@parameter.around",
"@comment.inside",
"@comment.around",
],
}
ALLOWED_SETTINGS_HELIX = {
"injections.scm": [
"injection.combined",
"injection.language",
"injection.include-children",
"injection.include-unnamed-children",
]
}
REPLACEMENTS_HELIX = [
(r"@keyword.module", r"@keyword.directive"),
(r"@function.call", r"@function"),
(r"@spell ?", r""),
# nothing more specific than `@local.(reference,definition)`
(r"(@local\.\w+)\.[.\w]+", r"\1"),
]
# Documentation not yet complete
ALLOWED_CAPS_ZED = ALLOWED_CAPS_HELIX
ALLOWED_SETTINGS_ZED = ALLOWED_SETTINGS_HELIX
REPLACEMENTS_ZED = REPLACEMENTS_HELIX
ALLOWED_CAPS_LAPCE = ALLOWED_CAPS_HELIX
ALLOWED_SETTINGS_LAPCE = ALLOWED_SETTINGS_HELIX
REPLACEMENTS_LAPCE = REPLACEMENTS_HELIX
# (rname, eplacements, allowed caps, base path) mappings to create a flavor
FLAVOR_MAPPINGS = [
(
"new NeoVim",
"NVIM",
REPLACEMENTS_NVIM,
ALLOWED_CAPS_NVIM,
ALLOWED_SETTINGS_NVIM,
Path("queries") / "just",
),
(
"Helix",
"HELIX",
REPLACEMENTS_HELIX,
ALLOWED_CAPS_HELIX,
ALLOWED_SETTINGS_HELIX,
Path("queries-flavored") / "helix",
),
(
"Zed",
"ZED",
REPLACEMENTS_ZED,
ALLOWED_CAPS_ZED,
ALLOWED_SETTINGS_ZED,
Path("queries-flavored") / "zed",
),
(
"Lapce",
"LAPCE",
REPLACEMENTS_LAPCE,
ALLOWED_CAPS_LAPCE,
ALLOWED_SETTINGS_LAPCE,
Path("queries-flavored") / "lapce",
),
]
def main():
sources = glob("queries-src/*.scm")
for fname in sources:
qname = Path(fname).name
print(f"Generating flavored queries from {fname}")
base_contents = "; File autogenerated by build-queries-nvim.py; do not edit\n\n"
with open(fname) as f:
base_contents += f.read()
for (
name,
tag,
replacements,
allowed_caps,
allowed_settings,
basepath,
) in FLAVOR_MAPPINGS:
# Remove lines as indicated by directives
contents = "\n".join(
(
line
for line in base_contents.splitlines()
if f"SKIP-{tag}" not in line
)
)
# Delete other directives
contents = re.sub(r";?\s*SKIP-[\w-]+", "", contents)
for rep in replacements:
pat = rep[0]
sub = rep[1]
flags = 0
if len(rep) > 2:
flags = rep[2]
contents = re.sub(pat, sub, contents, flags=flags)
# Remove trailing whitespace and duplicate newlines
contents = re.sub(r"[\s;]+$", "", contents)
contents = "\n".join((line.rstrip() for line in contents.splitlines()))
contents = re.sub(r"((?:\r?\n){2,})(?:\r?\n)+", r"\1", contents)
if not contents.endswith("\n"):
contents += "\n"
basepath.mkdir(parents=True, exist_ok=True)
dest = basepath / Path(fname).name
with open(dest, "w") as f:
f.write(contents)
# Validate all captures are valid
for qcap in re.finditer(r"@[\w.]+", contents):
matched = qcap[0]
# Internal captures
if matched.startswith("@_"):
continue
allowed = allowed_caps[qname]
assert (
matched in allowed or matched in ALLOWED_CAPS_ALL
), f"found disallowed query '{matched}' in '{dest}' ({name}). Allowed: {allowed}"
# Validate all settings are valid
for qsetting in re.finditer(r"#set!\s+([\w.-]+)", contents):
matched = qsetting[1]
allowed = allowed_settings[qname]
assert (
matched in allowed
), f"found disallowed setting '{qsetting[0]}' in '{dest}' ({name}). Allowed: {allowed}"
if __name__ == "__main__":
main()