Skip to content

Commit 3f13004

Browse files
Refactor: Move duplicateAndIncrement and duplicateAndDecrement logic to increment-decrement.ts
1 parent 9ec3b5a commit 3f13004

File tree

2 files changed

+75
-35
lines changed

2 files changed

+75
-35
lines changed

src/commands/increment-decrement.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as vscode from "vscode";
12
import { CommandFunction } from "./types";
23

34
export const increment: CommandFunction = (str: string) =>
@@ -6,5 +7,65 @@ export const increment: CommandFunction = (str: string) =>
67
export const decrement: CommandFunction = (str: string) =>
78
str.replace(/-?\d+/g, (n) => String(Number(n) - 1));
89

10+
// These functions are placeholders as the actual implementation is in the stringFunction
11+
// They're kept here for type consistency in the command registry
912
export const duplicateAndIncrement: CommandFunction = () => "";
1013
export const duplicateAndDecrement: CommandFunction = () => "";
14+
15+
// Helper function to handle duplicate and increment/decrement operations
16+
export function handleDuplicateAndIncrementDecrement(
17+
editor: vscode.TextEditor,
18+
selections: readonly vscode.Selection[],
19+
operation: (str: string) => string
20+
): {
21+
selectionMap: {
22+
[key: number]: { selection: vscode.Selection; replaced: string };
23+
};
24+
replacedSelections: string[];
25+
} {
26+
const selectionMap: {
27+
[key: number]: { selection: vscode.Selection; replaced: string };
28+
} = {};
29+
const replacedSelections: string[] = [];
30+
31+
for (const [index, selection] of selections.entries()) {
32+
const text = editor.document.getText(selection);
33+
const replaced = text + operation(text);
34+
35+
replacedSelections.push(replaced);
36+
selectionMap[index] = { selection, replaced };
37+
}
38+
39+
return { selectionMap, replacedSelections };
40+
}
41+
42+
// Helper function to update selections after duplicate operations
43+
export function updateSelectionsAfterDuplicate(
44+
editor: vscode.TextEditor,
45+
selectionMap: {
46+
[key: number]: { selection: vscode.Selection; replaced: string };
47+
}
48+
): vscode.Selection[] {
49+
return editor.selections.map((selection, index) => {
50+
const originalSelection = selectionMap[index].selection;
51+
const originalText = editor.document.getText(originalSelection);
52+
53+
// Calculate the start position of the duplicated text
54+
const startPos = originalSelection.end;
55+
56+
// Calculate the end position based on the original text length
57+
let endLine = startPos.line;
58+
let endChar = startPos.character + originalText.length;
59+
60+
// Handle multi-line selections
61+
const lines = originalText.split("\n");
62+
if (lines.length > 1) {
63+
endLine = startPos.line + lines.length - 1;
64+
// If multi-line, the end character should be the length of the last line
65+
endChar = lines[lines.length - 1].length;
66+
}
67+
68+
const endPos = new vscode.Position(endLine, endChar);
69+
return new vscode.Selection(startPos, endPos);
70+
});
71+
}

src/commands/index.ts

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
decrement,
1212
duplicateAndIncrement,
1313
duplicateAndDecrement,
14+
handleDuplicateAndIncrementDecrement,
15+
updateSelectionsAfterDuplicate,
1416
} from "./increment-decrement";
1517
import { sequence } from "./sequence";
1618
import { randomCase } from "./random-case";
@@ -75,15 +77,15 @@ export const stringFunction = async (
7577
return;
7678
}
7779

78-
const selectionMap: {
80+
let selectionMap: {
7981
[key: number]: { selection: vscode.Selection; replaced: string };
8082
} = {};
8183

8284
let multiselectData = {};
8385

8486
let stringFunc: (str: string) => string;
8587

86-
let replacedSelections = [];
88+
let replacedSelections: string[] = [];
8789

8890
if (functionNamesWithArgument.includes(commandName)) {
8991
const valueStr = await vscode.window.showInputBox();
@@ -107,16 +109,16 @@ export const stringFunction = async (
107109
commandName === "duplicateAndIncrement" ||
108110
commandName === "duplicateAndDecrement"
109111
) {
110-
for (const [index, selection] of editor.selections.entries()) {
111-
const text = editor.document.getText(selection);
112-
113-
const operation =
114-
commandName === "duplicateAndIncrement" ? increment : decrement;
115-
const replaced = text + operation(text);
112+
const operation =
113+
commandName === "duplicateAndIncrement" ? increment : decrement;
116114

117-
replacedSelections.push(replaced);
118-
selectionMap[index] = { selection, replaced };
119-
}
115+
const result = handleDuplicateAndIncrementDecrement(
116+
editor,
117+
editor.selections,
118+
operation as (str: string) => string
119+
);
120+
selectionMap = result.selectionMap;
121+
replacedSelections = result.replacedSelections;
120122
} else {
121123
for (const [index, selection] of editor.selections.entries()) {
122124
const text = editor.document.getText(selection);
@@ -139,30 +141,7 @@ export const stringFunction = async (
139141
commandName === "duplicateAndIncrement" ||
140142
commandName === "duplicateAndDecrement"
141143
) {
142-
const newSelections = editor.selections.map((selection, index) => {
143-
const originalSelection = selectionMap[index].selection;
144-
const originalText = editor.document.getText(originalSelection);
145-
146-
// Calculate the start position of the duplicated text
147-
const startPos = originalSelection.end;
148-
149-
// Calculate the end position based on the original text length
150-
let endLine = startPos.line;
151-
let endChar = startPos.character + originalText.length;
152-
153-
// Handle multi-line selections
154-
const lines = originalText.split("\n");
155-
if (lines.length > 1) {
156-
endLine = startPos.line + lines.length - 1;
157-
// If multi-line, the end character should be the length of the last line
158-
endChar = lines[lines.length - 1].length;
159-
}
160-
161-
const endPos = new vscode.Position(endLine, endChar);
162-
return new vscode.Selection(startPos, endPos);
163-
});
164-
165-
editor.selections = newSelections;
144+
editor.selections = updateSelectionsAfterDuplicate(editor, selectionMap);
166145
}
167146

168147
context.globalState.update("lastAction", commandName);

0 commit comments

Comments
 (0)