From d4337539ad5007a46da2351e8a44218bd5b7054a Mon Sep 17 00:00:00 2001 From: Andrew Dupont Date: Sat, 7 Sep 2024 17:34:25 -0700 Subject: [PATCH] Fix regression with indentation --- src/wasm-tree-sitter-language-mode.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/wasm-tree-sitter-language-mode.js b/src/wasm-tree-sitter-language-mode.js index 23fd98cb7f..49ea29883a 100644 --- a/src/wasm-tree-sitter-language-mode.js +++ b/src/wasm-tree-sitter-language-mode.js @@ -4654,7 +4654,7 @@ class IndentResolver { // We were able to resolve the `@match` capture, so we’ll be // returning early. scopeResolver.reset(); - let finalIndent = Math.max(matchIndentLevel - existingIndent, 0); + let finalIndent = Math.max(matchIndentLevel - Math.floor(existingIndent), 0); if (!options.skipEvent) { this.emitter.emit('did-suggest-indent', { currentRow: row, @@ -4716,9 +4716,13 @@ class IndentResolver { // to signify that other Phase 1 logic was ignored altogether. let finalIndent = baseline + indentDelta + dedentDelta; - // Finally, we might have to adjust for the existing leading whitespace - // if `options.preserveLeadingWhitespace` is `true`. - let adjustedIndent = Math.max(finalIndent - existingIndent, 0); + // Finally, we might have to adjust for the existing leading whitespace if + // `options.preserveLeadingWhitespace` is `true`. + // + // We call `Math.floor` because we should only subtract whole units of + // indentation here. “Leading whitespace” seems not to consider (for + // example) a single leading space character if `editor.tabLength` is `2`. + let adjustedIndent = Math.max(finalIndent - Math.floor(existingIndent), 0); // Emit an event with all this information. This makes it possible for // tooling to help a grammar author understand the indentation logic