Skip to content

Commit

Permalink
added apropos (per Michel Salim, plus re support)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarthalloway committed Apr 12, 2010
1 parent bbe248f commit 4f9a78d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/main/clojure/clojure/contrib/repl_utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
; Utilities meant to be used interactively at the REPL

(ns
#^{:author "Chris Houser, Christophe Grand, Stephen Gilardi",
#^{:author "Chris Houser, Christophe Grand, Stephen Gilardi, Michel Salim",
:doc "Utilities meant to be used interactively at the REPL"}
clojure.contrib.repl-utils
(:import (java.io File LineNumberReader InputStreamReader PushbackReader)
(java.lang.reflect Modifier Method Constructor)
(clojure.lang RT Compiler Compiler$C))
(:require [clojure.contrib.string :as s])
(:use [clojure.contrib.seq :only (indexed)]
[clojure.contrib.javadoc.browse :only (browse-url)]
[clojure.contrib.string :as s :only ()]))
[clojure.contrib.javadoc.browse :only (browse-url)]))

;; ----------------------------------------------------------------------
;; Examine Java classes
Expand Down Expand Up @@ -125,6 +125,18 @@
[n]
`(println (or (get-source '~n) (str "Source not found"))))

(defn apropos
"Given a regular expression or stringable thing, return a seq of
all definitions in all currently-loaded namespaces that match the
str-or-pattern."
[str-or-pattern]
(let [matches? (if (instance? java.util.regex.Pattern str-or-pattern)
#(re-find str-or-pattern (str %))
#(s/substring? (str str-or-pattern) (str %)))]
(mapcat (fn [ns]
(filter matches? (keys (ns-publics ns))))
(all-ns))))

;; ----------------------------------------------------------------------
;; Handle Ctrl-C keystrokes

Expand Down
21 changes: 21 additions & 0 deletions src/test/clojure/clojure/contrib/test_repl_utils.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(ns clojure.contrib.test-repl-utils
(:use clojure.test
clojure.contrib.repl-utils
[clojure.contrib.seq :only (includes?)]))

(deftest test-apropos
(testing "with a regular expression"
(is (= '[defmacro] (apropos #"^defmacro$")))
(is (includes? (apropos #"def.acr.") 'defmacro))
(is (= [] (apropos #"nothing-has-this-name"))))


(testing "with a string"
(is (includes? (apropos "defmacro") 'defmacro))
(is (includes? (apropos "efmac") 'defmacro))
(is (= [] (apropos "nothing-has-this-name"))))

(testing "with a symbol"
(is (includes? (apropos 'defmacro) 'defmacro))
(is (includes? (apropos 'efmac) 'defmacro))
(is (= [] (apropos 'nothing-has-this-name)))))

0 comments on commit 4f9a78d

Please sign in to comment.