From 80e909090dd4feb58a2ed87e469427c4e0965070 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Fri, 3 Dec 2021 17:49:31 -0700 Subject: [PATCH] Command to import KTxt2 input file --- projects/kiss-vscode/config/KissConfig.kiss | 8 ++++++-- projects/kiss-vscode/package-lock.json | 1 - projects/kiss-vscode/src/Util.kiss | 2 ++ .../kiss-vscode/src/commands/KTxt2Tools.kiss | 20 +++++++++++++++++++ projects/kiss-vscode/src/ktxt2/KTxt2.kiss | 12 ++++++++++- .../src/ktxt2/KTxt2EditorProvider.kiss | 12 +---------- 6 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 projects/kiss-vscode/src/commands/KTxt2Tools.kiss diff --git a/projects/kiss-vscode/config/KissConfig.kiss b/projects/kiss-vscode/config/KissConfig.kiss index 8e2e4853..921edbb8 100644 --- a/projects/kiss-vscode/config/KissConfig.kiss +++ b/projects/kiss-vscode/config/KissConfig.kiss @@ -209,13 +209,15 @@ (loadFrom "kiss-vscode" "src/commands/KissTools.kiss") (loadFrom "kiss-vscode" "src/commands/ExtensionTools.kiss") +(loadFrom "kiss-vscode" "src/commands/KTxt2Tools.kiss") // Provided from Main.kiss via (set): (var &mut :Command tryLoadConfig) (function :Void registerBuiltins [&opt leaderKeys] (unless leaderKeys (set leaderKeys "")) - (let [prefix "Kiss-VSCode:$(if leaderKeys " [${leaderKeys}]" "")"] + (let [prefix "Kiss-VSCode:$(if leaderKeys " [${leaderKeys}]" "")" + ktxt2Prefix "KTxt2:$(if leaderKeys " [${leaderKeys}]" "")"] // In Main.kiss: (registerCommand "${prefix} [r]eload Config.kiss" tryLoadConfig) // In this file: @@ -228,7 +230,9 @@ (registerCommand "${prefix} open corresponding .kiss/.hx [f]ile" showCorrespondingFile) // ExtensionTools.kiss: (registerCommand "${prefix} [u]pdate Kiss-VSCode" updateKissVscode) - (registerCommand "${prefix} [t]est and [u]pdate Kiss-VSCode" testAndUpdateKissVscode))) + (registerCommand "${prefix} [t]est and [u]pdate Kiss-VSCode" testAndUpdateKissVscode) + // KTxt2.kiss: + (registerCommand "${ktxt2Prefix} [i]mport input file" importKTxt2InputFile))) // TODO standardize this with KissInterp (function :Void prepareInterp [] diff --git a/projects/kiss-vscode/package-lock.json b/projects/kiss-vscode/package-lock.json index 66de0f49..1a9ba4f2 100644 --- a/projects/kiss-vscode/package-lock.json +++ b/projects/kiss-vscode/package-lock.json @@ -5,7 +5,6 @@ "requires": true, "packages": { "": { - "name": "kiss-vscode", "version": "0.0.18", "dependencies": { "monaco-editor": "^0.29.1" diff --git a/projects/kiss-vscode/src/Util.kiss b/projects/kiss-vscode/src/Util.kiss index 66a2755f..ab45eb06 100644 --- a/projects/kiss-vscode/src/Util.kiss +++ b/projects/kiss-vscode/src/Util.kiss @@ -27,6 +27,8 @@ (awaitLet [chosenItem (_quickPick (for =>key value stringMap (quickPickItem key (Std.string value))))] (when chosenItem (dictGet stringMap chosenItem.label)))) +(defAlias &call openDialog Vscode.window.showOpenDialog) + // commands (defAlias &call executeCommand Vscode.commands.executeCommand) (function repeatCommand [command times] diff --git a/projects/kiss-vscode/src/commands/KTxt2Tools.kiss b/projects/kiss-vscode/src/commands/KTxt2Tools.kiss new file mode 100644 index 00000000..725de505 --- /dev/null +++ b/projects/kiss-vscode/src/commands/KTxt2Tools.kiss @@ -0,0 +1,20 @@ +// Command to import a file to a new KTxt2 file: +(function :Void importKTxt2InputFile [&opt _] + (awaitLet [uris (openDialog (object openLabel "Import" title "File to import as KTxt2 input" canSelectMany false)) + outputType (inputBox (object prompt "Output file extension")) + splitBy (quickPickMap [=>"↵ (new line)" "\n" =>"¶ (new paragraph)" "\n\n" =>"Other (specify via text box)" ""])] + (withValueOrInputBox splitBy + (let [file .fsPath (first uris) + outputType (if (outputType.startsWith ".") (outputType.substr 1) outputType) + ktxt2Filename "${file}.${outputType}.ktxt2" + &mut newContents "" + :kiss.List inputBlocks (.split (File.getContent file) splitBy)] + (doFor block (inputBlocks.slice 0 -1) + (+= newContents (_blockOf block splitBy))) + (+= newContents (_blockOf (last inputBlocks) "")) + (File.saveContent ktxt2Filename newContents) + (executeCommand "workbench.action.quickOpen" ktxt2Filename))))) + +(function _blockOf [inputText :String splitBy] + (let [blockText (KTxt2.insertSpecialChars "${inputText}${splitBy}")] + "${KTxt2.blockStartEnd}${blockText}${KTxt2.unlockedStart}${KTxt2.blockStartEnd}")) \ No newline at end of file diff --git a/projects/kiss-vscode/src/ktxt2/KTxt2.kiss b/projects/kiss-vscode/src/ktxt2/KTxt2.kiss index 26e70e28..44fd0452 100644 --- a/projects/kiss-vscode/src/ktxt2/KTxt2.kiss +++ b/projects/kiss-vscode/src/ktxt2/KTxt2.kiss @@ -119,4 +119,14 @@ (when (= 1 (count conversions)) (let [onlyConversion (first (collect (conversions.iterator)))] (set element.output (onlyConversion.convert element.source)))))))) - (toString elements)))) \ No newline at end of file + (toString elements)))) + +(function :String insertSpecialChars [:String text] + (let [text (text.replace "\r" "")] + (cond + ((text.endsWith "\n\n") + (+ (substr text 0 -2) "¶")) + ((text.endsWith "\n") + (+ (substr text 0 -1) "↵")) + (true + text)))) \ No newline at end of file diff --git a/projects/kiss-vscode/src/ktxt2/KTxt2EditorProvider.kiss b/projects/kiss-vscode/src/ktxt2/KTxt2EditorProvider.kiss index 675821f9..6558e7dc 100644 --- a/projects/kiss-vscode/src/ktxt2/KTxt2EditorProvider.kiss +++ b/projects/kiss-vscode/src/ktxt2/KTxt2EditorProvider.kiss @@ -9,16 +9,6 @@ (defNew [&prop :ExtensionContext context]) -(method :String insertSpecialChars [:String text] - (let [text (text.replace "\r" "")] - (cond - ((text.endsWith "\n\n") - (+ (substr text 0 -2) "¶")) - ((text.endsWith "\n") - (+ (substr text 0 -1) "↵")) - (true - text)))) - (method :Promise resolveCustomTextEditor [:TextDocument document :WebviewPanel webviewPanel :CancellationToken _token] // When a blank file with the ktxt2 extension is opened for the first time, give it an empty // block so the user can actually add to it: @@ -52,7 +42,7 @@ (infoMessage message)) ((objectWith [type "replace"] text start end) (makeEdit - ->edit (edit.replace document.uri (rangeFromStartEnd start end) (insertSpecialChars text)))) + ->edit (edit.replace document.uri (rangeFromStartEnd start end) (KTxt2.insertSpecialChars text)))) ((objectWith [type "insertBefore"] text position) (makeEdit ->edit (edit.insert document.uri (streamPosToOffsetDocumentPos position -KTxt2.blockStartEnd.length) text)))