From 81caf3fc7e2d8a8558175a484c41ffeb1de28df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Nuno=20Monteiro?= Date: Sun, 23 Apr 2017 22:53:40 -0700 Subject: [PATCH] add `find-doc` REPL function fixes #141 --- CHANGELOG.md | 4 ++++ src/cljs/snapshot/lumo/repl.cljs | 21 +++++++++++++++++++ .../__tests__/__snapshots__/cli-test.js.snap | 2 ++ src/js/cli.js | 1 + src/js/cljs.js | 2 +- 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57321bae..6a7c3bfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## [master](https://github.com/anmonteiro/lumo/compare/1.4.1...HEAD) (unreleased) +### New features + +- Add `find-doc` REPL function ([#141](https://github.com/anmonteiro/lumo/issues/141)). + ## [1.4.1](https://github.com/anmonteiro/lumo/compare/1.4.0...1.4.1) (2017-04-20) ### Changes diff --git a/src/cljs/snapshot/lumo/repl.cljs b/src/cljs/snapshot/lumo/repl.cljs index 279b5d95..0bf7941e 100644 --- a/src/cljs/snapshot/lumo/repl.cljs +++ b/src/cljs/snapshot/lumo/repl.cljs @@ -685,6 +685,27 @@ :arglists (seq sigs)}])) (into {}))))))))) +(defn- namespace-doc [nspace] + (select-keys (get-in @st [::ana/namespaces nspace]) [:name :doc])) + +(defn find-doc + "Prints documentation for any var whose documentation or name + contains a match for re-string-or-pattern" + [re-string-or-pattern] + (let [re (re-pattern re-string-or-pattern) + ms (concat (mapcat #(sort-by :name + (map (fn [[k v]] + (assoc (:meta v) :name (symbol % k))) + (get-in @st [::ana/namespaces % :defs]))) + (all-ns)) + (map namespace-doc (all-ns)) + (map special-doc (keys special-doc-map)))] + (doseq [m ms + :when (and (:doc m) + (or (re-find re (:doc m)) + (re-find re (str (:name m)))))] + (doc* (:name m))))) + ;; -------------------- ;; Code evaluation diff --git a/src/js/__tests__/__snapshots__/cli-test.js.snap b/src/js/__tests__/__snapshots__/cli-test.js.snap index e9ef4845..1c71c324 100644 --- a/src/js/__tests__/__snapshots__/cli-test.js.snap +++ b/src/js/__tests__/__snapshots__/cli-test.js.snap @@ -16,6 +16,7 @@ Array [ ClojureScript clojurescript-version Node.js X.X.X Docs: (doc function-name-here) + (find-doc \\"part-of-name-here\\") Exit: Control+D or :cljs/quit or exit ", @@ -34,6 +35,7 @@ Array [ ClojureScript clojurescript-version Node.js X.X.X Docs: (doc function-name-here) + (find-doc \\"part-of-name-here\\") Exit: Control+D or :cljs/quit or exit ", diff --git a/src/js/cli.js b/src/js/cli.js index 9dc7bddb..a2eb7318 100644 --- a/src/js/cli.js +++ b/src/js/cli.js @@ -40,6 +40,7 @@ export function createBanner(): string { ClojureScript ${lumo.load('clojurescript-version')} Node.js ${process.version} Docs: (doc function-name-here) + (find-doc "part-of-name-here") Exit: Control+D or :cljs/quit or exit `; diff --git a/src/js/cljs.js b/src/js/cljs.js index c3273c14..30e57717 100644 --- a/src/js/cljs.js +++ b/src/js/cljs.js @@ -362,7 +362,7 @@ export default function startClojureScriptEngine(opts: CLIOptsType): void { setPrintFns(new DiscardingSender()); execute( - "(require '[lumo.repl :refer-macros [doc dir]])", + "(require '[lumo.repl :refer [find-doc] :refer-macros [dir doc]])", 'text', true, false,