forked from guillermooo/Vintageous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_main.py
318 lines (256 loc) · 11.4 KB
/
ex_main.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import sublime
import sublime_plugin
import os
from Vintageous.ex import ex_error
from Vintageous.ex.ex_command_parser import EX_COMMANDS
from Vintageous.ex.ex_command_parser import parse_command
from Vintageous.ex.completions import iter_paths
from Vintageous.ex.completions import parse
from Vintageous.ex.completions import parse_for_setting
from Vintageous.ex.completions import wants_fs_completions
from Vintageous.ex.completions import wants_setting_completions
from Vintageous.vi.settings import iter_settings
from Vintageous.vi.sublime import show_ipanel
from Vintageous.vi.utils import modes
from Vintageous.state import VintageState
from Vintageous.vi.utils import mark_as_widget
def plugin_loaded():
v = sublime.active_window().active_view()
state = VintageState(v)
d = os.path.dirname(v.file_name()) if v.file_name() else os.getcwd()
state.settings.vi['_cmdline_cd'] = d
COMPLETIONS = sorted([x[0] for x in EX_COMMANDS.keys()])
EX_HISTORY_MAX_LENGTH = 20
EX_HISTORY = {
'cmdline': [],
'searches': []
}
def update_command_line_history(item, slot_name):
if len(EX_HISTORY[slot_name]) >= EX_HISTORY_MAX_LENGTH:
EX_HISTORY[slot_name] = EX_HISTORY[slot_name][1:]
if item in EX_HISTORY[slot_name]:
EX_HISTORY[slot_name].pop(EX_HISTORY[slot_name].index(item))
EX_HISTORY[slot_name].append(item)
class ViColonInput(sublime_plugin.WindowCommand):
# Indicates whether the user issued the call.
interactive_call = True
def is_enabled(self):
return bool(self.window.active_view())
def __init__(self, window):
sublime_plugin.WindowCommand.__init__(self, window)
def run(self, initial_text=':', cmd_line=''):
# non-interactive call
if cmd_line:
self.non_interactive = True
self.on_done(cmd_line)
return
FsCompletion.invalidate()
state = VintageState(self.window.active_view())
if state.mode in (modes.VISUAL, modes.VISUAL_LINE):
initial_text = ":'<,'>" + initial_text[1:]
v = mark_as_widget(show_ipanel(self.window,
initial_text=initial_text,
on_done=self.on_done,
on_change=self.on_change))
v.set_syntax_file('Packages/Vintageous/VintageousEx Cmdline.tmLanguage')
v.settings().set('gutter', False)
v.settings().set('rulers', [])
state.reset_during_init = False
def on_change(self, s):
if ViColonInput.interactive_call:
cmd, prefix, only_dirs = parse(s)
if cmd:
FsCompletion.prefix = prefix
FsCompletion.is_stale = True
cmd, prefix, _ = parse_for_setting(s)
if cmd:
ViSettingCompletion.prefix = prefix
ViSettingCompletion.is_stale = True
if not cmd:
return
ViColonInput.interactive_call = True
def on_done(self, cmd_line):
if not getattr(self, 'non_interactive', None):
update_command_line_history(cmd_line, 'cmdline')
else:
self.non_interactive = False
ex_cmd = parse_command(cmd_line)
print(ex_cmd)
if ex_cmd and ex_cmd.parse_errors:
ex_error.display_error(ex_cmd.parse_errors[0])
return
if ex_cmd and ex_cmd.name:
if ex_cmd.can_have_range:
ex_cmd.args["line_range"] = ex_cmd.line_range
if ex_cmd.forced:
ex_cmd.args['forced'] = ex_cmd.forced
self.window.run_command(ex_cmd.command, ex_cmd.args)
else:
ex_error.display_error(ex_error.ERR_UNKNOWN_COMMAND, cmd_line)
class ViColonRepeatLast(sublime_plugin.WindowCommand):
def is_enabled(self):
return ((len(self.window.views()) > 0) and
(len(EX_HISTORY['cmdline']) > 0))
def run(self):
self.window.run_command('vi_colon_input',
{'cmd_line': EX_HISTORY['cmdline'][-1]})
class ExCompletionsProvider(sublime_plugin.EventListener):
CACHED_COMPLETIONS = []
CACHED_COMPLETION_PREFIXES = []
def on_query_completions(self, view, prefix, locations):
if view.score_selector(0, 'text.excmdline') == 0:
return []
if len(prefix) + 1 != view.size():
return []
if prefix and prefix in self.CACHED_COMPLETION_PREFIXES:
return self.CACHED_COMPLETIONS
compls = [x for x in COMPLETIONS if x.startswith(prefix) and
x != prefix]
self.CACHED_COMPLETION_PREFIXES = [prefix] + compls
# S3 can only handle lists, not iterables.
self.CACHED_COMPLETIONS = list(zip([prefix] + compls,
compls + [prefix]))
return self.CACHED_COMPLETIONS
class CycleCmdlineHistory(sublime_plugin.TextCommand):
HISTORY_INDEX = None
def run(self, edit, backwards=False):
if CycleCmdlineHistory.HISTORY_INDEX is None:
CycleCmdlineHistory.HISTORY_INDEX = -1 if backwards else 0
else:
CycleCmdlineHistory.HISTORY_INDEX += -1 if backwards else 1
if CycleCmdlineHistory.HISTORY_INDEX == len(EX_HISTORY['cmdline']) or \
CycleCmdlineHistory.HISTORY_INDEX < -len(EX_HISTORY['cmdline']):
CycleCmdlineHistory.HISTORY_INDEX = -1 if backwards else 0
self.view.erase(edit, sublime.Region(0, self.view.size()))
self.view.insert(edit, 0, \
EX_HISTORY['cmdline'][CycleCmdlineHistory.HISTORY_INDEX])
class HistoryIndexRestorer(sublime_plugin.EventListener):
def on_deactivated(self, view):
# Because views load asynchronously, do not restore history index
# .on_activated(), but here instead. Otherwise, the .score_selector()
# call won't yield the desired results.
if view.score_selector(0, 'text.excmdline') > 0:
CycleCmdlineHistory.HISTORY_INDEX = None
class WriteFsCompletion(sublime_plugin.TextCommand):
def run(self, edit, cmd, completion):
if self.view.score_selector(0, 'text.excmdline') == 0:
return
ViColonInput.interactive_call = False
self.view.sel().clear()
self.view.replace(edit, sublime.Region(0, self.view.size()),
cmd + ' ' + completion)
self.view.sel().add(sublime.Region(self.view.size()))
class FsCompletion(sublime_plugin.TextCommand):
# Last user-provided path string.
prefix = ''
frozen_dir = ''
is_stale = False
items = None
@staticmethod
def invalidate():
FsCompletion.prefix = ''
FsCompletion.frozen_dir = ''
FsCompletion.is_stale = True
FsCompletion.items = None
def run(self, edit):
if self.view.score_selector(0, 'text.excmdline') == 0:
return
state = VintageState(self.view)
FsCompletion.frozen_dir = (FsCompletion.frozen_dir or
(state.settings.vi['_cmdline_cd'] + '/'))
cmd, prefix, only_dirs = parse(self.view.substr(self.view.line(0)))
if not cmd:
return
if not (FsCompletion.prefix or FsCompletion.items) and prefix:
FsCompletion.prefix = prefix
FsCompletion.is_stale = True
if prefix == '..':
FsCompletion.prefix = '../'
self.view.run_command('write_fs_completion', {
'cmd': cmd,
'completion': '../'})
if prefix == '~':
path = os.path.expanduser(prefix) + '/'
FsCompletion.prefix = path
self.view.run_command('write_fs_completion', {
'cmd': cmd,
'completion': path})
return
if (not FsCompletion.items) or FsCompletion.is_stale:
FsCompletion.items = iter_paths(from_dir=FsCompletion.frozen_dir,
prefix=FsCompletion.prefix,
only_dirs=only_dirs)
FsCompletion.is_stale = False
try:
self.view.run_command('write_fs_completion', {
'cmd': cmd,
'completion': next(FsCompletion.items)
})
except StopIteration:
FsCompletion.items = iter_paths(prefix=FsCompletion.prefix,
from_dir=FsCompletion.frozen_dir,
only_dirs=only_dirs)
self.view.run_command('write_fs_completion', {
'cmd': cmd,
'completion': FsCompletion.prefix
})
class ViSettingCompletion(sublime_plugin.TextCommand):
# Last user-provided path string.
prefix = ''
is_stale = False
items = None
@staticmethod
def invalidate():
ViSettingCompletion.prefix = ''
is_stale = True
items = None
def run(self, edit):
if self.view.score_selector(0, 'text.excmdline') == 0:
return
cmd, prefix, _ = parse_for_setting(self.view.substr(self.view.line(0)))
if not cmd:
return
if (ViSettingCompletion.prefix is None) and prefix:
ViSettingCompletion.prefix = prefix
ViSettingCompletion.is_stale = True
elif ViSettingCompletion.prefix is None:
ViSettingCompletion.items = iter_settings('')
ViSettingCompletion.is_stale = False
if not ViSettingCompletion.items or ViSettingCompletion.is_stale:
ViSettingCompletion.items = iter_settings(ViSettingCompletion.prefix)
ViSettingCompletion.is_stale = False
try:
self.view.run_command('write_fs_completion',
{'cmd': cmd,
'completion': next(ViSettingCompletion.items)})
except StopIteration:
try:
ViSettingCompletion.items = iter_settings(ViSettingCompletion.prefix)
self.view.run_command('write_fs_completion',
{'cmd': cmd,
'completion': next(ViSettingCompletion.items)})
except StopIteration:
return
class CmdlineContextProvider(sublime_plugin.EventListener):
"""
Provides contexts for the cmdline input panel.
"""
def on_query_context(self, view, key, operator, operand, match_all):
if view.score_selector(0, 'text.excmdline') == 0:
return
if key == 'vi_cmdline_at_fs_completion':
value = wants_fs_completions(view.substr(view.line(0)))
value = value and view.sel()[0].b == view.size()
if operator == sublime.OP_EQUAL:
if operand == True:
return value
elif operand == False:
return not value
if key == 'vi_cmdline_at_setting_completion':
value = wants_setting_completions(view.substr(view.line(0)))
value = value and view.sel()[0].b == view.size()
if operator == sublime.OP_EQUAL:
if operand == True:
return value
elif operand == False:
return not value