Make tree-sitter indent methods delegate to textmate ones for now

This commit is contained in:
Max Brunsfeld
2018-01-05 20:26:41 -08:00
parent fe4d14444f
commit 3f11fa57ee
2 changed files with 46 additions and 26 deletions

View File

@@ -74,10 +74,15 @@ class TextMateLanguageMode {
//
// Returns a {Number}.
suggestedIndentForBufferRow (bufferRow, tabLength, options) {
return this._suggestedIndentForTokenizedLineAtBufferRow(
const line = this.buffer.lineForRow(bufferRow)
const tokenizedLine = this.tokenizedLineForRow(bufferRow)
const iterator = tokenizedLine.getTokenIterator()
iterator.next()
const scopeDescriptor = new ScopeDescriptor({scopes: iterator.getScopes()})
return this._suggestedIndentForLineWithScopeAtBufferRow(
bufferRow,
this.buffer.lineForRow(bufferRow),
this.tokenizedLineForRow(bufferRow),
line,
scopeDescriptor,
tabLength,
options
)
@@ -90,10 +95,14 @@ class TextMateLanguageMode {
//
// Returns a {Number}.
suggestedIndentForLineAtBufferRow (bufferRow, line, tabLength) {
return this._suggestedIndentForTokenizedLineAtBufferRow(
const tokenizedLine = this.buildTokenizedLineForRowWithText(bufferRow, line)
const iterator = tokenizedLine.getTokenIterator()
iterator.next()
const scopeDescriptor = new ScopeDescriptor({scopes: iterator.getScopes()})
return this._suggestedIndentForLineWithScopeAtBufferRow(
bufferRow,
line,
this.buildTokenizedLineForRowWithText(bufferRow, line),
scopeDescriptor,
tabLength
)
}
@@ -111,7 +120,7 @@ class TextMateLanguageMode {
const currentIndentLevel = this.indentLevelForLine(line, tabLength)
if (currentIndentLevel === 0) return
const scopeDescriptor = this.scopeDescriptorForPosition([bufferRow, 0])
const scopeDescriptor = this.scopeDescriptorForPosition(new Point(bufferRow, 0))
const decreaseIndentRegex = this.decreaseIndentRegexForScopeDescriptor(scopeDescriptor)
if (!decreaseIndentRegex) return
@@ -138,11 +147,7 @@ class TextMateLanguageMode {
return desiredIndentLevel
}
_suggestedIndentForTokenizedLineAtBufferRow (bufferRow, line, tokenizedLine, tabLength, options) {
const iterator = tokenizedLine.getTokenIterator()
iterator.next()
const scopeDescriptor = new ScopeDescriptor({scopes: iterator.getScopes()})
_suggestedIndentForLineWithScopeAtBufferRow (bufferRow, line, scopeDescriptor, tabLength, options) {
const increaseIndentRegex = this.increaseIndentRegexForScopeDescriptor(scopeDescriptor)
const decreaseIndentRegex = this.decreaseIndentRegexForScopeDescriptor(scopeDescriptor)
const decreaseNextIndentRegex = this.decreaseNextIndentRegexForScopeDescriptor(scopeDescriptor)

View File

@@ -2,6 +2,7 @@ const {Document} = require('tree-sitter')
const {Point, Range, Emitter} = require('atom')
const ScopeDescriptor = require('./scope-descriptor')
const TokenizedLine = require('./tokenized-line')
const TextMateLanguageMode = require('./text-mate-language-mode')
let nextId = 0
@@ -19,6 +20,10 @@ class TreeSitterLanguageMode {
this.rootScopeDescriptor = new ScopeDescriptor({scopes: [this.grammar.id]})
this.emitter = new Emitter()
this.isFoldableCache = []
// TODO: Remove this once TreeSitterLanguageMode implements its own auto-indentation system. This
// is temporarily needed in order to delegate to the TextMateLanguageMode's auto-indent system.
this.regexesByPattern = {}
}
getLanguageId () {
@@ -83,24 +88,22 @@ class TreeSitterLanguageMode {
*/
suggestedIndentForLineAtBufferRow (row, line, tabLength) {
return this.suggestedIndentForBufferRow(row, tabLength)
return this._suggestedIndentForLineWithScopeAtBufferRow(
row,
line,
this.rootScopeDescriptor,
tabLength
)
}
suggestedIndentForBufferRow (row, tabLength, options) {
let precedingRow
if (!options || options.skipBlankLines !== false) {
precedingRow = this.buffer.previousNonBlankRow(row)
if (precedingRow == null) return 0
} else {
precedingRow = row - 1
if (precedingRow < 0) return 0
}
return this.indentLevelForLine(this.buffer.lineForRow(precedingRow), tabLength)
}
suggestedIndentForEditedBufferRow (row) {
return null
return this._suggestedIndentForLineWithScopeAtBufferRow(
row,
this.buffer.lineForRow(row),
this.rootScopeDescriptor,
tabLength,
options
)
}
indentLevelForLine (line, tabLength = tabLength) {
@@ -508,3 +511,15 @@ class TreeSitterTextBufferInput {
function last (array) {
return array[array.length - 1]
}
// TODO: Remove this once TreeSitterLanguageMode implements its own auto-indent system.
[
'_suggestedIndentForLineWithScopeAtBufferRow',
'suggestedIndentForEditedBufferRow',
'increaseIndentRegexForScopeDescriptor',
'decreaseIndentRegexForScopeDescriptor',
'decreaseNextIndentRegexForScopeDescriptor',
'regexForPattern'
].forEach(methodName => {
module.exports.prototype[methodName] = TextMateLanguageMode.prototype[methodName]
})