In case you’ve managed to miss it: @borkdude and I have cooked up something pretty exciting. (I know, biased, but you can bring it to the bank that I am excited about it.)
Joyride is an extension for Visual Studio Code that brings Clojure Interactive Programming powers over VS Code and its Extensions into user space (compare with Emacs and ELisp). Now things that otherwise takes a full blown extension to do, now can be done interactively in the editor itself. By you as a user of VS Code, while you are using it. Yes, we are leveraging on SCI to create this thing!
Joyride is very young. First baby steps, if there ever were any such, anywhere. But it is already quite useful: Some key features:
- Full access to the dynamic parts of the VS Code extension API
- Interactive Programming
- User and Workspace scripts, these can be run at will, and bound to keyboard shortcuts.
- User and Workspace
activate.cljs
scripts. These run at startup, i.e. when a new VS Code window is opened. - A command for running arbitrary Clojure code. Can be run at will, and bound to keyboard shortcuts.
Example: Type code fast!
Here’s a non-contrived example: When recording a demo video of Joyride I needed something so that the audience don’t start thinking of ways to kill themselves while I slowly type my Clojure code. So I tried to use the MacOS built in text-substitution mechanism. It is exactly what I need for this. Then I discovered that for some reason the feature does not work in VS Code!
Now what? I recalled that some year ago I released an extension that pastes text from the clipboard applying whatever replacements you configure. The extension is Paste Replaced and it is not a block buster, if I put it that way. (It had 35 downloads when I checked earlier today). But it was quite close to the tool I needed right now. So. I configured some replacers for it. But it was a bit too awkward to type some text, select it, copy it, then do paste-replaced. Having spent some time googling and asking around for a solution … face palm … I can use Joyride for it!
Demo video of Type code fast!
What’s involved
The ingredients in this solutions are:
-
Paste Replace (that other extension)
- Settings for this extension
-
Joyride
- Two functions in my
my-lib
Joyride User script namespace. (This namespace is required in my Useractivate.cljs
script.) - Two keyboard shortcuts
- Two functions in my
Here are the settings for Paste Replaced:
"paste-replaced.replacers": [
[
[ "\"", "\\\"", "g" ],
[ " +", " ", "g" ],
[ "\n", "\\n", "g" ],
[
"^!r7$",
"(repeat 7 \"I am using the REPL! 💪\")",
],
[
"^!hw2$",
"(p/let [choice (vscode/window.showInformationMessage \"Be a Joyrider 🎸\" \"Yes\" \"Of course!\")]\n (if choice\n (.appendLine (joyride/output-channel) (str \"You choose: \" choice \" 🎉\"))\n (.appendLine (joyride/output-channel) \"You just closed it? 😭\")))"
],
[
"^!hw$",
"(vscode/window.showInformationMessage \"Hello World!\")"
],
]
],
Here are the two functions in <joyride user scripts>/my-lib.cljs
:
(defn replace-all-text []
(p/do (vscode/commands.executeCommand "editor.action.selectAll")
(vscode/commands.executeCommand "execCopy")
(vscode/commands.executeCommand "paste-replaced.paste")))
(defn replace-last-word []
(p/do (vscode/commands.executeCommand "cursorWordLeftSelect")
(vscode/commands.executeCommand "execCopy")
(vscode/commands.executeCommand "paste-replaced.paste")))
The keybindings:
{
"key": "cmd+f4",
"command": "joyride.runCode",
"args": "(my-lib/replace-all-text)",
"when": "!editorTextFocus"
},
{
"key": "cmd+f4",
"command": "joyride.runCode",
"args": "(my-lib/replace-last-word)",
"when": "editorTextFocus"
},
The reason I need two functions and two keybinding definitions is that the in the text editor, I can’t replace all text, it should just be the just typed ”word”. And outside the text editor (input prompts) I don’t know how to select just a word, but selecting all text works. The when
clause takes care of letting the same keys work for both commands.
More examples
Please roam the examples in the Joyride repository for more ways to use it. And please consider contributing with how you use it!
It would be fantastic if you would like to try this out. Please don’t hesitate to ask for help here, in the Discussion on the repo, or in the #joyride
channel at the Clojurians Slack.