diff --git a/CHANGELOG.md b/CHANGELOG.md index d41b7644..e467356f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ SCI is used in [babashka](https://github.com/babashka/babashka), ## Unreleased +- `clojure.repl/doc` on `&`, `catch` and `finally` prints the docs of `fn` and `try` - Speed up `binding` (about 4x on the JVM, 2x in native bb) and `try` without a catch (about 4x) - Clojure 1.13 map destructuring as of clojure/clojure 98d735fa: `:excess` and `:missing` directives, `:defaults` removed. Adds the `selector` macro to `clojure.core`. - Minimum Clojure version is `1.11.1` diff --git a/src/sci/impl/namespaces.cljc b/src/sci/impl/namespaces.cljc index e541c7d5..669f94d5 100644 --- a/src/sci/impl/namespaces.cljc +++ b/src/sci/impl/namespaces.cljc @@ -2158,16 +2158,17 @@ (defn doc [_ _ sym] ;; emit ~'resolve etc unqualified, the cljd host would qualify them into this ns - `(let [special-doc# (try (~'resolve '~'clojure.repl/special-doc) (catch #?(:cljs :default :default ~'Exception) ~'_ nil)) - special# (when special-doc# (special-doc# '~sym))] - (if special# - (~'clojure.repl/print-doc special#) - (if-let [var# (~'resolve '~sym)] - (when (~'var? var#) - (~'clojure.repl/print-doc (meta var#))) - (if-let [ns# (~'find-ns '~sym)] - (~'clojure.repl/print-doc (assoc (meta ns#) - :name (~'ns-name ns#)))))))) + (let [sym (get '{& fn catch try finally try} sym sym)] + `(let [special-doc# (try (~'resolve '~'clojure.repl/special-doc) (catch #?(:cljs :default :default ~'Exception) ~'_ nil)) + special# (when special-doc# (special-doc# '~sym))] + (if special# + (~'clojure.repl/print-doc special#) + (if-let [var# (~'resolve '~sym)] + (when (~'var? var#) + (~'clojure.repl/print-doc (meta var#))) + (if-let [ns# (~'find-ns '~sym)] + (~'clojure.repl/print-doc (assoc (meta ns#) + :name (~'ns-name ns#))))))))) (defn find-doc "Prints documentation for any var whose documentation or name diff --git a/test/sci/repl_test.cljc b/test/sci/repl_test.cljc index 4f4040d4..51b38f44 100644 --- a/test/sci/repl_test.cljc +++ b/test/sci/repl_test.cljc @@ -38,7 +38,11 @@ clojure.core/inc (is (= (str/trim "-------------------------\nfoo\n foodoc\n") (str/trim (sci/with-out-str (eval* "(ns foo \"foodoc\") (clojure.repl/doc foo)"))))) - (is (empty? (sci/with-out-str (tu/eval* "(clojure.repl/doc x)" {:bindings {'x 1}})))))) + (is (empty? (sci/with-out-str (tu/eval* "(clojure.repl/doc x)" {:bindings {'x 1}})))) + (is (str/includes? (sci/with-out-str (eval* "(clojure.repl/doc &)")) "clojure.core/fn")) + (let [opts {:namespaces {'clojure.repl {'special-doc (fn [sym] (when (= 'try sym) {:name 'try :doc "trydoc" :special-form true}))}}}] + (is (str/includes? (sci/with-out-str (tu/eval* "(clojure.repl/doc catch)" opts)) "trydoc")) + (is (str/includes? (sci/with-out-str (tu/eval* "(clojure.repl/doc finally)" opts)) "trydoc"))))) (deftest repl-find-doc-test (when-not tu/native?