-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathLoadFileToRepl.py
174 lines (150 loc) · 5.79 KB
/
LoadFileToRepl.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import sublime, sublime_plugin
import sys
SETTINGS_FILE = 'LoadFileToRepl.sublime-settings'
def bug_report(message):
'''Offers to report about problem to the github issue tracker
'''
if sublime.ok_cancel_dialog(message, 'Open issue tracker'):
sublime.active_window().run_command('open_url',
{'url': 'https://github.com/laughedelic/LoadFileToRepl/issues'})
def peek(view_gen):
''' if it's a generator, return next, else return the value itself
'''
# print(type(view_gen))
# if view_gen == None: return None
try:
return next(view_gen)
except StopIteration:
return None
except:
return view_gen
class LoadFileToReplListener(sublime_plugin.EventListener):
def on_query_context(self, view, key, operator, operand, match_all):
'''Checks if default keybindings should work or not
'''
if key == 'use_load_file_to_repl_keybindings':
settings = sublime.load_settings(SETTINGS_FILE)
return (settings.get('use_load_file_to_repl_keybindings') and
((operator == sublime.OP_EQUAL and operand) or
(operator == sublime.OP_NOT_EQUAL and not operand)))
else: return False
class LoadFileToReplOpenSettingsCommand(sublime_plugin.ApplicationCommand):
def run(self):
'''This command opens plugins settings file in the User package,
or creates it with defaults if it doesn't exist or is empty
'''
import os.path
f = sublime.packages_path() + "/User/" + SETTINGS_FILE
if (not os.path.isfile(f)) or (not os.path.getsize(f)):
defaults = sublime.load_resource("Packages/Load file to REPL/"+SETTINGS_FILE).encode('utf8')
with open(f, 'wb') as fw: fw.write(defaults)
sublime.active_window().open_file(f)
class LoadFileToReplCommand(sublime_plugin.WindowCommand):
def run(self, clear=None, save_focus=None, split=None):
# import is done inside of function, because this plugin
# may be loaded before SublimeREPL
try:
if sys.version.split('.')[0] == '2':
from sublimerepl import manager as repl_manager
else:
from SublimeREPL.sublimerepl import manager as repl_manager
except ImportError: # something strange...
bug_report(
'Looks like SublimeREPL plugin cannot be loaded. '
'Try to restart Sublime Text. '
'If it doesn\'t help, report about this issue, please.')
return
# if options are not set, use defaults from settings
settings = sublime.load_settings(SETTINGS_FILE)
if clear == None: clear = settings.get('clear')
if save_focus == None: save_focus = settings.get('save_focus')
if split == None: split = settings.get('split')
# source is where we are right now
source_group = self.window.active_group()
source_view = self.window.active_view()
if source_view == None:
sublime.error_message('LoadFileToRepl: No file is selected.')
return
filename = source_view.file_name()
filetype = ".".join(source_view.scope_name(0).split(' ')[0].split('.')[1:])
#for the case if user hasn't saved file yet
if filetype == 'plain':
sublime.error_message(
'LoadFileToRepl: Plain text is not supported. '
'Change file type, please.')
return
# check if such filetype is supported
load_command_format = settings.get(filetype + '_load_command')
if not load_command_format:
bug_report(
'%s language is not supported by this plugin.\n' %
filetype.title() +
'If you know suitable load command for it, please, '
'write it to the issue tracker and I\'ll add it.')
return
peek_repl = peek(repl_manager.find_repl(filetype))
# if there is only one group and repl view is not present or is in current window, split window
# i.e if repl view is in different window don't split
if self.window.num_groups() == 1 and (peek_repl == None or peek_repl._view.window() == self.window):
if split == 'vertically':
self.window.run_command('set_layout', {
'cols' : [0.0, 0.5, 1.0],
'rows' : [0.0, 1.0],
'cells' : [[0, 0, 1, 1], [1, 0, 2, 1]]
})
elif split == 'horizontally':
self.window.run_command('set_layout', {
'cols' : [0.0, 1.0],
'rows' : [0.0, 0.5, 1.0],
'cells' : [[0, 0, 1, 1], [0, 1, 1, 2]]
})
# else no any split
next_group = (source_group + 1) % self.window.num_groups()
# if there is no opened repl
if peek_repl == None:
config_title = filetype.title()
repl_id = filetype
if repl_id == 'js':
repl_id = 'node'
config_title = 'NodeJS'
elif repl_id == 'racket':
repl_id = 'plt_racket'
elif repl_id == 'fsharp':
repl_id = 'f#'
config_title = 'F'
# focus on another group to open repl there
self.window.focus_group(next_group)
# open repl according to the type of source file
self.window.run_command('run_existing_window_command', {
'id' : 'repl_' + repl_id,
'file' : 'config/' + config_title + '/Main.sublime-menu'
})
# reveal repl view and move to another group
peek_repl = peek(repl_manager.find_repl(filetype))
if peek_repl == None:
sublime.error_message('LoadFileToRepl: Couldn\'t open the repl.')
return
repl_view = peek_repl._view
self.window.focus_view(repl_view)
if source_group == repl_view.window().active_group():
repl_view.window().run_command(
'move_to_group', {'group': next_group})
# clear repl if needed
if clear:
repl_view.run_command('repl_clear')
# focus back on source file if needed
if save_focus:
source_view.window().focus_view(source_view)
# and finally, load file to repl!
if load_command_format:
if sublime.platform() == 'windows':
load_command = load_command_format % filename.replace('\\','\\\\')
else:
load_command = load_command_format % filename
source_view.run_command('save')
self.window.run_command('repl_send', {
'external_id' : filetype,
'text' : load_command
})
# just to show user that everything is ok
sublime.status_message(filetype.title() + ' REPL > ' + load_command)