Make scopeDescriptorForPosition work correctly between tokens

Fixes atom/bracket-matcher#365
This commit is contained in:
Max Brunsfeld
2018-11-08 16:45:48 -08:00
parent b3393eabbe
commit 108b232107
2 changed files with 26 additions and 2 deletions

View File

@@ -1517,6 +1517,27 @@ describe('TreeSitterLanguageMode', () => {
'source.js'
])
})
it('works when the given position is between tokens', () => {
const grammar = new TreeSitterGrammar(atom.grammars, jsGrammarPath, {
scopeName: 'source.js',
parser: 'tree-sitter-javascript',
scopes: {
program: 'source.js',
comment: 'comment.block',
}
})
buffer.setText('a // b')
buffer.setLanguageMode(new TreeSitterLanguageMode({buffer, grammar}))
expect(editor.scopeDescriptorForBufferPosition([0, 2]).getScopesArray()).toEqual([
'source.js'
])
expect(editor.scopeDescriptorForBufferPosition([0, 3]).getScopesArray()).toEqual([
'source.js',
'comment.block'
])
})
})
describe('.syntaxTreeScopeDescriptorForPosition', () => {

View File

@@ -479,13 +479,16 @@ class TreeSitterLanguageMode {
}
scopeDescriptorForPosition (point) {
point = Point.fromObject(point)
const iterator = this.buildHighlightIterator()
const scopes = []
for (const scope of iterator.seek(point)) {
scopes.push(this.grammar.scopeNameForScopeId(scope))
}
for (const scope of iterator.getOpenScopeIds()) {
scopes.push(this.grammar.scopeNameForScopeId(scope))
if (point.isEqual(iterator.getPosition())) {
for (const scope of iterator.getOpenScopeIds()) {
scopes.push(this.grammar.scopeNameForScopeId(scope))
}
}
if (scopes.length === 0 || scopes[0] !== this.grammar.scopeName) {
scopes.unshift(this.grammar.scopeName)