Skip to content

Commit

Permalink
Add clear-eval-results + readme update
Browse files Browse the repository at this point in the history
  • Loading branch information
darthdeus committed Jan 10, 2014
1 parent 8ddc64d commit e69f1d1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
7 changes: 5 additions & 2 deletions README.md
Expand Up @@ -29,8 +29,11 @@ puts a

**MAKE SURE YOU SAVE THE FILE FIRST, OTHERWISE THE UNSAVED CHANGES WILL GET OVERRIDEN**, because `xmpfilter` reads the contents from the actual file. (TODO - fix this)

You can run the evaluation either by invoking the behavior directly via `Ctrl-Space -> Ruby: eval current buffer`, or by making a keyboard shortcut, for example
You can run the evaluation either by invoking the behavior directly via `Ctrl-Space -> Ruby: eval current buffer`, or by making a keyboard shortcut (see below). After the eval you can clear the results using `Ruby: clear eval results`.

```
"ctrl-l" [:eval-ruby-buffer]
:editor.ruby {
"ctrl-l" [:ruby.eval-buffer]
"ctrl-shift-l" [:ruby.clear-eval-results]
}
```
18 changes: 15 additions & 3 deletions ruby_compiled.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 24 additions & 4 deletions src/lt/plugins/ruby.cljs
Expand Up @@ -32,17 +32,37 @@
#js {:line 0 :ch 0}
#js {:line (.lineCount (editor/->cm-ed (pool/last-active))) :ch 0})))

(defn current-file-name [] (-> @(pool/last-active) :info :path))

(defn strip-xmpfilter-result [line]
"Removes results of applying xmpfilter (see below for examples)"
(clojure.string/replace (clojure.string/replace line #"# =>.+$" "# =>") #"^# >>.+$" ""))

(= "1 + 1 # =>" (strip-xmpfilter-result "1 + 1 # => 2"))
(= "" (strip-xmpfilter-result "# >> 2"))


(defn replace-ruby-contents [err stdout stderr]
"Replaces content of the current buffer from an output of an external process"
(replace-buffer (str stdout stderr)))

(defn current-file-name [] (-> @(pool/last-active) :info :path))
(defn ruby-clear-eval-results []
"Clears results from previous eval"
(replace-buffer (clojure.string/join "\n"
(map strip-xmpfilter-result
(clojure.string/split (current-buffer-content) #"\n")))))

(defn eval-ruby-buffer []
(defn ruby-eval-buffer []
"Evaluates the current buffer using xmpfilter"
((.log js/console "eval-ruby-buffer")
(.exec (js/require "child_process") (str "xmpfilter " (current-file-name)) replace-ruby-contents)))

(cmd/command {:command :eval-ruby-buffer
;; TODO - add command for inserting # => on the current line

(cmd/command {:command :ruby.eval-buffer
:desc "Ruby: eval current buffer"
:exec eval-ruby-buffer})
:exec ruby-eval-buffer})

(cmd/command {:command :ruby.clear-eval-results
:desc "Ruby: clear eval results"
:exec ruby-clear-eval-results})
1 change: 1 addition & 0 deletions test.rb
Expand Up @@ -17,3 +17,4 @@ def with_a_method(and_params, with_stuff)
a = 4 + 7 # =>

puts a
# >> 11

0 comments on commit e69f1d1

Please sign in to comment.