Skip to content

Commit

Permalink
Merge pull request atom#19726 from atom/as-ns/fix-injected-grammars
Browse files Browse the repository at this point in the history
Only cover scope boundaries in the parent layer if there is a scope boundary in the injected layer
  • Loading branch information
Nathan Sobo authored Jul 30, 2019
2 parents 0fddbc7 + a467402 commit cac1a77
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
44 changes: 43 additions & 1 deletion spec/tree-sitter-language-mode-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const pythonGrammarPath = require.resolve(
const jsGrammarPath = require.resolve(
'language-javascript/grammars/tree-sitter-javascript.cson'
);
const jsdocGrammarPath = require.resolve(
'language-javascript/grammars/tree-sitter-jsdoc.cson'
);
const htmlGrammarPath = require.resolve(
'language-html/grammars/tree-sitter-html.cson'
);
Expand Down Expand Up @@ -518,7 +521,10 @@ describe('TreeSitterLanguageMode', () => {
'template_substitution > "}"': 'interpolation'
},
injectionRegExp: 'javascript',
injectionPoints: [HTML_TEMPLATE_LITERAL_INJECTION_POINT]
injectionPoints: [
HTML_TEMPLATE_LITERAL_INJECTION_POINT,
JSDOC_INJECTION_POINT
]
});

htmlGrammar = new TreeSitterGrammar(atom.grammars, htmlGrammarPath, {
Expand Down Expand Up @@ -834,6 +840,32 @@ describe('TreeSitterLanguageMode', () => {
]);
});

it('only covers scope boundaries in parent layers if a nested layer has a boundary at the same position', async () => {
const jsdocGrammar = new TreeSitterGrammar(
atom.grammars,
jsdocGrammarPath,
{
scopeName: 'jsdoc',
parser: 'tree-sitter-jsdoc',
scopes: {},
injectionRegExp: 'jsdoc',
injectionPoints: []
}
);
atom.grammars.addGrammar(jsGrammar);
atom.grammars.addGrammar(jsdocGrammar);

editor.setGrammar(jsGrammar);
editor.setText('/**\n*/\n{\n}');

expectTokensToEqual(editor, [
[{ text: '/**', scopes: ['comment'] }],
[{ text: '*/', scopes: ['comment'] }],
[{ text: '{', scopes: [] }],
[{ text: '}', scopes: [] }]
]);
});

it('respects the `includeChildren` property of injection points', async () => {
const rustGrammar = new TreeSitterGrammar(
atom.grammars,
Expand Down Expand Up @@ -2417,3 +2449,13 @@ const SCRIPT_TAG_INJECTION_POINT = {
return node.child(1);
}
};

const JSDOC_INJECTION_POINT = {
type: 'comment',
language(comment) {
if (comment.text.startsWith('/**')) return 'jsdoc';
},
content(comment) {
return comment;
}
};
3 changes: 2 additions & 1 deletion src/tree-sitter-language-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,8 @@ class HighlightIterator {
if (
next.offset === first.offset &&
next.atEnd === first.atEnd &&
next.depth > first.depth
next.depth > first.depth &&
next.openTags.length + next.closeTags.length > 0
) {
this.currentScopeIsCovered = true;
return;
Expand Down

0 comments on commit cac1a77

Please sign in to comment.