From 482f3189584da72793da4e8d8a88705dcefd5253 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 21 Sep 2020 19:56:51 +0100 Subject: [PATCH 1/2] Allow toggling keyboard shortcuts Closes #784 We introduce a `calva.keybindingsEnabled` config option and a `calva.toggleKeybindingsEnabled` command which allows toggling keybindings on and off. The keybindings are updated to only activate when the `calva:keybindingsEnabled` context key is true, in addition to the existing conditions on the keymap. --- CHANGELOG.md | 1 + docs/site/finding-commands.md | 8 +- package.json | 218 ++++++++++++++++++++-------------- src/config.ts | 6 +- src/extension.ts | 15 +++ 5 files changed, 153 insertions(+), 95 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d31e3028..34ef7c91c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Changes to Calva. - [Fix: evals should be ignored during parsing](https://github.com/BetterThanTomorrow/calva/issues/763) - Fix: [Test runner can't find tests under cursor when using a custom test macro](https://github.com/BetterThanTomorrow/calva/issues/786) - Fix: [Test runner output only partially commented](https://github.com/BetterThanTomorrow/calva/issues/787) +- [Allow toggling keyboard shortcuts](https://github.com/BetterThanTomorrow/calva/issues/784) ## [2.0.124] - 2020-08-31 - Re-fix: [Can't jack-in when no project file is open](https://github.com/BetterThanTomorrow/calva/issues/734) diff --git a/docs/site/finding-commands.md b/docs/site/finding-commands.md index 90929b88e..d9c7c1362 100644 --- a/docs/site/finding-commands.md +++ b/docs/site/finding-commands.md @@ -5,7 +5,7 @@ Calva relies a lot on that VS Code makes it really easy to find commands by open To leverage this, all Calva commands are prefixed with `Calva`. As an example, say you want to evaluate a form and pretty print it. Then you can do this: 1. Open the command palette -2. Type `calevpr` +2. Type `calevpr` VS Code will match `cal` to ”**Cal**va”, `ev` to ”**Ev**aluate”, and `pr` to ”**pr**etty” in ”**pr**etty print”. It looks like so: @@ -17,6 +17,10 @@ Now might be a good time to see [Calva Top 10 Commands](commands-top10.md) ## All the Settings and Commands -Did you know? There is a complete list of Calva settings and commands in the *Contributions* tab of the Calva entry in the *Extensions* pane in VS Code. +Did you know? There is a complete list of Calva settings and commands in the *Contributions* tab of the Calva entry in the *Extensions* pane in VS Code. ![The Calva Contributions Tab](https://user-images.githubusercontent.com/30010/66733740-c754b800-ee60-11e9-877b-962f6b920cd7.png) + +## Toggling Keyboard Shortcuts On/Off + +The command `calva.toggleKeybindingsEnabled` can be used to quickly enable and disable (almost) all keyboard shortcuts. By default it is bound to `ctrl+alt+c ctrl+alt+k`, and this shortcut is the only one which will not be disabled. This allows you to quickly toggle between Calva keybindings and other keybindings which would otherwise not be available when Calva is enabled. This is particularly useful with the Paredit keyboard shortcuts, whose default shortcuts conflict with the default VS Code shortcuts for textual (non-structural) editing. diff --git a/package.json b/package.json index 6df1e3842..7784c807f 100644 --- a/package.json +++ b/package.json @@ -487,6 +487,12 @@ "type": "boolean", "markdownDescription": "By default, the **Calva says** window opens on startup.", "default": true + }, + "calva.keybindingsEnabled": { + "type": "boolean", + "description": "Activate keybindings.", + "default": true, + "scope": "window" } } }, @@ -747,6 +753,11 @@ "enablement": "calva:connected", "category": "Calva" }, + { + "category": "Calva", + "command": "calva.toggleKeybindingsEnabled", + "title": "Toggle Keybindings Enabled" + }, { "category": "Calva Paredit", "command": "paredit.togglemode", @@ -1086,438 +1097,463 @@ { "command": "calva.tellAboutNewChordingKey", "key": "ctrl+alt+v", - "when": "editorLangId == clojure" + "when": "calva:keybindingsEnabled && editorLangId == clojure" }, { "command": "calva.debug.instrument", "key": "ctrl+alt+c i", - "when": "editorLangId == clojure && calva:connected" + "when": "calva:keybindingsEnabled && editorLangId == clojure && calva:connected" }, { "command": "calva.jackIn", - "key": "ctrl+alt+c ctrl+alt+j" + "key": "ctrl+alt+c ctrl+alt+j", + "when": "calva:keybindingsEnabled" }, { "command": "calva.connect", - "key": "ctrl+alt+c ctrl+alt+c" + "key": "ctrl+alt+c ctrl+alt+c", + "when": "calva:keybindingsEnabled" }, { "command": "calva.connectNonProjectREPL", - "key": "ctrl+alt+c alt+c" + "key": "ctrl+alt+c alt+c", + "when": "calva:keybindingsEnabled" }, { "command": "calva.toggleCLJCSession", - "key": "ctrl+alt+c ctrl+alt+s" + "key": "ctrl+alt+c ctrl+alt+s", + "when": "calva:keybindingsEnabled" }, { "command": "calva.switchCljsBuild", - "key": "ctrl+alt+c ctrl+alt+b" + "key": "ctrl+alt+c ctrl+alt+b", + "when": "calva:keybindingsEnabled" }, { "command": "calva.selectCurrentForm", - "key": "ctrl+alt+c ctrl+s" + "key": "ctrl+alt+c ctrl+s", + "when": "calva:keybindingsEnabled" }, { "command": "calva.clearInlineResults", "key": "escape", - "when": "editorTextFocus && !editorHasMultipleSelections && !editorHasSelection && !editorReadOnly && !hasOtherSuggestions && !parameterHintsVisible && !selectionAnchorSet && !suggestWidgetVisible && editorLangId == 'clojure'" + "when": "calva:keybindingsEnabled && editorTextFocus && !editorHasMultipleSelections && !editorHasSelection && !editorReadOnly && !hasOtherSuggestions && !parameterHintsVisible && !selectionAnchorSet && !suggestWidgetVisible && editorLangId == 'clojure'" }, { "command": "calva.evaluateSelection", "key": "ctrl+alt+c e", - "win": "ctrl+alt+c v" + "win": "ctrl+alt+c v", + "when": "calva:keybindingsEnabled" }, { "command": "calva.evaluateSelection", "key": "ctrl+enter", - "when": "editorTextFocus" + "when": "calva:keybindingsEnabled && editorTextFocus" }, { "command": "calva.interruptAllEvaluations", - "key": "ctrl+alt+c ctrl+alt+d" + "key": "ctrl+alt+c ctrl+alt+d", + "when": "calva:keybindingsEnabled" }, { "command": "calva.evaluateCurrentTopLevelForm", - "key": "ctrl+alt+c space" + "key": "ctrl+alt+c space", + "when": "calva:keybindingsEnabled" }, { "command": "calva.evaluateCurrentTopLevelForm", "key": "alt+enter", - "when": "editorTextFocus" + "when": "calva:keybindingsEnabled && editorTextFocus" }, { "command": "calva.evaluateSelectionReplace", - "key": "ctrl+alt+c r" + "key": "ctrl+alt+c r", + "when": "calva:keybindingsEnabled" }, { "command": "calva.evaluateSelectionAsComment", - "key": "ctrl+alt+c c" + "key": "ctrl+alt+c c", + "when": "calva:keybindingsEnabled" }, { "command": "calva.evaluateTopLevelFormAsComment", - "key": "ctrl+alt+c ctrl+space" + "key": "ctrl+alt+c ctrl+space", + "when": "calva:keybindingsEnabled" }, { "command": "calva.copyLastResults", - "key": "ctrl+alt+c ctrl+c" + "key": "ctrl+alt+c ctrl+c", + "when": "calva:keybindingsEnabled" }, { "command": "calva.loadFile", - "key": "ctrl+alt+c enter" + "key": "ctrl+alt+c enter", + "when": "calva:keybindingsEnabled" }, { "command": "calva.togglePrettyPrint", - "key": "ctrl+alt+c p" + "key": "ctrl+alt+c p", + "when": "calva:keybindingsEnabled" }, { "command": "calva.requireREPLUtilities", - "key": "ctrl+alt+c ctrl+u" + "key": "ctrl+alt+c ctrl+u", + "when": "calva:keybindingsEnabled" }, { "command": "calva.runNamespaceTests", - "key": "ctrl+alt+c t" + "key": "ctrl+alt+c t", + "when": "calva:keybindingsEnabled" }, { "command": "calva.runAllTests", - "key": "ctrl+alt+c shift+t" + "key": "ctrl+alt+c shift+t", + "when": "calva:keybindingsEnabled" }, { "command": "calva.rerunTests", - "key": "ctrl+alt+c ctrl+t" + "key": "ctrl+alt+c ctrl+t", + "when": "calva:keybindingsEnabled" }, { "command": "calva.runTestUnderCursor", - "key": "ctrl+alt+c ctrl+alt+t" + "key": "ctrl+alt+c ctrl+alt+t", + "when": "calva:keybindingsEnabled" }, { "command": "calva.runCustomREPLCommand", - "key": "ctrl+alt+c ." + "key": "ctrl+alt+c .", + "when": "calva:keybindingsEnabled" + }, + { + "command": "calva.toggleKeybindingsEnabled", + "key": "ctrl+alt+c ctrl+alt+k" }, { "command": "paredit.togglemode", "key": "ctrl+alt+p ctrl+alt+m", - "when": "paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.forwardSexp", "key": "ctrl+alt+right", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.backwardSexp", "key": "ctrl+alt+left", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.forwardDownSexp", "key": "ctrl+down", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.backwardDownSexp", "key": "ctrl+alt+up", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.forwardUpSexp", "key": "ctrl+alt+down", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.backwardUpSexp", "key": "ctrl+up", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.closeList", "key": "ctrl+end", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.openList", "key": "ctrl+home", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.selectForwardSexp", "key": "ctrl+shift+alt+right", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.selectBackwardSexp", "key": "ctrl+shift+alt+left", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.selectForwardDownSexp", "key": "ctrl+shift+down", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.selectBackwardDownSexp", "key": "ctrl+shift+alt+up", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.selectForwardUpSexp", "key": "ctrl+shift+alt+down", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.selectBackwardUpSexp", "key": "ctrl+shift+up", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.selectCloseList", "key": "ctrl+shift+end", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.selectOpenList", "key": "ctrl+shift+home", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.rangeForDefun", "key": "ctrl+alt+w space", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.sexpRangeExpansion", "key": "ctrl+w", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.sexpRangeContraction", "key": "ctrl+shift+w", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.slurpSexpForward", "key": "ctrl+right", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.slurpSexpBackward", "key": "ctrl+shift+left", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.barfSexpForward", "key": "ctrl+left", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.barfSexpBackward", "key": "ctrl+shift+right", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.spliceSexp", "key": "ctrl+alt+s", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.splitSexp", "key": "ctrl+shift+s", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.joinSexp", "key": "ctrl+shift+j", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.raiseSexp", "key": "ctrl+alt+p ctrl+alt+r", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.transpose", "key": "ctrl+alt+t", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.dragSexprBackward", "key": "ctrl+shift+alt+b", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.dragSexprForward", "key": "ctrl+shift+alt+f", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.dragSexprBackwardUp", "key": "ctrl+shift+alt+u", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.dragSexprForwardDown", "key": "ctrl+shift+alt+d", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.dragSexprForwardUp", "key": "ctrl+shift+alt+k", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.dragSexprBackwardDown", "key": "ctrl+shift+alt+j", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.convolute", "key": "ctrl+shift+c", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.killSexpForward", "key": "ctrl+shift+delete", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.killSexpBackward", "key": "ctrl+alt+backspace", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.killListForward", "key": "ctrl+delete", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.killListBackward", "key": "ctrl+backspace", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.spliceSexpKillForward", "key": "ctrl+alt+shift+delete", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.spliceSexpKillBackward", "key": "ctrl+alt+shift+backspace", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.wrapAroundParens", "key": "ctrl+alt+shift+p", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.wrapAroundSquare", "key": "ctrl+alt+shift+s", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.wrapAroundCurly", "key": "ctrl+alt+shift+c", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.wrapAroundQuote", "key": "ctrl+alt+shift+q", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.rewrapParens", "key": "ctrl+alt+r ctrl+alt+p", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.rewrapSquare", "key": "ctrl+alt+r ctrl+alt+s", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.rewrapCurly", "key": "ctrl+alt+r ctrl+alt+c", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.rewrapQuote", "key": "ctrl+alt+r ctrl+alt+q", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap =~ /original|strict/" }, { "command": "paredit.deleteForward", "key": "delete", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap == strict && !editorReadOnly && !editorHasMultipleSelections" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap == strict && !editorReadOnly && !editorHasMultipleSelections" }, { "command": "paredit.deleteBackward", "key": "backspace", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap == strict && !editorReadOnly && !editorHasMultipleSelections" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap == strict && !editorReadOnly && !editorHasMultipleSelections" }, { "command": "paredit.forceDeleteForward", "key": "alt+delete", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap == strict && !editorReadOnly && !editorHasMultipleSelections" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap == strict && !editorReadOnly && !editorHasMultipleSelections" }, { "command": "paredit.forceDeleteBackward", "key": "alt+backspace", - "when": "editorLangId == clojure && editorTextFocus && paredit:keyMap == strict && !editorReadOnly && !editorHasMultipleSelections" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap == strict && !editorReadOnly && !editorHasMultipleSelections" }, { "command": "calva-fmt.formatCurrentForm", "key": "tab", - "when": "editorLangId == clojure && editorTextFocus && !editorReadOnly && !inSnippetMode && !suggestWidgetVisible && !hasOtherSuggestions" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && !editorReadOnly && !inSnippetMode && !suggestWidgetVisible && !hasOtherSuggestions" }, { "command": "calva-fmt.alignCurrentForm", "key": "ctrl+alt+l", - "when": "editorLangId == clojure && editorTextFocus && !editorReadOnly && !suggestWidgetVisible && !hasOtherSuggestions" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && !editorReadOnly && !suggestWidgetVisible && !hasOtherSuggestions" }, { "command": "calva-fmt.inferParens", "key": "ctrl+alt+f ctrl+alt+p", - "when": "editorLangId == clojure && editorTextFocus && !editorReadOnly && !suggestWidgetVisible && !hasOtherSuggestions" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && !editorReadOnly && !suggestWidgetVisible && !hasOtherSuggestions" }, { "command": "calva-fmt.tabIndent", "key": "ctrl+i", - "when": "editorLangId == clojure && editorTextFocus && !editorReadOnly && !suggestWidgetVisible && !hasOtherSuggestions" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && !editorReadOnly && !suggestWidgetVisible && !hasOtherSuggestions" }, { "command": "calva-fmt.tabDedent", "key": "shift+ctrl+i", - "when": "editorLangId == clojure && editorTextFocus && !editorReadOnly && !suggestWidgetVisible && !hasOtherSuggestions" + "when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && !editorReadOnly && !suggestWidgetVisible && !hasOtherSuggestions" }, { "command": "calva.showOutputWindow", "key": "ctrl+alt+c o", - "when": "calva:connected && !calva:outputWindowActive" + "when": "calva:keybindingsEnabled && calva:connected && !calva:outputWindowActive" }, { "command": "calva.setOutputWindowNamespace", "key": "ctrl+alt+c alt+n", - "when": "editorLangId == clojure && calva:connected && !calva:outputWindowActive" + "when": "calva:keybindingsEnabled && editorLangId == clojure && calva:connected && !calva:outputWindowActive" }, { "command": "calva.sendCurrentFormToOutputWindow", "key": "ctrl+alt+c ctrl+alt+e", "win": "ctrl+alt+c ctrl+alt+v", - "when": "editorLangId == clojure && calva:connected && !calva:outputWindowActive" + "when": "calva:keybindingsEnabled && editorLangId == clojure && calva:connected && !calva:outputWindowActive" }, { "command": "calva.sendCurrentTopLevelFormToOutputWindow", "key": "ctrl+alt+c ctrl+alt+space", - "when": "editorLangId == clojure && calva:connected && !calva:outputWindowActive" + "when": "calva:keybindingsEnabled && editorLangId == clojure && calva:connected && !calva:outputWindowActive" }, { "command": "calva.showPreviousReplHistoryEntry", "key": "alt+up", - "when": "editorLangId == clojure && calva:connected && calva:outputWindowActive" + "when": "calva:keybindingsEnabled && editorLangId == clojure && calva:connected && calva:outputWindowActive" }, { "command": "calva.showNextReplHistoryEntry", "key": "alt+down", - "when": "editorLangId == clojure && calva:connected && calva:outputWindowActive" + "when": "calva:keybindingsEnabled && editorLangId == clojure && calva:connected && calva:outputWindowActive" } ], "menus": { diff --git a/src/config.ts b/src/config.ts index 9b277ddd0..20063a278 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,5 +1,7 @@ const config = { - REPL_FILE_EXT: 'calva-repl' + REPL_FILE_EXT: 'calva-repl', + KEYBINDINGS_ENABLED_CONFIG_KEY: 'calva.keybindingsEnabled', + KEYBINDINGS_ENABLED_CONTEXT_KEY: 'calva:keybindingsEnabled' }; type ReplSessionType = 'clj' | 'cljs'; @@ -8,4 +10,4 @@ export { ReplSessionType } -export default config; \ No newline at end of file +export default config; diff --git a/src/extension.ts b/src/extension.ts index 12827f0ba..368cab869 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -25,6 +25,7 @@ import * as debug from './debugger/calva-debug'; import * as model from './cursor-doc/model'; import * as outputWindow from './results-output/results-doc'; import * as replHistory from './results-output/repl-history'; +import config from './config'; function onDidSave(document) { let { @@ -53,6 +54,10 @@ function onDidOpen(document) { } } +function setKeybindingsEnabledContext() { + let keybindingsEnabled = vscode.workspace.getConfiguration().get(config.KEYBINDINGS_ENABLED_CONFIG_KEY); + vscode.commands.executeCommand('setContext', config.KEYBINDINGS_ENABLED_CONTEXT_KEY, keybindingsEnabled); +} function activate(context: vscode.ExtensionContext) { state.cursor.set('analytics', new Analytics(context)); @@ -156,6 +161,10 @@ function activate(context: vscode.ExtensionContext) { context.subscriptions.push(vscode.commands.registerCommand('calva.showPreviousReplHistoryEntry', replHistory.showPreviousReplHistoryEntry)); context.subscriptions.push(vscode.commands.registerCommand('calva.showNextReplHistoryEntry', replHistory.showNextReplHistoryEntry)); context.subscriptions.push(vscode.commands.registerCommand('calva.clearReplHistory', replHistory.clearHistory)); + context.subscriptions.push(vscode.commands.registerCommand('calva.toggleKeybindingsEnabled', () => { + let keybindingsEnabled = vscode.workspace.getConfiguration().get(config.KEYBINDINGS_ENABLED_CONFIG_KEY); + vscode.workspace.getConfiguration().update(config.KEYBINDINGS_ENABLED_CONFIG_KEY, !keybindingsEnabled, vscode.ConfigurationTarget.Global); + })); // Temporary command to teach new default keyboard shortcut chording key context.subscriptions.push(vscode.commands.registerCommand('calva.tellAboutNewChordingKey', () => { @@ -167,6 +176,7 @@ function activate(context: vscode.ExtensionContext) { vscode.commands.executeCommand("setContext", "calva:launching", false); vscode.commands.executeCommand("setContext", "calva:connected", false); vscode.commands.executeCommand("setContext", "calva:connecting", false); + setKeybindingsEnabledContext(); // PROVIDERS context.subscriptions.push(vscode.languages.registerCompletionItemProvider(state.documentSelector, new CalvaCompletionItemProvider())); @@ -212,6 +222,11 @@ function activate(context: vscode.ExtensionContext) { outputWindow.setContextForOutputWindowActive(false); } })); + context.subscriptions.push(vscode.workspace.onDidChangeConfiguration((e: vscode.ConfigurationChangeEvent) => { + if (e.affectsConfiguration(config.KEYBINDINGS_ENABLED_CONFIG_KEY)) { + setKeybindingsEnabledContext(); + } + })); // Clojure debug adapter setup const provider = new debug.CalvaDebugConfigurationProvider(); From 632c34670aa53df8da84d1dec1dc0657df6106cf Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 21 Sep 2020 20:05:46 +0100 Subject: [PATCH 2/2] Decouple UI update from internal state Previously the Paredit StatusBar's toggleBarItem was being updated throughout multiple setters. Now we refactor the UI update into a single method and remove the internal `_enabled` state which is now unused. --- src/paredit/statusbar.ts | 41 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/src/paredit/statusbar.ts b/src/paredit/statusbar.ts index 59c96b3de..77f857e2a 100644 --- a/src/paredit/statusbar.ts +++ b/src/paredit/statusbar.ts @@ -5,7 +5,6 @@ import * as paredit from './extension'; export class StatusBar { - private _enabled: Boolean; private _visible: Boolean; private _keyMap: String; @@ -16,13 +15,12 @@ export class StatusBar { this._toggleBarItem.text = "(λ)"; this._toggleBarItem.tooltip = ""; this._toggleBarItem.command = 'paredit.togglemode'; - this._enabled = false; this._visible = false; this.keyMap = keymap; paredit.onPareditKeyMapChanged((keymap) => { this.keyMap = keymap; - }) + }) } get keyMap() { @@ -30,42 +28,29 @@ export class StatusBar { } set keyMap(keymap: String) { - - switch (keymap.trim().toLowerCase()) { + this._keyMap = keymap; + this.updateUIState(); + } + + updateUIState() { + switch (this.keyMap.trim().toLowerCase()) { case 'original': - this._keyMap = 'original'; - this.enabled = true; this.visible = true; this._toggleBarItem.text = "(λ)"; - this._toggleBarItem.tooltip = "Toggle to strict Mode" + this._toggleBarItem.tooltip = "Toggle to Strict Mode"; + this._toggleBarItem.color = undefined; break; case 'strict': - this._keyMap = 'strict'; - this.enabled = true; this.visible = true; this._toggleBarItem.text = "[λ]"; - this._toggleBarItem.tooltip = "Toggle to original Mode" + this._toggleBarItem.tooltip = "Toggle to Original Mode"; + this._toggleBarItem.color = undefined; break; default: - this._keyMap = 'none'; - this.enabled = false; this.visible = true; this._toggleBarItem.text = "λ"; this._toggleBarItem.tooltip = "Calva Paredit Keymap is set to none, Toggle to Strict Mode is Disabled" - } - } - - get enabled() { - return this._enabled; - } - - set enabled(value: Boolean) { - this._enabled = value; - - if (this._enabled) { - this._toggleBarItem.color = undefined; - } else { - this._toggleBarItem.color = statusbar.color.inactive; + this._toggleBarItem.color = statusbar.color.inactive; } } @@ -84,4 +69,4 @@ export class StatusBar { dispose() { this._toggleBarItem.dispose(); } -} \ No newline at end of file +}