Skip to content

Commit

Permalink
fix: Update ElinInstantConnect command to accept no argument and sele…
Browse files Browse the repository at this point in the history
…ct a project to connect to
  • Loading branch information
liquidz committed Jan 2, 2025
1 parent dfdcee2 commit 3f2f910
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 4 deletions.
2 changes: 1 addition & 1 deletion plugin/elin.vim
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ endif

" Connection
command! -nargs=? ElinConnect call elin#notify('elin.handler.connect/connect', <q-args> ? [str2nr(<q-args>)] : [])
command! -nargs=1 ElinInstantConnect call elin#notify('elin.handler.connect/instant', [<q-args>])
command! -nargs=? ElinInstantConnect call elin#notify('elin.handler.connect/instant', [<q-args>])
command! ElinDisconnect call elin#notify('elin.handler.connect/disconnect', [])
command! ElinJackIn call elin#notify('elin.handler.connect/jack-in', [])
command! ElinSwitchConnection call elin#notify('elin.handler.connect/switch', [])
Expand Down
28 changes: 25 additions & 3 deletions src/elin/handler/connect.clj
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,35 @@
(def ^:private ?InstantParams
[:catn
[:project
(apply vector :enum
(map name e.c.jack-in/supported-project-types))]])
(->> (map name e.c.jack-in/supported-project-types)
(cons "")
(apply vector :enum))]])

(defn- select-instant-connect-project
[elin]
(if-let [selected (->> (map name e.c.jack-in/supported-project-types)
(e.f.select/select-from-candidates elin))]
[selected nil]
[nil "No project is selected."]))

(defn instant
"Launch nREPL server of the specified project and connect to it."
"Launch nREPL server of the specified project and connect to it.
.Available parameters
[%autowidth,cols=\"a,a\"]
|===
| Parameter | Description
| `[]` | Select a project to launch nREPL server and connect to it.
| `[project]` | Connect to the nREPL server of the specified project.
|==="
[{:as elin :component/keys [host] :keys [message]}]
(let [[{:keys [project]} error] (e.u.param/parse ?InstantParams (:params message))
[project error] (if (and (or (not project)
(= "" project))
(not error))
(select-instant-connect-project elin)
[project error])
{:keys [port language]} (when-not error
(e.f.jack-in/launch-process elin {:forced-project (keyword project)}))]
(if error
Expand Down
79 changes: 79 additions & 0 deletions test/elin/handler/connect_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
(:require
[clojure.string :as str]
[clojure.test :as t]
[elin.constant.jack-in :as e.c.jack-in]
[elin.function.jack-in :as e.f.jack-in]
[elin.function.select :as e.f.select]
[elin.handler.connect :as sut]
[elin.message :as e.message]
[elin.protocol.nrepl :as e.p.nrepl]
[elin.test-helper :as h]))

Expand Down Expand Up @@ -56,3 +60,78 @@

(t/is (= ["Already connected to localhost:1234"]
(h/get-outputs host)))))))

(t/deftest instant-test
(let [context (h/test-elin)]
(t/testing "Selected project"
(t/testing "Positive"
(let [context (assoc-in context [:message :params] [""])
launched (atom [])
connected (atom [])
test-project "test-project"
test-port (rand-int 10000)]
(with-redefs [e.f.select/select-from-candidates (constantly test-project)
e.f.jack-in/launch-process (fn [_ {:keys [forced-project]}]
(swap! launched conj forced-project)
{:port test-port :language (name forced-project)})
sut/connect* (fn [_ params]
(swap! connected conj params))]
(t/is (= [{:hostname "localhost"
:port test-port
:language test-project
:wait? true}]
(sut/instant context)))

(t/is (= [{:hostname "localhost"
:port test-port
:language test-project
:wait? true}]
@connected))

(t/is (= [(keyword test-project)]
@launched)))))

(t/testing "Negative"
(t/testing "No selection"
(let [context (assoc-in context [:message :params] [""])
error (atom [])]
(with-redefs [e.f.select/select-from-candidates (constantly nil)
e.message/error (fn [_ & texts] (swap! error concat texts) nil)]
(t/is (nil? (sut/instant context)))
(t/is (= ["Invalid parameter" "No project is selected."]
@error)))))))

(t/testing "Specified project"
(t/testing "Positive"
(let [context (assoc-in context [:message :params] [(name e.c.jack-in/babashka)])
launched (atom [])
connected (atom [])
test-port (rand-int 10000)]
(with-redefs [e.f.jack-in/launch-process (fn [_ {:keys [forced-project]}]
(swap! launched conj forced-project)
{:port test-port :language "clojure"})
sut/connect* (fn [_ params]
(swap! connected conj params))]
(t/is (= [{:hostname "localhost"
:port test-port
:language "clojure"
:wait? true}]
(sut/instant context)))

(t/is (= [{:hostname "localhost"
:port test-port
:language "clojure"
:wait? true}]
@connected))

(t/is (= [e.c.jack-in/babashka]
@launched)))))

(t/testing "Negative"
(t/testing "Invalid project"
(let [context (assoc-in context [:message :params] ["invalid project"])
error (atom [])]
(with-redefs [e.message/error (fn [_ & texts] (swap! error concat texts) nil)]
(t/is (nil? (sut/instant context)))
(t/is (= "Invalid parameter"
(first @error))))))))))

0 comments on commit 3f2f910

Please sign in to comment.