forked from quinnj/Sublime-Julia
/
text_transfer.py
111 lines (91 loc) · 3.46 KB
/
text_transfer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from __future__ import absolute_import, unicode_literals, print_function, division
import re
import sublime_plugin
import sublime
from collections import defaultdict
import tempfile
import binascii
try:
from .sublimejulia import manager, SETTINGS_FILE
except (ImportError, ValueError):
from sublimejulia import manager, SETTINGS_FILE
def default_sender(repl, text, view=None):
repl.write(text)
"""Senders is a dict of functions used to transfer text to repl as a repl
specific load_file action"""
SENDERS = defaultdict(lambda: default_sender)
def sender(external_id,):
def wrap(func):
SENDERS[external_id] = func
return wrap
class ReplViewWrite(sublime_plugin.TextCommand):
def run(self, edit, external_id, text):
for rv in manager.find_repl(external_id):
rv.append_input_text(text)
break # send to first repl found
else:
sublime.error_message("Cannot find REPL for '{}'".format(external_id))
class ReplWriteSend(sublime_plugin.TextCommand):
def run(self, edit, external_id, text):
for rv in manager.find_repl(external_id):
rv.append_input_text(text)
rv.enter()
break # send to first repl found
else:
sublime.error_message("Cannot find REPL for '{}'".format(external_id))
class ReplSend(sublime_plugin.TextCommand):
def run(self, edit, external_id, text, with_auto_postfix=True):
for rv in manager.find_repl(external_id):
if with_auto_postfix:
text += rv.repl.cmd_postfix
if sublime.load_settings(SETTINGS_FILE).get('show_transferred_text'):
rv.append_input_text(text)
rv.adjust_end()
SENDERS[external_id](rv.repl, text, self.view)
break
else:
sublime.error_message("Cannot find REPL for '{}'".format(external_id))
class ReplTransferCurrent2(sublime_plugin.TextCommand):
def run(self, edit, scope="selection", action="send"):
text = ""
if scope == "selection":
text = self.selected_text()
elif scope == "lines":
text = self.selected_lines()
elif scope == "function":
text = self.selected_functions()
elif scope == "block":
text = self.selected_blocks()
elif scope == "file":
text = self.selected_file()
cmd = "repl_" + action
self.view.window().run_command(cmd, {"external_id": self.repl_external_id(), "text": text})
def repl_external_id(self):
return self.view.scope_name(0).split(" ")[0].split(".", 1)[1]
def selected_text(self):
v = self.view
parts = [v.substr(region) for region in v.sel()]
return "".join(parts)
def selected_blocks(self):
# TODO: Clojure only for now
v = self.view
strs = []
old_sel = list(v.sel())
v.run_command("expand_selection", {"to": "brackets"})
v.run_command("expand_selection", {"to": "brackets"})
for s in v.sel():
strs.append(v.substr(s))
v.sel().clear()
for s in old_sel:
v.sel().add(s)
return "\n\n".join(strs)
def selected_lines(self):
v = self.view
parts = []
for sel in v.sel():
for line in v.lines(sel):
parts.append(v.substr(line))
return "\n".join(parts)
def selected_file(self):
v = self.view
return v.substr(sublime.Region(0, v.size()))