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 ElinCycleFunctionAndTest command #7

Merged
merged 6 commits into from
Nov 16, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dev/analysis.edn

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions doc/elin-mapping.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ ElinCycleSourceAndTest
Calls `elin.handler.navigate/cycle-source-and-test` handler.
Key is mapped to |<Plug>(elin_cycle_source_and_test)|.

*ElinCycleFunctionAndTest*
ElinCycleFunctionAndTest
Calls `elin.handler.navigate/cycle-function-and-test` handler.
Key is mapped to |<Plug>(elin_cycle_function_and_test)|.

*ElinToggleInfoBuffer*
ElinToggleInfoBuffer
Calls `elin#internal#buffer#info#toggle()`.
Expand Down Expand Up @@ -338,6 +343,10 @@ MAPPINGS *elin-mappings*
<Plug>(elin_cycle_source_and_test)
Same as |ElinCycleSourceAndTest|.

*<Plug>(elin_cycle_function_and_test)*
<Plug>(elin_cycle_function_and_test)
Same as |ElinCycleFunctionAndTest|.

*<Plug>(elin_toggle_info_buffer)*
<Plug>(elin_toggle_info_buffer)
Same as |ElinToggleInfoBuffer|.
Expand Down Expand Up @@ -385,6 +394,7 @@ n <Leader>tn <Plug>(elin_test_in_ns)
n <Leader>tl <Plug>(elin_test_last)
n <Leader>tr <Plug>(elin_test_last_failed)
n tt <Plug>(elin_cycle_source_and_test)
n TT <Plug>(elin_cycle_function_and_test)
n <Leader>ss <Plug>(elin_toggle_info_buffer)
n <Leader>sl <Plug>(elin_clear_info_buffer)
n <Leader><Esc> <Plug>(elin_clear_virtual_texts)
Expand Down
3 changes: 3 additions & 0 deletions plugin/elin.vim
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ command! ElinTestInNs call elin#notify('elin.handler.test/run-tests-in-ns', [])
command! ElinTestLast call elin#notify('elin.handler.test/rerun-last-tests', [])
command! ElinTestLastFailed call elin#notify('elin.handler.test/rerun-last-failed-tests', [])
command! ElinCycleSourceAndTest call elin#notify('elin.handler.navigate/cycle-source-and-test', [])
command! ElinCycleFunctionAndTest call elin#notify('elin.handler.navigate/cycle-function-and-test', [])

" Misc
command! ElinToggleInfoBuffer call elin#internal#buffer#info#toggle()
Expand Down Expand Up @@ -189,6 +190,7 @@ nnoremap <silent> <Plug>(elin_test_in_ns) <Cmd>ElinTestInNs<CR>
nnoremap <silent> <Plug>(elin_test_last) <Cmd>ElinTestLast<CR>
nnoremap <silent> <Plug>(elin_test_last_failed) <Cmd>ElinTestLastFailed<CR>
nnoremap <silent> <Plug>(elin_cycle_source_and_test) <Cmd>ElinCycleSourceAndTest<CR>
nnoremap <silent> <Plug>(elin_cycle_function_and_test) <Cmd>ElinCycleFunctionAndTest<CR>

" Misc
nnoremap <silent> <Plug>(elin_toggle_info_buffer) <Cmd>ElinToggleInfoBuffer<CR>
Expand Down Expand Up @@ -269,6 +271,7 @@ function! s:default_key_mappings() abort
call s:define_mapping('nmap', '<Leader>tl', '<Plug>(elin_test_last)')
call s:define_mapping('nmap', '<Leader>tr', '<Plug>(elin_test_last_failed)')
call s:define_mapping('nmap', 'tt', '<Plug>(elin_cycle_source_and_test)')
call s:define_mapping('nmap', 'TT', '<Plug>(elin_cycle_function_and_test)')

" Misc
call s:define_mapping('nmap', '<Leader>ss', '<Plug>(elin_toggle_info_buffer)')
Expand Down
21 changes: 14 additions & 7 deletions resources/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@

