From 85a14964fa230bf5f5636efb182fc4453c4a8c28 Mon Sep 17 00:00:00 2001 From: Ben Jervis Date: Tue, 13 Feb 2024 16:17:18 +1100 Subject: [PATCH] Respect indentation levels when commenting --- src/Playroom/CodeEditor/keymaps/comment.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Playroom/CodeEditor/keymaps/comment.ts b/src/Playroom/CodeEditor/keymaps/comment.ts index 91ee5d64..cc7a2a6e 100644 --- a/src/Playroom/CodeEditor/keymaps/comment.ts +++ b/src/Playroom/CodeEditor/keymaps/comment.ts @@ -39,9 +39,12 @@ export const wrapInComment = (cm: Editor) => { to = new Pos(to.line - 1); } - const existingContent = cm.getRange(from, to); - const existingIndent = - existingContent.length - existingContent.trimStart().length; + const fullContent = cm.getRange(new Pos(from.line, 0), to); + const existingIndent = fullContent.length - fullContent.trimStart().length; + + const selectedContent = cm.getRange(from, to); + const selectedLeadingWhitespace = + selectedContent.length - selectedContent.trimStart().length; const isMultiLineSelection = to.line !== from.line; @@ -58,7 +61,7 @@ export const wrapInComment = (cm: Editor) => { const newSelectionRangeFrom = new Pos( from.line, - from.ch + BLOCK_COMMENT_OFFSET + from.ch + BLOCK_COMMENT_OFFSET + selectedLeadingWhitespace ); const newSelectionRangeTo = new Pos(to.line, to.ch + toOffset); @@ -71,7 +74,7 @@ export const wrapInComment = (cm: Editor) => { cm.operation(() => { for (const range of [...tagRanges].reverse()) { - const newRangeFrom = new Pos(range.from.line, 0); + const newRangeFrom = new Pos(range.from.line, range.existingIndent); const newRangeTo = new Pos(range.to.line); const existingContent = cm.getRange(newRangeFrom, newRangeTo);