Skip to content

Commit

Permalink
Add clojure-lsp my-lib example (#64)
Browse files Browse the repository at this point in the history
Fixes #63
  • Loading branch information
PEZ authored May 15, 2022
1 parent c97d596 commit 206cfc2
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions assets/getting-started-content/user/activate.cljs
Original file line number Diff line number Diff line change
@@ -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]))

Expand Down
26 changes: 26 additions & 0 deletions assets/getting-started-content/user/my_lib.cljs
Original file line number Diff line number Diff line change
@@ -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/<symbol>` 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": "<some-keyboard-shortcut>",
;; "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`
47 changes: 46 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,49 @@ Joyride API used:
* `vscode/window.showInformationMessage`
* `vscode/workspace.findFiles`
* `vscode/workspace.openTextDocument`
* `vscode/window.showTextDocument`
* `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": "<some-keyboard-shortcut>",
"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.
11 changes: 10 additions & 1 deletion src/main/joyride/getting_started.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 []
Expand Down

0 comments on commit 206cfc2

Please sign in to comment.