Skip to content

Commit 4697d1f

Browse files
joshf26Marc Lipovsky
authored andcommitted
Add multiselect support for sequence command
1 parent f34b237 commit 4697d1f

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

extension.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ const chicagoStyleTitleCase = require("chicago-capitalize");
55
const slugify = require("@sindresorhus/slugify");
66
const defaultFunction = (commandName, option) => (str) =>
77
_string[commandName](str, option);
8-
const sequence = (str) => {
9-
let initial;
8+
const sequence = (str, multiselectData = {}) => {
109
return str.replace(/-?\d+/g, (n) => {
11-
const isFirst = typeof initial !== "number";
12-
initial = isFirst ? Number(n) : initial + 1;
13-
return initial;
10+
const isFirst = typeof multiselectData.offset !== "number";
11+
multiselectData.offset = isFirst ? Number(n) : multiselectData.offset + 1;
12+
return multiselectData.offset;
1413
});
1514
};
1615
const increment = (str) => str.replace(/-?\d+/g, (n) => Number(n) + 1);
@@ -75,6 +74,7 @@ const stringFunction = async (commandName, context) => {
7574
const selectionMap = {};
7675
if (!editor) return;
7776

77+
let multiselectData = {};
7878
editor.selections.forEach(async (selection, index) => {
7979
const text = editor.document.getText(selection);
8080
const textParts = text.split("\n");
@@ -87,7 +87,7 @@ const stringFunction = async (commandName, context) => {
8787
.reduce((prev, curr) => prev.push(stringFunc(curr)) && prev, [])
8888
.join("\n");
8989
} else if (numberFunctionNames.includes(commandName)) {
90-
replaced = commandNameFunctionMap[commandName](text);
90+
replaced = commandNameFunctionMap[commandName](text, multiselectData);
9191
} else {
9292
stringFunc = commandNameFunctionMap[commandName];
9393
replaced = textParts

test/suite/extension.test.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,24 @@ suite("Extension Test Suite", () => {
9595
"a14 b15 c16\n17d 18e 19f 20x y21 22z23",
9696
],
9797
["sequence", "-3 4 5 6 7", "-3 -2 -1 0 1"],
98+
[
99+
"sequence",
100+
"1 2 3 7 8 9",
101+
"4 5 6 7 8 9",
102+
{ multiselectData: { offset: 3 } },
103+
],
98104
["utf8ToChar", "\\u0061\\u0062\\u0063\\u4e2d\\u6587\\ud83d\\udc96", "abc中文💖"],
99105
["charToUtf8", "abc中文💖", "\\u0061\\u0062\\u0063\\u4e2d\\u6587\\ud83d\\udc96"],
100106
];
101107
suite("commandNameFunctionMap outputs correctly for all methods", () => {
102108
tests.forEach(
103-
([funcName, originalString, expectedString, { functionArg } = {}]) => {
104-
test(`${funcName} returns ${expectedString} when called with ${originalString}`, () => {
109+
([funcName, originalString, expectedString, { multiselectData, functionArg } = {}]) => {
110+
const arguments = `${originalString}${multiselectData ? `, ${JSON.stringify(multiselectData)}` : ''}`;
111+
test(`${funcName} returns ${expectedString} when called with ${arguments}`, () => {
105112
const func = functionArg
106113
? myExtension.commandNameFunctionMap[funcName](functionArg)
107114
: myExtension.commandNameFunctionMap[funcName];
108-
assert.equal(func(originalString), expectedString);
115+
assert.equal(func(originalString, multiselectData), expectedString);
109116
});
110117
}
111118
);

0 commit comments

Comments
 (0)