Skip to content

Commit b425dd9

Browse files
acamargoericdallo
andauthored
Fix code suggestion for unresolved-symbol (#2018)
* Add test scenario for issue 2017 * Remove local var definitions from suggestions * Refactor test to use matcher-combinators * Update CHANGELOG * Update lib/src/clojure_lsp/feature/add_missing_libspec.clj Co-authored-by: Eric Dallo <[email protected]> * Refactor thread last as transducer * Move matcher-combinators dependency to test profile * Make tests to pass on Windows * 💅 --------- Co-authored-by: Eric Dallo <[email protected]>
1 parent 336a273 commit b425dd9

File tree

5 files changed

+70
-43
lines changed

5 files changed

+70
-43
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Bump clj-kondo to `2025.04.07`.
88
- Editor
99
- Improve timbre context log.
10+
- Fix suggestion for add require code action. #2017
1011

1112
## 2025.03.27-20.21.36
1213

lib/deps.edn

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
:aliases {:test {:extra-deps {clojure-lsp/common-test {:local/root "../common-test"}
2727
lambdaisland/kaocha {:mvn/version "1.91.1392"}
2828
com.taoensso/timbre {:mvn/version "6.7.0-alpha1"}
29-
org.clojure/test.check {:mvn/version "1.1.1"}}
29+
org.clojure/test.check {:mvn/version "1.1.1"}
30+
nubank/matcher-combinators {:mvn/version "3.9.1"}}
3031
:jvm-opts ["-XX:-OmitStackTraceInFastThrow"]
3132
:extra-paths ["test"]
3233
:main-opts ["-m" "kaocha.runner"]}

lib/src/clojure_lsp/feature/add_missing_libspec.clj

+11-5
Original file line numberDiff line numberDiff line change
@@ -576,18 +576,24 @@
576576
namespace-suggestions (find-namespace-suggestions
577577
(or cursor-namespace-str cursor-name-str)
578578
(find-alias-ns-pairs db uri))
579+
uri-nses (->> (get-in db [:analysis uri :namespace-definitions])
580+
(map :name)
581+
set)
579582
suggestions (if (namespace cursor-sym)
580583
namespace-suggestions
581584
(concat
582585
namespace-suggestions
583586
(if-let [common-refer (get common-sym/common-refers->info (symbol cursor-name-str))]
584587
[{:ns (name common-refer)
585588
:refer cursor-name-str}]
586-
(->> (q/find-all-var-definitions db)
587-
(filter #(= cursor-name-str (str (:name %))))
588-
(map (fn [element]
589-
{:ns (str (:ns element))
590-
:refer cursor-name-str}))))))]
589+
(into []
590+
(comp
591+
(remove #(uri-nses (:ns %)))
592+
(filter #(= cursor-name-str (str (:name %))))
593+
(map (fn [element]
594+
{:ns (str (:ns element))
595+
:refer cursor-name-str})))
596+
(q/find-all-var-definitions db)))))]
591597
suggestions)))
592598

593599
(defn ^:private find-forms [zloc p?]

lib/test/clojure_lsp/feature/code_actions_test.clj

+22-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
[clojure-lsp.test-helper :as h]
88
[clojure.java.io :as io]
99
[clojure.string :as string]
10-
[clojure.test :refer [deftest is testing]]))
10+
[clojure.test :refer [deftest is testing]]
11+
[matcher-combinators.matchers :as m]
12+
[matcher-combinators.test :refer [match?]]))
1113

1214
(h/reset-components-before-test)
1315

@@ -35,6 +37,25 @@
3537
{:workspace {:workspace-edit true}}
3638
(h/db))))))
3739

40+
(deftest avoid-self-refer-suggestion
41+
(h/load-code-and-locs (h/code "(ns baz)"
42+
"(defn func-2 []"
43+
" (func-1))"
44+
"(defn func-1 [] ())"))
45+
(testing "Given a unresolved-var because the function is declared after its use
46+
When I ask for code actions
47+
Then I don't get an 'Add require' suggestion"
48+
(is (match? (m/mismatch [{:title "Add require '[baz :refer [func-1]]'"}])
49+
(f.code-actions/all (zloc-of h/default-uri)
50+
h/default-uri
51+
3
52+
4
53+
[{:code "unresolved-symbol"
54+
:message "Unresolved symbol: func-1"
55+
:range {:start {:line 2 :character 3}}}]
56+
{}
57+
(h/db))))))
58+
3859
(deftest add-alias-suggestion-code-actions
3960
(h/load-code-and-locs "(ns clojure.set)" (h/file-uri "file:///clojure.core.clj"))
4061
(h/load-code-and-locs "(ns medley.core)" (h/file-uri "file:///medley.core.clj"))

lib/test/clojure_lsp/feature/diagnostics_test.clj

+34-36
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
[clojure-lsp.shared :as shared]
66
[clojure-lsp.test-helper :as h]
77
[clojure.core.async :as async]
8-
[clojure.test :refer [deftest is testing]]))
8+
[clojure.test :refer [deftest is testing]]
9+
[matcher-combinators.test :refer [match?]]))
910

