Add text getter to SyntaxNode

Co-Authored-By: Ashi Krishnan <queerviolet@github.com>
This commit is contained in:
Max Brunsfeld
2018-06-27 13:13:08 -07:00
parent 4d3916f74e
commit 613d4d65e9
3 changed files with 19 additions and 11 deletions

View File

@@ -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",

View File

@@ -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
}
}]
})

View File

@@ -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)