Skip to content

Commit 4f46658

Browse files
mkphilippamarkovics
authored andcommitted
Support imported vars (e.g. by potemkin) in location analysis (#508)
By considering `:file` on var meta in location analysis. Previously we would not find a location for vars where the namespace did not match the source file. As we're not caching negative findings this can speed up analysis for deps with a large number of imported vars significantly.
1 parent 5e86230 commit 4f46658

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Changes can be:
2424

2525
* 🍒 Add support for cherry as an alternative to sci to evaluate `:render-fn`s. You can change it per form (using form metadata or viewer opts) or doc-wide (using ns metadata) with `{:nextjournal.clerk/render-evaluator :cherry}`.
2626

27+
* 🏳️‍🌈 Syntax highlighting for code listings in all [languages supported by codemirror](https://github.com/codemirror/language-data) ([#500](https://github.com/nextjournal/clerk/issues/500)).
28+
2729
* ⭐️ Adds support for customization of viewer options
2830

2931
Support both globally (via ns metadata or a settings marker) or locally (via form metadata or the viewer options map).
@@ -42,7 +44,9 @@ Changes can be:
4244

4345
* 💫 Support non-evaluated clojure code listings in markdown documents by specifying `{:nextjournal.clerk/code-listing true}` after the language ([#482](https://github.com/nextjournal/clerk/issues/482)).
4446

45-
* 🏳️‍🌈 Syntax highlighting for code listings in all [languages supported by codemirror](https://github.com/codemirror/language-data) ([#500](https://github.com/nextjournal/clerk/issues/500)).
47+
* 💫 Support imported vars (e.g. by potemkin) in location analysis
48+
49+
By considering `:file` on var meta in location analysis. Previously we would not find a location for vars where the namespace did not match the source file. As we're not caching negative findings this can speed up analysis for deps with a large number of imported vars significantly.
4650

4751
* 🐜 Turn off analyzer pass for validation of `:type` tags, fixes [#488](https://github.com/nextjournal/clerk/issues/488) @craig-latacora
4852

deps.edn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
org.clojure/data.csv {:mvn/version "1.0.0"}
6969
hickory/hickory {:mvn/version "0.7.1"}
7070
sicmutils/sicmutils {:mvn/version "0.20.0"}
71+
io.github.mentat-collective/emmy {:git/sha "b98fef51d80d3edcff3100562b929f9980ff34d7"
72+
:exclusions [org.babashka/sci]}
7173
io.github.nextjournal/clerk-slideshow {:git/sha "0e6e890fd4f862fa3535290cb7e29591c6e0ec85"}}}
7274

7375
:build {:deps {io.github.nextjournal/clerk {:local/root "."}

notebooks/emmy.clj

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
(ns emmy-repro
2+
(:require [emmy.env :as e]
3+
[emmy.mechanics.lagrange]))
4+
5+
;; ## BUG 1:
6+
7+
;; This notebook takes close to 2 seconds to evaluate:
8+
9+
;; Clerk evaluated '/Users/sritchie/code/clj/clerk-demo/notebooks/emmy_repro.clj' in 1853.674042ms.
10+
11+
;; For the final Langrangian in generalized coordinates (the angles of each
12+
;; segment) by composing `L-rect` with a properly transformed `angles->rect`
13+
;; coordinate transform!
14+
15+
;; ## BUG 2:
16+
;;
17+
;; The following form:
18+
19+
#_
20+
(let [L (emmy.mechanics.lagrange/L-pendulum 'g 'm 'l)]
21+
(((e/Lagrange-equations L)
22+
(e/literal-function 'theta_1))
23+
't))
24+
25+
;; Evaluates to this:
26+
(e/literal-number
27+
'(- (* 1/2 m 2 l (((expt D 2) theta_1) t) l) (* g m l (- (sin (theta_1 t))))))
28+
29+
30+
;; But if I include it in a notebook, I get this:
31+
32+
;; Execution error (NullPointerException) at clojure.tools.analyzer.jvm.utils/members* (utils.clj:272).
33+
;; Cannot invoke "java.lang.Class.getName()" because the return value of "clojure.lang.IFn.invoke(Object)" is null

src/nextjournal/clerk/analyzer.clj

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,14 @@
406406
[".clj" ".cljc"]))
407407
(cp/classpath-directories)))
408408

409+
(defn var->file [var]
410+
(when-let [file-from-var (-> var meta :file)]
411+
(some (fn [classpath-dir]
412+
(let [path (str classpath-dir fs/file-separator file-from-var)]
413+
(when (fs/exists? path)
414+
path)))
415+
(cp/classpath-directories))))
416+
409417
(defn normalize-filename [f]
410418
(if (fs/windows?)
411419
(-> f fs/normalize fs/unixify)
@@ -441,14 +449,27 @@
441449
#_(symbol->jar 'io.methvin.watcher.PathUtils/cast)
442450
#_(symbol->jar 'java.net.http.HttpClient/newHttpClient)
443451

452+
(defn var->location [var]
453+
(when-let [file (:file (meta var))]
454+
(if (fs/absolute? file)
455+
(when (fs/exists? file)
456+
(fs/relativize (fs/cwd) (fs/file file)))
457+
(when-let [resource (io/resource file)]
458+
(let [protocol (.getProtocol resource)]
459+
(or (and (= "jar" protocol)
460+
(second (re-find #"^file:(.*)!" (.getFile resource))))
461+
(and (= "file" protocol)
462+
(.getFile resource))))))))
444463

445464
(defn find-location [sym]
446465
(if (deref? sym)
447466
(find-location (second sym))
448-
(if-let [ns (and (qualified-symbol? sym) (-> sym namespace symbol find-ns))]
449-
(or (ns->file ns)
450-
(ns->jar ns))
451-
(symbol->jar sym))))
467+
(or (some-> sym resolve var->location)
468+
(if-let [ns (and (qualified-symbol? sym) (-> sym namespace symbol find-ns))]
469+
(or (ns->file ns)
470+
(ns->jar ns))
471+
(symbol->jar sym)))))
472+
452473

453474
#_(find-location `inc)
454475
#_(find-location '@nextjournal.clerk.webserver/!doc)

0 commit comments

Comments
 (0)