Skip to content

Commit 1b37383

Browse files
Merge pull request #83 from casperc/master
Find text
2 parents 68daff1 + 5453f1e commit 1b37383

File tree

5 files changed

+56
-14
lines changed

5 files changed

+56
-14
lines changed

src/clooj/core.clj

+30-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
[clooj.search]
2929
[clooj.help :only (arglist-from-caret-pos show-tab-help setup-tab-help
3030
setup-completion-list help-handle-caret-move
31-
find-focused-text-pane)]
31+
find-focused-text-pane safe-resolve
32+
token-from-caret-pos)]
3233
[clooj.project :only (add-project load-tree-selection
3334
load-expanded-paths load-project-set
3435
save-expanded-paths
@@ -53,7 +54,8 @@
5354
scroll-to-caret when-lets
5455
constrain-to-parent make-split-pane
5556
gen-map on-click
56-
remove-text-change-listeners get-text-str)]
57+
remove-text-change-listeners get-text-str
58+
scroll-to-line get-directories)]
5759
[clooj.indent :only (setup-autoindent fix-indent-selected-lines)]
5860
[clooj.style :only (get-monospaced-fonts show-font-window)])
5961
(:require [clojure.main :only (repl repl-prompt)]
@@ -650,7 +652,29 @@
650652
(update-project-tree (:docs-tree app))
651653
(restart-doc app f))))))
652654

