mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
Implement TreeSitterLanguageMode.scopeDescriptorForPosition
This commit is contained in:
@@ -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, {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user