-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathneo.py
398 lines (343 loc) · 13.4 KB
/
neo.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import contextlib
import os
import queue
import sublime
import sys
import threading
import time
import traceback
from .lib import neovim
from .lib import util
from . import settings
from .screen import Screen
# os.environ['NVIM_LOG_FILE'] = '/Users/aegis/.nvimlog'
if not '_loaded' in globals():
NEOVIM_PATH = None
_loaded = False
_loading = False
INSERT_MODES = ['i', 'R']
VISUAL_MODES = ['V', 'v', '\x16']
MODES = {
'n': 'normal',
'c': 'command',
# ex mode goes and stays "not ready"
# think I need UI hook to support it for now
'i': 'insert',
'R': 'replace',
'v': 'visual',
'V': 'visual line',
'\x16': 'visual block',
# TODO: select, vreplace?
}
def plugin_loaded():
global NEOVIM_PATH
settings.load()
NEOVIM_PATH = sublime.load_settings('ActualVim.sublime-settings').get('neovim_path')
if not NEOVIM_PATH:
NEOVIM_PATH = util.which('nvim')
if sys.platform == 'win32':
if not NEOVIM_PATH:
candidates = [
r'C:\Program Files\Neovim',
r'C:\Program Files (x86)\Neovim',
r'C:\Neovim',
]
chocoroot = os.getenv('ChocolateyBinRoot')
if chocoroot: candidates.insert(0, os.path.join(chocoroot, r'\neovim\Neovim'))
else: candidates.insert(0, r'C:\tools\neovim\Neovim')
for c in candidates:
path = os.path.join(c, r'bin\nvim.exe')
if os.path.exists(path):
NEOVIM_PATH = path
break
elif os.path.isdir(NEOVIM_PATH):
for c in [r'bin\nvim.exe', 'nvim.exe']:
path = os.path.join(NEOVIM_PATH, c)
if os.path.exists(path):
NEOVIM_PATH = path
break
else:
NEOVIM_PATH = None
if not NEOVIM_PATH:
raise Exception('cannot find nvim executable')
print('ActualVim: using nvim binary path:', NEOVIM_PATH)
global vim, _loaded, _loading
try:
start = time.time()
vim = Vim()
_loading = True
vim._setup()
_loaded = True
_loading = False
from .view import neovim_loaded
neovim_loaded()
print('ActualVim: nvim started in {:.2f}ms'.format((time.time() - start) * 1000))
except Exception:
print('ActualVim: Error during nvim setup.')
traceback.print_exc()
_loaded = False
_loading = False
vim = None
del vim
def plugin_unloaded():
from .view import neovim_unloaded
neovim_unloaded()
global vim, _loaded
if _loaded:
vim.nv.command('qa!', async=True)
vim = None
_loaded = False
class Vim:
def __init__(self, nv=None):
self.nv = nv
self.ready = threading.Lock()
self.status_lock = threading.Lock()
self.status_last = {}
self.status_dirty = True
self.av = None
self.width = 80
self.height = 24
def _setup(self):
self.screen = Screen()
self.views = {}
args = settings.get('neovim_args') or []
if not isinstance(args, list):
print('ActualVim: ignoring non-list ({}) args: {}'.format(type(args), repr(args)))
args = []
self.nv = neovim.attach('child', argv=[NEOVIM_PATH, '--embed', '-n'] + args)
# toss in <FocusGained> in case there's a blocking prompt on startup (like vimrc errors)
self.nv.input('<FocusGained>')
messages = self.nv.eval('execute("messages")').strip()
if messages:
print('ActualVim: nvim startup error:')
print('-'*20)
print(messages)
print('-'*20)
sublime.active_window().run_command('show_panel', {'panel': 'console'})
self._sem = threading.Semaphore(0)
self._thread = t = threading.Thread(target=self._event_loop)
t.daemon = True
t.start()
self._sem.acquire()
# set up UI (before anything else so we can see errors)
options = {
'ext_popupmenu': True,
'ext_cmdline': True,
'rgb': True,
}
self.nv.ui_attach(self.width, self.height, options)
# hidden buffers allow us to multiplex them
self.nv.options['hidden'] = True
# folds aren't implemented
self.cmd('set nofoldenable')
rpc_id = self.nv.channel_id
# set up buffer read/write commands
cmd = 'autocmd {{}} * :call rpcrequest({}, "{{}}", expand("<abuf>"), expand("<afile>"))'.format(rpc_id)
# self.cmd(cmd.format('BufWritePre', 'write_pre'))
self.cmd(cmd.format('BufReadCmd', 'read'))
self.cmd(cmd.format('BufWriteCmd', 'write'))
self.cmd(cmd.format('BufEnter', 'enter'))
def funcdef(prototype, body):
self.eval(r'''execute(":function! {} \n {} \n endfunction")'''.format(prototype, body))
# set up autocomplete from Sublime via completefunc (ctrl-x, ctrl-u)
# controlled via bufopts['completefunc'] in ActualVim settings
complete = r'''return rpcrequest({}, \"complete\", bufnr(\"%\"), a:findstart, a:base)'''.format(rpc_id)
funcdef('ActualVimComplete(findstart, base)', complete)
# FIXME: these just hang for now
funcdef('ActualVimWinCmd(name, args)', r'call rpcnotify({}, \"wincmd\", bufnr(\"%\"), a:name, a:args)'.format(rpc_id))
funcdef('ActualVimTextCmd(name, args)', r'call rpcnotify({}, \"textcmd\", bufnr(\"%\"), a:name, a:args)'.format(rpc_id))
funcdef('ActualVimAppCmd(name, args)', r'call rpcnotify({}, \"appcmd\", bufnr(\"%\"), a:name, a:args)'.format(rpc_id))
self.nvim_mode = False
try:
res = self.nv.request('nvim_get_mode')
if isinstance(res, dict):
self.nvim_mode = True
except neovim.api.NvimError:
pass
def _event_loop(self):
def on_notification(method, data):
# if vim exits, we might get a notification on the way out
if not (_loaded or _loading):
return
if method == 'redraw':
for cmd in data:
name, args = cmd[0], cmd[1:]
# TODO: allow subscribing to these
if name == 'bell' and self.av:
self.av.on_bell()
elif name in ('popupmenu_show', 'popupmenu_hide', 'popupmenu_select'):
self.av.on_popupmenu(name, args)
elif name in ('cmdline_show', 'cmdline_pos', 'cmdline_special_char', 'cmdline_hide',
'cmdline_block_show', 'cmdline_block_append', 'cmdline_block_hide'):
self.av.on_cmdline(name, args)
vim.screen.redraw(data)
if self.av:
self.av.on_redraw(data, vim.screen)
elif method == 'nvim_buf_lines_event':
buf, changedtick, start, end, lines, more = data
av = self.views.get(buf.number)
if av:
av.on_nvim_lines(changedtick, start, end, lines, more)
elif method == 'nvim_buf_changedtick_event':
buf, changedtick = data
av = self.views.get(buf.number)
if av:
av.on_nvim_changedtick(changedtick)
elif method == 'appcmd':
av = self.views.get(data[0])
if av:
av.on_appcmd(data[1], data[2])
elif method == 'wincmd':
av = self.views.get(data[0])
if av:
av.on_wincmd(data[1], data[2])
elif method == 'textcmd':
av = self.views.get(data[0])
if av:
av.on_textcmd(data[1], data[2])
def on_request(method, args):
# TODO: what if I need to handle requests that don't start with bufid?
bufid = int(args.pop(0))
av = self.views.get(bufid)
if not av:
# TODO: this spews on first "enter"
print('ActualVim: request "{}" failed: buf:{} has no view'.format(method, bufid))
return
if method == 'write':
# TODO: filename arg?
return av.on_write()
elif method == 'read':
# TODO: filename arg?
# TODO: pivot view?
pass
elif method == 'enter':
# TODO: focus view?
pass
elif method == 'complete':
return av.on_complete(args[0], args[1])
def on_setup():
self._sem.release()
self.nv.run_loop(on_request, on_notification, on_setup)
def cmd(self, *args, **kwargs):
return self.nv.command_output(*args, **kwargs)
def eval(self, *cmds, **kwargs):
if len(cmds) != 1:
cmd = '[' + (', '.join(cmds)) + ']'
else:
cmd = cmds[0]
return self.nv.eval(cmd, **kwargs)
def activate(self, av):
if self.av != av:
self.av = av
self.cmd('b! {:d}'.format(av.buf.number))
return True
return False
# buffer methods
def buf_new(self, view):
self.cmd('enew')
buf = max((b.number, b) for b in self.nv.buffers)[1]
buf.options['buftype'] = 'acwrite'
for k, v in settings.get('bufopts').items():
buf.options[k] = v
self.views[buf.number] = view
return buf
def buf_close(self, buf):
self.views.pop(buf.number, None)
self.cmd('bw! {:d}'.format(buf.number))
# neovim 'readiness' methods
# if you don't use check/force_ready and control your input/cmd interleaving, you'll hang all the time
def check_ready(self):
ready = self.ready.acquire(False)
if ready:
self.ready.release()
return ready
def force_ready(self):
for i in range(3):
if self.check_ready():
break
time.sleep(0.0001)
else:
self.nv.input('<c-\\><c-n>')
def press(self, key, onready=None):
self.status_dirty = True
mode_last = self.status_last.get('mode')
was_ready = self.ready.acquire(False)
ret = self.nv.input(key)
if self.nvim_mode:
res = self.nv.request('nvim_get_mode') or {}
ready = not res.get('blocking', True)
else:
ready = False
def tmp():
# need to acquire/release so ready lock doesn't get stuck
self.ready.acquire(False)
self.ready.release()
onready()
self.status(cb=tmp)
if ready:
self.ready.release()
return ret, ready
def status(self, update=True, force=False, cb=None):
# TODO: use nvim_atomic? we need to get sel, buf, mode, everything at once if possible
with self.status_lock:
if self.status_dirty and update or force:
items = {
'mode': 'mode()',
'modified': '&modified',
'expandtab': '&expandtab',
'ts': '&ts',
'changedtick': 'getbufvar(bufnr("%"), "changedtick")',
'wrap': '&wrap',
'cline': 'line(".") - 1',
'ccol': 'col(".") - 1',
'vline': 'line("v") - 1',
'vcol': 'col("v") - 1',
'wview': 'winsaveview()',
'wwidth': 'winwidth(winnr())',
'wheight': 'winheight(winnr())',
'screenrow': 'screenrow()',
'screencol': 'screencol()',
}
expr = '[' + (', '.join(items.values())) + ']'
def update(*a):
self.status_last = dict(zip(items.keys(), a[-1]))
self.status_dirty = False
if cb:
# callbacks aren't decoded automatically
self.status_last['mode'] = self.status_last['mode'].decode('utf8')
cb()
if cb:
self.eval(expr, cb=update)
else:
update(self.eval(expr))
return self.status_last
def setpos(self, expr, line, col):
return self.eval('setpos("{}", [0, {:d}, {:d}])'.format(expr, line, col))
def select(self, a, b=None, mode='v'):
if b is None:
if self.mode in VISUAL_MODES:
self.nv.input('<c-\\><c-n>')
self.status_dirty = True
self.eval('cursor({:d}, {:d})'.format(a[0], a[1]))
else:
special = mode.startswith('<c-')
if self.mode in VISUAL_MODES:
self.nv.input('<c-\\><c-n>')
self.status_dirty = True
self.setpos('.', *a)
if special:
self.cmd('exe "normal! \\{}"'.format(mode))
else:
self.cmd('normal! {}'.format(mode))
self.setpos('.', *b)
def resize(self, width, height):
w, h = int(width), int(height)
if w and h and (w != self.width or h != self.height) and self.check_ready():
self.width, self.height = w, h
self.nv.ui_try_resize(w, h)
@property
def mode(self):
return self.status()['mode']
@property
def status_line(self):
return self.screen[-1].strip()