1-
21// const log = console.debug // in dev
3- const log = ( ) => { } // in production
2+ const log = ( ) => { } ; // in production
43
54module . exports = class TreeIndenter {
6- constructor ( languageMode , scopes = undefined ) {
7- this . languageMode = languageMode
8- this . scopes = scopes || languageMode . config . get ( 'editor.scopes' ,
9- { scope : this . languageMode . rootScopeDescriptor } )
10- log ( '[TreeIndenter] constructor' , this . scopes )
5+ constructor ( languageMode , scopes = undefined ) {
6+ this . languageMode = languageMode ;
7+ this . scopes =
8+ scopes ||
9+ languageMode . config . get ( 'editor.scopes' , {
10+ scope : this . languageMode . rootScopeDescriptor
11+ } ) ;
12+ log ( '[TreeIndenter] constructor' , this . scopes ) ;
1113 }
1214
1315 /** tree indenter is configured for this language */
14- get isConfigured ( ) {
15- return ( ! ! this . scopes )
16+ get isConfigured ( ) {
17+ return ! ! this . scopes ;
1618 }
1719
1820 // Given a position, walk up the syntax tree, to find the highest level
1921 // node that still starts here. This is to identify the column where this
2022 // node (e.g., an HTML closing tag) ends.
21- _getHighestSyntaxNodeAtPosition ( row , column = null ) {
23+ _getHighestSyntaxNodeAtPosition ( row , column = null ) {
2224 if ( column == null ) {
2325 // Find the first character on the row that is not whitespace + 1
24- column = this . languageMode . buffer . lineForRow ( row ) . search ( / \S / ) + 1
26+ column = this . languageMode . buffer . lineForRow ( row ) . search ( / \S / ) + 1 ;
2527 }
2628
27- let syntaxNode
29+ let syntaxNode ;
2830 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
31+ syntaxNode = this . languageMode . getSyntaxNodeAtPosition ( { row, column } ) ;
32+ while (
33+ syntaxNode &&
34+ syntaxNode . parent &&
35+ syntaxNode . parent . startPosition . row === syntaxNode . startPosition . row &&
36+ syntaxNode . parent . endPosition . row === syntaxNode . startPosition . row &&
37+ syntaxNode . parent . startPosition . column ===
38+ syntaxNode . startPosition . column
3439 ) {
35- syntaxNode = syntaxNode . parent
40+ syntaxNode = syntaxNode . parent ;
3641 }
37- return syntaxNode
42+ return syntaxNode ;
3843 }
3944 }
4045
@@ -47,74 +52,87 @@ module.exports = class TreeIndenter {
4752 It might make more sense to reverse the direction of this walk, i.e.,
4853 go from root to leaf instead.
4954 */
50- _treeWalk ( node , lastScope = null ) {
55+ _treeWalk ( node , lastScope = null ) {
5156 if ( node == null || node . parent == null ) {
52- return 0
57+ return 0 ;
5358 } else {
54- let increment = 0
59+ let increment = 0 ;
5560
5661 const notFirstOrLastSibling =
57- ( node . previousSibling != null && node . nextSibling != null )
62+ node . previousSibling != null && node . nextSibling != null ;
5863
59- const isScope = this . scopes . indent [ node . parent . type ]
60- notFirstOrLastSibling && isScope && increment ++
64+ const isScope = this . scopes . indent [ node . parent . type ] ;
65+ notFirstOrLastSibling && isScope && increment ++ ;
6166
62- const isScope2 = this . scopes . indentExceptFirst [ node . parent . type ]
63- ! increment && isScope2 && node . previousSibling != null && increment ++
67+ const isScope2 = this . scopes . indentExceptFirst [ node . parent . type ] ;
68+ ! increment && isScope2 && node . previousSibling != null && increment ++ ;
6469
65- const isScope3 = this . scopes . indentExceptFirstOrBlock [ node . parent . type ]
66- ! increment && isScope3 && node . previousSibling != null && increment ++
70+ const isScope3 = this . scopes . indentExceptFirstOrBlock [ node . parent . type ] ;
71+ ! increment && isScope3 && node . previousSibling != null && increment ++ ;
6772
6873 // 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
74+ let typeDent = 0 ;
75+ this . scopes . types . indent [ node . type ] && typeDent ++ ;
76+ this . scopes . types . outdent [ node . type ] && increment && typeDent -- ;
77+ increment += typeDent ;
7378
7479 // check whether the last (lower) indentation happend due to a scope that
7580 // started on the same row and ends directly before this.
76- if ( lastScope && increment > 0 &&
81+ if (
82+ lastScope &&
83+ increment > 0 &&
7784 // previous (lower) scope was a two-sided scope, reduce if starts on
7885 // same row and ends right before
7986 // TODO: this currently only works for scopes that have a single-character
8087 // closing delimiter (like statement_blocks, but not HTML, for instance).
8188 ( ( node . parent . startPosition . row === lastScope . node . startPosition . row &&
82- ( node . parent . endIndex <= lastScope . node . endIndex + 1 ) ) ||
89+ node . parent . endIndex <= lastScope . node . endIndex + 1 ) ||
8390 // 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
91+ ( isScope3 && lastScope . node . endIndex === node . endIndex ) )
92+ ) {
93+ log ( 'ignoring repeat' , node . parent . type , lastScope ) ;
94+ increment = 0 ;
8795 }
8896
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
97+ log ( 'treewalk' , {
98+ node,
99+ notFirstOrLastSibling,
100+ type : node . parent . type ,
101+ increment
102+ } ) ;
103+ const newLastScope =
104+ isScope || isScope2 ? { node : node . parent } : lastScope ;
105+ return this . _treeWalk ( node . parent , newLastScope ) + increment ;
92106 }
93107 }
94108
95- suggestedIndentForBufferRow ( row , tabLength , options ) {
109+ suggestedIndentForBufferRow ( row , tabLength , options ) {
96110 // get current indentation for row
97- const line = this . languageMode . buffer . lineForRow ( row )
98- const currentIndentation = this . languageMode . indentLevelForLine ( line , tabLength )
111+ const line = this . languageMode . buffer . lineForRow ( row ) ;
112+ const currentIndentation = this . languageMode . indentLevelForLine (
113+ line ,
114+ tabLength
115+ ) ;
99116
100- const syntaxNode = this . _getHighestSyntaxNodeAtPosition ( row )
117+ const syntaxNode = this . _getHighestSyntaxNodeAtPosition ( row ) ;
101118 if ( ! syntaxNode ) {
102- return 0
119+ return 0 ;
103120 }
104- let indentation = this . _treeWalk ( syntaxNode )
121+ let indentation = this . _treeWalk ( syntaxNode ) ;
105122
106123 // Special case for comments
107- if ( syntaxNode . type === 'comment' &&
124+ if (
125+ syntaxNode . type === 'comment' &&
108126 syntaxNode . startPosition . row < row &&
109- syntaxNode . endPosition . row > row ) {
110- indentation += 1
127+ syntaxNode . endPosition . row > row
128+ ) {
129+ indentation += 1 ;
111130 }
112131
113132 if ( options && options . preserveLeadingWhitespace ) {
114- indentation -= currentIndentation
133+ indentation -= currentIndentation ;
115134 }
116135
117- return indentation
136+ return indentation ;
118137 }
119-
120- }
138+ } ;
0 commit comments