From 62b7355e3457045485f3fed12af312abaa6f4e55 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Fri, 22 Oct 2021 19:02:04 -0400 Subject: [PATCH] Refactor kvscode aliases and macros to Util.kiss --- projects/kiss-vscode/config/KissConfig.kiss | 45 ++---------------- projects/kiss-vscode/src/Util.kiss | 47 +++++++++++++++++++ .../src/ktxt2/KTxt2EditorProvider.kiss | 2 + 3 files changed, 53 insertions(+), 41 deletions(-) diff --git a/projects/kiss-vscode/config/KissConfig.kiss b/projects/kiss-vscode/config/KissConfig.kiss index abfc19b7..a2e48394 100644 --- a/projects/kiss-vscode/config/KissConfig.kiss +++ b/projects/kiss-vscode/config/KissConfig.kiss @@ -1,28 +1,4 @@ -/** - * Aliases - */ - -// TODO pass these aliases to the KissState of "eval kiss expression" - -// output -(defAlias &call infoMessage Vscode.window.showInformationMessage) -(defAlias &call warningMessage Vscode.window.showWarningMessage) -(defAlias &call errorMessage Vscode.window.showErrorMessage) - -// input -(defAlias &call inputBox Vscode.window.showInputBox) -(defAlias &call quickPick Vscode.window.showQuickPick) - -// commands -(defAlias &call executeCommand Vscode.commands.executeCommand) -(function repeatCommand [command times] - (let [iteration - ->[&opt _] (executeCommand command) - &mut promise - (iteration)] - (doFor i (range (- times 1)) - (set promise (promise.then iteration))) - promise)) +(loadFrom "kiss-vscode" "src/Util.kiss") // ui (defAlias &ident activeTextEditor Vscode.window.activeTextEditor) @@ -40,12 +16,6 @@ (document.getText range)) "")) -(defMacro withValueOrInputBox [v &body body] - `(if ,v - {,@body} - (awaitLet [,v (inputBox)] - ,@body))) - (function insertAt [:vscode.Position pos text] (activeTextEditor.edit (lambda [e] @@ -69,6 +39,7 @@ * Functionality */ +// TODO pass macros and aliases from Util.kiss to the KissState of "eval kiss expression" (function :Dynamic evalString [:String kissStr] (try (interp.execute @@ -90,17 +61,9 @@ (unless inputText (set inputText (selectedText))) (if command (_runCommandFunc command) - (let [commandList - (for description (commands.keys) - (object - label description - description null - detail null - picked null - alwaysShow null))] - (awaitLet [chosenCommand (quickPick commandList)] + (awaitLet [chosenCommand (quickPick (collect (commands.keys)))] (when chosenCommand - (_runCommandFunc chosenCommand.label)))))) + (_runCommandFunc chosenCommand))))) (function :Void runLastCommand [&opt _] (if lastCommand diff --git a/projects/kiss-vscode/src/Util.kiss b/projects/kiss-vscode/src/Util.kiss index f3b26c8f..c5d8483a 100644 --- a/projects/kiss-vscode/src/Util.kiss +++ b/projects/kiss-vscode/src/Util.kiss @@ -1,3 +1,50 @@ +/** + * Aliases + */ + + +// output +(defAlias &call infoMessage Vscode.window.showInformationMessage) +(defAlias &call warningMessage Vscode.window.showWarningMessage) +(defAlias &call errorMessage Vscode.window.showErrorMessage) + +// input +(defAlias &call inputBox Vscode.window.showInputBox) +(defAlias &call _quickPick Vscode.window.showQuickPick) + +(function quickPickItem [label &opt description] + (object + label label + description description + detail null + picked null + alwaysShow null)) + +(function quickPick [:Array strings] + (awaitLet [chosenItem (_quickPick (for string strings (quickPickItem string)))] + chosenItem.label)) + +(function quickPickMap [:Map stringMap] + (awaitLet [chosenItem (_quickPick (for =>key value stringMap (quickPickItem key (Std.string value))))] + (dictGet stringMap chosenItem.label))) + +// commands +(defAlias &call executeCommand Vscode.commands.executeCommand) +(function repeatCommand [command times] + (let [iteration + ->[&opt _] (executeCommand command) + &mut promise + (iteration)] + (doFor i (range (- times 1)) + (set promise (promise.then iteration))) + promise)) + +(defMacro withValueOrInputBox [v &body body] + `(if ,v + {,@body} + (awaitLet [,v (inputBox)] + ,@body))) + // This has to be a macro so it can return from tryLoadConfig (defMacro trySpawnSync [command args options onError] `(let [command ,command diff --git a/projects/kiss-vscode/src/ktxt2/KTxt2EditorProvider.kiss b/projects/kiss-vscode/src/ktxt2/KTxt2EditorProvider.kiss index 8f611a6d..009d7f7c 100644 --- a/projects/kiss-vscode/src/ktxt2/KTxt2EditorProvider.kiss +++ b/projects/kiss-vscode/src/ktxt2/KTxt2EditorProvider.kiss @@ -1,3 +1,5 @@ +(load "../Util.kiss") + // Based on https://github.com/microsoft/vscode-extension-samples/blob/main/custom-editor-sample/src/catScratchEditor.ts (function register [context]