defCommand don't leave stale entries in package.json

This commit is contained in:
2023-03-19 10:55:14 -06:00
parent 6486acb975
commit e43dc4ea49

View File

@@ -165,6 +165,8 @@
} }
(Vscode.window.showErrorMessage errorMessage))) (Vscode.window.showErrorMessage errorMessage)))
(defMacroVar firstDefCommand true)
// Example: // Example:
/* /*
(defCommand customCommand "Custom command that does something" "C-; C-c" [<args...>] <body...>) (defCommand customCommand "Custom command that does something" "C-; C-c" [<args...>] <body...>)
@@ -192,40 +194,28 @@
extensionName extensionName
packageJson.name packageJson.name
keybindings keybindings
(or packageJson.contributes.keybindings []) (if firstDefCommand
[]
packageJson.contributes.keybindings)
commands commands
(or packageJson.contributes.commands []) (if firstDefCommand
[]
packageJson.contributes.commands)
id id
"${extensionName}.${id}" "${extensionName}.${id}"]
&mut keyBindingIndex null (setMacroVar firstDefCommand false)
&mut commandIndex null]
(doFor [idx binding] (enumerate keybindings)
(when (= binding.command id)
(set keyBindingIndex idx)
(break)))
(doFor [idx command] (enumerate commands)
(when (= command.command id)
(set commandIndex idx)
(break)))
// Manage the command entry in JSON // Manage the command entry in JSON
(unless commandIndex (set commandIndex commands.length)) (commands.push
(setNth commands commandIndex
(object (object
command id command id
title "${extensionName}: ${description}")) title "${extensionName}: ${description}"))
// Manage the keybinding entry in JSON // Manage the keybinding entry in JSON
(cond (when shortcut
(shortcut (keybindings.push
(unless keyBindingIndex (set keyBindingIndex keybindings.length))
(setNth keybindings keyBindingIndex
(object (object
command id command id
mac (StringTools.replace shortcutWithHyphensProcessed "C" "Cmd") mac (StringTools.replace shortcutWithHyphensProcessed "C" "Cmd")
key (StringTools.replace shortcutWithHyphensProcessed "C" "Ctrl")))) key (StringTools.replace shortcutWithHyphensProcessed "C" "Ctrl"))))
// A binding element is in the JSON that needs to be removed:
(keyBindingIndex
(keybindings.splice keyBindingIndex 1)))
(set packageJson.contributes.commands commands) (set packageJson.contributes.commands commands)
(set packageJson.contributes.keybindings keybindings) (set packageJson.contributes.keybindings keybindings)
(File.saveContent "package.json" (Json.stringify packageJson null "\t")) (File.saveContent "package.json" (Json.stringify packageJson null "\t"))