Skip to content

Commit

Permalink
Copy evaluation result (closes #37)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Dec 14, 2021
1 parent 0bd53b5 commit 9722f74
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Default (Linux).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@
// {"keys": ["ctrl+alt+d"],
// "command": "sublime_clojure_lookup_symbol",
// "context": [{"key": "selector", "operator": "equal", "operand": "source.clojure"}]},

// // Copy Evaluation Result
// {"keys": ["ctrl+c"],
// "command": "sublime_clojure_copy",
// "context": [{"key": "selector", "operator": "equal", "operand": "source.clojure"}]},
]
5 changes: 5 additions & 0 deletions Default (OSX).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@
// {"keys": ["ctrl+d"],
// "command": "sublime_clojure_lookup_symbol",
// "context": [{"key": "selector", "operator": "equal", "operand": "source.clojure"}]},

// // Copy Evaluation Result
// {"keys": ["super+c"],
// "command": "sublime_clojure_copy",
// "context": [{"key": "selector", "operator": "equal", "operand": "source.clojure"}]},
]
5 changes: 5 additions & 0 deletions Default (Windows).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@
// {"keys": ["ctrl+alt+d"],
// "command": "sublime_clojure_lookup_symbol",
// "context": [{"key": "selector", "operator": "equal", "operand": "source.clojure"}]},

// // Copy Evaluation Result
// {"keys": ["ctrl+c"],
// "command": "sublime_clojure_copy",
// "context": [{"key": "selector", "operator": "equal", "operand": "source.clojure"}]},
]
4 changes: 4 additions & 0 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"caption": "Clojure REPL: Interrupt Pending Evaluations",
"command": "sublime_clojure_interrupt_eval"
},
{
"caption": "Clojure REPL: Copy Evaluation Result",
"command": "sublime_clojure_copy"
},
{
"caption": "Clojure REPL: Toggle Stacktrace",
"command": "sublime_clojure_toggle_trace"
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ By default, Sublime Clojure will also print evaluation time if it took more than

<img src="https://raw.github.com/tonsky/sublime-clojure/master/screenshots/eval_elapsed.png" width="500" height="139" alt="Elapsed time">

### Copying evaluation results

Sometimes you want to copy evaluation result. It is recommended to rebind `Cmd+C`/`Ctrl+C` from `copy` to `sublime_clojure_copy`. This will copy evaluation result if inside evaluated region and fallback to default `copy` otherwise.

### Interrupting

If your evaluation runs too long and you want to interrupt it, run `Clojure REPL: Interrupt Pending Evaluations`:
Expand Down Expand Up @@ -176,6 +180,7 @@ Evaluate Buffer | <kbd>Ctrl</kbd> <kbd>B</kbd> | <kbd>Ctrl</kb
Interrupt Pending Evaluations | <kbd>Ctrl</kbd> <kbd>C</kbd> | <kbd>Ctrl</kbd> <kbd>Alt</kbd> <kbd>C</kbd> | [C]ancel
Toggle Info | <kbd>Ctrl</kbd> <kbd>I</kbd> | <kbd>Ctrl</kbd> <kbd>Alt</kbd> <kbd>I</kbd> | [I]nfo
Clear Evaluation Results | <kbd>Ctrl</kbd> <kbd>L</kbd> | <kbd>Ctrl</kbd> <kbd>Alt</kbd> <kbd>L</kbd> | c[L]ear
Copy Evaluation Results | <kbd>Command</kbd> <kbd>C</kbd> | <kbd>Ctrl</kbd> <kbd>C</kbd> | [C]opy

To set it up, run `Preferences: Sublime Clojure Key Bindings` command and copy example keybindings to your local Key Bindings file.

Expand Down
13 changes: 13 additions & 0 deletions package.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(self, view, region):
self.msg = None
self.trace = None
self.phantom_id = None
self.value = None
Eval.next_id += 1
self.update("pending", None, region)

Expand Down Expand Up @@ -59,6 +60,7 @@ def region(self):

def update(self, status, value, region = None, time_taken = None):
self.status = status
self.value = value
region = region or self.region()
if region:
scope, color = self.scope_color()
Expand Down Expand Up @@ -424,6 +426,17 @@ def run(self, code):
def is_enabled(self):
return conn.ready()

class SublimeClojureCopyCommand(sublime_plugin.TextCommand):
def eval(self):
view = self.view
return conn.find_eval(view, view.sel()[0])

def run(self, edir):
if conn.ready() and len(self.view.sel()) == 1 and self.view.sel()[0].empty() and (eval := self.eval()) and eval.value:
sublime.set_clipboard(eval.value)
else:
self.view.run_command("copy", {})

class SublimeClojureClearEvalsCommand(sublime_plugin.TextCommand):
def run(self, edit):
conn.erase_evals(lambda eval: eval.status not in {"pending", "interrupt"}, self.view)
Expand Down

0 comments on commit 9722f74

Please sign in to comment.