Skip to content

Commit d35a9e7

Browse files
committed
Refactor find-symbol
1 parent 152b159 commit d35a9e7

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

src/refactor_nrepl/client.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@
179179
:ns-string ns-string
180180
:debug-fns debug-fns})
181181
invocations (-> result first :value)]
182-
(when-not (empty? invocations)
182+
(when (seq invocations)
183183
(->> ns-string
184184
str/split-lines
185185
(remove-invocations

src/refactor_nrepl/find/find_symbol.clj

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
(comp :end-line :env)
5858
(comp :column :env)
5959
(comp :end-column :env)
60-
pred))))
60+
pred))
61+
(map #(zipmap [:line-beg :line-end :col-beg :col-end] %))))
6162
([name asts pred]
6263
(find-nodes (map #(postwalk % (partial dissoc-macro-nodes name)) asts) pred)))
6364

@@ -111,17 +112,18 @@
111112
(let [file-content (slurp file)
112113
locs (try (->> (ns-ast file-content)
113114
(find-symbol-in-ast fully-qualified-name)
114-
(filter first))
115-
(catch Throwable th
115+
(filter :line-beg))
116+
(catch Exception e
116117
(when-not ignore-errors
117-
(throw th))))
118+
(throw e))))
118119
gather (fn [info]
119-
(conj info
120-
(.getCanonicalPath file)
121-
(match file-content
122-
(first info)
123-
(second info))))]
124-
(when (seq locs) (map gather locs))))
120+
(merge info
121+
{:file (.getCanonicalPath file)
122+
:name fully-qualified-name
123+
:match (match file-content
124+
(:line-beg info)
125+
(:line-end info))}))]
126+
(map gather locs)))
125127

126128
(defn- find-global-symbol [file ns var-name clj-dir ignore-errors]
127129
(let [dir (or clj-dir ".")
@@ -158,31 +160,32 @@
158160
(filter (partial util/node-at-loc? line column))
159161
first
160162
:name)]
161-
(map #(conj (vec (take 4 %))
162-
var-name
163-
(.getCanonicalPath (java.io.File. file))
164-
(match file-content
165-
(first %)
166-
(second %)))
163+
(map #(merge %
164+
{:name var-name
165+
:file (.getCanonicalPath (java.io.File. file))
166+
:match (match file-content
167+
(:line-beg %)
168+
(:line-end %))})
167169
(find-nodes var-name
168170
[top-level-form-ast]
169171
#(and (#{:local :binding} (:op %))
170172
(= local-var-name (-> % :name))
171173
(:local %))))))))
172174

173-
(defn to-find-symbol-result
175+
(defn- to-find-symbol-result
174176
[{:keys [line-beg line-end col-beg col-end name file match]}]
175177
[line-beg line-end col-beg col-end name file match])
176178

177179
(defn find-symbol [{:keys [file ns name dir line column ignore-errors]}]
178180
(util/throw-unless-clj-file file)
179-
(or
180-
;; find-macro is first because find-global-symbol returns garb for macros
181-
(some->> name find-macro (map to-find-symbol-result))
182-
(not-empty (find-local-symbol file name line column))
183-
(find-global-symbol file ns name dir (and ignore-errors
184-
(or (not (coll? ignore-errors))
185-
(not-empty ignore-errors))))))
181+
(map to-find-symbol-result
182+
(or
183+
;; find-macro is first because find-global-symbol returns garb for macros
184+
(some->> name find-macro)
185+
(and (seq file) (not-empty (find-local-symbol file name line column)))
186+
(find-global-symbol file ns name dir (and ignore-errors
187+
(or (not (coll? ignore-errors))
188+
(not-empty ignore-errors)))))))
186189

187190
(defn create-result-alist
188191
[line-beg line-end col-beg col-end name file match]
@@ -195,6 +198,7 @@
195198
:match match))
196199

197200
(defn find-debug-fns [{:keys [ns-string debug-fns]}]
198-
(let [res (-> ns-string ns-ast (find-invokes debug-fns))]
201+
(let [res (-> ns-string ns-ast (find-invokes debug-fns))
202+
res (map to-find-symbol-result res)]
199203
(when (seq res)
200204
res)))

0 commit comments

Comments
 (0)