:http-server {:port #empty-port []}

:common {:template {;; NOTE {extension {:src "skeleton for source" :test "skeleton for test"}}
:.clj {:src #resource "template/clj_src_skeleton.mustache"
:test #resource "template/clj_test_skeleton.mustache"
:test-var #resource "template/var_test_skeleton.mustache"}
:.cljs {:src #resource "template/clj_src_skeleton.mustache"
:test #resource "template/cljs_test_skeleton.mustache"
:test-var #resource "template/var_test_skeleton.mustache"}
:.cljc {:src #resource "template/clj_src_skeleton.mustache"
:test #resource "template/cljc_test_skeleton.mustache"
:test-var #resource "template/var_test_skeleton.mustache"}}}

:interceptor {:uses [elin.interceptor.autocmd/deinitialize {}
elin.interceptor.autocmd/switch-connection {}
elin.interceptor.autocmd/ns-load {}
elin.interceptor.autocmd/skeleton {;; NOTE {extension {:src "skeleton for source" :test "skeleton for test"}}
:.clj {:src #resource "template/clj_src_skeleton.mustache"
:test #resource "template/clj_test_skeleton.mustache"}
:.cljs {:src #resource "template/clj_src_skeleton.mustache"
:test #resource "template/cljs_test_skeleton.mustache"}
:.cljc {:src #resource "template/clj_src_skeleton.mustache"
:test #resource "template/cljc_test_skeleton.mustache"}}
elin.interceptor.autocmd/skeleton #ref [:common :template]
elin.interceptor.autocmd/clj-kondo-analyzing {}
elin.interceptor.connect/connected {}
elin.interceptor.connect/raw-message-channel {}
Expand Down Expand Up @@ -95,6 +100,8 @@
:java-classes #include "java_classes.edn"
:interceptor {:includes [elin.interceptor.handler.namespace/show-result]}}
elin.handler.navigate/cycle-source-and-test {:interceptor {:includes [elin.interceptor.handler/jump-to-file]}}
elin.handler.navigate/cycle-function-and-test {:template #ref [:common :template]
:interceptor {:includes [elin.interceptor.handler/jump-to-file]}}
elin.handler.navigate/jump-to-definition {:interceptor {:includes [elin.interceptor.handler/jump-to-file]}}
elin.handler.navigate/references {:interceptor {:includes [elin.interceptor.handler/jump-to-file]}}
elin.handler.navigate/local-references {:interceptor {:includes [elin.interceptor.handler/jump-to-file]}}
Expand Down
3 changes: 3 additions & 0 deletions resources/template/var_test_skeleton.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(t/deftest {{name}}
(t/testing "Positive"
(t/is (thrown? Exception (sut/{{source-name}})))))
22 changes: 21 additions & 1 deletion src/elin/function/evaluate.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
[elin.function.nrepl :as e.f.nrepl]
[elin.function.sexpr :as e.f.sexpr]
[elin.protocol.host :as e.p.host]
[elin.util.nrepl :as e.u.nrepl]))
[elin.util.nrepl :as e.u.nrepl]
[rewrite-clj.zip :as r.zip]))

(defn- eval!!
[nrepl code options]
Expand Down Expand Up @@ -117,3 +118,22 @@
{})
resp (e.f.nrepl/eval!! nrepl code' options)]
resp))

(defn- extract-multi-method-name
[code]
(let [zloc (-> (r.zip/of-string code)
(r.zip/down))]
(when (contains? #{'defmulti 'defmethod} (r.zip/sexpr zloc))
(some-> zloc
(r.zip/next)
(r.zip/sexpr)
(str)))))

(defn get-var-name-from-current-top-list
[elin]
(e/let [{:as resp :keys [code response options]} (evaluate-current-top-list elin)
{ns-str :ns} options]
(assoc-in resp [:options :var-name]
(or (some->> (extract-multi-method-name code)
(str ns-str "/"))
(str/replace (:value response) #"^#'" "")))))
6 changes: 6 additions & 0 deletions src/elin/function/nrepl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,9 @@
(async/<!!)
(e.u.nrepl/merge-messages)
(:completions)))

(defn get-cycled-var-name
[var-name]
(if (str/ends-with? var-name "-test")
(str/replace var-name #"-test$" "")
(str var-name "-test")))
54 changes: 53 additions & 1 deletion src/elin/handler/navigate.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
(ns elin.handler.navigate
(:require
[clojure.core.async :as async]
[clojure.java.io :as io]
[clojure.string :as str]
[elin.error :as e]
[elin.function.clj-kondo :as e.f.clj-kondo]
[elin.function.evaluate :as e.f.evaluate]
[elin.function.lookup :as e.f.lookup]
[elin.function.nrepl :as e.f.nrepl]
[elin.function.nrepl.namespace :as e.f.n.namespace]
[elin.function.quickfix :as e.f.quickfix]
[elin.function.sexpr :as e.f.sexpr]
Expand All @@ -13,7 +16,8 @@
[elin.schema.handler :as e.s.handler]
[elin.util.file :as e.u.file]
[elin.util.handler :as e.u.handler]
[malli.core :as m]))
[malli.core :as m]
[pogonos.core :as pogonos]))

(defn- select-ns-and-sym-str [ns-str sym-str]
(let [sym (symbol sym-str)]
Expand Down Expand Up @@ -86,6 +90,54 @@
{:ns ns-str :path ns-path :file-separator file-sep})]
(e.u.handler/jump-to-file-response cycled-path)))

(defn cycle-function-and-test
[elin]
(e/let [{:keys [template]} (e.u.handler/config elin #'cycle-function-and-test)
{:keys [options]} (e.f.evaluate/get-var-name-from-current-top-list elin)
{ns-str :ns var-name :var-name path :file} options
file-sep (e.u.file/guess-file-separator path)
cycled-path (e.f.n.namespace/get-cycled-namespace-path
{:ns ns-str :path path :file-separator file-sep})
cycled-ns-str (or (e.f.n.namespace/guess-namespace-from-path cycled-path)
;; TODO fallback to another process
(e/fault))
var-name (-> var-name
(str/split #"/" 2)
(second))
cycled-var-name (e.f.nrepl/get-cycled-var-name var-name)
lookup-resp (e/error-or (e.f.lookup/lookup elin cycled-ns-str cycled-var-name))]
(if lookup-resp
(e.u.handler/jump-to-file-response (:file lookup-resp)
(:line lookup-resp)
(:column lookup-resp))
(if (not (str/ends-with? cycled-ns-str "-test"))
(e.u.handler/jump-to-file-response cycled-path)
(e/let [cycled-file (io/file cycled-path)
ext (e.u.file/get-file-extension cycled-path)
ns-template (or (get-in template [(keyword ext) :test])
(e/not-found))
var-template (or (get-in template [(keyword ext) :test-var])
(e/not-found))
params {:path cycled-path
:ns cycled-ns-str
:source-ns ns-str
:test? true
:name cycled-var-name
:source-name var-name}]
(when-not (.exists (io/file cycled-path))
(spit cycled-path
(pogonos/render-string ns-template params)))

(let [tail-lnum (-> (slurp cycled-file)
(str/split-lines)
(count))]
(spit cycled-file
(str "\n" (pogonos/render-string var-template params))
:append true)
(e.u.handler/jump-to-file-response cycled-path
(+ 2 tail-lnum)
1)))))))

;; (m/=> references [:=> [:cat e.s.handler/?Elin] (e.schema/error-or e.s.handler/?JumpToFile)])
(defn references
"Find the places where the symbol under the cursor is used, and jump if there is only one.
Expand Down
21 changes: 3 additions & 18 deletions src/elin/handler/test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns elin.handler.test
(:require
[clojure.core.async :as async]
[clojure.string :as str]
[elin.constant.interceptor :as e.c.interceptor]
[elin.constant.nrepl :as e.c.nrepl]
[elin.error :as e]
Expand All @@ -17,28 +16,14 @@
[elin.protocol.nrepl :as e.p.nrepl]
[elin.schema.handler :as e.s.handler]
[elin.util.map :as e.u.map]
[malli.core :as m]
[rewrite-clj.zip :as r.zip]))

(defn- extract-multi-method-name
[code]
(let [zloc (-> (r.zip/of-string code)
(r.zip/down))]
(when (contains? #{'defmulti 'defmethod} (r.zip/sexpr zloc))
(some-> zloc
(r.zip/next)
(r.zip/sexpr)
(str)))))
[malli.core :as m]))

(m/=> run-test-under-cursor [:=> [:cat e.s.handler/?Elin] any?])
(defn run-test-under-cursor
"Run test under cursor."
[{:as elin :component/keys [interceptor session-storage]}]
(e/let [{:keys [code response options]} (e.f.evaluate/evaluate-current-top-list elin)
{ns-str :ns} options
var-name (or (some->> (extract-multi-method-name code)
(str ns-str "/"))
(str/replace (:value response) #"^#'" ""))
(e/let [{:keys [options]} (e.f.evaluate/get-var-name-from-current-top-list elin)
{ns-str :ns var-name :var-name} options
context (-> (e.u.map/select-keys-by-namespace elin :component)
(assoc :ns ns-str
:line (:line options)
Expand Down