From 07f33351a3057704042818180a23979747051c64 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Thu, 5 Jun 2025 16:58:09 -0500 Subject: [PATCH] find and replace token command --- src/commands/Lines.kiss | 43 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/commands/Lines.kiss b/src/commands/Lines.kiss index b95de819..54ddc843 100644 --- a/src/commands/Lines.kiss +++ b/src/commands/Lines.kiss @@ -38,4 +38,45 @@ (_sortLinesSync (selectedText) Reflect.compare)) (defCommand context sortLinesSyncEOL "Sort the selected lines lexicographically from the end of each line" "" [] - (_sortLinesSync (selectedText) ->[:String s1 :String s2] (Reflect.compare (s1.reverse) (s2.reverse)))) \ No newline at end of file + (_sortLinesSync (selectedText) ->[:String s1 :String s2] (Reflect.compare (s1.reverse) (s2.reverse)))) + +// Example: editMonster(monster, MONSTER) -> editItem(item, ITEM) +(defCommand context findReplaceTokenMatchCase "Find and replace a token, matching case" "" [] + (localVar &mut idx 0) + (localVar &mut output "") + (awaitLet [find (inputBox) + replace (inputBox) + &sync findLower (find.toLowerCase) + &sync text (selectedText) + &sync textLower (text.toLowerCase) + &sync textHits [] + &sync textHitTokens []] + (unless text (print "No text selected!") (return)) + (unless find (print "Nothing to find!") (return)) + (while (< idx text.length) + (set idx (textLower.indexOf findLower idx)) + (when (= -1 idx) (break)) + (textHits.push idx) + (textHitTokens.push (text.substr idx find.length)) + (+= idx find.length)) + (set idx 0) + (doFor [hit :String hitToken] (zip textHits textHitTokens) + (+= output (text.substr idx (- hit idx))) + (cond + // all caps + ((hitToken.isUpperCase) + (+= output (replace.toUpperCase))) + // all lower + ((hitToken.isLowerCase) + (+= output (replace.toLowerCase))) + // first letter capitalized + ((.isUpperCase (hitToken.charAt 0)) + (+= output (replace.toUpperCamel)))) + (set idx (+ hit find.length))) + (+= output (text.substr idx)) + (.edit activeTextEditor + (lambda [e] + (let [editor activeTextEditor] + (e.delete editor.selection) + (e.insert editor.selection.active output)))) + (return))) \ No newline at end of file