Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions script/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions spec/tree-sitter-language-mode-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,58 @@ describe('TreeSitterLanguageMode', () => {
]);
});

it('provides the grammar with the text of leaf nodes only', async () => {
const grammar = new TreeSitterGrammar(atom.grammars, jsGrammarPath, {
parser: 'tree-sitter-javascript',
scopes: {
program: 'source',
'call_expression > identifier': 'function',
property_identifier: 'property',
'call_expression > member_expression > property_identifier': 'method'
}
});
const original = grammar.idForScope.bind(grammar);
let tokens = [];
grammar.idForScope = function(scope, text) {
if (text && tokens[tokens.length - 1] !== text) {
tokens.push(text);
}
return original(scope, text);
};

buffer.setText('aa.bbb = cc(d.eee());');

const languageMode = new TreeSitterLanguageMode({ buffer, grammar });
buffer.setLanguageMode(languageMode);

expectTokensToEqual(editor, [
[
{ text: 'aa.', scopes: ['source'] },
{ text: 'bbb', scopes: ['source', 'property'] },
{ text: ' = ', scopes: ['source'] },
{ text: 'cc', scopes: ['source', 'function'] },
{ text: '(d.', scopes: ['source'] },
{ text: 'eee', scopes: ['source', 'method'] },
{ text: '());', scopes: ['source'] }
]
]);

expect(tokens).toEqual([
'aa',
'.',
'bbb',
'=',
'cc',
'(',
'd',
'.',
'eee',
'(',
')',
';'
]);
});

it('can start or end multiple scopes at the same position', async () => {
const grammar = new TreeSitterGrammar(atom.grammars, jsGrammarPath, {
parser: 'tree-sitter-javascript',
Expand Down
3 changes: 3 additions & 0 deletions src/tree-sitter-grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ module.exports = class TreeSitterGrammar {
}

idForScope(scopeName) {
if (!scopeName) {
return undefined;
}
let id = this.idsByScope[scopeName];
if (!id) {
id = this.nextScopeId += 2;
Expand Down
8 changes: 7 additions & 1 deletion src/tree-sitter-language-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,13 @@ class LayerHighlightIterator {
this.treeCursor.nodeIsNamed
);
const scopeName = applyLeafRules(value, this.treeCursor);
if (scopeName) {
const node = this.treeCursor.currentNode;
if (!node.childCount) {
return this.languageLayer.languageMode.grammar.idForScope(
scopeName,
node.text
);
} else if (scopeName) {
return this.languageLayer.languageMode.grammar.idForScope(scopeName);
}
}
Expand Down