1011
(def findings (atom []))
1112

@@ -121,7 +122,7 @@
121122
{:clojure-lsp/unused-public-var
122123
{:level :info
123124
:ignore-test-references? false}}}})
124-
(h/assert-submaps [] @findings))
125+
(is (match? [] @findings)))
125126
(testing "Given a public var foo/bar in a src namespace
126127
And being referenced in a test namespace foo-test
127128
When linter level is :info
@@ -137,17 +138,16 @@
137138
{:clojure-lsp/unused-public-var
138139
{:level :info
139140
:ignore-test-references? true}}}})
140-
(h/assert-submaps
141-
[{:end-row 1
142-
:type :clojure-lsp/unused-public-var
143-
:level :info
144-
:filename "/project/src/foo.clj"
145-
:col 16
146-
:uri "file:///project/src/foo.clj"
147-
:end-col 19
148-
:message "Unused public var 'foo/bar'"
149-
:row 1}]
150-
@findings)))
141+
(is (match? [{:end-row 1
142+
:type :clojure-lsp/unused-public-var
143+
:level :info
144+
:filename (h/file-path "/project/src/foo.clj")
145+
:col 16
146+
:uri (h/file-uri "file:///project/src/foo.clj")
147+
:end-col 19
148+
:message "Unused public var 'foo/bar'"
149+
:row 1}]
150+
@findings))))
151151

152152
(deftest unused-public-var-declared-inside-test-namespace
153153
(swap! (h/db*) merge {:project-root-uri (h/file-uri "file:///project")
@@ -167,17 +167,16 @@
167167
{:clojure-lsp/unused-public-var
168168
{:level :info
169169
:ignore-test-references? false}}}})
170-
(h/assert-submaps
171-
[{:end-row 1
172-
:type :clojure-lsp/unused-public-var
173-
:level :info
174-
:filename "/project/test/foo_test.clj"
175-
:col 21
176-
:uri "file:///project/test/foo_test.clj"
177-
:end-col 27
178-
:message "Unused public var 'foo-test/helper'"
179-
:row 1}]
180-
@findings))
170+
(is (match? [{:end-row 1
171+
:type :clojure-lsp/unused-public-var
172+
:level :info
173+
:filename (h/file-path "/project/test/foo_test.clj")
174+
:col 21
175+
:uri (h/file-uri "file:///project/test/foo_test.clj")
176+
:end-col 27
177+
:message "Unused public var 'foo-test/helper'"
178+
:row 1}]
179+
@findings)))
181180
(testing "Given a public var foo-test/helper in a test namespace
182181
When linter level is :info
183182
And the :ignore-test-references? flag is on
@@ -190,17 +189,16 @@
190189
:config {:linters {:clojure-lsp/unused-public-var
191190
{:level :info
192191
:ignore-test-references? true}}}})
193-
(h/assert-submaps
194-
[{:end-row 1
195-
:type :clojure-lsp/unused-public-var
196-
:level :info
197-
:filename "/project/test/foo_test.clj"
198-
:col 21
199-
:uri "file:///project/test/foo_test.clj"
200-
:end-col 27
201-
:message "Unused public var 'foo-test/helper'"
202-
:row 1}]
203-
@findings))
192+
(is (match? [{:end-row 1
193+
:type :clojure-lsp/unused-public-var
194+
:level :info
195+
:filename (h/file-path "/project/test/foo_test.clj")
196+
:col 21
197+
:uri (h/file-uri "file:///project/test/foo_test.clj")
198+
:end-col 27
199+
:message "Unused public var 'foo-test/helper'"
200+
:row 1}]
201+
@findings)))
204202
(testing "Given a public var bar-test/baz in a test namespace
205203
When linter level is :info
206204
And the :ignore-test-references? flag is on
@@ -215,7 +213,7 @@
215213
:config {:linters {:clojure-lsp/unused-public-var
216214
{:level :info
217215
:ignore-test-references? true}}}})
218-
(h/assert-submaps [] @findings)))
216+
(is (match? [] @findings))))
219217

220218
(deftest lint-project-public-vars
221219
(swap! (h/db*) merge {:project-root-uri (h/file-uri "file:///project")

0 commit comments

Comments
 (0)