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

Introduce modify-code interceptor instead of code-change interceptor #5

Merged
merged 7 commits into from
Nov 15, 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.

9 changes: 4 additions & 5 deletions resources/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@
elin.interceptor.test/append-test-result-to-info-buffer
elin.interceptor.test/apply-test-result-to-quickfix
elin.interceptor.test/store-last-failed-test-query
elin.interceptor.test/output-test-result-to-cmdline]}
:code-change {:includes [elin.interceptor.code-change/show-code-changed-result]}}}
elin.interceptor.test/output-test-result-to-cmdline]}}}

:handler {:uses [elin.handler.callback/callback {}
elin.handler.complete/complete {:log {:min-level :error}}
Expand Down Expand Up @@ -89,10 +88,11 @@
:interceptor {:uses [elin.interceptor.handler/append-result-to-info-buffer {:show-temporarily? true}]}}
elin.handler.namespace/add-libspec {:favorites #include "favorite_aliases.edn"
:java-classes #include "java_classes.edn"
:interceptor #ref [:interceptor :preset :code-change]}
:interceptor {:includes [elin.interceptor.handler.namespace/show-result
elin.interceptor.handler.namespace/yank-alias]}}
elin.handler.namespace/add-missing-libspec {:favorites #include "favorite_aliases.edn"
:java-classes #include "java_classes.edn"
:interceptor #ref [:interceptor :preset :code-change]}
: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/jump-to-definition {:interceptor {:includes [elin.interceptor.handler/jump-to-file]}}
elin.handler.navigate/references {:interceptor {:includes [elin.interceptor.handler/jump-to-file]}}
Expand All @@ -107,7 +107,6 @@
elin-alias-run-test-focused-current-testing {:handler elin.handler.test/run-test-under-cursor
:config {:interceptor {:includes [elin.interceptor.test/focus-current-testing]}}}}


:initialize {:export {"g:elin_http_server_port" #ref [:http-server :port]}}}

:clj-kondo {:command "clj-kondo"
Expand Down
1 change: 0 additions & 1 deletion src/elin/component/interceptor.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
[elin.config :as e.config]
[elin.constant.interceptor :as e.c.interceptor]
[elin.interceptor.autocmd]
[elin.interceptor.code-change]
[elin.interceptor.connect]
[elin.interceptor.nrepl]
[elin.interceptor.output]
Expand Down
2 changes: 1 addition & 1 deletion src/elin/constant/interceptor.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
(def test-result ::test-result)
(def quickfix ::quickfix)
(def debug ::debug)
(def code-change ::code-change)
(def modify-code ::modify-code)
77 changes: 39 additions & 38 deletions src/elin/handler/namespace.clj
Original file line number Diff line number Diff line change
Expand Up @@ -42,54 +42,55 @@
(symbol alias-str))
{ns-form :code lnum :lnum col :col} (e.f.sexpr/get-namespace-sexpr elin)
context (-> (e.u.map/select-keys-by-namespace elin :component)
(assoc :code ns-form
:type :add-libspec
:target {:namespace-symbol ns-sym
:alias-symbol alias-sym}))]
(e.p.interceptor/execute
interceptor e.c.interceptor/code-change context
(fn [{:as ctx :keys [code target]}]
(let [{:keys [namespace-symbol alias-symbol]} target]
(if (has-namespace? code namespace-symbol)
(assoc ctx :response false)
(e/let [ns-form' (e.u.sexpr/add-require code namespace-symbol alias-symbol)]
(e.f.sexpr/replace-list-sexpr ctx lnum col ns-form')
(e.f.evaluate/evaluate-namespace-form ctx)
(assoc ctx :response true))))))))
(assoc :code ns-form))
{:keys [result code]} (e.p.interceptor/execute
interceptor e.c.interceptor/modify-code context
(fn [{:as ctx :keys [code]}]
(if (has-namespace? code ns-sym)
(assoc ctx :result false)
(e/let [code' (e.u.sexpr/add-require code ns-sym alias-sym)]
(assoc ctx :result true :code code')))))]
(when result
(e.f.sexpr/replace-list-sexpr elin lnum col code)
(e.f.evaluate/evaluate-namespace-form elin))
{:result result
:target ns-sym
:alias alias-sym}))

(defn- add-missing-import*
[{:as elin :component/keys [interceptor]} class-name-sym]
(e/let [{ns-form :code lnum :lnum col :col} (e.f.sexpr/get-namespace-sexpr elin)
context (-> (e.u.map/select-keys-by-namespace elin :component)
(assoc :code ns-form
:type :add-missing-import
:target {:class-name-symbol class-name-sym}))]
(e.p.interceptor/execute
interceptor e.c.interceptor/code-change context
(fn [{:as ctx :keys [code target]}]
(let [ns-form' (e.u.sexpr/add-import code (:class-name-symbol target))]
(e.f.sexpr/replace-list-sexpr ctx lnum col ns-form')
(e.f.evaluate/evaluate-namespace-form ctx)
(assoc ctx :response true))))))
(assoc :code ns-form))
{:keys [result code]} (e.p.interceptor/execute
interceptor e.c.interceptor/modify-code context
(fn [{:as ctx :keys [code]}]
(e/let [code' (e.u.sexpr/add-import code class-name-sym)]
(assoc ctx :result true :code code'))))]
(when result
(e.f.sexpr/replace-list-sexpr elin lnum col code)
(e.f.evaluate/evaluate-namespace-form elin))
{:result result
:target class-name-sym}))

(defn- add-missing-require*
[{:as elin :component/keys [interceptor]} alias-sym ns-sym]
(e/let [{ns-form :code lnum :lnum col :col} (e.f.sexpr/get-namespace-sexpr elin)
context (-> (e.u.map/select-keys-by-namespace elin :component)
(assoc :code ns-form
:type :add-missing-require
:target {:namespace-symbol ns-sym
:alias-symbol alias-sym}))]
(e.p.interceptor/execute
interceptor e.c.interceptor/code-change context
(fn [{:as ctx :keys [code target]}]
(let [{:keys [namespace-symbol alias-symbol]} target]
(if (has-namespace? code namespace-symbol)
(assoc ctx :response false)
(let [ns-form' (e.u.sexpr/add-require code namespace-symbol alias-symbol)]
(e.f.sexpr/replace-list-sexpr elin lnum col ns-form')
(e.f.evaluate/evaluate-namespace-form ctx)
(assoc ctx :response true))))))))
(assoc :code ns-form))
{:keys [result code]} (e.p.interceptor/execute
interceptor e.c.interceptor/modify-code context
(fn [{:as ctx :keys [code]}]
(if (has-namespace? code ns-sym)
(assoc ctx :result false)
(e/let [code' (e.u.sexpr/add-require code ns-sym alias-sym)]
(assoc ctx :result true :code code')))))]
(when result
(e.f.sexpr/replace-list-sexpr elin lnum col code)
(e.f.evaluate/evaluate-namespace-form elin))
{:result result
:target ns-sym
:alias alias-sym}))

(defn- add-missing-libspec*
[elin ns-str alias-str]
Expand Down
34 changes: 0 additions & 34 deletions src/elin/interceptor/code_change.clj

This file was deleted.

2 changes: 1 addition & 1 deletion src/elin/interceptor/debug.clj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
e.c.interceptor/test e.s.interceptor/?TestContext
e.c.interceptor/test-result e.s.interceptor/?TestResultContext
e.c.interceptor/quickfix e.s.interceptor/?QuickfixContext
e.c.interceptor/code-change e.s.interceptor/?CodeChangeContext})
e.c.interceptor/modify-code e.s.interceptor/?ModifyCodeContext})

(def interceptor-context-checking
{:kind e.c.interceptor/all
Expand Down
31 changes: 31 additions & 0 deletions src/elin/interceptor/handler/namespace.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(ns elin.interceptor.handler.namespace
(:require
[elin.constant.interceptor :as e.c.interceptor]
[elin.message :as e.message]
[elin.protocol.host :as e.p.host]
[exoscale.interceptor :as ix]))

(def show-result
{:kind e.c.interceptor/handler
:leave (-> (fn [{:component/keys [host] :keys [response]}]
(let [{result :result target-symbol :target alias-symbol :alias} response]
(cond
(not result)
(e.message/warning host (format "'%s' already exists." target-symbol))

(and target-symbol alias-symbol)
(e.message/info host (format "'%s' added as '%s'."
target-symbol alias-symbol))

:else
(e.message/info host (format "'%s' added."
target-symbol)))))
(ix/discard))})

(def yank-alias
{:kind e.c.interceptor/handler
:leave (-> (fn [{:component/keys [host] :keys [response]}]
(let [{result :result alias-symbol :alias} response]
(when (and result alias-symbol)
(e.p.host/yank host (str alias-symbol)))))
(ix/discard))})
8 changes: 3 additions & 5 deletions src/elin/schema/interceptor.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
e.c.interceptor/test
e.c.interceptor/test-result
e.c.interceptor/quickfix
e.c.interceptor/code-change])
e.c.interceptor/modify-code])

(def ?Interceptor
[:map
Expand Down Expand Up @@ -112,12 +112,10 @@
[:list sequential?]]
(m.util/merge e.s.handler/?Components)))

(def ?CodeChangeContext
(def ?ModifyCodeContext
(-> [:map
;; ENTER
[:type [:enum :add-libspec :add-missing-import :add-missing-require]]
[:code string?]
[:target map?]
;; LEAVE
[:response {:optional true} boolean?]]
[:result {:optional true} boolean?]]
(m.util/merge e.s.handler/?Components)))