Skip to content

Commit

Permalink
Streamline special symbol's meta handling
Browse files Browse the repository at this point in the history
  - Rename resolve-special -> special-sym-meta
  - Don't check for special-symbol? explicitly but rely on special-sym-meta instead
  - Remove unnecessary duplicated computation in info-clj
  • Loading branch information
vspinu committed Jun 18, 2017
1 parent ea70c41 commit 650d8af
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
12 changes: 6 additions & 6 deletions src/cider/nrepl/middleware/info.clj
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@

(defn info-clj
[ns sym]
(cond
(or
;; it's a special (special-symbol? or :special-form)
(m/resolve-special sym) (m/resolve-special sym)
(m/special-sym-meta sym)
;; it's a var
(m/var-meta (m/resolve-var ns sym)) (m/var-meta (m/resolve-var ns sym))
(m/var-meta (m/resolve-var ns sym))
;; sym is an alias for another ns
(get (m/resolve-aliases ns) sym) (m/ns-meta (get (m/resolve-aliases ns) sym))
(m/ns-meta (get (m/resolve-aliases ns) sym))
;; it's simply a full ns
(find-ns sym) (m/ns-meta (find-ns sym))
(m/ns-meta (find-ns sym))
;; it's a Java class/member symbol...or nil
:else (java/resolve-symbol ns sym)))
(java/resolve-symbol ns sym)))

(defn info-cljs
[env symbol ns]
Expand Down
46 changes: 25 additions & 21 deletions src/cider/nrepl/middleware/util/meta.clj
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
;; internals, but returns the metadata rather than just printing. Oddly, the
;; only place in the Clojure API that special form metadata is available *as
;; data* is a private function. Lame. Just call through the var.
(defn resolve-special
(defn special-sym-meta
"Return info for the symbol if it's a special form, or nil otherwise. Adds
`:url` unless that value is explicitly set to `nil` -- the same behavior
used by `clojure.repl/doc`."
Expand All @@ -108,11 +108,13 @@
:protocol :line :column :static :added :deprecated :resource])

(defn var-meta
"Return a map of metadata for v.
"Return a map of metadata for var or special form v.
If whitelist is missing use var-meta-whitelist."
([v] (var-meta v var-meta-whitelist))
([v whitelist]
(let [meta-map (-> v meta maybe-protocol
(let [meta-map (-> (or (special-sym-meta v)
(meta v))
maybe-protocol
(select-keys (or whitelist var-meta-whitelist))
map-seq maybe-add-file)]
(maybe-add-spec v meta-map))))
Expand All @@ -125,19 +127,20 @@
'[& catch finally]))

(defn var-name
"Return a special form's name or var's namespace-qualified name as a string."
"Return special form or var's namespace-qualified name as string."
[v]
(if (special-symbol? v)
(str (:name (resolve-special v)))
(str/join "/" ((juxt (comp ns-name :ns) :name) (meta v)))))
(let [mta (or (special-sym-meta v)
(meta v))]
(if-let [ns (:ns mta)]
(str (ns-name ns) "/" (:name mta))
(name (:name mta)))))

(defn var-doc
"Return a special form or var's docstring, optionally limiting the number of
sentences returned."
"Return special form or var's docstring, optionally limiting the number of
sentences returned to n."
([v]
(or (if (special-symbol? v)
(:doc (resolve-special v))
(:doc (meta v)))
(or (:doc (or (special-sym-meta v)
(meta v)))
"(not documented)"))
([n v]
(->> (-> (var-doc v)
Expand Down Expand Up @@ -200,15 +203,16 @@

(defn ns-meta
[ns]
(merge
(meta ns)
{:ns ns
:file (-> (ns-publics ns)
first
second
var-meta
:file)
:line 1}))
(when ns
(merge
(meta ns)
{:ns ns
:file (-> (ns-publics ns)
first
second
var-meta
:file)
:line 1})))

;;; ## Manipulation

Expand Down
12 changes: 6 additions & 6 deletions test/clj/cider/nrepl/middleware/info_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,21 @@
(finally
(System/clearProperty "fake.class.path")))))))

(deftest resolve-special-test
(deftest special-sym-meta-test
(testing "Resolves all special forms"
(let [specials (keys clojure.lang.Compiler/specials)]
(is (every? (fn [[sym {:keys [name special-form]}]]
(and (= sym name)
(true? special-form)))
(map #(vector % (info/resolve-special %)) specials)))))
(map #(vector % (m/special-sym-meta %)) specials)))))

(testing "Names are correct for symbols #{&, catch, finally}"
(is (= '& (:name (info/resolve-special '&))))
(is (= 'catch (:name (info/resolve-special 'catch))))
(is (= 'finally (:name (info/resolve-special 'finally)))))
(is (= '& (:name (m/special-sym-meta '&))))
(is (= 'catch (:name (m/special-sym-meta 'catch))))
(is (= 'finally (:name (m/special-sym-meta 'finally)))))

(testing "Returns nil for unknown symbol"
(is (nil? (info/resolve-special 'unknown)))))
(is (nil? (m/special-sym-meta 'unknown)))))

(deftype T [])

Expand Down

0 comments on commit 650d8af

Please sign in to comment.