Skip to content

Commit 760cd23

Browse files
Autocomplete: #17, complete keys for schema, based on current type confirmation
- Following related schemas by the :confirms key, gather all possible keys - Supports keys gathering from the current and imported namespaces
1 parent c0543c3 commit 760cd23

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

server/src/zen_lang/lsp_server/impl/autocomplete.clj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
(ns zen-lang.lsp-server.impl.autocomplete)
22

3+
(defn gather-confirming-keys
4+
[ztx confirmings]
5+
(when (seq confirmings)
6+
(mapcat #(concat (keys (get-in @ztx [:symbols % :keys]))
7+
(gather-confirming-keys ztx (:confirms (get-in @ztx [:symbols %]))))
8+
confirmings)))
39

410
(defn find-completions [ztx {:keys [uri struct-path]}]
511
(cond
@@ -16,6 +22,17 @@
1622
sort
1723
(mapv str)))
1824

25+
;; :keys symbol suggestion based on :confirms key
26+
(= :keys (last struct-path))
27+
(let [current-ns (get-in @ztx [:file uri :last-valid-edn 'ns])
28+
pos-ctx-struct (get-in @ztx (into [:file uri :last-valid-edn] (butlast struct-path)))
29+
confirms (map #(if (nil? (namespace %))
30+
(symbol (str current-ns) (name %))
31+
%)
32+
(:confirms pos-ctx-struct))
33+
confirming-keys (gather-confirming-keys ztx confirms)]
34+
(vec (map str confirming-keys)))
35+
1936
;; schema :keys keyword suggestion
2037
:else
2138
(let [pos-ctx-struct (get-in @ztx (into [:file uri :last-valid-edn] struct-path))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{ns qux
2+
import #{baz}
3+
4+
grand-parent-schema
5+
{:zen/tags #{zen/schema}
6+
:type zen/map
7+
:keys {:grand-parent-key {:type zen/string}}
8+
:zen/desc "A cool schema"}
9+
10+
parent-schema
11+
{:zen/tags #{zen/schema}
12+
:type zen/map
13+
:confirms #{baz/schema2 grand-parent-schema}
14+
:keys {:parent-key {:type zen/string}}
15+
:zen/desc "A cool schema"}
16+
17+
schema
18+
{:zen/tags #{zen/schema}
19+
:type zen/map
20+
:confirms #{parent-schema}
21+
:keys {:hello {:type zen/string
22+
:confirms #{grand-parent-schema}
23+
:keys {}}
24+
:baz {}}
25+
:zen/desc "A cool schema"}}

server/test/zen_lsp/autocomplete_test.clj

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
(do (alter-var-root #'server/zen-ctx (constantly (server/new-context)))
1111
(server/initialize-paths {:root "test-resources/test-project"})
1212
(server/load-document (file->message "test-resources/test-project/zrc/baz.edn"))
13+
(server/load-document (file->message "test-resources/test-project/zrc/foo.edn"))
1314
server/zen-ctx))
1415

1516
(deftest find-completions-test
@@ -31,5 +32,17 @@
3132
(is (= (conj (->> @ztx :symbols keys (mapv str) set) "schema" "schema2")
3233
(let [f "test-resources/test-project/zrc/baz.edn"
3334
path ['schema2 :qux :type]]
34-
(set (zl/find-completions ztx {:uri f :struct-path path}))))) )
35+
(set (zl/find-completions ztx {:uri f :struct-path path}))))))
36+
37+
(testing "following hierarchy of symbols by the :confirm key gather all possible keys"
38+
(is (= (let [parent-keys (get-in @ztx [:symbols 'qux/parent-schema :keys])
39+
grand-parent-keys (get-in @ztx [:symbols 'qux/grand-parent-schema :keys])
40+
imported-keys (get-in @ztx [:symbols 'baz/schema2 :keys])]
41+
(->> (concat parent-keys grand-parent-keys imported-keys)
42+
keys
43+
set
44+
(map str)
45+
vec))
46+
(let [f "test-resources/test-project/zrc/qux.edn"]
47+
(zl/find-completions ztx {:uri f :struct-path ['schema :keys]})))))
3548
))

0 commit comments

Comments
 (0)