2014-11-06 19:54:49 -07:00
|
|
|
require('coxpcall')
|
2014-10-08 08:56:28 -07:00
|
|
|
local Loop = require('nvim.loop')
|
|
|
|
local MsgpackStream = require('nvim.msgpack_stream')
|
|
|
|
local AsyncSession = require('nvim.async_session')
|
|
|
|
local Session = require('nvim.session')
|
|
|
|
|
|
|
|
local nvim_prog = os.getenv('NVIM_PROG') or 'build/bin/nvim'
|
2014-11-06 19:54:49 -07:00
|
|
|
local nvim_argv = {nvim_prog, '-u', 'NONE', '-i', 'NONE', '-N', '--embed'}
|
2014-10-08 08:56:28 -07:00
|
|
|
|
|
|
|
if os.getenv('VALGRIND') then
|
|
|
|
local log_file = os.getenv('VALGRIND_LOG') or 'valgrind-%p.log'
|
|
|
|
local valgrind_argv = {'valgrind', '-q', '--tool=memcheck',
|
|
|
|
'--leak-check=yes', '--track-origins=yes',
|
2014-11-06 21:37:34 -07:00
|
|
|
'--show-possibly-lost=no',
|
2014-10-08 08:56:28 -07:00
|
|
|
'--suppressions=.valgrind.supp',
|
|
|
|
'--log-file='..log_file}
|
|
|
|
if os.getenv('VALGRIND_GDB') then
|
|
|
|
table.insert(valgrind_argv, '--vgdb=yes')
|
|
|
|
table.insert(valgrind_argv, '--vgdb-error=0')
|
|
|
|
end
|
|
|
|
local len = #valgrind_argv
|
|
|
|
for i = 1, #nvim_argv do
|
|
|
|
valgrind_argv[i + len] = nvim_argv[i]
|
|
|
|
end
|
|
|
|
nvim_argv = valgrind_argv
|
|
|
|
end
|
|
|
|
|
2014-11-06 19:54:49 -07:00
|
|
|
local session, loop_running, last_error
|
2014-10-08 08:56:28 -07:00
|
|
|
|
|
|
|
local function request(method, ...)
|
|
|
|
local status, rv = session:request(method, ...)
|
|
|
|
if not status then
|
2014-10-27 14:34:42 -07:00
|
|
|
if loop_running then
|
|
|
|
last_error = rv[2]
|
|
|
|
session:stop()
|
|
|
|
else
|
|
|
|
error(rv[2])
|
|
|
|
end
|
2014-10-08 08:56:28 -07:00
|
|
|
end
|
2014-10-23 14:20:33 -07:00
|
|
|
-- Make sure this will only return after all buffered characters have been
|
|
|
|
-- processed
|
|
|
|
session:request('vim_eval', '1')
|
2014-10-08 08:56:28 -07:00
|
|
|
return rv
|
|
|
|
end
|
|
|
|
|
2014-10-08 09:56:01 -07:00
|
|
|
local function next_message()
|
|
|
|
return session:next_message()
|
|
|
|
end
|
|
|
|
|
2014-11-06 19:54:49 -07:00
|
|
|
local function call_and_stop_on_error(...)
|
|
|
|
local status, result = copcall(...)
|
|
|
|
if not status then
|
|
|
|
session:stop()
|
|
|
|
last_error = result
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2014-10-08 09:56:01 -07:00
|
|
|
local function run(request_cb, notification_cb, setup_cb)
|
2014-11-06 19:54:49 -07:00
|
|
|
|
|
|
|
local function on_request(method, args)
|
|
|
|
return call_and_stop_on_error(request_cb, method, args)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function on_notification(method, args)
|
|
|
|
call_and_stop_on_error(notification_cb, method, args)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function on_setup()
|
|
|
|
call_and_stop_on_error(setup_cb)
|
|
|
|
end
|
|
|
|
|
2014-10-27 14:34:42 -07:00
|
|
|
loop_running = true
|
2014-11-06 19:54:49 -07:00
|
|
|
session:run(on_request, on_notification, on_setup)
|
2014-10-27 14:34:42 -07:00
|
|
|
loop_running = false
|
|
|
|
if last_error then
|
|
|
|
local err = last_error
|
|
|
|
last_error = nil
|
|
|
|
error(err)
|
|
|
|
end
|
2014-10-08 09:56:01 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local function stop()
|
|
|
|
session:stop()
|
|
|
|
end
|
|
|
|
|
2014-10-08 08:56:28 -07:00
|
|
|
local function nvim_command(cmd)
|
|
|
|
request('vim_command', cmd)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function nvim_eval(expr)
|
|
|
|
return request('vim_eval', expr)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function nvim_feed(input, mode)
|
|
|
|
mode = mode or ''
|
|
|
|
request('vim_feedkeys', input, mode)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function buffer_slice(start, stop, buffer_idx)
|
|
|
|
local include_end = false
|
|
|
|
if not stop then
|
|
|
|
stop = -1
|
|
|
|
include_end = true
|
|
|
|
end
|
|
|
|
local buffer = request('vim_get_buffers')[buffer_idx or 1]
|
|
|
|
local slice = request('buffer_get_line_slice', buffer, start or 0, stop,
|
|
|
|
true, include_end)
|
|
|
|
return table.concat(slice, '\n')
|
|
|
|
end
|
|
|
|
|
|
|
|
local function nvim_replace_termcodes(input)
|
2014-11-06 19:54:49 -07:00
|
|
|
return request('vim_replace_termcodes', input, false, true, true)
|
2014-10-08 08:56:28 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local function dedent(str)
|
|
|
|
-- find minimum common indent across lines
|
|
|
|
local indent = nil
|
|
|
|
for line in str:gmatch('[^\n]+') do
|
|
|
|
local line_indent = line:match('^%s+') or ''
|
|
|
|
if indent == nil or #line_indent < #indent then
|
|
|
|
indent = line_indent
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if #indent == 0 then
|
|
|
|
-- no minimum common indent
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
-- create a pattern for the indent
|
|
|
|
indent = indent:gsub('%s', '%%s')
|
|
|
|
-- strip it from the first line
|
|
|
|
str = str:gsub('^'..indent, '')
|
|
|
|
-- strip it from the remaining lines
|
|
|
|
str = str:gsub('[\n]'..indent, '\n')
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
|
2014-09-29 05:43:52 -07:00
|
|
|
local function feed(...)
|
|
|
|
for _, v in ipairs({...}) do
|
|
|
|
nvim_feed(nvim_replace_termcodes(dedent(v)))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-06 19:54:49 -07:00
|
|
|
local function rawfeed(...)
|
2014-09-29 05:43:52 -07:00
|
|
|
for _, v in ipairs({...}) do
|
|
|
|
nvim_feed(dedent(v), 'nt')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-06 19:54:49 -07:00
|
|
|
local function clear()
|
|
|
|
if session then
|
|
|
|
session:request('vim_command', 'qa!')
|
|
|
|
end
|
|
|
|
local loop = Loop.new()
|
|
|
|
local msgpack_stream = MsgpackStream.new(loop)
|
|
|
|
local async_session = AsyncSession.new(msgpack_stream)
|
|
|
|
session = Session.new(async_session)
|
|
|
|
loop:spawn(nvim_argv)
|
|
|
|
end
|
|
|
|
|
2014-09-29 05:43:52 -07:00
|
|
|
local function insert(...)
|
|
|
|
nvim_feed('i', 'nt')
|
|
|
|
rawfeed(...)
|
|
|
|
nvim_feed(nvim_replace_termcodes('<ESC>'), 'nt')
|
|
|
|
end
|
|
|
|
|
|
|
|
local function execute(...)
|
|
|
|
for _, v in ipairs({...}) do
|
|
|
|
if v:sub(1, 1) ~= '/' then
|
|
|
|
-- not a search command, prefix with colon
|
|
|
|
nvim_feed(':', 'nt')
|
|
|
|
end
|
|
|
|
nvim_feed(v, 'nt')
|
|
|
|
nvim_feed(nvim_replace_termcodes('<CR>'), 'nt')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-01 05:31:57 -07:00
|
|
|
local function eq(expected, actual)
|
|
|
|
return assert.are.same(expected, actual)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function neq(expected, actual)
|
|
|
|
return assert.are_not.same(expected, actual)
|
|
|
|
end
|
|
|
|
|
2014-09-29 05:43:52 -07:00
|
|
|
local function expect(contents, first, last, buffer_index)
|
2014-10-08 09:56:01 -07:00
|
|
|
return eq(dedent(contents), buffer_slice(first, last, buffer_index))
|
2014-09-29 05:43:52 -07:00
|
|
|
end
|
|
|
|
|
2014-10-08 09:56:01 -07:00
|
|
|
local function ok(expr)
|
|
|
|
assert.is_true(expr)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function nvim(method, ...)
|
|
|
|
return request('vim_'..method, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function buffer(method, ...)
|
|
|
|
return request('buffer_'..method, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function window(method, ...)
|
|
|
|
return request('window_'..method, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function tabpage(method, ...)
|
|
|
|
return request('tabpage_'..method, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function curbuf(method, ...)
|
|
|
|
local buf = nvim('get_current_buffer')
|
|
|
|
if not method then
|
|
|
|
return buf
|
|
|
|
end
|
|
|
|
return buffer(method, buf, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function curbuf_contents()
|
|
|
|
return table.concat(curbuf('get_line_slice', 0, -1, true, true), '\n')
|
|
|
|
end
|
|
|
|
|
|
|
|
local function curwin(method, ...)
|
|
|
|
local win = nvim('get_current_window')
|
|
|
|
if not method then
|
|
|
|
return win
|
|
|
|
end
|
|
|
|
return window(method, win, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function curtab(method, ...)
|
|
|
|
local tab = nvim('get_current_tabpage')
|
|
|
|
if not method then
|
|
|
|
return tab
|
|
|
|
end
|
|
|
|
return tabpage(method, tab, ...)
|
|
|
|
end
|
|
|
|
|
2014-11-06 19:54:49 -07:00
|
|
|
clear()
|
2014-10-27 14:34:42 -07:00
|
|
|
|
2014-09-29 05:43:52 -07:00
|
|
|
return {
|
|
|
|
clear = clear,
|
2014-10-23 14:20:33 -07:00
|
|
|
dedent = dedent,
|
2014-09-29 05:43:52 -07:00
|
|
|
rawfeed = rawfeed,
|
|
|
|
insert = insert,
|
|
|
|
feed = feed,
|
|
|
|
execute = execute,
|
2014-10-27 14:34:42 -07:00
|
|
|
eval = nvim_eval,
|
|
|
|
command = nvim_command,
|
2014-10-08 09:56:01 -07:00
|
|
|
request = request,
|
|
|
|
next_message = next_message,
|
|
|
|
run = run,
|
|
|
|
stop = stop,
|
2014-10-01 05:31:57 -07:00
|
|
|
eq = eq,
|
|
|
|
neq = neq,
|
2014-10-08 09:56:01 -07:00
|
|
|
expect = expect,
|
|
|
|
ok = ok,
|
|
|
|
nvim = nvim,
|
|
|
|
buffer = buffer,
|
|
|
|
window = window,
|
|
|
|
tabpage = tabpage,
|
|
|
|
curbuf = curbuf,
|
|
|
|
curwin = curwin,
|
|
|
|
curtab = curtab,
|
|
|
|
curbuf_contents = curbuf_contents
|
2014-09-29 05:43:52 -07:00
|
|
|
}
|