Skip to content

Instantly share code, notes, and snippets.

@Immortalin
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Immortalin/3eb8afa551e97d0fc3f1 to your computer and use it in GitHub Desktop.
Save Immortalin/3eb8afa551e97d0fc3f1 to your computer and use it in GitHub Desktop.
diff --git a/src/clojure/nightcode/builders.clj b/src/clojure/nightcode/builders.clj
index cb5578b..b646aaa 100644
--- a/src/clojure/nightcode/builders.clj
+++ b/src/clojure/nightcode/builders.clj
@@ -95,6 +95,8 @@ top level expression."
(not ios-project?))
:#eval (and (not java-project?)
(not ios-project?))
+ :#pprint (and (not java-project?)
+ (not ios-project?))
:#test (not java-project?)
:#sdk android-project?
:#robovm ios-project?
@@ -121,6 +123,7 @@ top level expression."
:#run-repl (not running?)
:#reload (not (nil? @last-reload))
:#eval (not (nil? @last-reload))
+ :#pprint true
:#build (not running?)
:#test (not running?)
:#clean (not running?)
@@ -131,10 +134,16 @@ top level expression."
; create and show/hide builders for each project
-(def ^:dynamic *widgets* [:run :run-repl :reload :eval :build :test
+(def ^:dynamic *widgets* [:run :run-repl :reload :eval :pprint :build :test
:clean :check-versions :stop
:sdk :robovm :auto])
+(defn pprint-replace-form! []
+ (editors/replace-form-under-caret
+ (fn [form]
+ (with-out-str
+ (clojure.pprint/write form :dispatch clojure.pprint/code-dispatch)))))
+
(defn create-actions
[path console build-pane process auto-process last-reload]
{:run (fn [& _]
@@ -150,6 +159,8 @@ top level expression."
(reset! last-reload (System/currentTimeMillis)))
:eval (fn [& _]
(eval! console))
+ :pprint (fn [& _]
+ (pprint-replace-form!))
:build (fn [& _]
(lein/build-project! process (ui/get-io! console) path))
:test (fn [& _]
@@ -184,6 +195,9 @@ top level expression."
:eval (ui/button :id :eval
:text (utils/get-string :eval)
:listen [:action (:eval actions)])
+ :pprint (ui/button :id :pprint
+ :text (utils/get-string :pprint)
+ :listen [:action (:pprint actions)])
:build (ui/button :id :build
:text (utils/get-string :build)
:listen [:action (:build actions)])
diff --git a/src/clojure/nightcode/editors.clj b/src/clojure/nightcode/editors.clj
index 4acfaf1..425b8b7 100644
--- a/src/clojure/nightcode/editors.clj
+++ b/src/clojure/nightcode/editors.clj
@@ -79,6 +79,22 @@
(paredit.static-analysis/top-level-code-form point)
paredit.loc-utils/loc-text))))
+(defn replace-simple [s r]
+ (when-let [text-area (get-selected-text-area)]
+ (let [context (doto (SearchContext. s)
+ (.setMatchCase true))
+ _ (println r)
+ _ (println (type r))]
+ (.setReplaceWith context r)
+ (SearchEngine/replaceAll text-area context))))
+
+(defn replace-form-under-caret
+ "Finds the top-level-expression under the caret, applies f and replaces it with the result."
+ [f]
+ (when-let [tle (get-tle-under-caret)]
+ (replace-simple tle (f tle))))
+
+
; tabs
(def ^:dynamic *reorder-tabs?* true)
@@ -260,6 +276,8 @@
editor (get-selected-editor)
find-text (s/text (s/select editor [:#find]))
replace-text (s/text e)
+ _ (println replace-text)
+ _ (println (type replace-text))
context (doto (SearchContext. find-text)
(.setMatchCase true))]
(.setReplaceWith context replace-text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment