Skip to content

Commit

Permalink
Lazy text-forms, much faster response for everything
Browse files Browse the repository at this point in the history
  • Loading branch information
Aria Haghighi committed Aug 29, 2010
1 parent a063916 commit cef0113
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 83 deletions.
2 changes: 1 addition & 1 deletion Commands/Wrap sexpr.tmCommand
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ print "($1 #{t})$0"</string>
<key>input</key>
<string>selection</string>
<key>keyEquivalent</key>
<string>@W</string>
<string>~9</string>
<key>name</key>
<string>Wrap sexpr</string>
<key>output</key>
Expand Down
2 changes: 1 addition & 1 deletion Support/bin/compile_file.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
(clojure.core/println "<pre>Compilation finished.</pre>"))
(catch Exception e
(do
(textmate/print-stack-trace e)
(stacktrace/print-stack-trace e)
(clojure.core/println "</pre>")))))
(clojure.core/println "No file.")))
6 changes: 3 additions & 3 deletions Support/bin/eval_last_sexpr.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
(clojure.core/refer 'clojure.core)
(require '[clojure.java.io :as io])
(load-file (str (io/file (cake/*env* "TM_BUNDLE_SUPPORT") "utils.clj")))



(textmate/attempt
#_(println (get-last-sexpr))
(clojure.core/println
"<pre>"
(-> (get-last-sexpr)
clojure.core/eval
textmate/eval-in-file-ns
textmate/str-nil
textmate/ppstr-nil
textmate/htmlize)
"</pre>"))
3 changes: 1 addition & 2 deletions Support/bin/eval_pprint.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
(require '[clojure.string :as string])
(require '[clojure.java.io :as io])
(load-file (str (io/file (cake/*env* "TM_BUNDLE_SUPPORT") "utils.clj")))
(clojure.core/require '[clojure.pprint :as pprint])

(enter-file-ns)

(clojure.core/require '[clojure.pprint :as pprint])

(textmate/attempt
(clojure.core/println
(clojure.core/str
Expand Down
2 changes: 1 addition & 1 deletion Support/bin/load_file.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
(clojure.core/println "<pre>Loading finished.</pre>"))
(catch Exception e
(do
(textmate/print-stack-trace e)
(clojure.core/println (with-out-str (stacktrace/print-stack-trace e)))
(clojure.core/println "</pre>")))))
(clojure.core/println "No file.")))
12 changes: 6 additions & 6 deletions Support/bin/show_doc.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
(do (when-let [name (-> symb meta :name)]
(println "<h1>Name</h1>")
(println name))
(when-let [arg-list-str (-> symb meta :arglists)]
(println "<h1>Arg Lists</h1>")
(println (textmate/htmlize (str arg-list-str))))
(when-let [doc-str (-> symb meta :doc)]
(println "<h1>Doc</h1>")
(println (.replaceAll doc-str "\n" "<br>")))
Expand All @@ -17,16 +20,13 @@
(.replaceAll
(.replaceAll (str symb-ns) "<" "&lt;")
">" "&rt;")
"<br>"))
"<br>"))
(when-let [f (-> symb meta :file)]
(println "<h1>File</h1>")
(println
(format "<a href=\"txmt://open?line=%s&url=file:///%s\">%s:%s</a>"
(-> symb meta :line)
f
f
(-> symb meta :line))))
#_(when-let [source-txt (ru/get-source symb)]
(println "<h1>Source</h1>")
(println source-txt)))
(println "Couldn't resolve symbol: " (get-current-symbol-str) "<br>")))
(-> symb meta :line)))))
(println "Couldn't resolve symbol: " (get-current-symbol-str) "<br>")))
114 changes: 58 additions & 56 deletions Support/utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
(clojure.core/refer 'clojure.core)
(require '[clojure.string :as string])
(require '[clojure.java.io :as io])
(require '[clojure.stacktrace :as stacktrace])
(require '[clojure.contrib.seq-utils :as seq-utils])
(require '[clojure.contrib.pprint :as pprint])

(defonce *compiled-files* (atom #{}))

Expand All @@ -13,9 +15,15 @@
(.replaceAll ">" "&gt;")
(.replaceAll "\n" "<br>")))

(defn pprint-str [x]
(with-out-str (pprint/pprint x)))

(defn str-nil [o]
(if o (str o) "nil"))

(defn ppstr-nil [o]
(if o (pprint-str o) "nil"))

(defn escape-quotes [#^String s]
(-> s
(.replaceAll "\"" "\\\"")))
Expand All @@ -29,65 +37,54 @@
(defn escape-str [s]
(-> s #_escape-characters escape-quotes))

(defn print-stack-trace [exc]
(println (.getMessage exc))
(doall (map #(println (.toString %)) (seq (.getStackTrace exc)))))
; (defn print-stack-trace [exc]
; (println (.getMessage exc))
; (doall (map #(println (.toString %)) (seq (.getStackTrace exc)))))

(defmacro attempt [& body]
`(try
(do
~@body)
(catch Exception e#
(clojure.core/println
(clojure.core/str
"<pre>"
(with-out-str (textmate/print-stack-trace e#))
"</pre>")))))

(defn filepath->ns-str
"Convert filepath to ns-str"
[path]
(-> path
(string/replace ".clj" "")
(string/replace "_" "-")
(string/replace "/" ".")))
(clojure.core/println
"<pre>"
(with-out-str (.printStackTrace e#))
"</pre>"))))

(defn reader-empty? [#^java.io.PushbackReader rdr]
(let [ch (.read rdr)]
(do (.unread rdr ch)
(= ch -1))))

(defn text-forms
"Wrap the forms in text t in a vector. Used
for all the eval functions"
"Uses Clojure compiler to return a (lazy) seq of forms from text,
each form which yielded a parsing error returns a nil.
If text consists of a single well-formed sexpr, this should
return a single non-nil form."
[t]
(read-string (str "[" t "]")))

;(defn push-back-reader-from-path
; { :tag #^java.io.PushbackReader }
; [#^String path]
; (-> path java.io.FileReader.
; java.io.BufferedReader.
; java.io.PushbackReader.))

;(defn read-forms [#^java.io.PushbackReader reader]
; (loop [forms []]
; (try
; (let [form (read reader)]
; (cond )))))
(let [rdr (-> t java.io.StringReader. java.io.PushbackReader.)]
(for [_ (repeat nil) :while (not (reader-empty? rdr))]
(try (read rdr) (catch Exception _ nil)))))

(defn file-ns
"Find the namespace of a file; searches for the first ns (or in-ns)
form in the file and returns that symbol. Defaults to 'user if one
can't be found"
[]
(let [forms (-> (cake/*env* "TM_FILEPATH")
slurp
text-forms
#_push-back-reader-from-path)
[ns-fn ns] (first
(for [f forms
:when (and (seq? f)
(#{"ns" "in-ns"} (str (first f))))]
[(first f) (second f)]))]
(let [forms
(-> (cake/*env* "TM_FILEPATH")
slurp
text-forms)
ns-form?
(fn [f] (and (seq? f)
(#{"ns" "in-ns"} (str (first f)))))
[ns-fn ns]
(first
(for [f forms :when (ns-form? f)]
[(first f) (second f)]))]
(if ns
(if (= (str ns-fn) "ns") ns (eval ns))
'user)))
'user)))

(defn enter-ns
"Enter a ns, wrapped for debugging purposes"
Expand Down Expand Up @@ -155,30 +152,34 @@
;(defn str-escape [t]
; (.replaceAll #^String t "\\n" "\\n"))

(defn symbol-char?
[c]
(or (Character/isLetterOrDigit c) (#{\_ \! \. \? \- \/} c)))

(defn get-current-symbol-str
"Get the string of the current symbol of the cursor"
[]
(let [#^String line (-> "TM_CURRENT_LINE" cake/*env* escape-str)
index (int (last (carret-info)))
symbol-char? (fn [index]
(and (< index (.length line))
(let [c (.charAt line #^int index)]
(or (Character/isLetterOrDigit c)
(#{\_ \! \. \? \- \/} c)))))
symbol-index?
(fn [index]
(and (< index (.length line))
(let [c (.charAt line index)] (symbol-char? c))))
symbol-start
(loop [i index]
(if (or (= i 0) (-> i dec symbol-char? not))
(if (or (= i 0) (-> i dec symbol-index? not))
i (recur (dec i))))
symbol-stop
symbol-stop
(loop [i index]
(if (or (= i (inc (.length line))) (not (symbol-char? (inc i))))
(if (or (= i (inc (.length line))) (not (symbol-index? (inc i))))
i (recur (inc i))))]
(.substring line symbol-start (min (.length line) (inc symbol-stop)))))

(defn get-current-symbol
"Get current (selected) symbol. Enters file ns"
[]
(ns-resolve (enter-file-ns) (symbol (get-current-symbol-str))))2
(ns-resolve (enter-file-ns) (symbol (get-current-symbol-str))))


(defn find-last-delim [#^String t]
(let [c (last t)]
Expand All @@ -201,26 +202,27 @@

(defn find-last-sexpr [#^String t]
(let [t (.trim t)
d (find-last-delim t)]
#_(println "last delim: " d)
d (find-last-delim t)]
#_(do (println (htmlize (str "Input: " t)))
(println (htmlize (str "last delim: " d))))
(if (= :symbol d)
(get-current-symbol)
(first
(filter identity
(for [i (indices-of t (matching-delims d))]
(let [cur (.substring t i)]
#_(println "search: " i " " cur)
#_(println (htmlize (str "search: " i " " cur)))
(try
(let [forms (text-forms cur)]
#_(println "forms: " forms)
#_(println (htmlize (str "forms: " forms)))
(when (= (count forms) 1)
(first forms)))
(catch Exception _ nil)))))))))

(defn get-last-sexpr
"Get last sexpr before carret"
[]
(find-last-sexpr (text-before-carret)))
(-> (text-before-carret) find-last-sexpr))

(defn get-selected-sexpr
"Get highlighted sexpr"
Expand Down
6 changes: 6 additions & 0 deletions Syntaxes/Clojure.tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@
<key>name</key>
<string>constant.language.boolean.clojure</string>
</dict>
<dict>
<key>match</key>
<string>\S+\.(?=[\s)\]}])</string>
<key>name</key>
<string>constant.language.java.class.clojure</string>
</dict>
<dict>
<key>match</key>
<string>(\d+/\d+)</string>
Expand Down
18 changes: 5 additions & 13 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,12 @@
<dict>
<key>items</key>
<array>
<string>19F476FD-94C6-4CB6-957B-7FD183C10C3B</string>
<string>3CD4E21D-7146-495E-913E-0106D1196EAE</string>
<string>------------------------------------</string>
<string>C9D4C6DA-F941-4789-AFC2-E74A1E27F6A7</string>
<string>------------------------------------</string>
<string>CB13E6ED-64B4-430F-8177-4456D5232B99</string>
<string>CF9DF2CC-2742-44D9-974E-8A520AB4C7CC</string>
<string>6C33FB7B-EDD3-4AFF-8C6E-10AA6ECE3B8C</string>
<string>A685EDA6-CB80-4F99-87E5-38E53ED1F0A9</string>
<string>20BCB6E0-E1D4-4B64-8BB3-8B255F159485</string>
<string>E5274BF4-942E-41B8-B5F6-6E0CAD35D64B</string>
<string>4BAA6473-1980-42B8-B068-C3BE57295913</string>
<string>1811A2F3-E45B-4DBB-93B9-6904DC2B5A81</string>
<string>------------------------------------</string>
<string>B0A431F9-321E-4507-B457-70E07118C709</string>
<string>E5274BF4-942E-41B8-B5F6-6E0CAD35D64B</string>
<string>FCF29461-95F8-44B3-AD5C-C8AD74A1A3B5</string>
</array>
<key>submenus</key>
Expand All @@ -31,21 +22,22 @@
<key>ordering</key>
<array>
<string>6A87759F-F746-4E84-B788-965B46363202</string>
<string>3C7566E1-E339-4F14-813D-12B3EA6A38BD</string>
<string>40910C79-E8F5-4930-8493-EC63AC6AAF0F</string>
<string>3C7566E1-E339-4F14-813D-12B3EA6A38BD</string>
<string>CB13E6ED-64B4-430F-8177-4456D5232B99</string>
<string>CF9DF2CC-2742-44D9-974E-8A520AB4C7CC</string>
<string>FCF29461-95F8-44B3-AD5C-C8AD74A1A3B5</string>
<string>B0A431F9-321E-4507-B457-70E07118C709</string>
<string>6C33FB7B-EDD3-4AFF-8C6E-10AA6ECE3B8C</string>
<string>A685EDA6-CB80-4F99-87E5-38E53ED1F0A9</string>
<string>20BCB6E0-E1D4-4B64-8BB3-8B255F159485</string>
<string>1811A2F3-E45B-4DBB-93B9-6904DC2B5A81</string>
<string>E5274BF4-942E-41B8-B5F6-6E0CAD35D64B</string>
<string>4BAA6473-1980-42B8-B068-C3BE57295913</string>
<string>1811A2F3-E45B-4DBB-93B9-6904DC2B5A81</string>
<string>C9D4C6DA-F941-4789-AFC2-E74A1E27F6A7</string>
<string>DEC67163-4A95-4687-8296-83B89A5A2E98</string>
<string>3CD4E21D-7146-495E-913E-0106D1196EAE</string>
<string>19F476FD-94C6-4CB6-957B-7FD183C10C3B</string>
<string>B0A431F9-321E-4507-B457-70E07118C709</string>
</array>
<key>uuid</key>
<string>6B2BD209-0142-4CA6-A596-9250015AD8CA</string>
Expand Down

0 comments on commit cef0113

Please sign in to comment.