From a74add4dc001c4b36d16307fdd2cdc946743eeee Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Thu, 5 Jun 2025 17:38:40 -0500 Subject: [PATCH] command to increment number in varname on each line --- package-lock.json | 8 ++++---- package.json | 12 ++++++++++-- src/commands/Lines.kiss | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 33607cc8..e9727cf2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { - "name": "kiss-vscode", - "version": "0.0.18", + "name": "kiss-lang-vscode", + "version": "0.1.1", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "kiss-vscode", - "version": "0.0.18", + "name": "kiss-lang-vscode", + "version": "0.1.1", "devDependencies": { "vsce": "^2.15.0" }, diff --git a/package.json b/package.json index 53d1efca..a1206cd7 100644 --- a/package.json +++ b/package.json @@ -108,6 +108,14 @@ { "title": "kiss-lang-vscode: Sort the selected lines lexicographically from the end of each line", "command": "kiss-lang-vscode.sortLinesSyncEOL" + }, + { + "title": "kiss-lang-vscode: Find and replace a token, matching case", + "command": "kiss-lang-vscode.findReplaceTokenMatchCase" + }, + { + "title": "kiss-lang-vscode: Find text suffixed by a non-negative integer, and replace it, incrementing the number by line", + "command": "kiss-lang-vscode.findReplaceIncrement" } ], "languages": [ @@ -130,7 +138,7 @@ "devDependencies": { "vsce": "^2.15.0" }, - "version": "0.1.1", + "version": "0.1.2", "activationEvents": [], "displayName": "Kiss VSCode" -} \ No newline at end of file +} diff --git a/src/commands/Lines.kiss b/src/commands/Lines.kiss index 54ddc843..31ec4f54 100644 --- a/src/commands/Lines.kiss +++ b/src/commands/Lines.kiss @@ -79,4 +79,38 @@ (let [editor activeTextEditor] (e.delete editor.selection) (e.insert editor.selection.active output)))) + (return))) + +(defCommand context findReplaceIncrement "Find text suffixed by a non-negative integer, and replace it, incrementing the number by line" "" [] + (localVar &mut outputLines []) + (localVar &mut lineFound 0) + (awaitLet [findNumStr (inputBox) + &sync length findNumStr.length + &sync text (selectedText)] + (unless text (print "No text selected!") (return)) + (unless findNumStr (print "Nothing to find!") (return)) + // Find where the number suffix starts + (let [&mut numStartIdx 0] + (while (< numStartIdx length) + (when (.contains "0123456789" (findNumStr.charAt numStartIdx)) + (break)) + ++numStartIdx) + (when (>= numStartIdx length) (print "text must be suffixed by a non-negative integer!") (return)) + + (let [prefix (findNumStr.substr 0 numStartIdx) + num (Std.parseInt (findNumStr.substr numStartIdx)) + lines (text.split "\n")] + (doFor line lines + (if (line.contains findNumStr) + { + (let [newStr "${prefix}$(+ num lineFound)"] + (outputLines.push (line.replace findNumStr newStr))) + ++lineFound + } + (outputLines.push line))))) + (.edit activeTextEditor + (lambda [e] + (let [editor activeTextEditor] + (e.delete editor.selection) + (e.insert editor.selection.active (outputLines.join "\n"))))) (return))) \ No newline at end of file