-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Autocomplete: #17, implement basic keys and symbols completion
_ 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
Showing
5 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,6 @@ | |
{:zen/tags #{zen/schema} | ||
:type zen/map | ||
:keys {:foo {:confirms #{foo/schema}} ;; No errors expected | ||
}}} | ||
}} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
, | ||
}}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}))))) ) | ||
)) |