-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implant: implement a new link handler "Reveal via nREPL"
- Loading branch information
Showing
3 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
(ns dirac.implant.link-handlers | ||
(:require-macros [cljs.core.async.macros :refer [go go-loop]]) | ||
(:require [cljs.core.async :refer [put! <! chan timeout alts! close!]] | ||
[oops.core :refer [gset! oget oset! oset!+ ocall! ocall oapply gget gcall!]] | ||
[chromex.logging :refer-macros [log warn error info]] | ||
[dirac.implant.intercom :as intercom])) | ||
|
||
(defn ^:dynamic make-open-request-error-message [error url] | ||
(str "A request to reveal a link reported an error: " error "\n" | ||
"The url requested to be revealed was: " url "\n" | ||
"Please inspect your nREPL server terminal output for more details.")) | ||
|
||
(defn ^:dynamic make-repl-readiness-warning-message [url] | ||
(str "Cannot reveal a link via nREPL. REPL connection is not ready.\n" | ||
"The url requested to be revealed was: " url "\n" | ||
"Please connect to nREPL server in DevTools console first.")) | ||
|
||
; -- reveal-url ------------------------------------------------------------------------------------------------------------- | ||
|
||
(defn should-call-dirac-action? [event] | ||
(not (ocall event "getModifierState" "Meta"))) | ||
|
||
(defn should-call-original-action? [event] | ||
(not (ocall event "getModifierState" "Alt"))) | ||
|
||
(defn open-via-nrepl! [url line column] | ||
(if (intercom/repl-ready?) | ||
(go | ||
(let [payload {:url url :line (inc line) :column column} ; devtools line numbers are 0-based | ||
response (<! (intercom/send-devtools-request! :reveal-url payload)) | ||
result (:result response)] | ||
(if (some? result) | ||
(error (make-open-request-error-message result url))))) | ||
(warn (make-repl-readiness-warning-message url)))) | ||
|
||
(defn open-via-nrepl-link-handler [all-actions url line column event] | ||
(let [next-action (second all-actions)] | ||
(if (should-call-dirac-action? event) | ||
(open-via-nrepl! url line column)) | ||
(if (and (some? next-action) (should-call-original-action? event)) | ||
(ocall! next-action "handler")))) | ||
|
||
(defn register-open-via-nrepl-handler! [] | ||
(gcall! "dirac.registerDiracLinkAction" #js {:title "Reveal via nREPL" | ||
:handler open-via-nrepl-link-handler})) | ||
|
||
; -- installation ----------------------------------------------------------------------------------------------------------- | ||
|
||
(defn install! [] | ||
(register-open-via-nrepl-handler!)) |