|
| 1 | +import sublime, sublime_plugin |
| 2 | + |
| 3 | +sublime.CLASS_WORD_START | sublime.CLASS_WORD_END |
| 4 | + |
| 5 | +def cursol_symbol(v): |
| 6 | + pos = v.sel()[0].begin() |
| 7 | + cls = sublime.CLASS_WORD_START | sublime.CLASS_WORD_END |
| 8 | + r = v.expand_by_class(pos, cls, " ({[;") |
| 9 | + _r = v.find(r"[-/\w\d.]+[-\w\d]", r.begin()) |
| 10 | + return v.substr(_r) |
| 11 | + |
| 12 | +def cursol_block(v): |
| 13 | + # pos = v.sel()[0].begin() |
| 14 | + # cls = sublime.CLASS_PUNCTUATION_START | sublime.CLASS_PUNCTUATION_END |
| 15 | + strs = [] |
| 16 | + v.run_command("expand_selection", {"to": "brackets"}) |
| 17 | + v.run_command("expand_selection", {"to": "brackets"}) |
| 18 | + for s in v.sel(): |
| 19 | + strs.append(v.substr(s)) |
| 20 | + return "\n\n".join(strs) |
| 21 | + |
| 22 | +def repl_external_id(v): |
| 23 | + return v.scope_name(0).split(" ")[0].split(".", 1)[1] # may be "clojure" |
| 24 | + |
| 25 | +def repl_send(v, text): |
| 26 | + external_id = repl_external_id(v) |
| 27 | + v.run_command("repl_send", {"external_id": external_id, "text": text}) |
| 28 | + |
| 29 | +class ClojureReplDocCommand(sublime_plugin.TextCommand): |
| 30 | + def run(self, edit): |
| 31 | + v = self.view |
| 32 | + text = "(println) (clojure.repl/doc {})".format(cursol_symbol(v)) |
| 33 | + repl_send(v, text) |
| 34 | + |
| 35 | +class ClojureReplSourceCommand(sublime_plugin.TextCommand): |
| 36 | + def run(self, edit): |
| 37 | + v = self.view |
| 38 | + text = "(println) (clojure.repl/source {})".format(cursol_symbol(v)) |
| 39 | + repl_send(v, text) |
| 40 | + |
| 41 | +class ClojureReplMacroexpand1Command(sublime_plugin.TextCommand): |
| 42 | + def run(self, edit): |
| 43 | + v = self.view |
| 44 | + text = "(println) (clojure.core/macroexpand-1 '{})".format(cursol_block(v)) |
| 45 | + repl_send(v, text) |
| 46 | + |
| 47 | +class ClojureReplMacroexpandCommand(sublime_plugin.TextCommand): |
| 48 | + def run(self, edit): |
| 49 | + v = self.view |
| 50 | + text = "(println) (clojure.core/macroexpand '{})".format(cursol_block(v)) |
| 51 | + repl_send(v, text) |
0 commit comments