API: Context

This commit is contained in:
Abdelhakeem 2019-07-17 22:00:50 +02:00 committed by Justin M. Keyes
parent 411a06c8b6
commit a80f691a6a
2 changed files with 79 additions and 0 deletions

View File

@ -11,6 +11,7 @@ local meth_pcall = helpers.meth_pcall
local meths = helpers.meths local meths = helpers.meths
local ok, nvim_async, feed = helpers.ok, helpers.nvim_async, helpers.feed local ok, nvim_async, feed = helpers.ok, helpers.nvim_async, helpers.feed
local os_name = helpers.os_name local os_name = helpers.os_name
local parse_context = helpers.parse_context
local request = helpers.request local request = helpers.request
local source = helpers.source local source = helpers.source
local next_msg = helpers.next_msg local next_msg = helpers.next_msg
@ -680,6 +681,65 @@ describe('API', function()
end) end)
end) end)
describe('nvim_get_context', function()
it('returns context dictionary of current editor state', function()
local ctx_items = {'regs', 'jumps', 'buflist', 'gvars'}
eq({}, parse_context(nvim('get_context', ctx_items)))
feed('i1<cr>2<cr>3<c-[>ddddddqahjklquuu')
feed('gg')
feed('G')
command('edit! BUF1')
command('edit BUF2')
nvim('set_var', 'one', 1)
nvim('set_var', 'Two', 2)
nvim('set_var', 'THREE', 3)
local expected_ctx = {
['regs'] = {
{['rt'] = 1, ['rc'] = {'1'}, ['n'] = 49, ['ru'] = true},
{['rt'] = 1, ['rc'] = {'2'}, ['n'] = 50},
{['rt'] = 1, ['rc'] = {'3'}, ['n'] = 51},
{['rc'] = {'hjkl'}, ['n'] = 97},
},
['jumps'] = eval(([[
filter(map(add(
getjumplist()[0], { 'bufnr': bufnr('%'), 'lnum': getcurpos()[1] }),
'filter(
{ "f": expand("#".v:val.bufnr.":p"), "l": v:val.lnum },
{ k, v -> k != "l" || v != 1 })'), '!empty(v:val.f)')
]]):gsub('\n', '')),
['buflist'] = eval([[
filter(map(getbufinfo(), '{ "f": v:val.name }'), '!empty(v:val.f)')
]]),
['gvars'] = {{'one', 1}, {'Two', 2}, {'THREE', 3}},
}
eq(expected_ctx, parse_context(nvim('get_context', ctx_items)))
end)
end)
describe('nvim_load_context', function()
it('sets current editor state to given context dictionary', function()
local ctx_items = {'regs', 'jumps', 'buflist', 'gvars'}
eq({}, parse_context(nvim('get_context', ctx_items)))
nvim('set_var', 'one', 1)
nvim('set_var', 'Two', 2)
nvim('set_var', 'THREE', 3)
local ctx = nvim('get_context', ctx_items)
nvim('set_var', 'one', 'a')
nvim('set_var', 'Two', 'b')
nvim('set_var', 'THREE', 'c')
eq({'a', 'b' ,'c'}, eval('[g:one, g:Two, g:THREE]'))
nvim('load_context', ctx)
eq({1, 2 ,3}, eval('[g:one, g:Two, g:THREE]'))
end)
end)
describe('nvim_replace_termcodes', function() describe('nvim_replace_termcodes', function()
it('escapes K_SPECIAL as K_SPECIAL KS_SPECIAL KE_FILLER', function() it('escapes K_SPECIAL as K_SPECIAL KS_SPECIAL KE_FILLER', function()
eq('\128\254X', helpers.nvim('replace_termcodes', '\128', true, true, true)) eq('\128\254X', helpers.nvim('replace_termcodes', '\128', true, true, true))

View File

@ -13,6 +13,8 @@ local check_cores = global_helpers.check_cores
local check_logs = global_helpers.check_logs local check_logs = global_helpers.check_logs
local dedent = global_helpers.dedent local dedent = global_helpers.dedent
local eq = global_helpers.eq local eq = global_helpers.eq
local filter = global_helpers.filter
local map = global_helpers.map
local ok = global_helpers.ok local ok = global_helpers.ok
local sleep = global_helpers.sleep local sleep = global_helpers.sleep
local tbl_contains = global_helpers.tbl_contains local tbl_contains = global_helpers.tbl_contains
@ -763,6 +765,22 @@ local function load_adjust(num)
return math.ceil(num * load_factor) return math.ceil(num * load_factor)
end end
local function parse_context(ctx)
local parsed = {}
for _, item in ipairs({'regs', 'jumps', 'buflist', 'gvars'}) do
parsed[item] = filter(function(v)
return type(v) == 'table'
end, nvim_call('msgpackparse', ctx[item]))
end
parsed['buflist'] = parsed['buflist'][1]
return map(function(v)
if #v == 0 then
return nil
end
return v
end, parsed)
end
local module = { local module = {
NIL = mpack.NIL, NIL = mpack.NIL,
alter_slashes = alter_slashes, alter_slashes = alter_slashes,
@ -810,6 +828,7 @@ local module = {
nvim_prog_abs = nvim_prog_abs, nvim_prog_abs = nvim_prog_abs,
nvim_set = nvim_set, nvim_set = nvim_set,
os_name = os_name, os_name = os_name,
parse_context = parse_context,
pathroot = pathroot, pathroot = pathroot,
pending_win32 = pending_win32, pending_win32 = pending_win32,
prepend_argv = prepend_argv, prepend_argv = prepend_argv,