Skip to content

Commit

Permalink
An injection with no highlights query should still apply its root scope
Browse files Browse the repository at this point in the history
  • Loading branch information
savetheclocktower committed Jan 20, 2024
1 parent 4d8a721 commit 2a0ebac
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
26 changes: 26 additions & 0 deletions spec/wasm-tree-sitter-language-mode-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,32 @@ describe('WASMTreeSitterLanguageMode', () => {
]);
});

it('handles injections with no highlights query', async () => {
jasmine.useRealClock();
atom.grammars.addGrammar(jsGrammar);
atom.grammars.addGrammar(htmlGrammar);
htmlGrammar.highlightsQuery = false;
// Pretend this grammar doesn't have a highlights query.
spyOn(htmlGrammar, 'getQuery').andReturn(Promise.resolve(null));
const languageMode = new WASMTreeSitterLanguageMode({
grammar: jsGrammar,
buffer,
config: atom.config,
grammars: atom.grammars
});
buffer.setLanguageMode(languageMode);
await languageMode.ready;

buffer.setText('text = html`<p></p>`');
await languageMode.atTransactionEnd();

// An injection should still be able to add its root scope even when
// its grammar has no `highlightsQuery`.
let descriptor = editor.scopeDescriptorForBufferPosition([0, 15]);

expect(descriptor.getScopesArray()).toContain('text.html.basic');
});

it('terminates comment token at the end of an injection, so that the next injection is NOT a continuation of the comment', async () => {
jasmine.useRealClock();
const ejsGrammar = new WASMTreeSitterGrammar(
Expand Down
11 changes: 8 additions & 3 deletions src/wasm-tree-sitter-language-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3113,7 +3113,7 @@ class LanguageLayer {
// through a `ScopeResolver`.
getSyntaxBoundaries(from, to) {
let { buffer } = this.languageMode;
if (!(this.language && this.tree && this.highlightsQuery)) {
if (!(this.language && this.tree)) {
return [[], new OpenScopeMap()];
}

Expand All @@ -3123,7 +3123,12 @@ class LanguageLayer {
let boundaries = createTree(comparePoints);
let extent = this.getExtent();

const captures = this.highlightsQuery.captures(this.tree.rootNode, from, to);
let captures;
if (this.highlightsQuery) {
captures = this.highlightsQuery.captures(this.tree.rootNode, from, to);
} else {
captures = [];
}
this.scopeResolver.reset();

for (let capture of captures) {
Expand All @@ -3145,7 +3150,7 @@ class LanguageLayer {
// `allowEmpty` to force these to be considered, but for marking scopes,
// there's no need for it; it'd just cause us to open and close a scope
// in the same position.
if (node.text === '') { continue; }
if (node.childCount === 0 && node.text === '') { continue; }

// Ask the `ScopeResolver` to process each capture in turn. Some captures
// will be ignored if they fail certain tests, and some will have their
Expand Down

0 comments on commit 2a0ebac

Please sign in to comment.