Skip to content

Commit c8d52da

Browse files
committed
Implementing tree-sitter based indentation logic
- previously developed and tested in the sane-indentation package (> 0.9). Refer to atom/language-javascript#594 (comment) By itself this does nothing. The new logic is only used if the language package for the current language contains the necessary configuration (e.g., which scopes to indent on). So this PR goes together with, e.g., FILL-ME-IN in language-javascript. Updated: now without the need for 'precedingRowCondition' callbacks in the languages-specific configuration
1 parent 44e5e71 commit c8d52da

File tree

4 files changed

+139
-9
lines changed

4 files changed

+139
-9
lines changed

apm/package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tree-indenter.js

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
2+
// const log = console.debug // in dev
3+
const log = () => {} // in production
4+
5+
module.exports = class TreeIndenter {
6+
constructor (languageMode) {
7+
this.languageMode = languageMode
8+
this.scopes = languageMode.config.get('editor.scopes',
9+
{scope: this.languageMode.rootScopeDescriptor})
10+
log('[TreeIndenter] constructor', this.scopes)
11+
}
12+
13+
/** tree indenter is configured for this language */
14+
get isConfigured () {
15+
return (!!this.scopes)
16+
}
17+
18+
// Given a position, walk up the syntax tree, to find the highest level
19+
// node that still starts here. This is to identify the column where this
20+
// node (e.g., an HTML closing tag) ends.
21+
_getHighestSyntaxNodeAtPosition (row, column = null) {
22+
if (column == null) {
23+
// Find the first character on the row that is not whitespace + 1
24+
column = this.languageMode.buffer.lineForRow(row).search(/\S/) + 1
25+
}
26+
27+
let syntaxNode
28+
if (column >= 0) {
29+
syntaxNode = this.languageMode.getSyntaxNodeAtPosition({row, column})
30+
while (syntaxNode && syntaxNode.parent &&
31+
syntaxNode.parent.startPosition.row === syntaxNode.startPosition.row &&
32+
syntaxNode.parent.endPosition.row === syntaxNode.startPosition.row &&
33+
syntaxNode.parent.startPosition.column === syntaxNode.startPosition.column
34+
) {
35+
syntaxNode = syntaxNode.parent
36+
}
37+
return syntaxNode
38+
}
39+
}
40+
41+
/** Walk up the tree. Everytime we meet a scope type, check whether we
42+
are coming from the first (resp. last) child. If so, we are opening
43+
(resp. closing) that scope, i.e., do not count it. Otherwise, add 1.
44+
45+
This is the core function.
46+
47+
It might make more sense to reverse the direction of this walk, i.e.,
48+
go from root to leaf instead.
49+
*/
50+
_treeWalk (node, lastScope = null) {
51+
if (node == null || node.parent == null) {
52+
return 0
53+
} else {
54+
let increment = 0
55+
56+
const notFirstOrLastSibling =
57+
(node.previousSibling != null && node.nextSibling != null)
58+
59+
const isScope = this.scopes.indent[node.parent.type]
60+
notFirstOrLastSibling && isScope && increment++
61+
62+
const isScope2 = this.scopes.indentExceptFirst[node.parent.type]
63+
!increment && isScope2 && node.previousSibling != null && increment++
64+
65+
const isScope3 = this.scopes.indentExceptFirstOrBlock[node.parent.type]
66+
!increment && isScope3 && node.previousSibling != null && increment++
67+
68+
// apply current row, single line, type-based rules, e.g., 'else' or 'private:'
69+
let typeDent = 0
70+
this.scopes.types.indent[node.type] && typeDent++
71+
this.scopes.types.outdent[node.type] && increment && typeDent--
72+
increment += typeDent
73+
74+
// check whether the last (lower) indentation happend due to a scope that
75+
// started on the same row and ends directly before this.
76+
if (lastScope && increment > 0 &&
77+
// previous (lower) scope was a two-sided scope, reduce if starts on
78+
// same row and ends right before
79+
// TODO: this currently only works for scopes that have a single-character
80+
// closing delimiter (like statement_blocks, but not HTML, for instance).
81+
((node.parent.startPosition.row === lastScope.node.startPosition.row &&
82+
(node.parent.endIndex <= lastScope.node.endIndex + 1)) ||
83+
// or this is a special scope (like if, while) and it's ends coincide
84+
(isScope3 && lastScope.node.endIndex === node.endIndex))) {
85+
log('ignoring repeat', node.parent.type, lastScope)
86+
increment = 0
87+
}
88+
89+
log('treewalk', {node, notFirstOrLastSibling, type: node.parent.type, increment})
90+
const newLastScope = (isScope || isScope2 ? {node: node.parent} : lastScope)
91+
return this._treeWalk(node.parent, newLastScope) + increment
92+
}
93+
}
94+
95+
suggestedIndentForBufferRow (row, tabLength, options) {
96+
// get current indentation for row
97+
const line = this.languageMode.buffer.lineForRow(row)
98+
const currentIndentation = this.languageMode.indentLevelForLine(line, tabLength)
99+
100+
const syntaxNode = this._getHighestSyntaxNodeAtPosition(row)
101+
if (!syntaxNode) {
102+
return 0
103+
}
104+
let indentation = this._treeWalk(syntaxNode)
105+
106+
// Special case for comments
107+
if (syntaxNode.type === 'comment' &&
108+
syntaxNode.startPosition.row < row &&
109+
syntaxNode.endPosition.row > row) {
110+
indentation += 1
111+
}
112+
113+
if (options && options.preserveLeadingWhitespace) {
114+
indentation -= currentIndentation
115+
}
116+
117+
return indentation
118+
}
119+
120+
}

src/tree-sitter-language-mode.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const Token = require('./token')
77
const TokenizedLine = require('./tokenized-line')
88
const TextMateLanguageMode = require('./text-mate-language-mode')
99
const {matcherForSelector} = require('./selectors')
10+
const TreeIndenter = require('./tree-indenter')
1011

1112
let nextId = 0
1213
const MAX_RANGE = new Range(Point.ZERO, Point.INFINITY).freeze()
@@ -188,13 +189,22 @@ class TreeSitterLanguageMode {
188189
}
189190

190191
suggestedIndentForBufferRow (row, tabLength, options) {
191-
return this._suggestedIndentForLineWithScopeAtBufferRow(
192-
row,
193-
this.buffer.lineForRow(row),
194-
this.rootScopeDescriptor,
195-
tabLength,
196-
options
197-
)
192+
if (!this.treeIndenter) {
193+
this.treeIndenter = new TreeIndenter(this)
194+
}
195+
196+
if (this.treeIndenter.isConfigured) {
197+
const indent = this.treeIndenter.suggestedIndentForBufferRow(row, tabLength, options)
198+
return indent
199+
} else {
200+
return this._suggestedIndentForLineWithScopeAtBufferRow(
201+
row,
202+
this.buffer.lineForRow(row),
203+
this.rootScopeDescriptor,
204+
tabLength,
205+
options
206+
)
207+
}
198208
}
199209

200210
indentLevelForLine (line, tabLength = tabLength) {

0 commit comments

Comments
 (0)