Skip to content

Commit

Permalink
Add multiselect support for sequence command
Browse files Browse the repository at this point in the history
  • Loading branch information
joshf26 authored and marclipovsky committed Sep 27, 2024
1 parent f34b237 commit 4697d1f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
12 changes: 6 additions & 6 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ const chicagoStyleTitleCase = require("chicago-capitalize");
const slugify = require("@sindresorhus/slugify");
const defaultFunction = (commandName, option) => (str) =>
_string[commandName](str, option);
const sequence = (str) => {
let initial;
const sequence = (str, multiselectData = {}) => {
return str.replace(/-?\d+/g, (n) => {
const isFirst = typeof initial !== "number";
initial = isFirst ? Number(n) : initial + 1;
return initial;
const isFirst = typeof multiselectData.offset !== "number";
multiselectData.offset = isFirst ? Number(n) : multiselectData.offset + 1;
return multiselectData.offset;
});
};
const increment = (str) => str.replace(/-?\d+/g, (n) => Number(n) + 1);
Expand Down Expand Up @@ -75,6 +74,7 @@ const stringFunction = async (commandName, context) => {
const selectionMap = {};
if (!editor) return;

let multiselectData = {};
editor.selections.forEach(async (selection, index) => {
const text = editor.document.getText(selection);
const textParts = text.split("\n");
Expand All @@ -87,7 +87,7 @@ const stringFunction = async (commandName, context) => {
.reduce((prev, curr) => prev.push(stringFunc(curr)) && prev, [])
.join("\n");
} else if (numberFunctionNames.includes(commandName)) {
replaced = commandNameFunctionMap[commandName](text);
replaced = commandNameFunctionMap[commandName](text, multiselectData);
} else {
stringFunc = commandNameFunctionMap[commandName];
replaced = textParts
Expand Down
13 changes: 10 additions & 3 deletions test/suite/extension.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,24 @@ suite("Extension Test Suite", () => {
"a14 b15 c16\n17d 18e 19f 20x y21 22z23",
],
["sequence", "-3 4 5 6 7", "-3 -2 -1 0 1"],
[
"sequence",
"1 2 3 7 8 9",
"4 5 6 7 8 9",
{ multiselectData: { offset: 3 } },
],
["utf8ToChar", "\\u0061\\u0062\\u0063\\u4e2d\\u6587\\ud83d\\udc96", "abc中文💖"],
["charToUtf8", "abc中文💖", "\\u0061\\u0062\\u0063\\u4e2d\\u6587\\ud83d\\udc96"],
];
suite("commandNameFunctionMap outputs correctly for all methods", () => {
tests.forEach(
([funcName, originalString, expectedString, { functionArg } = {}]) => {
test(`${funcName} returns ${expectedString} when called with ${originalString}`, () => {
([funcName, originalString, expectedString, { multiselectData, functionArg } = {}]) => {
const arguments = `${originalString}${multiselectData ? `, ${JSON.stringify(multiselectData)}` : ''}`;
test(`${funcName} returns ${expectedString} when called with ${arguments}`, () => {
const func = functionArg
? myExtension.commandNameFunctionMap[funcName](functionArg)
: myExtension.commandNameFunctionMap[funcName];
assert.equal(func(originalString), expectedString);
assert.equal(func(originalString, multiselectData), expectedString);
});
}
);
Expand Down

0 comments on commit 4697d1f

Please sign in to comment.