Skip to content

Commit 19b4258

Browse files
authored
Merge pull request #3084 from jramosg/toggle-comment-unformatted-partial-selection
Toggle comment on unformatted partial selection Fixes #3083
2 parents 3e383d9 + dec3b7a commit 19b4258

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Changes to Calva.
55
## [Unreleased]
66

77
- Fix: [Toggle comments off, indent glitch](https://github.com/BetterThanTomorrow/calva/issues/3078)
8+
- Fix: [Toggle Line Comment can break structure for unformatted partial multiline selections](https://github.com/BetterThanTomorrow/calva/issues/3083)
89
- Fix: [Clojure-lsp not starting when offline](https://github.com/BetterThanTomorrow/calva/issues/1299)
910

1011
## [2.0.555] - 2026-02-19

src/edit.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ async function applyStructuralCommentsToSingleSelectionLines(
8686
) {
8787
const originalSelections = [...editor.selections];
8888
const singleSelection = editor.selections[0];
89+
const partialSelectionStartColumn =
90+
!singleSelection.isEmpty && affectedLineNumbers.length > 1
91+
? singleSelection.start.character
92+
: undefined;
8993
const descendingLineNumbers = [...new Set(affectedLineNumbers)].sort((a, b) => b - a);
9094
const affectedLineSet = new Set(affectedLineNumbers);
9195
const originalFirstNonWSMap = new Map<number, number>();
@@ -137,7 +141,11 @@ async function applyStructuralCommentsToSingleSelectionLines(
137141
affectedLineNumbers.length > 1
138142
? resolvedAlignedCommentColumn
139143
: originalInsertionColumnMap.get(lineNum) ?? firstNonWhitespace;
140-
const insertionColumn = Math.min(rawInsertionColumn, currentLine.text.length);
144+
const firstLineInsertionColumn =
145+
partialSelectionStartColumn !== undefined && lineNum === singleSelection.start.line
146+
? Math.max(rawInsertionColumn, partialSelectionStartColumn)
147+
: rawInsertionColumn;
148+
const insertionColumn = Math.min(firstLineInsertionColumn, currentLine.text.length);
141149
originalInsertionColumnMap.set(lineNum, insertionColumn);
142150
const insertionOffset = editor.document.offsetAt(
143151
new vscode.Position(lineNum, insertionColumn)

src/extension-test/integration/suite/toggle-comment-test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,20 @@ suite(suiteName, () => {
281281
);
282282
});
283283

284+
it('should structurally comment unformatted multiline partial selection in a defn body', async () => {
285+
assert.equal(
286+
await toggleCommentUsingActiveEditor('(defn foo []•(a |(b c•d•e)|•f))'),
287+
'(defn foo []•(a |;; (b c•;; d•;; e)|•f))'
288+
);
289+
});
290+
291+
it('should structurally comment unformatted nested multiline partial selection without breaking structure', async () => {
292+
assert.equal(
293+
await toggleCommentUsingActiveEditor('(defn foo []•(a |(b c•(d e•f)•g•h)|•i•j))'),
294+
'(defn foo []•(a |;; (b c•;; (d e•;; f)•;; g•;; h)|•i•j))'
295+
);
296+
});
297+
284298
it('should structurally comment multiline selection nested in parent form and preserve full selected range', async () => {
285299
assert.equal(
286300
await toggleCommentUsingActiveEditor('(x• (y |(a b• c)|)• z)'),

0 commit comments

Comments
 (0)