Skip to content

Commit

Permalink
Autocomplete: #17, implement basic keys and symbols completion
Browse files Browse the repository at this point in the history
_ Complete keys of struct under cursor. Based of :type value.
- Complete symbols when cursor placed after :type keyword

Symbols completions sources:
- from current ns
- from zen namespace
- from imported namespaces
  • Loading branch information
qdzo committed Apr 4, 2022
1 parent 147680b commit f51e63a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 2 deletions.
28 changes: 28 additions & 0 deletions server/src/zen_lang/lsp_server/impl/autocomplete.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns zen-lang.lsp-server.impl.autocomplete)


(defn find-completions [ztx {:keys [uri struct-path]}]
(cond
;; :type symbol suggestion
(= :type (last struct-path))
(let [cur-ns-edn (get-in @ztx [:file uri :last-valid-edn])
zen-symbols (keys (:symbols @ztx))
cur-ns-symbols (-> cur-ns-edn (dissoc 'import 'ns) keys)
imported-symbols (reduce (fn [acc ns']
(into acc (keys (get-in @ztx [:ns ns']))))
[]
(get cur-ns-edn 'import))]
(->> (concat zen-symbols cur-ns-symbols imported-symbols)
sort
(mapv str)))

;; schema :keys keyword suggestion
:else
(let [pos-ctx-struct (get-in @ztx (into [:file uri :last-valid-edn] struct-path))
type-symbol (:type pos-ctx-struct)]
(when type-symbol
(->> (get-in @ztx [:symbols type-symbol :keys]) keys sort (mapv str))))))


(comment
)
5 changes: 4 additions & 1 deletion server/src/zen_lang/lsp_server/impl/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
:refer [get-location
location->zloc
zloc->path]]
[zen-lang.lsp-server.impl.autocomplete :as autocomplete]
[zen.core :as zen]
[zen.store :as store])
(:import
Expand Down Expand Up @@ -244,8 +245,10 @@
_ (debug :path path)
namespaces (keys (:ns @zen-ctx))
symbols (keys (:symbols @zen-ctx))
zen-completions' (autocomplete/find-completions zen-ctx {:uri uri :struct-path path})
completions (map #(org.eclipse.lsp4j.CompletionItem. %)
(map str (concat namespaces symbols)))]
(or zen-completions'
(map str (concat namespaces symbols))))]
(CompletableFuture/completedFuture
(vec completions))))

Expand Down
4 changes: 3 additions & 1 deletion server/test-resources/test-project/zrc/bar.edn
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
{:zen/tags #{zen/schema}
:type zen/map
:keys {:foo {:confirms #{foo/schema}} ;; No errors expected
}}}
}}

}
22 changes: 22 additions & 0 deletions server/test-resources/test-project/zrc/baz.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{ns baz
import #{foo}

schema
{:zen/tags #{zen/schema}
:type zen/map
:keys {:foo {:confirms #{foo/schema}} ;; No errors expected
}}

schema2
{:zen/tags #{zen/schema}
:type zen/map
:keys {:foo {:type zen/coll
,
}
:baz {:type zen/string
,

}
:qux {:type zen/string
,
}}}}
35 changes: 35 additions & 0 deletions server/test/zen_lsp/autocomplete_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(ns zen-lsp.autocomplete-test
(:require [clojure.test :as test :refer [deftest is testing]]
[zen-lang.lsp-server.impl.server :as server]
[zen-lang.lsp-server.impl.autocomplete :as zl]))

(defn file->message [f]
{:text (slurp f) :uri f})

(def ztx
(do (alter-var-root #'server/zen-ctx (constantly (server/new-context)))
(server/initialize-paths {:root "test-resources/test-project"})
(server/load-document (file->message "test-resources/test-project/zrc/baz.edn"))
server/zen-ctx))

(deftest find-completions-test
(testing "`find-completion` should"

(testing "find keys of schema under cursor (keywords)"

(is (= [":every" ":maxItems" ":minItems" ":schema-index"]
(let [f "test-resources/test-project/zrc/baz.edn"
path ['schema2 :keys :foo]]
(zl/find-completions ztx {:uri f :struct-path path}))))

(is (= [":maxLength" ":minLength" ":regex" ":tags"]
(let [f "test-resources/test-project/zrc/baz.edn"
path ['schema2 :keys :baz]]
(zl/find-completions ztx {:uri f :struct-path path})))))

(testing "find `symbols` applied by given context"
(is (= (conj (->> @ztx :symbols keys (mapv str) set) "schema" "schema2")
(let [f "test-resources/test-project/zrc/baz.edn"
path ['schema2 :qux :type]]
(set (zl/find-completions ztx {:uri f :struct-path path}))))) )
))

0 comments on commit f51e63a

Please sign in to comment.