Skip to content

Commit b7d604b

Browse files
Propertly duplicate selection for multi-line and multi-select for commands duplicateAndIncrement and duplicateAndDecrement
fixes #59
1 parent ba7bb42 commit b7d604b

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

src/commands.ts

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ const commandNameFunctionMap: { [key: string]: CommandFunction } = {
8282
repeat: (n: number) => defaultFunction("repeat", n),
8383
increment,
8484
decrement,
85-
duplicateAndIncrement: (str: string) => str + increment(str),
86-
duplicateAndDecrement: (str: string) => str + decrement(str),
85+
duplicateAndIncrement: () => "",
86+
duplicateAndDecrement: () => "",
8787
sequence,
8888
utf8ToChar: (str: string) =>
8989
str
@@ -153,12 +153,28 @@ export const stringFunction = async (
153153
stringFunc = commandNameFunctionMap[commandName] as StringFunction;
154154
}
155155

156-
for (const [index, selection] of editor.selections.entries()) {
157-
const text = editor.document.getText(selection);
158-
const textParts = text.split("\n");
159-
const replaced = textParts.map((part) => stringFunc(part)).join("\n");
160-
replacedSelections.push(replaced);
161-
selectionMap[index] = { selection, replaced };
156+
if (
157+
commandName === "duplicateAndIncrement" ||
158+
commandName === "duplicateAndDecrement"
159+
) {
160+
for (const [index, selection] of editor.selections.entries()) {
161+
const text = editor.document.getText(selection);
162+
163+
const operation =
164+
commandName === "duplicateAndIncrement" ? increment : decrement;
165+
const replaced = text + operation(text);
166+
167+
replacedSelections.push(replaced);
168+
selectionMap[index] = { selection, replaced };
169+
}
170+
} else {
171+
for (const [index, selection] of editor.selections.entries()) {
172+
const text = editor.document.getText(selection);
173+
const textParts = text.split("\n");
174+
const replaced = textParts.map((part) => stringFunc(part)).join("\n");
175+
replacedSelections.push(replaced);
176+
selectionMap[index] = { selection, replaced };
177+
}
162178
}
163179

164180
if (shouldApply) {
@@ -168,6 +184,37 @@ export const stringFunction = async (
168184
});
169185
});
170186

187+
// Set the selection to the duplicated part for duplicateAndIncrement and duplicateAndDecrement
188+
if (
189+
commandName === "duplicateAndIncrement" ||
190+
commandName === "duplicateAndDecrement"
191+
) {
192+
const newSelections = editor.selections.map((selection, index) => {
193+
const originalSelection = selectionMap[index].selection;
194+
const originalText = editor.document.getText(originalSelection);
195+
196+
// Calculate the start position of the duplicated text
197+
const startPos = originalSelection.end;
198+
199+
// Calculate the end position based on the original text length
200+
let endLine = startPos.line;
201+
let endChar = startPos.character + originalText.length;
202+
203+
// Handle multi-line selections
204+
const lines = originalText.split("\n");
205+
if (lines.length > 1) {
206+
endLine = startPos.line + lines.length - 1;
207+
// If multi-line, the end character should be the length of the last line
208+
endChar = lines[lines.length - 1].length;
209+
}
210+
211+
const endPos = new vscode.Position(endLine, endChar);
212+
return new vscode.Selection(startPos, endPos);
213+
});
214+
215+
editor.selections = newSelections;
216+
}
217+
171218
context.globalState.update("lastAction", commandName);
172219
}
173220

src/test/extension.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ suite("Extension Test Suite", () => {
468468

469469
assert.strictEqual(
470470
output,
471-
/* a1 b2 c3 4d 5e 6f 12x y23 34z45\n */ "a1 b2 c3 4d 5e 6f 12x y23 34z45a2 b3 c4 5d 6e 7f 13x y24 35z46"
471+
/* a1 b2 c3 4d 5e 6f 12x y23 34z45\n */ "a2 b3 c4 5d 6e 7f 13x y24 35z46"
472472
);
473473
});
474474

@@ -485,7 +485,7 @@ suite("Extension Test Suite", () => {
485485

486486
assert.strictEqual(
487487
output,
488-
/* a1 b2 c3 4d 5e 6f 12x y23 34z45\n */ "a1 b2 c3 4d 5e 6f 12x y23 34z45a0 b1 c2 3d 4e 5f 11x y22 33z44"
488+
/* a1 b2 c3 4d 5e 6f 12x y23 34z45\n */ "a0 b1 c2 3d 4e 5f 11x y22 33z44"
489489
);
490490
});
491491

0 commit comments

Comments
 (0)