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

Next release #27

Merged
merged 2 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix: Fix elin.util.sexpr/extract-local-binding-by-position for destru…
…cturing
  • Loading branch information
liquidz committed Mar 1, 2025
commit a666a8c2bbcbf84a0a8662ac723487c58fd700b6
23 changes: 20 additions & 3 deletions src/elin/util/sexpr.clj
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@
(catch Exception ex
(e/not-found {:message (ex-message ex)}))))

(defn- up-until-top-level-binding
[zloc]
(loop [zloc zloc]
(when-let [zloc' (some-> zloc r.zip/up)]
(if (r.zip/list? (r.zip/up zloc'))
zloc
(recur zloc')))))

(m/=> extract-local-binding-by-position [:=> [:cat string? int? int?] (e.schema/error-or string?)])
(defn extract-local-binding-by-position
"Extract a local binding at the given position.
Expand All @@ -134,10 +142,19 @@
(try
(let [zloc (-> (r.zip/of-string code {:track-position? true})
(r.zip/find-last-by-pos [line col]))
s (str (r.zip/string zloc)
destructuring? (some-> zloc
(r.zip/up)
(r.zip/up)
(r.zip/list?)
(not))
zloc' (if destructuring?
(up-until-top-level-binding zloc)
zloc)
[_ col'] (r.zip/position zloc')
s (str (r.zip/string zloc')
" "
(r.zip/string (r.zip/right zloc)))]
(e.u.string/trim-indent (dec col) s 1))
(r.zip/string (r.zip/right zloc')))]
(e.u.string/trim-indent (dec col') s 1))
(catch Exception ex
(e/not-found {:message (ex-message ex)}))))

Expand Down
20 changes: 15 additions & 5 deletions test/elin/util/sexpr_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,21 @@
(sut/extract-form-by-position code 1 18)))))

(t/deftest extract-local-binding-by-position-test
(let [code "(let [a (inc 1) b (* 2 3)] (+ a b))"]
(t/is (= "a (inc 1)"
(sut/extract-local-binding-by-position code 1 7)))
(t/is (= "b (* 2 3)"
(sut/extract-local-binding-by-position code 1 17)))))
(let [code "(let [a (+ 1\n 2)]\n (+ a 1))"]
(t/is (= "a (+ 1\n 2)"
(sut/extract-local-binding-by-position code 1 7))))

(let [code "(let [[a b] [1\n 2]]\n (+ a b))"]
(t/is (= "[a b] [1\n 2]"
(sut/extract-local-binding-by-position code 1 8))))

(let [code "(let [[[a] [b]] [[1]\n [2]]]\n (+ a b))"]
(t/is (= "[[a] [b]] [[1]\n [2]]"
(sut/extract-local-binding-by-position code 1 9))))

(let [code "(let [{:keys [a b]} {:a 1\n :b 2}]\n (+ a b))"]
(t/is (= "{:keys [a b]} {:a 1\n :b 2}"
(sut/extract-local-binding-by-position code 1 15)))))

(t/deftest apply-cider-coordination-test
(let [code "(defn- foo [a b] #dbg (+ b (+ a 1)))"]
Expand Down