|
3 | 3 | [clojure.java.io :as io] |
4 | 4 | [clojure.string :as str] |
5 | 5 | [clojure.tools.namespace.parse :as parse] |
| 6 | + [clojure.tools.reader :as reader] |
6 | 7 | [clojure.tools.reader.reader-types :as readers] |
7 | 8 | [orchard.java.classpath :as cp] |
8 | 9 | [orchard.misc :as misc] |
9 | 10 | [refactor-nrepl.config :as config] |
10 | 11 | [refactor-nrepl.s-expressions :as sexp] |
11 | 12 | [refactor-nrepl.util :as util :refer [normalize-to-unix-path]]) |
12 | 13 | (:import |
| 14 | + (clojure.lang LineNumberingPushbackReader) |
13 | 15 | (java.io File FileNotFoundException FileReader PushbackReader StringReader))) |
14 | 16 |
|
15 | 17 | ;; Require our `fs` customizations before `fs` is loaded: |
|
182 | 184 | (defn cljc-file? |
183 | 185 | [path-or-file] |
184 | 186 | (let [path (.getPath (io/file path-or-file))] |
185 | | - (and (cljc-extension? path) |
186 | | - (read-ns-form path)))) |
| 187 | + (boolean (and (cljc-extension? path) |
| 188 | + (read-ns-form path))))) |
187 | 189 |
|
188 | 190 | (defn cljs-extension? [^String path] |
189 | 191 | (.endsWith path ".cljs")) |
190 | 192 |
|
191 | 193 | (defn cljs-file? |
192 | 194 | [path-or-file] |
193 | 195 | (let [path (.getPath (io/file path-or-file))] |
194 | | - (and (cljs-extension? path) |
195 | | - (read-ns-form path)))) |
| 196 | + (boolean (and (cljs-extension? path) |
| 197 | + (read-ns-form path))))) |
196 | 198 |
|
197 | 199 | (defn clj-extension? [^String path] |
198 | 200 | (.endsWith path ".clj")) |
199 | 201 |
|
200 | 202 | (defn clj-file? |
201 | 203 | [path-or-file] |
202 | 204 | (let [path (.getPath (io/file path-or-file))] |
203 | | - (and (not (util/data-file? path-or-file)) |
204 | | - (clj-extension? path) |
205 | | - (read-ns-form path)))) |
| 205 | + (boolean (and (not (util/data-file? path-or-file)) |
| 206 | + (clj-extension? path) |
| 207 | + (read-ns-form path))))) |
| 208 | + |
| 209 | +(defn clj-or-cljc-file? |
| 210 | + [path-or-file] |
| 211 | + (or (clj-file? path-or-file) |
| 212 | + (cljc-file? path-or-file))) |
206 | 213 |
|
207 | 214 | (defn source-file? |
208 | 215 | "True for clj, cljs or cljc files. |
|
409 | 416 | ([no-error path] (when-not (cljs-file? path) |
410 | 417 | (some-> path read-ns-form-with-meta parse/name-from-ns-decl (safe-find-ns no-error))))) |
411 | 418 |
|
| 419 | +(defn file-forms |
| 420 | + "For a given `file`, get all the forms from it. |
| 421 | +
|
| 422 | + If `file` is .cljc, `features` (a set) will be used. |
| 423 | +
|
| 424 | + Please prefer this helper over `#'slurp`, |
| 425 | + so that reader conditionals are properly handled." |
| 426 | + [file features] |
| 427 | + (let [reader (LineNumberingPushbackReader. (StringReader. (slurp file))) |
| 428 | + reader-opts {:read-cond :allow |
| 429 | + :eof ::eof |
| 430 | + :features (case (file->dialect file) |
| 431 | + :clj #{:clj} |
| 432 | + :cljc features |
| 433 | + :cljs #{:cljs})}] |
| 434 | + (loop [forms [] |
| 435 | + form (reader/read reader-opts reader)] |
| 436 | + (if (not= form ::eof) |
| 437 | + (recur (conj forms form) |
| 438 | + (reader/read reader-opts reader)) |
| 439 | + (str/join " " forms))))) |
| 440 | + |
412 | 441 | (defn file-content-sans-ns |
413 | 442 | "Read the content of file after the ns. |
414 | 443 |
|
|
420 | 449 | ([file-content dialect] |
421 | 450 | ;; NOTE: It's tempting to trim this result but |
422 | 451 | ;; find-macros relies on this not being trimmed |
423 | | - (let [rdr-opts {:read-cond :allow :features #{dialect}} |
424 | | - rdr (PushbackReader. (StringReader. file-content))] |
425 | | - (read rdr-opts rdr) |
426 | | - (slurp rdr)))) |
| 452 | + (let [reader-opts {:read-cond :allow :features #{dialect}} |
| 453 | + reader (PushbackReader. (StringReader. file-content))] |
| 454 | + (read reader-opts reader) |
| 455 | + (slurp reader)))) |
427 | 456 |
|
428 | 457 | (defn ns-form-from-string |
429 | 458 | ([file-content] |
|
433 | 462 | (catch Exception _e |
434 | 463 | (throw (IllegalArgumentException. "Malformed ns form!"))))) |
435 | 464 | ([dialect file-content] |
436 | | - (let [rdr-opts {:read-cond :allow :features #{dialect}}] |
| 465 | + (let [reader-opts {:read-cond :allow :features #{dialect}}] |
437 | 466 | (try |
438 | | - (with-meta (parse/read-ns-decl (PushbackReader. (StringReader. file-content)) rdr-opts) |
| 467 | + (with-meta (parse/read-ns-decl (PushbackReader. (StringReader. file-content)) reader-opts) |
439 | 468 | (extract-ns-meta file-content)) |
440 | 469 | (catch Exception _e |
441 | 470 | (throw (IllegalArgumentException. "Malformed ns form!"))))))) |
|
0 commit comments