diff --git a/CHANGELOG.md b/CHANGELOG.md index 03551f7..a6eeaac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Changes to Joyride ## [Unreleased] +- [Add `my-lib` example to Getting Started user content](https://github.com/BetterThanTomorrow/joyride/issues/63) + ## [0.0.10] - 2022-05-15 - [Make it easy to open scripts for editing](https://github.com/BetterThanTomorrow/joyride/issues/56) diff --git a/assets/getting-started-content/user/activate.cljs b/assets/getting-started-content/user/activate.cljs index 147534d..cb7c8b4 100644 --- a/assets/getting-started-content/user/activate.cljs +++ b/assets/getting-started-content/user/activate.cljs @@ -1,5 +1,6 @@ (ns activate (:require ["vscode" :as vscode] + [my-lib] ; See this script for why we require it [promesa.core :as p] [joyride.core :as joyride])) diff --git a/assets/getting-started-content/user/my_lib.cljs b/assets/getting-started-content/user/my_lib.cljs new file mode 100644 index 0000000..6d6f281 --- /dev/null +++ b/assets/getting-started-content/user/my_lib.cljs @@ -0,0 +1,26 @@ +(ns my-lib + (:require ["vscode" :as vscode] + [promesa.core :as p])) + +;; Assuming this namespace i required by `activate.cljs` +;; you can reach vars in `my-lib` using `my-lib/` in +;; `joyride.runCode` keybindings without requiring `my-lib`` +;; there. + +;; As an example, take the feature request on Calva to add a +;; **Restart clojure-lsp** command. It can be implemented with +;; with this function: + +(defn restart-clojure-lsp [] + (p/do (vscode/commands.executeCommand "calva.clojureLsp.stop") + (vscode/commands.executeCommand "calva.clojureLsp.start"))) + +;; And then this shortcut definition in `keybindings.json` +;; { +;; "key": "", +;; "command": "joyride.runCode", +;; "args": "(my-lib/restart-clojure-lsp)" +;; }, + +;; If you get complaints about `my-lib` not found, you probably +;; have not required it from `activate.cljs` \ No newline at end of file diff --git a/examples/README.md b/examples/README.md index c63c458..b5cf3d6 100644 --- a/examples/README.md +++ b/examples/README.md @@ -89,4 +89,49 @@ Joyride API used: * `vscode/window.showInformationMessage` * `vscode/workspace.findFiles` * `vscode/workspace.openTextDocument` -* `vscode/window.showTextDocument` \ No newline at end of file +* `vscode/window.showTextDocument` + +## Restarting clojure-lsp + +NB: _This example is included as **Getting started** User scripts content. See your user scripts, or `assets/getting-started-content/user/my_lib.cljs` in this repository._ + +A somewhat frequent feature request on Calva is a command for restarting clojure-lsp. It can be implemented in several ways, including: + +1. Define a `joyride.runCode` keyboard shortcut with the code to stop and start clojure-lsp +2. Make a Joyride script +3. Define a function in a namespace that you know is required and call the function from a `joyride.runCode` keyboard shortcut + +The third way is probably the most scalable/easiest to maintain: + +Have a User script `my_lib.cljs` with this content: + +```clojure +(ns my-lib + (:require ["vscode" :as vscode] + [promesa.core :as p])) + +(defn restart-clojure-lsp [] + (p/do (vscode/commands.executeCommand "calva.clojureLsp.stop") + (vscode/commands.executeCommand "calva.clojureLsp.start"))) +``` + +Make sure it is required from User `activate.cljs`: + +```clojure +(ns activate + (:require ... + [my-lib] + ...)) +``` + +You can then have this in `keybindings.json` + +```json + { + "key": "", + "command": "joyride.runCode", + "args": "(my-lib/restart-clojure-lsp)" + }, +``` + +Please be inspired to add functions to your `my-lib` namespace (and to rename it, if you fancy) to give yourself commands that VS Code and/or some extension is lacking. \ No newline at end of file diff --git a/src/main/joyride/getting_started.cljs b/src/main/joyride/getting_started.cljs index 262e25a..9b16828 100644 --- a/src/main/joyride/getting_started.cljs +++ b/src/main/joyride/getting_started.cljs @@ -34,6 +34,11 @@ "user" "hello_joyride_user_script.cljs"]) +(defn user-my-lib-uri-section-and-subpath [] + [(conf/user-abs-scripts-path) + "user" + "my_lib.cljs"]) + (defn workspace-activate-uri-section-and-subpath [] [(conf/workspace-abs-scripts-path) "workspace" @@ -57,7 +62,11 @@ (p/let [section-and-subpath (user-hello-uri-section-and-subpath) [hello-dest-uri hello-exists?+] (dest-uri-uri-exists?+ section-and-subpath)] (when-not hello-exists?+ - (apply (partial create-content-file+ hello-dest-uri) section-and-subpath)))) + (apply (partial create-content-file+ hello-dest-uri) section-and-subpath))) + (p/let [section-and-subpath (user-my-lib-uri-section-and-subpath) + [my-lib-dest-uri my-lib-exists?+] (dest-uri-uri-exists?+ section-and-subpath)] + (when-not my-lib-exists?+ + (apply (partial create-content-file+ my-lib-dest-uri) section-and-subpath)))) (defn create-and-open-content-file+ [content-file-uri section-and-subpath] (fn []