-
-
Notifications
You must be signed in to change notification settings - Fork 149
/
file_management.clj
394 lines (363 loc) · 19.2 KB
/
file_management.clj
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
(ns clojure-lsp.feature.file-management
(:require
[babashka.fs :as fs]
[clojure-lsp.clj-depend :as lsp.depend]
[clojure-lsp.config :as config]
[clojure-lsp.dep-graph :as dep-graph]
[clojure-lsp.feature.diagnostics :as f.diagnostic]
[clojure-lsp.feature.rename :as f.rename]
[clojure-lsp.kondo :as lsp.kondo]
[clojure-lsp.logger :as logger]
[clojure-lsp.producer :as producer]
[clojure-lsp.queries :as q]
[clojure-lsp.settings :as settings]
[clojure-lsp.shared :as shared]
[clojure.core.async :as async]
[clojure.java.io :as io]
[clojure.set :as set]
[clojure.string :as string]
[medley.core :as medley]))
(set! *warn-on-reflection* true)
(defn create-ns-changes [uri text db]
(when-let [new-ns (and (string/blank? text)
(contains? #{:clj :cljs :cljc} (shared/uri->file-type uri))
(not (get (:create-ns-blank-files-denylist db) uri))
(shared/uri->namespace uri db))]
(when (settings/get db [:auto-add-ns-to-new-files?] true)
(let [new-text (format "(ns %s)" new-ns)
changes [{:text-document {:version (get-in db [:documents uri :v] 0) :uri uri}
:edits [{:range (shared/->range {:row 1 :end-row 999999 :col 1 :end-col 999999})
:new-text new-text}]}]]
(shared/client-changes changes db)))))
(defn load-document! [uri text db*]
(swap! db* update-in [:documents uri] assoc :v 0 :text text :saved-on-disk false))
(defn did-open [uri text {:keys [db* edits-chan] :as components} allow-create-ns]
(load-document! uri text db*)
(let [kondo-result* (future (lsp.kondo/run-kondo-on-text! text uri db*))
depend-result* (future (lsp.depend/analyze-uri! uri @db*))
kondo-result @kondo-result*
depend-result @depend-result*]
(swap! db* (fn [state-db]
(-> state-db
(lsp.kondo/db-with-results kondo-result)
(lsp.depend/db-with-results depend-result))))
(f.diagnostic/publish-diagnostics! uri components))
(when allow-create-ns
(when-let [create-ns-edits (create-ns-changes uri text @db*)]
(async/>!! edits-chan create-ns-edits))))
(defn ^:private set-xor [a b]
(into (set/difference a b)
(set/difference b a)))
(defn ^:private find-changed-elems
"Detect elements that went from 0 to 1 occurrence, or from 1 to 0
occurrences."
[old-local-buckets new-local-buckets bucket signature-fn]
(let [signature-with-elem (fn [elem]
(with-meta (signature-fn elem) {:elem elem}))
old-signs (into #{} (map signature-with-elem) (bucket old-local-buckets))
new-signs (into #{} (map signature-with-elem) (bucket new-local-buckets))]
(map (comp :elem meta)
(set-xor old-signs new-signs))))
(def ^:private definition-signature
"Attributes of a var definition that, if they change, merit re-linting files
that use the var. This isn't the full list of attributets copied by kondo from
defs to usages, but it includes the attributes we care about."
(juxt :ns :name :private :fixed-arities :defined-by :defined-by->lint-as))
(defn reference-uris [uri db-before db-after]
(let [old-local-buckets (get-in db-before [:analysis uri])
new-local-buckets (get-in db-after [:analysis uri])
changed-var-definitions (find-changed-elems old-local-buckets new-local-buckets :var-definitions definition-signature)
changed-var-usages (find-changed-elems old-local-buckets new-local-buckets :var-usages q/var-usage-signature)
changed-kw-usages (find-changed-elems old-local-buckets new-local-buckets :keyword-usages q/kw-signature)
project-db (-> db-after
q/db-with-internal-analysis
;; don't notify self
(update :analysis dissoc uri))
var-dependent-uri (when (seq changed-var-definitions)
(let [def-signs (into #{}
(mapcat q/var-definition-signatures)
changed-var-definitions)
usage-of-changed-def? (fn [var-usage]
(contains? def-signs (q/var-usage-signature var-usage)))]
(into #{}
(keep (fn [[uri {:keys [var-usages]}]]
(when (some usage-of-changed-def? var-usages)
uri)))
(q/uri-dependents-analysis project-db uri))))
var-dependency-uris (when (seq changed-var-usages)
;; If definition is in both a clj and cljs file, and this is a clj
;; usage, we are careful to notify only the clj file. But, it wouldn't
;; really hurt to notify both files. So, if it helps readability,
;; maintenance, or symmetry with `var-dependent-uris`, we could look
;; at just signature, not signature and lang.
(let [usage-signs->langs (->> changed-var-usages
(reduce (fn [result usage]
(assoc result (q/var-usage-signature usage) (q/elem-langs usage)))
{}))
def-of-changed-usage? (fn [var-def]
(when-let [usage-langs (some usage-signs->langs
(q/var-definition-signatures var-def))]
(some usage-langs (q/elem-langs var-def))))]
(into #{}
(keep (fn [[uri {:keys [var-definitions]}]]
(when (some def-of-changed-usage? var-definitions)
uri)))
(q/uri-dependencies-analysis project-db uri))))
kw-dependency-uris (when (seq changed-kw-usages)
(let [usage-signs (into #{}
(map q/kw-signature)
changed-kw-usages)
def-of-changed-usage? (fn [kw-def]
(contains? usage-signs (q/kw-signature kw-def)))]
(into #{}
(keep (fn [[uri {:keys [keyword-definitions]}]]
(when (some def-of-changed-usage? keyword-definitions)
uri)))
(:analysis project-db))))]
;; TODO: see note on `notify-references` We may want to handle these
;; sets of files differently.
(set/union (or var-dependent-uri #{})
(or var-dependency-uris #{})
(or kw-dependency-uris #{}))))
(defn analyze-reference-uris! [uris db*]
(let [result (lsp.kondo/run-kondo-on-reference-uris! uris db*)]
(swap! db* lsp.kondo/db-with-results result)))
(defn ^:private notify-references [uri db-before db-after {:keys [db* producer] :as components}]
(async/thread
(shared/logging-task
:notify-references
(let [uris (shared/logging-task
:reference-files/find
(reference-uris uri db-before db-after))]
(when (seq uris)
(logger/debug "Analyzing references for files:" uris)
(shared/logging-task
:reference-files/analyze
;; TODO: We process the dependent and dependency files together, but
;; it may be possible to be more efficient by processing them
;; separately.
;;
;; The dependents may have been affected by changes to var
;; definitions. Since some var definition data is copied to var
;; usage data, this will change their analysis slightly. They may
;; also gain or lose clj-kondo lint like unresolved-var or
;; invalid-arity.
;;
;; The dependencies may have been affected by changes to var usages.
;; Their analysis won't have changed, but they may gain or lose
;; custom unused-public-var lint.
;;
;; So, we could send the dependents to kondo, bypassing custom-lint.
;; And we could send the dependencies to custom-lint, bypassing
;; kondo. See
;; https://github.com/clojure-lsp/clojure-lsp/issues/1027 and
;; https://github.com/clojure-lsp/clojure-lsp/issues/1028.
(analyze-reference-uris! uris db*))
(f.diagnostic/publish-all-diagnostics! uris components)
(producer/refresh-code-lens producer))))))
(defn ^:private offsets [lines line character end-line end-character]
(loop [lines (seq lines)
offset 0
idx 0]
(if (or (not lines)
(= line idx))
[(+ offset character line)
(loop [lines lines
offset offset
idx idx]
(if (or (not lines)
(= end-line idx))
(+ offset end-character end-line)
(recur (next lines)
(+ offset (count (first lines)))
(inc idx))))]
(recur (next lines)
(+ offset (count (first lines)))
(inc idx)))))
(defn replace-text
"Returns ORIGINAL text but replacing its substring from LINE number at
CHARACTER position to END-LINE number at END-CHARACTER position with
REPLACEMENT text.
All line endings of the returned string are replaced with that of
the ORIGINAL's line ending type.
The ORIGINAL's line ending type is set to be its first line ending
type (either `\r\n` or `\n`), or, if no newline is found, the
system's line separator."
[original replacement line character end-line end-character]
(let [original-nl (or (re-find #"\r\n|\n" original)
shared/line-separator)
replacement-nl (re-find #"\r\n|\n" replacement)
;; normalize `\r\n`'s to `\n` so that `\r`'s do not creap in
;; as normal chars
original (cond-> original
(= original-nl "\r\n") (string/replace #"\r\n" "\n"))
replacement (cond-> replacement
(= replacement-nl "\r\n") (string/replace #"\r\n" "\n"))
lines (string/split-lines original) ;; don't use OS specific line delimiter!
[start-offset end-offset] (offsets lines line character end-line end-character)
[prefix suffix] [(subs original 0 start-offset)
(subs original (min end-offset (count original)) (count original))]]
;; restore normalized string to ORIGINAL's line ending type.
(cond-> (string/join [prefix replacement suffix])
(= original-nl "\r\n") (string/replace #"\n" "\r\n"))))
(defn ^:private handle-change
"Handle a TextDocumentContentChangeEvent"
[old-text change]
(let [new-text (:text change)]
(if-let [r (:range change)]
(let [{{start-line :line
start-character :character} :start
{end-line :line
end-character :character} :end} r]
(replace-text old-text new-text start-line start-character end-line end-character))
;; If range and rangeLength are omitted the new text is considered to be
;; the full content of the document.
new-text)))
(defn ^:private bump-version
"Advance the analyzed-version, but only if it's newer, in case analysis
completes out of order."
[old-version new-version]
(if (or (not old-version)
(< old-version new-version))
new-version
old-version))
(defn analyze-changes [{:keys [uri text version]} {:keys [producer db*] :as components}]
(loop [state-db @db*]
(when (>= version (get-in state-db [:documents uri :v] -1))
(let [kondo-result* (future
(shared/logging-time
(str "changes analyzed by clj-kondo took %s")
(lsp.kondo/run-kondo-on-text! text uri db*)))
depend-result* (future
(shared/logging-time
(str "changes analyzed by clj-depend took %s")
(lsp.depend/analyze-uri! uri state-db)))
kondo-result @kondo-result*
depend-result @depend-result*
old-db @db*]
(if (compare-and-set! db* state-db
(-> state-db
(lsp.kondo/db-with-results kondo-result)
(lsp.depend/db-with-results depend-result)
(update-in [:documents uri :analyzed-version]
bump-version version)))
(let [db @db*]
(f.diagnostic/publish-diagnostics! uri components)
(when (settings/get db [:notify-references-on-file-change] true)
(notify-references uri old-db db components))
(producer/refresh-test-tree producer [uri]))
(recur @db*))))))
(defn did-change [uri changes version {:keys [db* current-changes-chan]}]
(let [old-text (get-in @db* [:documents uri :text])
final-text (reduce handle-change old-text changes)]
(swap! db* (fn [state-db] (-> state-db
(assoc-in [:documents uri :v] version)
(assoc-in [:documents uri :text] final-text))))
(async/>!! current-changes-chan {:uri uri
:text final-text
:version version})))
(defn analyze-watched-files! [uris {:keys [db* producer] :as components}]
(let [filenames (->> uris
;; we check if file still exists/should be linted
(filter #(get-in @db* [:documents %]))
(map shared/uri->filename))
result (shared/logging-time
"Watched files analyzed, took %s"
(lsp.kondo/run-kondo-on-paths! filenames db* {:external? false} nil))]
(swap! db* lsp.kondo/db-with-results result)
(f.diagnostic/publish-all-diagnostics! uris components)
(producer/refresh-test-tree producer uris)
(doseq [uri uris]
(when (get-in @db* [:documents uri :v])
(when-let [text (shared/slurp-uri uri)]
(swap! db* assoc-in [:documents uri :text] text))))))
(defn ^:private db-without-uri [state-db uri]
(-> state-db
(dep-graph/remove-doc uri)
(shared/dissoc-in [:documents uri])
(shared/dissoc-in [:analysis uri])
(shared/dissoc-in [:findings uri])))
(defn ^:private files-deleted [{:keys [db*], :as components} uris]
(swap! db* #(reduce db-without-uri % uris))
(f.diagnostic/publish-empty-diagnostics! uris components))
(defn ^:private dir-or-file-uri->analyzable-uris [uri db]
;; If the URI is for an entire directory that has been created/deleted, we
;; expand it.
(let [source-paths-ignore-regexs (get-in db [:settings :source-paths-ignore-regex] config/default-source-path-ignore-regexs)
root-path (shared/uri->path (:project-root-uri db))
files (-> uri shared/uri->filename io/file file-seq)]
(into []
(comp
;; avoid watching ignored source-paths
(remove (fn [file]
(let [relative-source-path (shared/relativize-filepath (str file) (str root-path))]
(some #(re-matches (re-pattern %) relative-source-path) source-paths-ignore-regexs))))
(map #(.getAbsolutePath ^java.io.File %))
(map #(shared/filename->uri % db))
(remove #(identical? :unknown (shared/uri->file-type %))))
files)))
(defn did-change-watched-files
[changes
{:keys [db* watched-files-chan] :as components}]
(let [db @db*
observe-changed? (settings/get db [:compute-external-file-changes] true)
{:keys [created changed deleted]}
(->> changes
(group-by :type)
(medley/map-vals
(fn [changes]
(mapcat #(dir-or-file-uri->analyzable-uris (:uri %) db) changes))))]
(doseq [created-or-changed (concat created (when observe-changed? changed))]
(async/>!! watched-files-chan created-or-changed))
(when (seq deleted)
(shared/logging-task
:delete-watched-files
(files-deleted components deleted)))))
(defn did-close [uri {:keys [db*] :as components}]
(let [filename (shared/uri->filename uri)
source-paths (settings/get @db* [:source-paths])
external-filename? (shared/external-filename? filename source-paths)]
(if external-filename?
(f.diagnostic/publish-empty-diagnostics! [uri] components)
(when (not (shared/file-exists? (io/file filename)))
(files-deleted components [uri])))))
(defn force-get-document-text
"Get document text from db, if document not found, tries to open the document"
[uri {:keys [db*] :as components}]
(or (get-in @db* [:documents uri :text])
(when-let [text (shared/slurp-uri uri)]
(did-open uri text components false)
(get-in @db* [:documents uri :text]))))
(defn ^:private lint-opened-files-when-any-config-changed!
[uri {:keys [db* producer] :as components}]
(let [db @db*
project-root-filename (shared/uri->filename (:project-root-uri db))
config-files #{(io/file project-root-filename ".clj-kondo" "config.edn")
(io/file project-root-filename ".lsp" "config.edn")}
config-file-saved? (some (comp #(= uri %)
#(shared/filename->uri % db)
str
fs/canonicalize)
config-files)]
(when config-file-saved?
(let [all-opened-uris (->> (:documents db)
(keep (fn [[uri document]] (when (:v document) uri)))
set)]
(analyze-reference-uris! all-opened-uris db*)
(f.diagnostic/publish-all-diagnostics! all-opened-uris components)
(producer/refresh-code-lens producer)))))
(defn did-save [uri {:keys [db*] :as components}]
(swap! db* #(assoc-in % [:documents uri :saved-on-disk] true))
(lint-opened-files-when-any-config-changed! uri components))
(defn will-rename-files [files db]
(->> files
(keep (fn [{:keys [old-uri new-uri]}]
(let [new-ns (shared/uri->namespace new-uri db)
old-ns-definition (q/find-namespace-definition-by-uri db old-uri)]
(when (and new-ns
old-ns-definition
(not= new-ns (name (:name old-ns-definition))))
(f.rename/rename-element new-ns db old-ns-definition :rename-file)))))
(reduce #(shared/deep-merge %1 %2) {:document-changes []})))
(defn did-rename-files [files components]
(files-deleted components (mapv :old-uri files)))