Report scopes from shallower layers at the start or end of an injection

This commit is contained in:
Antonio Scandurra 2019-08-01 15:48:00 +02:00
parent cac1a77e40
commit 0693b4f48c
2 changed files with 23 additions and 4 deletions

View File

@ -866,6 +866,23 @@ describe('TreeSitterLanguageMode', () => {
]);
});
it('reports scopes from shallower layers when they are at the start or end of an injection', async () => {
await atom.packages.activatePackage('language-javascript')
editor.setGrammar(atom.grammars.grammarForScopeName('source.js'))
editor.setText('/** @babel */\n{\n}')
expectTokensToEqual(editor, [
[
{ text: '/** ', scopes: ['source js', 'comment block'] },
{ text: '@babel', scopes: ['source js', 'comment block', 'keyword control'] },
{ text: ' *', scopes: ['source js', 'comment block'] },
{ text: '/', scopes: ['source js', 'comment block', 'meta delimiter slash'] }
],
[{ text: '{', scopes: ['source js', 'punctuation definition function body begin bracket curly'] }],
[{ text: '}', scopes: ['source js', 'punctuation definition function body end bracket curly'] }]
]);
})
it('respects the `includeChildren` property of injection points', async () => {
const rustGrammar = new TreeSitterGrammar(
atom.grammars,

View File

@ -32,7 +32,7 @@ class TreeSitterLanguageMode {
this.config = config;
this.grammarRegistry = grammars;
this.parser = new Parser();
this.rootLanguageLayer = new LanguageLayer(this, grammar, 0);
this.rootLanguageLayer = new LanguageLayer(this, grammar, 0, buffer.getRange());
this.injectionsMarkerLayer = buffer.addMarkerLayer();
if (syncTimeoutMicros != null) {
@ -637,13 +637,14 @@ class TreeSitterLanguageMode {
}
class LanguageLayer {
constructor(languageMode, grammar, depth) {
constructor(languageMode, grammar, depth, range) {
this.languageMode = languageMode;
this.grammar = grammar;
this.tree = null;
this.currentParsePromise = null;
this.patchSinceCurrentParseStarted = null;
this.depth = depth;
this.range = range
}
buildHighlightIterator() {
@ -885,7 +886,8 @@ class LanguageLayer {
marker.languageLayer = new LanguageLayer(
this.languageMode,
grammar,
this.depth + 1
this.depth + 1,
injectionRange
);
marker.parentLanguageLayer = this;
}
@ -1011,7 +1013,7 @@ class HighlightIterator {
next.offset === first.offset &&
next.atEnd === first.atEnd &&
next.depth > first.depth &&
next.openTags.length + next.closeTags.length > 0
next.languageLayer.range.containsPoint(first.getPosition(), true)
) {
this.currentScopeIsCovered = true;
return;