Implement TreeSitterLanguageMode.scopeDescriptorForPosition

This commit is contained in:
Max Brunsfeld
2017-12-15 17:10:20 -08:00
parent 4adfba47cc
commit c844a253e0
2 changed files with 33 additions and 1 deletions

View File

@@ -410,6 +410,31 @@ describe('TreeSitterLanguageMode', () => {
})
})
describe('.scopeDescriptorForPosition', () => {
it('returns a scope descriptor representing the given position in the syntax tree', () => {
const grammar = new TreeSitterGrammar(atom.grammars, jsGrammarPath, {
id: 'javascript',
parser: 'tree-sitter-javascript'
})
buffer.setLanguageMode(new TreeSitterLanguageMode({buffer, grammar}))
buffer.setText('foo({bar: baz});')
editor.screenLineForScreenRow(0)
expect(editor.scopeDescriptorForBufferPosition({row: 0, column: 6}).getScopesArray()).toEqual([
'javascript',
'program',
'expression_statement',
'call_expression',
'arguments',
'object',
'pair',
'property_identifier'
])
})
})
describe('TextEditor.selectLargerSyntaxNode and .selectSmallerSyntaxNode', () => {
it('expands and contract the selection based on the syntax tree', () => {
const grammar = new TreeSitterGrammar(atom.grammars, jsGrammarPath, {

View File

@@ -293,7 +293,14 @@ class TreeSitterLanguageMode {
}
scopeDescriptorForPosition (point) {
return this.rootScopeDescriptor
const result = []
let node = this.document.rootNode.descendantForPosition(point)
while (node) {
result.push(node.type)
node = node.parent
}
result.push(this.grammar.id)
return new ScopeDescriptor({scopes: result.reverse()})
}
hasTokenForSelector (scopeSelector) {