Skip to content

Commit

Permalink
Fix completion to consider required namespaces without alias or refers.
Browse files Browse the repository at this point in the history
Fixes #1352
  • Loading branch information
ericdallo committed Nov 2, 2022
1 parent 4a1aa35 commit ac5dc9f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- For users with fewer cores, avoid unnecessary waits for file analysis.
- Reduce CPU usage by aborting requests that the client won't use.
- Fix to mark some code actions as preferred, so editors can emphasize them. https://github.com/clojure-lsp/lsp4clj/issues/32
- Fix completion to consider required namespaces without alias or refers. #1352

## 2022.10.05-16.39.51

Expand Down
18 changes: 16 additions & 2 deletions lib/src/clojure_lsp/feature/completion.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[clojure-lsp.dep-graph :as dep-graph]
[clojure-lsp.feature.add-missing-libspec :as f.add-missing-libspec]
[clojure-lsp.feature.completion-snippet :as f.completion-snippet]
[clojure-lsp.feature.format]
[clojure-lsp.feature.hover :as f.hover]
[clojure-lsp.parser :as parser]
[clojure-lsp.queries :as q]
Expand Down Expand Up @@ -109,9 +110,12 @@
(#{:keyword-usages :keyword-definitions} bucket)
(keyword-element->str element cursor-alias priority)

(#{:namespace-alias :namespace-usages} bucket)
(#{:namespace-alias} bucket)
(some-> alias name)

(#{:namespace-usages} bucket)
(some-> element :name name)

(#{:java-class-usages} bucket)
(java-element->class-name element)

Expand Down Expand Up @@ -227,6 +231,10 @@
[_bucket matches-fn _cursor-element]
(name-matches-xf matches-fn))

(defmethod bucket-elems-xf :namespace-usages
[_bucket matches-fn _cursor-element]
(name-matches-xf matches-fn))

(defmethod bucket-elems-xf :var-definitions
[_bucket matches-fn {cursor-from :from cursor-bucket :bucket}]
(let [on-var-usage? (identical? :var-usages cursor-bucket)]
Expand Down Expand Up @@ -269,7 +277,13 @@
(contains? cursor-langs (:lang %)))))
(get local-buckets bucket))))
(map #(element->completion-item % nil :simple-cursor resolve-support)))
[:namespace-definitions :var-definitions :keyword-definitions :keyword-usages :locals :java-class-usages])))
[:namespace-definitions
:namespace-usages
:var-definitions
:keyword-definitions
:keyword-usages
:locals
:java-class-usages])))

(defn ^:private with-definition-kws-args-element-items
[matches-fn {:keys [arglist-kws name-row name-col uri]} resolve-support]
Expand Down
19 changes: 19 additions & 0 deletions lib/test/clojure_lsp/feature/completion_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,25 @@
(h/db*)))
(swap! (h/db*) merge {:settings {:completion {:additional-edits-warning-text nil}}})))

(deftest completing-namespace-usages
(h/load-code-and-locs
(h/code "(ns some.foo-ns)") (h/file-uri "file:///a.clj"))
(h/load-code-and-locs
(h/code "(ns some.bar-ns)") (h/file-uri "file:///b.clj"))
(let [[[row col]] (h/load-code-and-locs
(h/code "(ns some.baz-ns"
" (:require [some.foo-ns :as f]"
" [some.bar-ns]))"
"some.|") (h/file-uri "file:///c.clj"))]
(testing "completing all available namespace-usages"
(h/assert-submaps
[{:label "some.bar-ns" :kind :module :detail ""}
{:label "some.baz-ns" :kind :module}
;; TODO avoid adding same namespace twice
{:label "some.foo-ns" :kind :module :detail ""}
{:label "some.foo-ns" :kind :property :detail ":as f"}]
(f.completion/completion (h/file-uri "file:///c.clj") row col (h/db))))))

(deftest completing-refers
(h/load-code-and-locs
(h/code "(ns some-ns)"
Expand Down

0 comments on commit ac5dc9f

Please sign in to comment.