|
| 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 | +} |
0 commit comments