Skip to content

Commit

Permalink
Added comment and now last s-expression actually works
Browse files Browse the repository at this point in the history
  • Loading branch information
Aria Haghighi committed Aug 19, 2010
1 parent 7905ae7 commit 636b04e
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Commands/cake eval.tmCommand
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>name</key>
<string>Eval</string>
<key>output</key>
<string>showAsTooltip</string>
<string>showAsHTML</string>
<key>scope</key>
<string>source.clojure</string>
<key>uuid</key>
Expand Down
24 changes: 24 additions & 0 deletions Preferences/Comment.tmPreferences
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>name</key>
<string>Comment</string>
<key>scope</key>
<string>source.clojure</string>
<key>settings</key>
<dict>
<key>shellVariables</key>
<array>
<dict>
<key>name</key>
<string>TM_COMMENT_START</string>
<key>value</key>
<string>; </string>
</dict>
</array>
</dict>
<key>uuid</key>
<string>40910C79-E8F5-4930-8493-EC63AC6AAF0F</string>
</dict>
</plist>
5 changes: 4 additions & 1 deletion Support/bin/compile_file.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
(require '[clojure.java.io :as io])
(load-file (str (io/file (cake/*env* "TM_BUNDLE_SUPPORT") "utils.clj")))

clojure.core/*compile-path*
(let [tm-filepath (cake/*env* "TM_FILEPATH")]
(if (not (= tm-filepath ""))
(do
(swap! *compiled-files* conj tm-filepath)
;(clojure.core/println "<pre>Compiling...")
(try
(let [cur-ns (file-ns)]
(compile cur-ns)
(eval-in-ns 'clojure.core
(binding [*compile-path* "classes/"]
(compile cur-ns)))
(clojure.core/println "<pre>Compilation finished.</pre>"))
(catch Exception e
(do
Expand Down
12 changes: 9 additions & 3 deletions Support/bin/eval.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
(require '[clojure.java.io :as io])
(load-file (str (io/file (cake/*env* "TM_BUNDLE_SUPPORT") "utils.clj")))

;(clojure.core/println "#" (-> (get-selected-sexpr)) "#")


(textmate/attempt
(clojure.core/println
(clojure.core/str
"<pre>"
(textmate/eval-in-file-ns (clojure.core/eval (get-selected-sexpr)))
"</pre>")))
(-> (get-selected-sexpr)
clojure.core/eval
textmate/eval-in-file-ns
textmate/str-nil
#_textmate/htmlize)
"</pre>"))
9 changes: 6 additions & 3 deletions Support/bin/eval_last_sexpr.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
(textmate/attempt
#_(println (get-last-sexpr))
(clojure.core/println
(clojure.core/str
"<pre>"
(textmate/eval-in-file-ns (clojure.core/eval (get-last-sexpr)))
"</pre>")))
(-> (get-last-sexpr)
clojure.core/eval
textmate/eval-in-file-ns
textmate/str-nil
#_textmate/htmlize)
"</pre>"))
100 changes: 87 additions & 13 deletions Support/utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@

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

(defn htmlize [#^String text]
(-> text
(.replaceAll "&" "&amp;")
(.replaceAll "\n" "<br>")
(.replaceAll "<" "&lt;")
(.replaceAll ">" "&gt;")))

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

(defn escape-quotes [#^String s]
(-> s
(.replaceAll "\"" "\\\"")))

(defn escape-characters [#^String s]
(let [#^java.util.regex.Matcher m (.matcher #"\\(\S)" s)]
(if (.matches m)
(.replaceAll m "\\\\$1")
s)))

(defn escape-str [s]
(-> s #_escape-characters escape-quotes))


(defn print-stack-trace [exc]
(println (.getMessage exc))
(doall (map #(println (.toString %)) (seq (.getStackTrace exc)))))
Expand Down Expand Up @@ -34,11 +58,22 @@
[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 )))))

(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)
(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)]))]
(if ns
Expand All @@ -57,6 +92,16 @@
(let [ns (file-ns)]
(enter-ns ns)))

(defmacro eval-in-ns
""
[the-ns & forms]
`(let [old-ns# *ns*]
(enter-ns ~the-ns)
(let [r# ~@forms]
(enter-ns (-> old-ns# str symbol))
r#)))


(defmacro eval-in-file-ns
"For the current file, enter the ns (if any)
and evaluate the form in that ns, then pop
Expand Down Expand Up @@ -104,38 +149,67 @@

;(defn str-escape [t]
; (.replaceAll #^String t "\\n" "\\n"))

(defn find-last-delim [#^String t]
(let [c (last t)]
(cond
((hash-set \) \] \}) c) c
((hash-set \( \[ \{) c)
(throw (RuntimeException. (str "Not a valid form ending in '" c "'")))
:default :symbol)))

(defn indices-of [#^String t #^Character target]
(reverse (for [[i c] (seq-utils/indexed t)
:when (= c target)] i)))

(def matching-delims
{ \) \(
\] \[
\} \{ })

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

(defn get-last-sexpr
"Get last sexpr before carret"
[]
(let [last (-> (text-before-carret) text-forms last)]
#_(println "Last SEXPR" last)
last))
(find-last-sexpr (text-before-carret)))

(defn get-selected-sexpr
"Get highlighted sexpr"
[]
(-> "TM_SELECTED_TEXT" cake/*env* clojure.core/read-string))
(-> "TM_SELECTED_TEXT" cake/*env* escape-str clojure.core/read-string))

(defn get-enclosing-sexpr [])

(defn get-current-symbol-str
"Get the string of the current symbol of the cursor"
[]
(let [#^String line (-> "TM_CURRENT_LINE" cake/*env*)
index (last (carret-info))
(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-start
(loop [i index]
(if (or (= i 0) (not (symbol-char? (dec i))))
i (recur (dec i))))
(loop [i index]
(if (or (= i 0) (-> i dec symbol-char? not))
i (recur (dec i))))
symbol-stop
(loop [i index]
(if (or (= i (inc (.length line))) (not (symbol-char? (inc i))))
i (recur (inc i))))]
(loop [i index]
(if (or (= i (inc (.length line))) (not (symbol-char? (inc i))))
i (recur (inc i))))]
(.substring line symbol-start (min (.length line) (inc symbol-stop)))))

(defn get-current-symbol
Expand Down
6 changes: 6 additions & 0 deletions Syntaxes/Clojure.tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@
<key>name</key>
<string>constant.numeric.octal.clojure</string>
</dict>
<dict>
<key>match</key>
<string>\\\S</string>
<key>name</key>
<string>constant.character.clojure</string>
</dict>
<dict>
<key>match</key>
<string>(\d+)</string>
Expand Down
8 changes: 5 additions & 3 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@
<key>ordering</key>
<array>
<string>6A87759F-F746-4E84-B788-965B46363202</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>95F2C352-4BFD-4688-A0E7-24E6385C3BD5</string>
<string>9DD40369-9FF9-4498-962E-AAFC399D3401</string>
<string>C9D4C6DA-F941-4789-AFC2-E74A1E27F6A7</string>
<string>3CD4E21D-7146-495E-913E-0106D1196EAE</string>
<string>19F476FD-94C6-4CB6-957B-7FD183C10C3B</string>
</array>
<key>uuid</key>
<string>6B2BD209-0142-4CA6-A596-9250015AD8CA</string>
Expand Down

0 comments on commit 636b04e

Please sign in to comment.