-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeing_is_believing.py
78 lines (63 loc) · 2.92 KB
/
seeing_is_believing.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
import sublime, sublime_plugin, subprocess, os
class SeeingIsBelieving(sublime_plugin.TextCommand):
def run(self, edit):
# assume one cursor b/c I'm fucking lazy, store its row/col
(row, col) = self.view.rowcol(self.view.sel()[0].begin())
# load the text
region = sublime.Region(0, self.view.size())
text = self.view.substr(region)
# load settings
settings = sublime.load_settings("Seeing Is Believing.sublime-settings")
# set up env vars
env = os.environ.copy()
env_variables = settings.get("environment_variables")
environment_variables = ({} if env_variables is None else env_variables) # prob a better way to do this, if you know the pythons, feel free to do it for me :D
for (name, value) in environment_variables.iteritems():
env[name] = value
# set up the args
args = []
ruby_command = os.path.expanduser(settings.get("ruby_command"))
args.append(ruby_command)
args.append('-S')
args.append('seeing_is_believing')
args.append('--shebang')
args.append(ruby_command)
if self.view.file_name() != None:
args.append("--as")
args.append(self.view.file_name())
# subclass defines this
self.setup_flags(args, settings)
# call seeing is believing
s = subprocess.Popen(args, env=env, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = s.communicate(text.encode('utf-8'))
# display error
# error code 1 is displayable errors like exceptions getting raised
# non zero/one errors can't be displayed, like syntax error, so we need a dialog box
if self.should_display_stderr(s.returncode):
sublime.message_dialog(out[1])
return
# replace body with result, reset the selection
self.view.replace(edit, region, out[0])
point = self.view.text_point(row, col)
self.view.sel().clear()
self.view.sel().add(sublime.Region(point))
class YouKnowThatPlaceBetweenSleepAndAwakeThatPlaceWhereYouStillRememberDreamingThatsWhereIllAlwaysLoveYou(SeeingIsBelieving):
def setup_flags(self, args, settings):
for (name, value) in settings.get("flags").iteritems():
args.append(str(name))
args.append(str(value))
def should_display_stderr(self, returncode):
return returncode != None and returncode != 0 and returncode != 1
class IDrankPoisonForYou(SeeingIsBelieving):
def setup_flags(self, args, settings):
args.append('--xmpfilter-style')
for (name, value) in settings.get("flags").iteritems():
args.append(str(name))
args.append(str(value))
def should_display_stderr(self, returncode):
return returncode != None and returncode != 0 and returncode != 1
class EveryTimeSomeoneSaysIDoNotBelieveInFairiesSomewhereTheresAFairyThatFallsDownDead(SeeingIsBelieving):
def setup_flags(self, args, settings):
args.append('--clean')
def should_display_stderr(self, returncode):
return returncode != None and returncode != 0