mirror of
https://github.com/atom/atom.git
synced 2026-01-26 07:19:06 -05:00
Add text getter to SyntaxNode
Co-Authored-By: Ashi Krishnan <queerviolet@github.com>
This commit is contained in:
@@ -72,7 +72,7 @@
|
||||
"sinon": "1.17.4",
|
||||
"temp": "^0.8.3",
|
||||
"text-buffer": "13.14.3",
|
||||
"tree-sitter": "0.12.14",
|
||||
"tree-sitter": "0.12.15",
|
||||
"typescript-simple": "1.0.0",
|
||||
"underscore-plus": "^1.6.8",
|
||||
"winreg": "^1.2.1",
|
||||
|
||||
@@ -307,12 +307,14 @@ describe('TreeSitterLanguageMode', () => {
|
||||
},
|
||||
injectionPoints: [{
|
||||
type: 'call_expression',
|
||||
language: (node, getText) => {
|
||||
language (node) {
|
||||
if (node.lastChild.type === 'template_string' && node.firstChild.type === 'identifier') {
|
||||
return getText(node.firstChild)
|
||||
return node.firstChild.text
|
||||
}
|
||||
},
|
||||
content: node => node.lastChild
|
||||
content (node) {
|
||||
return node.lastChild
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
|
||||
@@ -10,7 +10,18 @@ let nextId = 0
|
||||
const MAX_RANGE = new Range(Point.ZERO, Point.INFINITY).freeze()
|
||||
|
||||
class TreeSitterLanguageMode {
|
||||
static _patchSyntaxNode() {
|
||||
if (!Parser.SyntaxNode.prototype.hasOwnProperty('text')) {
|
||||
Object.defineProperty(Parser.SyntaxNode.prototype, 'text', {
|
||||
get () {
|
||||
return this.tree.buffer.getTextInRange(new Range(this.startPosition, this.endPosition))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
constructor ({buffer, grammar, config, grammars}) {
|
||||
TreeSitterLanguageMode._patchSyntaxNode()
|
||||
this.id = nextId++
|
||||
this.buffer = buffer
|
||||
this.grammar = grammar
|
||||
@@ -25,7 +36,6 @@ class TreeSitterLanguageMode {
|
||||
this.isFoldableCache = []
|
||||
this.hasQueuedParse = false
|
||||
|
||||
this.getNodeText = this.getNodeText.bind(this)
|
||||
this.grammarForLanguageString = this.grammarForLanguageString.bind(this)
|
||||
this.emitRangeUpdate = this.emitRangeUpdate.bind(this)
|
||||
|
||||
@@ -314,10 +324,6 @@ class TreeSitterLanguageMode {
|
||||
return this.getRangeForSyntaxNodeContainingRange(new Range(position, position))
|
||||
}
|
||||
|
||||
getNodeText (node) {
|
||||
return this.buffer.getTextInRange(new Range(node.startPosition, node.endPosition))
|
||||
}
|
||||
|
||||
/*
|
||||
Section - Backward compatibility shims
|
||||
*/
|
||||
@@ -456,7 +462,6 @@ class LanguageLayer {
|
||||
injectionsMarkerLayer,
|
||||
grammarForLanguageString,
|
||||
emitRangeUpdate,
|
||||
getNodeText
|
||||
} = this.languageMode
|
||||
|
||||
let includedRanges
|
||||
@@ -470,6 +475,7 @@ class LanguageLayer {
|
||||
syncOperationLimit: 1000,
|
||||
includedRanges
|
||||
})
|
||||
tree.buffer = this.languageMode.buffer
|
||||
|
||||
let affectedRange
|
||||
let existingInjectionMarkers
|
||||
@@ -509,7 +515,7 @@ class LanguageLayer {
|
||||
)
|
||||
|
||||
for (const node of nodes) {
|
||||
const languageName = injectionPoint.language(node, getNodeText)
|
||||
const languageName = injectionPoint.language(node)
|
||||
if (!languageName) continue
|
||||
|
||||
const grammar = grammarForLanguageString(languageName)
|
||||
|
||||
Reference in New Issue
Block a user