Skip to content
Newer
Older
100644 84 lines (75 sloc) 3.27 KB
68658ce @stuarthalloway everything javadoc needs from contrib
stuarthalloway authored
1 ; Copyright (c) Rich Hickey. All rights reserved.
2 ; The use and distribution terms for this software are covered by the
3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4 ; which can be found in the file epl-v10.html at the root of this distribution.
5 ; By using this software in any fashion, you are agreeing to be bound by
6 ; the terms of this license.
7 ; You must not remove this notice, or any other, from this software.
8 (ns
9 ^{:author "Christophe Grand, Stuart Sierra",
10 :doc "A repl helper to quickly open javadocs."}
11 clojure.java.javadoc
12 (:use [clojure.java.browse :only (browse-url)] )
13 (:import
14 (java.io File)))
15
16 (def *feeling-lucky-url* "http://www.google.com/search?btnI=I%27m%20Feeling%20Lucky&q=allinurl:")
17 (def *feeling-lucky* true)
18
19 (def
20 #^{:doc "Ref to a list of local paths for Javadoc-generated HTML files."}
21 *local-javadocs* (ref (list)))
22
23 (def *core-java-api*
24 (if (= "1.5" (System/getProperty "java.specification.version"))
25 "http://java.sun.com/j2se/1.5.0/docs/api/"
26 "http://java.sun.com/javase/6/docs/api/"))
27
28 (def
29 #^{:doc "Ref to a map from package name prefixes to URLs for remote
30 Javadocs."}
31 *remote-javadocs*
32 (ref (sorted-map
33 "java." *core-java-api*
34 "javax." *core-java-api*
35 "org.ietf.jgss." *core-java-api*
36 "org.omg." *core-java-api*
37 "org.w3c.dom." *core-java-api*
38 "org.xml.sax." *core-java-api*
39 "org.apache.commons.codec." "http://commons.apache.org/codec/api-release/"
40 "org.apache.commons.io." "http://commons.apache.org/io/api-release/"
41 "org.apache.commons.lang." "http://commons.apache.org/lang/api-release/")))
42
43 (defn add-local-javadoc
44 "Adds to the list of local Javadoc paths."
45 [path]
46 (dosync (commute *local-javadocs* conj path)))
47
48 (defn add-remote-javadoc
49 "Adds to the list of remote Javadoc URLs. package-prefix is the
50 beginning of the package name that has docs at this URL."
51 [package-prefix url]
52 (dosync (commute *remote-javadocs* assoc package-prefix url)))
53
54 (defn- find-javadoc-url
55 "Searches for a URL for the given class name. Tries
56 *local-javadocs* first, then *remote-javadocs*. Returns a string."
57 {:tag String}
58 [#^String classname]
59 (let [file-path (.replace classname \. File/separatorChar)
60 url-path (.replace classname \. \/)]
61 (if-let [file #^File (first
62 (filter #(.exists #^File %)
63 (map #(File. (str %) (str file-path ".html"))
64 @*local-javadocs*)))]
65 (-> file .toURI str)
66 ;; If no local file, try remote URLs:
67 (or (some (fn [[prefix url]]
68 (when (.startsWith classname prefix)
69 (str url url-path ".html")))
70 @*remote-javadocs*)
71 ;; if *feeling-lucky* try a web search
72 (when *feeling-lucky* (str *feeling-lucky-url* url-path ".html"))))))
73
74 (defn javadoc
75 "Opens a browser window displaying the javadoc for the argument.
76 Tries *local-javadocs* first, then *remote-javadocs*."
77 [class-or-object]
78 (let [#^Class c (if (instance? Class class-or-object)
79 class-or-object
80 (class class-or-object))]
81 (if-let [url (find-javadoc-url (.getName c))]
82 (browse-url url)
83 (println "Could not find Javadoc for" c))))
Something went wrong with that request. Please try again.