-
Notifications
You must be signed in to change notification settings - Fork 6
/
autocomplete.cljc
199 lines (180 loc) · 8.47 KB
/
autocomplete.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
(ns axel-f.autocomplete
(:require [clojure.string :as string]
[axel-f.core :as axel-f]
[axel-f.functions :as functions]
[clj-fuzzy.metrics :as fuzzy]))
(defn- fuzzy-match? [ex dict]
(if (string? ex)
(->> dict
(map #(hash-map :value %
:distance (if (string/starts-with? (string/lower-case %)
(string/lower-case ex))
0
(fuzzy/jaccard ex %))))
(filter #(< (:distance %) 0.6))
(sort-by :distance)
(map :value))
[]))
(defn- fix-up-objref [ref]
(string/replace ref #"\.$" ""))
(defn- valid-reference? [ref]
(try
(let [ref (axel-f/compile (fix-up-objref ref))]
(and (seqable? ref) (= (first ref) :OBJREF)))
(catch #?(:clj clojure.lang.ExceptionInfo
:cljs ExceptionInfo) _
false)))
(defn- fix-up-fncall [fncall]
(let [fncall (string/replace fncall #",[ ]*$" "")]
(str fncall
(when (> (count (filter #{\(} fncall))
(count (filter #{\)} fncall)))
")"))))
(defn- valid-fncall? [fncall]
(try
(let [fncall (axel-f/compile (fix-up-fncall fncall))]
(and (seqable? fncall) (= (first fncall) :FNCALL)))
(catch #?(:clj clojure.lang.ExceptionInfo
:cljs js/Error) _
false)))
(defn- build-suggestion [type item & [args]]
(merge {:type type}
(case type
:OBJREF {:value (axel-f/->string item)
:desc "Field in the context"}
:FN (merge {:value item}
(select-keys (get functions/functions-map item)
[:desc :args]))
:FNCALL (merge {:value item
:current-arg (let [position (dec (count args))]
(if (< position 0)
0
position))}
(select-keys (get functions/functions-map item)
[:desc :args])))))
(defn- reconstruct-path [path]
(string/join "." (map (fn [s]
(if (integer? s)
(str "[" s "]")
s))
path)))
(defn- build-new-context [context path maybe-path]
(let [new-context (axel-f/run (reconstruct-path path)
context)]
(cond
(map? new-context) (->> new-context
keys
(map axel-f/->string)
(fuzzy-match? maybe-path)
(map #(build-suggestion :OBJREF %)))
(seqable? new-context) (if-let [seq-of-maps (not-empty (filter map? new-context))]
(->> seq-of-maps
(apply merge)
keys
(map axel-f/->string)
dedupe
(fuzzy-match? maybe-path)
(map #(build-suggestion :OBJREF %)))
[]))))
(defn- build-suggestions-for-objref [objref context]
(concat (->> functions/functions-map
keys
(fuzzy-match? objref)
(map #(build-suggestion :FN %)))
(let [[_ & fields] (axel-f/compile (fix-up-objref objref))]
(cond
(> (count fields) 1)
(let [known-path (map (fn [c]
(if (= "*" c)
"[]"
c))
(if (string/ends-with? objref ".")
fields
(butlast fields)))
maybe-path (if (string/ends-with? objref ".")
""
(last fields))]
(cond
(= maybe-path "*")
(->> (axel-f/run (reconstruct-path known-path)
context)
(filter map?)
(apply merge)
keys
(map #(build-suggestion :OBJREF %)))
:otherwise
(build-new-context context known-path maybe-path)))
(string/ends-with? objref ".")
(build-new-context context fields "")
:otherwise
(->> context
keys
(map axel-f/->string)
(fuzzy-match? (first fields))
(map #(build-suggestion :OBJREF %)))))))
(defn- build-suggestions-for-fncall [fncall]
(let [[_ f args] (axel-f/compile (fix-up-fncall fncall))]
(->> functions/functions-map
keys
(filter #(= % f))
(map #(build-suggestion :FNCALL % args)))))
(defn get-last-part [incomplete-formula]
(let [chx (string/split incomplete-formula #"")]
(loop [ch (last chx) chx (butlast chx) acc [] terms {:open-round 0
:close-round 0
:unbalanced-single-quote false
:unbalanced-double-quote false
:pre-terminate false
:terminate false}]
(cond
(and (= ch " ")
(not (:unbalanced-single-quote terms))
(not (:unbalanced-double-quote terms)))
(recur (last chx) (butlast chx) acc terms)
(and (not= ch "\"")
(not-empty ch)
(:unbalanced-double-quote terms))
(recur (last chx) (butlast chx) (cons ch acc) terms)
(and (not= ch "'")
(not-empty ch)
(:unbalanced-single-quote terms))
(recur (last chx) (butlast chx) (cons ch acc) terms)
:otherwise
(if (or (not ch) (:terminate terms))
(apply str acc)
(let [terms (cond-> terms
(= "\"" ch) (update :unbalanced-double-quote not)
(= "'" ch) (update :unbalanced-single-quote not)
(= ")" ch) (update :close-round inc)
(= "(" ch) (#(if (or (valid-reference? (apply str acc))
(> (:open-round %)
(:close-round %)))
(assoc % :terminate true)
(-> %
(update :open-round inc)
(assoc :pre-terminate false))))
(= "," ch) (#(if (valid-reference? (apply str acc))
(assoc % :terminate true)
(assoc % :pre-terminate false)))
(re-matches #"[A-Z]" ch) (#(if (>= (:open-round %) (:close-round %))
(assoc % :pre-terminate true)
%))
(not (re-matches #"[A-Z]" ch)) (#(if (:pre-terminate %)
(assoc % :terminate true)
%)))]
(if (:terminate terms)
(apply str acc)
(recur (last chx) (butlast chx) (cons ch acc) terms))))))))
(defn- build-suggestions-for-fn []
(->> functions/functions-map
(map (fn [[fn-name _]]
(build-suggestion :FN fn-name)))))
(defn autocomplete
([incomplete-formula] (autocomplete incomplete-formula {}))
([incomplete-formula context]
(let [last-part (get-last-part incomplete-formula)]
(or (cond
(= "" last-part) (build-suggestions-for-fn)
(valid-reference? last-part) (build-suggestions-for-objref last-part context)
(valid-fncall? last-part) (build-suggestions-for-fncall last-part))
[]))))