653-
655+
(defn- dir-rank [dir]
656+
(get {"src" 0 "test" 1 "lib" 2} (.getName dir) 100))
657+
658+
(defn- find-file [project-path relative-file-path]
659+
(let [classpath-dirs (sort-by dir-rank < (get-directories (File. project-path)))
660+
file-candidates (map
661+
#(File. (str (.getAbsolutePath %) File/separatorChar relative-file-path))
662+
classpath-dirs)]
663+
(first (filter #(and (.exists %) (.isFile %)) file-candidates))))
664+
665+
(defn goto-definition [ns app]
666+
(let [text-comp (:doc-text-area app)
667+
pos (.getCaretPosition text-comp)
668+
text (.getText text-comp)
669+
src-file (:file (meta (safe-resolve (find-ns (symbol ns)) (token-from-caret-pos ns text pos))))
670+
line (:line (meta (safe-resolve (find-ns (symbol ns)) (token-from-caret-pos ns text pos))))
671+
project-path (first (get-selected-projects app))
672+
file (find-file project-path src-file)]
673+
(when (and file line)
674+
(when (not= file @(:file app))
675+
(restart-doc app file)
676+
(set-tree-selection (:docs-tree app) (.getAbsolutePath file)))
677+
(scroll-to-line text-comp line))))
654678

655679
(defn make-menus [app]
656680
(when (is-mac)
@@ -677,7 +701,8 @@
677701
["Fix indentation" "F" "cmd1 BACK_SLASH" #(fix-indent-selected-lines (:doc-text-area app))]
678702
["Indent lines" "I" "cmd1 CLOSE_BRACKET" #(indent (:doc-text-area app))]
679703
["Unindent lines" "D" "cmd1 OPEN_BRACKET" #(indent (:doc-text-area app))]
680-
["Name search/docs" "S" "TAB" #(show-tab-help app (find-focused-text-pane app) inc)])
704+
["Name search/docs" "S" "TAB" #(show-tab-help app (find-focused-text-pane app) inc)]
705+
["Goto definition" "G" "F3" #(goto-definition (get-file-ns app) app)])
681706
(add-menu menu-bar "REPL" "R"
682707
["Evaluate here" "E" "cmd1 ENTER" #(send-selected-to-repl app)]
683708
["Evaluate entire file" "F" "cmd1 E" #(send-doc-to-repl app)]
@@ -772,3 +797,4 @@
772797
; []
773798
; (.setVisible (@current-app :frame) false)
774799
; (startup))
800+

src/clooj/help.clj

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
(java.awt Color Point)
1111
(java.util Vector)
1212
(javax.swing DefaultListCellRenderer ListSelectionModel)
13-
(javax.swing.event ListSelectionListener))
13+
(javax.swing.event ListSelectionListener)
14+
(java.io File))
1415
(:use [clooj.brackets :only (find-enclosing-brackets)]
1516
[clooj.repl :only (get-file-ns get-repl-ns)]
1617
[clooj.utils :only (attach-action-keys attach-child-action-keys
1718
on-click awt-event when-lets get-text-str)]
19+
[clooj.project :only (get-selected-projects)]
1820
[clojure.repl :only (source-fn)])
1921
(:require [clojure.string :as string]))
2022

src/clooj/repl.clj

+4-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
(:use [clooj.utils :only (attach-child-action-keys attach-action-keys
1616
awt-event
1717
get-line-of-offset get-line-start-offset
18-
append-text when-lets get-text-str)]
18+
append-text when-lets get-text-str get-directories)]
1919
[clooj.brackets :only (find-line-group find-enclosing-brackets)]
2020
[clojure.pprint :only (pprint)]
2121
[clooj.project :only (get-temp-file)])
@@ -48,12 +48,10 @@
4848
(when project-path
4949
(let [project-dir (File. project-path)]
5050
(when (and (.exists project-dir) (.isDirectory project-dir))
51-
(let [sub-dirs (filter #(and (.isDirectory %)
52-
(not (.startsWith (.getName %) ".")))
53-
(.listFiles project-dir))]
51+
(let [sub-dirs (get-directories project-dir)]
5452
(concat sub-dirs
55-
(filter #(.endsWith (.getName %) ".jar")
56-
(mapcat #(.listFiles %) (file-seq project-dir)))))))))
53+
(filter #(.endsWith (.getName %) ".jar")
54+
(mapcat #(.listFiles %) (file-seq project-dir)))))))))
5755

5856
(defn selfish-class-loader [url-array parent]
5957
(proxy [URLClassLoader] [url-array nil]

src/clooj/search.clj

+6-2
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,16 @@
6464

6565
(defn start-find [app]
6666
(let [sta (app :search-text-area)
67-
arg (app :arglist-label)]
67+
arg (app :arglist-label)
68+
dta (:doc-text-area app)
69+
sel-text (.getSelectedText dta)]
6870
(.setVisible arg false)
6971
(doto sta
7072
(.setVisible true)
7173
(.requestFocus)
72-
(.selectAll))))
74+
(.selectAll))
75+
(if (not (empty? sel-text))
76+
(.setText sta sel-text))))
7377

7478
(defn stop-find [app]
7579
(let [sta (app :search-text-area)

src/clooj/utils.clj

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
55

66
(ns clooj.utils
7-
(:import (java.util UUID)
7+
(:require [clojure.string :as string :only (join split)])
8+
(:import (java.util UUID)
89
(java.awt FileDialog Point Window)
910
(java.awt.event ActionListener MouseAdapter)
1011
(java.util.prefs Preferences)
@@ -196,6 +197,12 @@
196197
(.setViewPosition v
197198
(Point. 0 (min (- l h) (max 0 (- (.y r) (/ h 2)))))))))
198199

200+
(defn scroll-to-line [text-comp line]
201+
(let [text (.getText text-comp)
202+
pos (inc (.length (string/join "\n" (take (dec line) (string/split text #"\n")))))]
203+
(.setCaretPosition text-comp pos)
204+
(scroll-to-pos text-comp pos)))
205+
199206
(defn scroll-to-caret [text-comp]
200207
(scroll-to-pos text-comp (.getCaretPosition text-comp)))
201208

@@ -389,6 +396,11 @@
389396
(.setCurrentDirectory (if last-open-dir (File. last-open-dir) nil)))
390397
(if (= JFileChooser/APPROVE_OPTION (.showOpenDialog fc parent))
391398
(.getSelectedFile fc)))))
399+
400+
(defn get-directories [path]
401+
(filter #(and (.isDirectory %)
402+
(not (.startsWith (.getName %) ".")))
403+
(.listFiles path)))
392404

393405
;; tree seq on widgets (awt or swing)
394406

0 commit comments

Comments
 (0)