diff --git a/spec/tree-sitter-language-mode-spec.js b/spec/tree-sitter-language-mode-spec.js index 7897e3ded83..6bcd23ead66 100644 --- a/spec/tree-sitter-language-mode-spec.js +++ b/spec/tree-sitter-language-mode-spec.js @@ -1413,6 +1413,23 @@ describe('TreeSitterLanguageMode', () => { 'property.name' ]) }) + + it('includes the root scope name even when the given position is in trailing whitespace at EOF', () => { + const grammar = new TreeSitterGrammar(atom.grammars, jsGrammarPath, { + scopeName: 'source.js', + parser: 'tree-sitter-javascript', + scopes: { + program: 'source.js', + property_identifier: 'property.name' + } + }) + + buffer.setText('a; ') + buffer.setLanguageMode(new TreeSitterLanguageMode({buffer, grammar})) + expect(editor.scopeDescriptorForBufferPosition([0, 3]).getScopesArray()).toEqual([ + 'source.js' + ]) + }) }) describe('.bufferRangeForScopeAtPosition(selector?, position)', () => { diff --git a/src/tree-sitter-language-mode.js b/src/tree-sitter-language-mode.js index 1e364605df1..05aeaaa2b17 100644 --- a/src/tree-sitter-language-mode.js +++ b/src/tree-sitter-language-mode.js @@ -429,6 +429,9 @@ class TreeSitterLanguageMode { for (const scope of iterator.getOpenScopeIds()) { scopes.push(this.grammar.scopeNameForScopeId(scope, false)) } + if (scopes.length === 0 || scopes[0] !== this.grammar.scopeName) { + scopes.unshift(this.grammar.scopeName) + } return new ScopeDescriptor({scopes}) }