Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get-extension-context to joyride.core #34

Merged
merged 3 commits into from
May 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ the following namespaces:

### `joyride.core`

- `*file*`: dynamic var representing the currently executing file.
- `*file*`: dynamic var representing the currently executing file
- `get-extension-context`: function returning the Joyride [extension context](https://code.visualstudio.com/api/references/vscode-api#ExtensionContext) object

NB: While using `*file*` bare works, this will probably stop working soon. Always use it from `joyride.core`, e.g.:
NB: While using `*file*` bare works, it will probably stop working soon. Always use it from `joyride.core`, e.g.:

```clojure
(ns your-awesome-script
Expand Down
19 changes: 14 additions & 5 deletions examples/.joyride/scripts/joyride_api.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
(:require ["vscode" :as vscode]
[promesa.core :as p]))

(def joyride (vscode/extensions.getExtension "betterthantomorrow.joyride"))

(def joyApi (.-exports joyride))
(def joyrideExt (vscode/extensions.getExtension "betterthantomorrow.joyride"))
(def joyApi (.-exports joyrideExt))

(comment
;; Starting the nREPL server
Expand All @@ -20,14 +19,24 @@
;; Getting contexts
(.getContextValue joyApi "joyride.isNReplServerRunning")

;; Non-Joyride context keys returns nil
;; Non-Joyride context keys returns `nil`
(.getContextValue joyApi "foo.bar")

;; NB: Use the extension instance for this!
(.getContextValue joyApi "joyride.isActive")
;; Like so:
(.-isActive joyride)
(.-isActive joyrideExt)
;; (Not that it matters, you can't deactivate Joyride,
;; and you can't talk to a deactivated REPL server.)
)

(comment
;; Joyride scripts can also reach the Joyride extension
;; through `joyride.core`
(require '[joyride.core :as joyride])
(-> (joyride/get-extension-context)
.-extension
.-exports)
(require '[clojure.repl :refer [doc]])
(doc joyride/get-extension-context)
)
7 changes: 5 additions & 2 deletions playground/hello-joyride/.joyride/scripts/hello.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
(:require ["vscode" :as vscode]
[joyride.core :as j]))

#_(in-ns 'hello)
(def foo :foo)
(def f j/*file*)

(defn main []
(vscode/window.showInformationMessage "Joyride says hello from a selection! 👋"))

(comment
j/*file*)
j/*file*

(def ext-ctx (joyride/get-extension-context))
(.-extensionPath ext-ctx)
)

(when :invoked-file=*file*
(main))
1 change: 1 addition & 0 deletions src/main/joyride/extension.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
(reset! !db {:output-channel (vscode/window.createOutputChannel "Joyride")
:extension-context context
:disposables []})
(vreset! jsci/!extension-context context)
(say "🟢 Joyride VS Code with Clojure. 🚗"))
(let [{:keys [extension-context]} @!db]
(register-command! extension-context "joyride.runCode" #'run-code)
Expand Down
13 changes: 12 additions & 1 deletion src/main/joyride/sci.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@

(sci/alter-var-root sci/print-fn (constantly *print-fn*))

(def !extension-context (volatile! nil))

(defn get-extension-context
"Returns the Joyride extension context object.
See: https://code.visualstudio.com/api/references/vscode-api#ExtensionContext"
[]
@!extension-context)

(def joyride-ns (sci/create-ns 'joyride.core nil))

(defn ns->path [namespace]
(-> (str namespace)
(munge)
Expand All @@ -20,7 +30,8 @@
(sci/init {:classes {'js goog/global
:allow :all}
:namespaces (assoc (:namespaces pconfig/config)
'joyride.core {'*file* sci/file})
'joyride.core {'*file* sci/file
'get-extension-context (sci/copy-var get-extension-context joyride-ns)})
:load-fn (fn [{:keys [namespace opts]}]
(cond
(symbol? namespace)
Expand Down