From ab6f08101d9d8a94681c40663d0a699dde08c66c Mon Sep 17 00:00:00 2001 From: Andrew Dupont Date: Sat, 2 Mar 2024 17:15:10 -0800 Subject: [PATCH] Make divided folds end at the end of the preceding line much more often --- src/wasm-tree-sitter-language-mode.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/wasm-tree-sitter-language-mode.js b/src/wasm-tree-sitter-language-mode.js index 14d600ec97..18ead35aae 100644 --- a/src/wasm-tree-sitter-language-mode.js +++ b/src/wasm-tree-sitter-language-mode.js @@ -2388,13 +2388,21 @@ class FoldResolver { } } + // Returns `true` if there is no non-whitespace content on this position's + // row before this position's column. + positionIsNotPrecededByTextOnLine(position) { + let textForRow = this.buffer.lineForRow(position.row) + let precedingText = textForRow.substring(0, position.column) + return !(/\S/.test(precedingText)) + } + resolvePositionForDividedFold(capture) { let { name, node } = capture; if (name === 'fold.start') { return new Point(node.startPosition.row, Infinity); } else if (name === 'fold.end') { let end = node.startPosition; - if (end.column === 0) { + if (end.column === 0 || this.positionIsNotPrecededByTextOnLine(end)) { // If the fold ends at the start of the line, adjust it so that it // actually ends at the end of the previous line. This behavior is // implied in the existing specs.