-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
scope-resolver.js
996 lines (846 loc) · 32.2 KB
/
scope-resolver.js
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
const { CompositeDisposable } = require('event-kit');
const { Point } = require('text-buffer');
const ScopeDescriptor = require('./scope-descriptor');
// TODO: These utility functions are duplicated between this file and
// `wasm-tree-sitter-language-mode.js`. Eventually they might need to be moved
// into a `utils` file somewhere.
// Compares “informal” points like the ones in a tree-sitter tree; saves us
// from having to convert them to actual `Point`s.
function comparePoints(a, b) {
const rows = a.row - b.row;
if (rows === 0) {
return a.column - b.column;
} else {
return rows;
}
}
function rangeSpecToString(range) {
let [sp, ep] = [range.startPosition, range.endPosition];
return `(${sp.row}, ${sp.column}) - (${ep.row}, ${ep.column})`;
}
function resolveNodeDescriptor(node, descriptor) {
let parts = descriptor.split('.');
let result = node;
while (result !== null && parts.length > 0) {
let part = parts.shift();
if (!result[part]) { return null; }
result = result[part];
}
return result;
}
function resolveNodePosition(node, descriptor) {
let parts = descriptor.split('.');
let lastPart = parts.pop();
let result = parts.length === 0 ?
node :
resolveNodeDescriptor(node, parts.join('.'));
if (!result) { return null; }
return result[lastPart];
}
function interpretPredicateValue(value) {
if (value === "true") { return true; }
if (value === "false") { return false; }
if (/^\d+$/.test(value)) { return Number(value); }
return value;
}
function interpretPossibleKeyValuePair(rawValue, coerceValue = false) {
if (!rawValue.includes(' ')) { return [rawValue, null]; }
// Split on the first space. Everything after the first space is the value.
let parts = rawValue.split(' ');
let key = parts.shift(), value = parts.join(' ');
// We only want to interpret the value if we're comparing it to a config
// value; otherwise we want to compare strings to strings.
if (coerceValue) value = interpretPredicateValue(value);
return [key, value];
}
// `ScopeResolver`s can share a config cache if they have the same grammar.
// There are many such `ScopeResolver`s (because there are many such
// `LanguageLayer`s), and this consolidation cuts down on the number of
// `onDidChange` handlers (which are costly when observing all config values).
class ConfigCache {
constructor(config) {
this.subscriptions = new CompositeDisposable();
this.cachesByGrammar = new Map();
this.config = config;
this.subscriptions.add(
this.config.onDidChange(() => this.clearAll()),
atom.grammars.onDidAddGrammar(() => this.clearAll()),
atom.grammars.onDidUpdateGrammar(() => this.clearAll())
);
}
dispose() {
this.subscriptions.dispose();
}
clearAll() {
for (let cache of this.cachesByGrammar.values()) {
cache.clear();
}
}
getCacheForGrammar(grammar) {
let { scopeName } = grammar;
// We key on the scope name rather than the grammar instance. We need to be
// able to iterate over grammars, so we can't use a `WeakSet` here, and we
// have no lifecycle event to keep the map from getting stale.
//
// To prevent two different incarnations of the same grammar (after
// disabling and reenabling) from incorrectly sharing a cache, we clear all
// caches whenever grammars are added or updated.
let cache = this.cachesByGrammar.get(scopeName);
if (!cache) {
cache = new Map();
this.cachesByGrammar.set(scopeName, cache);
}
return cache;
}
}
// We can technically have more than one configuration object, though in
// practice this will only point to `atom.config`. We do it this way for ease
// of testing (e.g., if a test mocks a config object) and to avoid silly hacks.
ConfigCache.CACHES_FOR_CONFIG_OBJECTS = new Map();
ConfigCache.forConfig = (config) => {
let { CACHES_FOR_CONFIG_OBJECTS } = ConfigCache;
let configCache = CACHES_FOR_CONFIG_OBJECTS.get(config);
if (!configCache) {
configCache = new ConfigCache(config);
CACHES_FOR_CONFIG_OBJECTS.set(config, configCache);
}
return configCache;
};
ConfigCache.clear = () => {
ConfigCache.CACHES_FOR_CONFIG_OBJECTS.clear();
};
// A data structure for storing scope information while processing capture
// data. The data is reset in between each task.
//
// It also applies the conventions that we've adopted in SCM files
// (particularly in `highlights.scm`) that let us constrain the conditions
// under which various scopes get applied. When a given query capture is added,
// `ScopeResolver` may "reject" it if it fails to pass the given test.
//
// `ScopeResolver` also sets boundaries for possible consumption by a
// `HighlightIterator`. However, it is used to resolve several different kinds
// of query captures — not just highlights.
//
class ScopeResolver {
constructor(languageLayer, idForScope) {
this.languageLayer = languageLayer;
this.buffer = languageLayer.buffer;
this.config = languageLayer?.languageMode?.config ?? atom.config;
this.grammar = languageLayer.grammar;
this.idForScope = idForScope ?? (x => x);
this.boundaries = new Map;
this.rangeData = new Map;
this.pointKeyCache = new Map;
this.patternCache = new Map;
this.configCache = ConfigCache.forConfig(this.config)
.getCacheForGrammar(this.grammar);
}
getOrCompilePattern(pattern) {
let regex = this.patternCache.get(pattern);
if (!regex) {
regex = new RegExp(pattern);
this.patternCache.set(pattern, regex);
}
return regex;
}
getConfig(key) {
if (this.configCache.has(key)) {
return this.configCache.get(key);
}
let value = this.config.get(key, {
scope: new ScopeDescriptor({ scopes: [this.grammar.scopeName] })
});
this.configCache.set(key, value);
return value;
}
indexToPosition(index) {
return this.buffer.positionForCharacterIndex(index);
}
positionToIndex(position) {
return this.buffer.characterIndexForPosition(position);
}
adjustPositionByOffset(position, offset) {
let index = this.positionToIndex(position);
index += offset;
let newPosition = this.indexToPosition(index);
return this.buffer.clipPosition(newPosition);
}
shouldInvalidateOnChange(capture) {
return capture.setProperties &&
('highlight.invalidateOnChange' in capture.setProperties);
}
shouldInvalidateFoldOnChange(capture) {
return capture.setProperties &&
('fold.invalidateOnChange' in capture.setProperties);
}
// We want to index scope data on buffer position, but each `Point` (or
// ad-hoc point object) is a different object. We could normalize them to a
// string and use the string as the map key, but we'd have to convert them
// back to `Point`s later on, so let's just do it now.
//
// Here we make it so that every point that describes the same buffer
// position is keyed on the same `Point` instance.
_keyForPoint(point) {
let { row, column } = point;
let key = `${row},${column}`;
let normalized = this.pointKeyCache.get(key);
if (!normalized) {
normalized = new Point(Number(row), Number(column));
this.pointKeyCache.set(key, normalized.freeze());
}
return normalized;
}
_keyForRange(range) {
let { startIndex, endIndex } = range;
return `${startIndex}/${endIndex}`;
}
setDataForRange(range, props) {
let key = this._keyForRange(range);
let normalizedProps = { ...props };
// TEMP: No longer needed when we remove support for (#set! test.final
// true).
for (let prop of ['final', 'shy']) {
if (`test.${prop}` in normalizedProps) {
normalizedProps[`capture.${prop}`] = normalizedProps[`test.${prop}`];
}
}
return this.rangeData.set(key, normalizedProps);
}
getDataForRange(syntax) {
let key = this._keyForRange(syntax);
return this.rangeData.get(key);
}
isValidRange(range) {
let { startPosition, startIndex, endPosition, endIndex } = range;
if (!(
typeof startIndex === 'number' &&
typeof endIndex === 'number' &&
typeof startPosition === 'object' &&
typeof endPosition === 'object'
)) { return false; }
if (startIndex > endIndex) { return false; }
if (comparePoints(startPosition, endPosition) >= 0) { return false; }
return true;
}
// Detects whether a capture wants to alter its range from the default.
adjustsCaptureRange(capture) {
let { setProperties: props = {} } = capture;
let keys = Object.keys(props);
if (keys.length === 0) { return false; }
return keys.some(k => this.capturePropertyIsAdjustment(k));
}
rangeExceedsBoundsOfCapture(range, capture) {
return range.startIndex < capture.node.startIndex ||
range.endIndex > capture.node.endIndex;
}
normalizeAdjustmentProperty(prop) {
if (prop.startsWith('adjust.')) {
prop = prop.replace(/^adjust\./, '');
}
return prop;
}
capturePropertyIsAdjustment(prop) {
prop = this.normalizeAdjustmentProperty(prop);
return prop in ScopeResolver.ADJUSTMENTS;
}
applyAdjustment(prop, ...args) {
prop = this.normalizeAdjustmentProperty(prop);
return ScopeResolver.ADJUSTMENTS[prop](...args);
}
normalizeTestProperty(prop) {
if (prop.startsWith('test.')) {
prop = prop.substring(5);
}
// TEMP: Normalize `onlyIfNotFoo` and `onlyIfFoo` to `foo`.
if (prop.startsWith('onlyIfNot')) {
prop = prop.charAt(9).toLowerCase() + prop.substring(10);
}
if (prop.startsWith('onlyIf')) {
prop = prop.charAt(6).toLowerCase() + prop.substring(7);
}
return prop;
}
normalizeCaptureSettingProperty(prop) {
if (prop.startsWith('capture.')) {
prop = prop.substring(8);
}
// TEMP: Normalize `test.final` and `test.shy` to `final` and `shy`.
if (prop === 'test.final' || prop === 'test.shy') {
prop = prop.substring(5);
}
return prop;
}
capturePropertyIsTest(prop) {
prop = this.normalizeTestProperty(prop);
return prop in ScopeResolver.TESTS;
}
capturePropertyIsCaptureSetting(prop) {
// TEMP: Support `test.final` and `test.shy` temporarily.
if (prop === 'test.final' || prop === 'test.shy') {
return true;
}
if (prop.includes('.') && !prop.startsWith('capture.')) {
return false;
}
prop = this.normalizeCaptureSettingProperty(prop);
return prop in ScopeResolver.CAPTURE_SETTINGS;
}
applyTest(prop, ...args) {
let isLegacyNegation = prop.includes('onlyIfNot');
prop = this.normalizeTestProperty(prop);
let result = ScopeResolver.TESTS[prop](...args);
return isLegacyNegation ? !result : result;
}
applyCaptureSettingProperty(prop, ...args) {
prop = this.normalizeCaptureSettingProperty(prop);
return ScopeResolver.CAPTURE_SETTINGS[prop](...args);
}
warnAboutExceededRange(range, capture) {
let msg = ['Cannot extend past original range of capture!'];
msg.push(`Scope name: ${capture.name}`);
msg.push(`Original range: ${rangeSpecToString(capture.node)}`);
msg.push(`Adjusted range: ${rangeSpecToString(range)}`);
if (atom.inDevMode()) {
throw new Error(msg.join('\n'));
}
console.warn(msg.join('\n'));
}
// Given a capture and possible predicate data, determines the buffer range
// that this capture wants to cover.
determineCaptureRange(capture) {
// For our purposes, a "range" is not a `Range` object, but rather an
// object that has all four of `startPosition`, `endPosition`,
// `startIndex`, and `endIndex`. Any single node can thus fulfill this
// contract, but so can a plain object of our own construction.
let { setProperties: props = {} } = capture;
if (!this.adjustsCaptureRange(capture)) { return capture.node; }
let range = {
startPosition: capture.node.startPosition,
startIndex: capture.node.startIndex,
endPosition: capture.node.endPosition,
endIndex: capture.node.endIndex
};
for (let key in props) {
if (this.capturePropertyIsAdjustment(key)) {
let value = props[key];
// Transform the range successively. Later adjustments can optionally
// act on earlier adjustments, or they can ignore the current position
// and inspect the original node instead.
range = this.applyAdjustment(key, capture.node, value, range, this);
// If any single adjustment returns `null`, we shouldn't store this
// capture.
if (range === null) { return null; }
}
}
if (this.rangeExceedsBoundsOfCapture(range, capture)) {
this.warnAboutExceededRange(range, capture);
}
// Any invalidity in the returned range means we shouldn't store this
// capture.
if (!this.isValidRange(range)) { return null; }
return range;
}
isFinal(existingData = {}) {
return ('capture.final' in existingData) ||
('final' in existingData);
}
// Given a syntax capture, test whether we should include its scope in the
// document.
test(capture, existingData) {
let {
node,
setProperties: props = {},
assertedProperties: asserted = {},
refutedProperties: refuted = {}
} = capture;
if (this.isFinal(existingData)) {
return false;
}
// Capture settings (final/shy) are the only keys in `setProperties` that
// matter when testing this capture.
//
// TODO: For compatibility reasons, we're still checking tests of the form
// (#set! test.final) here, but this should be removed before modern
// Tree-sitter ships.
for (let key in props) {
let isCaptureSettingProperty = this.capturePropertyIsCaptureSetting(key);
let isTest = this.capturePropertyIsTest(key);
if (!(isCaptureSettingProperty || isTest)) { continue; }
let value = props[key] ?? true;
if (isCaptureSettingProperty) {
if (!this.applyCaptureSettingProperty(key, node, existingData, this)) {
return false;
}
} else {
// TODO: Remove this once third-party grammars have had time to adapt to
// the migration of tests to `#is?` and `#is-not?`.
if (!this.applyTest(key, node, value, existingData, this)) {
return false;
}
}
}
// Apply tests of the form `(#is? foo)`.
for (let key in asserted) {
if (!this.capturePropertyIsTest(key)) { continue; }
let value = asserted[key] ?? true;
let result = this.applyTest(key, node, value, existingData, this);
if (!result) return false;
}
// Apply tests of the form `(#is-not? foo)`.
for (let key in refuted) {
if (!this.capturePropertyIsTest(key)) { continue; }
let value = refuted[key] ?? true;
let result = this.applyTest(key, node, value, existingData, this);
if (result) return false;
}
return true;
}
// Attempt to add a syntax capture to the boundary data, along with its scope
// ID. Will apply any adjustments to determine the range of the scope being
// applied, then check any test rules to see whether the scope should be
// added for that range.
//
// Will return `false` if the scope should not be added for the given range;
// otherwise will return the computed range.
store(capture) {
let {
node,
name,
setProperties: props = {}
} = capture;
name = ScopeResolver.interpolateName(name, node);
// Find out which range this capture wants.
let range = this.determineCaptureRange(capture);
if (range === null) {
// This capture specified a range adjustment that turned out not to be
// valid. We view those adjustments as essential — that is, if the
// assumed conditions of the `#set!` rules result in invalidity, it means
// that we should not try to honor the capture in the first place.
return false;
}
let data = this.getDataForRange(range);
if (!this.test(capture, data)) {
return false;
} else {
this.setDataForRange(range, props);
}
if (name === '_IGNORE_' || name.startsWith('_IGNORE_.')) {
// "@_IGNORE_" is a magical variable in an SCM file that will not be
// applied in the grammar, but which allows us to prevent other kinds of
// scopes from matching. We purposefully allowed this syntax node to set
// data for a given range, but not to apply its scope ID to any
// boundaries.
//
// A query can also use multiple different @_IGNORE_-style variables by
// adding segments after the @_IGNORE_, such as @_IGNORE_.foo.bar.
return false;
}
let id = this.idForScope(
name,
node.childCount === 0 ? node.text : undefined,
);
let {
startPosition: start,
endPosition: end
} = range;
this.setBoundary(start, id, 'open');
this.setBoundary(end, id, 'close');
return range;
}
setBoundary(point, id, which, { root = false } = {}) {
let key = this._keyForPoint(point);
if (!this.boundaries.has(key)) {
this.boundaries.set(key, { open: [], close: [] })
}
let bundle = this.boundaries.get(key);
let idBundle = bundle[which];
// In general, we want to close scopes in the reverse order of when they
// were opened, and captures that match earlier get to open first. But
// `root` is a way of opting out of this behavior and asserting that a
// scope added later is more important. We use this to add the language's
// root scope if needed.
if (which === 'open') {
// If an earlier token has already opened at this point, we want to open
// after it.
if (root) { idBundle.unshift(id); }
else { idBundle.push(id); }
} else {
// If an earlier token has already closed at this point, we want to close
// before it.
if (root) { idBundle.push(id); }
else { idBundle.unshift(id); }
}
}
reset() {
this.boundaries.clear();
this.rangeData.clear();
}
destroy() {
this.reset();
this.patternCache.clear();
this.pointKeyCache.clear();
}
*[Symbol.iterator]() {
// Iterate in buffer position order.
let keys = [...this.boundaries.keys()];
keys.sort((a, b) => a.compare(b));
for (let key of keys) {
yield [key, this.boundaries.get(key)];
}
}
}
// Scope names can mark themselves with `TEXT` to interpolate the node's text
// into the capture, or `TYPE` to interpolate the anonymous node's type.
ScopeResolver.interpolateName = (name, node) => {
// Only interpolate `_TEXT_` if we know the text has no spaces. Spaces are
// not valid in scope names.
if (name.includes('_TEXT_') &&
!node.text.includes(' ')) {
name = name.replace('_TEXT_', node.text);
}
if (name.includes('_TYPE_')) {
name = name.replace('_TYPE_', node.type);
}
return name;
};
// Special `#set!` predicates that work on “claimed” and “unclaimed” ranges.
ScopeResolver.CAPTURE_SETTINGS = {
// Passes only if another capture has not already declared `final` for the
// exact same range. If a capture is the first one to define `exact`, then
// all other captures for that same range are ignored, whether they try to
// define `final` or not.
final(_node, existingData) {
if (!existingData) return true;
return !('capture.final' in existingData) && !('final' in existingData);
},
// Passes only if no earlier capture has occurred for the exact same range.
shy(_node, existingData) {
return existingData === undefined;
}
};
// These tests are used to define criteria under which the scope should be
// applied. Set them in a query file like so:
//
// (
// (foo) @some.scope.name
// (#is? test.first)
// )
//
// For boolean rules, the second argument to `#is?` can be omitted.
//
// A test can be negated with `#is-not?`:
//
// (
// (foo) @some.scope.name
// (#is-not? test.first)
// )
//
// These tests come in handy for criteria that can't be represented by the
// built-in predicates like `#match?` and `#eq?`.
//
// NOTE: Syntax queries will always be run through a `ScopeResolver`, but other
// kinds of queries may or may not, depending on purpose.
//
ScopeResolver.TESTS = {
// Passes only if the node is of the given type. Can accept multiple
// space-separated types.
type(node, nodeType) {
if (!nodeType.includes(' ')) { return node.type === nodeType }
let nodeTypes = nodeType.split(/\s+/);
return nodeTypes.some(t => t === node.type);
},
// Passes only if the node contains any descendant ERROR nodes.
hasError(node) {
return node.hasError();
},
// Passes when the node's tree belongs to an injection layer, rather than the
// buffer's root language layer.
injection(node, value, existingData, instance) {
return instance.languageLayer.depth > 0;
},
// Passes when the node has no parent.
root(node) {
return !node.parent;
},
// Passes only if the given node is the first among its siblings.
//
// Is not guaranteed to pass if descended from an ERROR node.
first(node) {
// Root nodes are always first.
if (!node.parent) { return true; }
// We're really paranoid on these because if the parse tree is in an error
// state, weird things can happen, like a node's parent not having a
// `firstChild`.
return node?.parent?.firstChild?.id === node.id;
},
// Passes only if the given node is the last among its siblings.
//
// Is not guaranteed to pass if descended from an ERROR node.
last(node) {
// Root nodes are always last.
if (!node.parent) { return true; }
return node?.parent?.lastChild?.id === node.id;
},
// Passes when the node is the first of its type among its siblings.
//
// Is not guaranteed to pass if descended from an ERROR node.
firstOfType(node) {
if (!node.parent) { return true; }
let type = node.type;
let parent = node.parent;
// Lots of optional chaining here to guard against weird states inside
// ERROR nodes.
if ((parent?.childCount ?? 0) === 0) { return false; }
for (let i = 0; i < parent.childCount; i++) {
let child = parent?.child(i);
if (!child) { continue; }
if (child?.id === node.id) { return true; }
else if (child?.type === type) { return false; }
}
return false;
},
// Passes when the node is the last of its type among its siblings.
//
// Is not guaranteed to pass if descended from an ERROR node.
lastOfType(node) {
if (!node.parent) { return true; }
let type = node.type;
let parent = node.parent;
if ((parent?.childCount ?? 0) === 0) { return false; }
for (let i = parent.childCount - 1; i >= 0; i--) {
let child = parent?.child(i);
if (!child) { continue; }
if (child?.id === node.id) { return true; }
else if (child?.type === type) { return false; }
}
return false;
},
// Passes when the node represents the last non-whitespace content on its
// row. Considers the node's ending row.
lastTextOnRow(node, value, existingData, instance) {
let { buffer } = instance;
let text = buffer.lineForRow(node.endPosition.row);
let textAfterNode = text.slice(node.endPosition.column);
return !/\S/.test(textAfterNode);
},
// Passes when the node represents the first non-whitespace content on its
// row. Considers the node's starting row.
firstTextOnRow(node, value, existingData, instance) {
let { buffer } = instance;
let text = buffer.lineForRow(node.startPosition.row);
let textBeforeNode = text.slice(0, node.startPosition.column);
return !/\S/.test(textBeforeNode);
},
// Passes if this node has any node of the given type(s) in its ancestor
// chain.
descendantOfType(node, type) {
let multiple = type.includes(' ');
let target = multiple ? type.split(/\s+/) : type;
let current = node;
while (current.parent) {
current = current.parent;
if (multiple && target.includes(current.type)) { return true; }
else if (!multiple && target === current.type) { return true; }
}
return false;
},
// Passes if there's an ancestor, but fails if the ancestor type matches
// the second,third,etc argument
ancestorTypeNearerThan(node, types) {
let [target, ...rejected] = types.split(/\s+/);
rejected = new Set(rejected)
let current = node;
while (current.parent) {
current = current.parent;
if (rejected.has(current.type)) { return false; }
if (target === current.type) { return true; }
}
return false;
},
// Passes if this node has at least one descendant of the given type(s).
ancestorOfType(node, type) {
let target = type.includes(' ') ? type.split(/\s+/) : type;
let descendants = node.descendantsOfType(target);
return descendants.length > 0;
},
// Passes if this range (after adjustments) has previously had data stored at
// the given key.
rangeWithData(node, rawValue, existingData) {
if (existingData === undefined) { return false; }
let [key, value] = interpretPossibleKeyValuePair(rawValue, false);
// Invalid predicates should be ignored.
if (!key) { return true; }
return (value !== null) ?
existingData[key] === value :
(key in existingData);
},
// Passes if one of this node's ancestors has stored data at the given key
// for its inherent range (ignoring adjustments).
descendantOfNodeWithData(node, rawValue, existingData, instance) {
let current = node;
let [key, value] = interpretPossibleKeyValuePair(rawValue, false);
// Invalid predicates should be ignored.
if (!key) { return true; }
while (current.parent) {
current = current.parent;
let data = instance.getDataForRange(current);
if (data === undefined) { continue; }
let passes = (value !== null) ? data[key] === value : (key in data);
if (passes) { return true; }
}
return false;
},
// Passes if this node starts on the same row as the one in the described
// position. Accepts a node position descriptor.
startsOnSameRowAs(node, descriptor) {
let otherNodePosition = resolveNodePosition(node, descriptor);
return otherNodePosition.row === node.startPosition.row;
},
// Passes if this node ends on the same row as the one in the described
// position. Accepts a node position descriptor.
endsOnSameRowAs(node, descriptor) {
let otherNodePosition = resolveNodePosition(node, descriptor);
return otherNodePosition.row === node.endPosition.row;
},
// Passes only when a given config option is present and truthy. Accepts
// either (a) a configuration key or (b) a configuration key and value
// separated by a space.
config(node, rawValue, existingData, instance) {
let [key, value] = interpretPossibleKeyValuePair(rawValue, true);
// Invalid predicates should be ignored.
if (!key) { return true; }
let configValue = instance.getConfig(key) ?? false;
return value === null ? !!configValue : configValue === value;
}
};
// Usually, we want to map a scope to the exact range of a node in the tree,
// but sometimes that isn't possible. "Adjustments" are pieces of metadata
// assigned to captures that can transform the range to a subset of the initial
// range.
//
// In order to retain our ability to know what scopes to apply when
// re-highlighting an arbitrary buffer region, scope adjustments cannot go
// beyond the bounds of their originally captured node. To have a capture span
// two siblings, for instance, you must capture the _parent_ node and adjust
// the range down from there.
//
ScopeResolver.ADJUSTMENTS = {
// Alter the given range to start at the start or end of a different node.
startAt(node, value, range, resolver) {
let start = resolveNodePosition(node, value);
if (!start) { return null; }
range.startPosition = start;
range.startIndex = resolver.positionToIndex(range.startPosition);
return range;
},
// Alter the given range to end at the start or end of a different node.
endAt(node, value, range, resolver) {
let end = resolveNodePosition(node, value);
if (!end) { return null; }
range.endPosition = end;
range.endIndex = resolver.positionToIndex(range.endPosition);
return range;
},
// Offset the start position by a fixed number of characters in either
// direction. Can act after other range alterations.
offsetStart(node, value, range, resolver) {
let offset = Number(value);
if (isNaN(offset)) { return null; }
let { startPosition } = range;
let offsetPosition = resolver.adjustPositionByOffset(startPosition, offset);
let offsetIndex = resolver.positionToIndex(offsetPosition);
range.startPosition = offsetPosition;
range.startIndex = offsetIndex;
return range;
},
// Offset the end position by a fixed number of characters in either
// direction. Can act after other range alterations.
offsetEnd(node, value, range, resolver) {
let offset = Number(value);
if (isNaN(offset)) { return null; }
let { endPosition } = range;
let offsetPosition = resolver.adjustPositionByOffset(endPosition, offset);
let offsetIndex = resolver.positionToIndex(offsetPosition);
range.endPosition = offsetPosition;
range.endIndex = offsetIndex;
return range;
},
// Change the start and end positions to correspond exactly to the extent of
// the match of the given regular expression. Will match against the text of
// the capture's node.
startAndEndAroundFirstMatchOf(node, value, position, resolver) {
let regex = resolver.getOrCompilePattern(value);
let match = node.text.match(regex);
if (!match) { return null; }
let oldStartPosition = { ...node.startPosition };
let startOffset = match.index;
let endOffset = match.index + match[0].length;
position.startPosition = resolver.adjustPositionByOffset(
oldStartPosition, startOffset);
position.endPosition = resolver.adjustPositionByOffset(
oldStartPosition, endOffset);
position.startIndex = resolver.positionToIndex(position.startPosition);
position.endIndex = resolver.positionToIndex(position.endPosition);
return position;
},
// Change the start position to the point at the beginning of the match of
// the given regular expression. Will match against the text of the capture's
// node.
startBeforeFirstMatchOf(node, value, position, resolver) {
let regex = resolver.getOrCompilePattern(value);
let match = node.text.match(regex);
if (!match) { return null; }
let oldStartPosition = { ...node.startPosition };
let startOffset = match.index;
position.startPosition = resolver.adjustPositionByOffset(
oldStartPosition, startOffset);
position.startIndex = resolver.positionToIndex(position.startPosition);
return position;
},
// Change the start position to the point at the end of the match of the
// given regular expression. Will match against the text of the capture's
// node.
startAfterFirstMatchOf(node, value, position, resolver) {
let regex = resolver.getOrCompilePattern(value);
let match = node.text.match(regex);
if (!match) { return null; }
let oldStartPosition = { ...node.startPosition };
let startOffset = match.index + match[0].length;
position.startPosition = resolver.adjustPositionByOffset(
oldStartPosition, startOffset);
position.startIndex = resolver.positionToIndex(position.startPosition);
return position;
},
// Change the end position to the point at the start of the match of the
// given regular expression. Will match against the text of the capture's
// node.
endBeforeFirstMatchOf(node, value, position, resolver) {
let regex = resolver.getOrCompilePattern(value);
let match = node.text.match(regex);
if (!match) { return null; }
let oldStartPosition = { ...node.startPosition };
let endOffset = match.index;
position.endPosition = resolver.adjustPositionByOffset(
oldStartPosition, endOffset);
position.endIndex = resolver.positionToIndex(position.endPosition);
return position;
},
// Change the end position to the point at the end of the match of the
// given regular expression. Will match against the text of the capture's
// node.
endAfterFirstMatchOf(node, value, position, resolver) {
let regex = resolver.getOrCompilePattern(value);
let match = node.text.match(regex);
if (!match) { return null; }
let oldStartPosition = { ...node.startPosition };
let endOffset = match.index + match[0].length;
position.endPosition = resolver.adjustPositionByOffset(
oldStartPosition, endOffset);
position.endIndex = resolver.positionToIndex(position.endPosition);
return position;
}
};
ScopeResolver.clearConfigCache = () => {
ConfigCache.clear();
};
module.exports = ScopeResolver;