1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 13:00:04 +08:00

fix(cmdlinemenu): fix cmdlinemenu lua api

This commit is contained in:
Eric Wong 2023-08-06 11:45:19 +08:00 committed by GitHub
parent e90c1a9d87
commit 6b4f50a203
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 32 deletions

View File

@ -79,6 +79,9 @@ if has('nvim-0.9.0')
endfunction endfunction
function! SpaceVim#plugins#runner#run_task(task) abort function! SpaceVim#plugins#runner#run_task(task) abort
lua require("spacevim.plugin.runner").run_task(
\ require("spacevim").eval("a:task")
\ )
endfunction endfunction

View File

@ -1,19 +1,17 @@
local M = {} local M = {}
local Key = require('spacevim.api').import('vim.keys')
M.__vim = require('spacevim.api').import('vim')
local function parse_input(char) local function echo(str)
if char == 27 then vim.api.nvim_echo({{str, 'Normal'}}, false, {})
return ''
else
return char
end
end end
local function next_item(list, item) local function next_item(list, item)
local id = vim.fn.index(list, item) local id = vim.fn.index(list, item)
if id == #list then if id == #list - 1 then
return list[1] return list[1]
else else
return list[id] return list[id + 2]
end end
end end
@ -28,14 +26,20 @@ end
local function parse_items(items) local function parse_items(items)
local is = {} local is = {}
for _, item in pairs(items) do local id = 1
local id = vim.fn.index(items, item) + 1 for _, item in ipairs(items) do
is[id] = item is[id] = item
is[id][1] = '(' .. id .. ')' .. item[1] is[id][1] = '(' .. id .. ')' .. item[1]
id = id + 1
end end
return is return is
end end
local function list_keys(l)
if #l == 0 then return {} end
return vim.fn.range(1, #l)
end
function M.menu(items) function M.menu(items)
local cancelled = false local cancelled = false
local saved_more = vim.o.more local saved_more = vim.o.more
@ -44,48 +48,47 @@ function M.menu(items)
items = parse_items(items) items = parse_items(items)
vim.o.cmdheight = #items + 1 vim.o.cmdheight = #items + 1
vim.cmd('redrawstatus!') vim.cmd('redrawstatus!')
local selected = '1' local selected = 1
local exit = false local exit = false
local indent = string.rep(' ', 7, '') local indent = string.rep(' ', 7, '')
while not exit do while not exit do
local menu = 'Cmdline menu: Use j/k/enter and the shortcuts indicated\n' local menu = 'Cmdline menu: Use j/k/enter and the shortcuts indicated\n'
for id, _ in pairs(items) do local id = 1
local m = items[id] for _, _ in pairs(items) do
if type(m) == 'table' then
m = m[1]
end
if id == selected then if id == selected then
menu = menu .. indent .. '>' .. items[id][1] .. '\n' menu = menu .. indent .. '>' .. items[id][1] .. '\n'
else else
menu = menu .. indent .. ' ' .. items[id][1] .. '\n' menu = menu .. indent .. ' ' .. items[id][1] .. '\n'
end end
id = id + 1
end end
vim.cmd('redraw!') vim.cmd('redraw!')
vim.api.nvim_echo({ { string.sub(menu, 1, #menu - 2), 'Nornal' } }, false, {}) echo(string.sub(menu, 1, #menu - 1))
local nr = vim.fn.getchar() local char = M.__vim.getchar()
if parse_input(nr) == #'' or nr == 3 then if char == Key.t('<Esc>') or char == Key.t('<C-c>') then
exit = false exit = true
cancelled = true cancelled = true
vim.cmd('normal! :') vim.cmd('normal! :')
elseif vim.fn.index(vim.fn.keys(items), vim.fn.nr2char(nr)) ~= -1 or nr == 13 then elseif vim.fn.index(list_keys(items), tonumber(char)) ~= -1 or char == Key.t('<Cr>') then
if nr ~= 13 then if char ~= Key.t('<Cr>') then
selected = vim.fn.nr2char(nr) selected = tonumber(char, 10)
end end
local value = items[selected][1] local value = items[selected][2]
vim.cmd('normal! :') vim.cmd('normal! :')
if vim.fn.type(value) == 2 then if type(value) == "function" then
local args = vim.fn.get(items[selected], 2, {}) local args = items[selected][3] or {}
pcall(value, unpack(args)) local ok, err = pcall(value, unpack(args))
if not ok then print(err) end
elseif type(value) == 'string' then elseif type(value) == 'string' then
vim.cmd(value) vim.cmd(value)
end end
exit = true exit = true
elseif vim.fn.nr2char(nr) == 'j' or nr == 9 then elseif char == 'j' or char == Key.t('<Tab>') then
selected = next_item(vim.fn.keys(items), selected) selected = next_item(list_keys(items), selected)
vim.cmd('normal! :') vim.cmd('normal! :')
elseif vim.fn.nr2char(nr) == 'k' then -- or nr == "\<S-Tab>" elseif char == 'k' or char == Key.t('<S-Tab>') then
selected = previous_item(vim.fn.keys(items), selected) selected = previous_item(list_keys(items), selected)
vim.cmd('normal! :') vim.cmd('normal! :')
else else
vim.cmd('normal! :') vim.cmd('normal! :')
@ -95,7 +98,7 @@ function M.menu(items)
vim.o.cmdheight = saved_cmdheight vim.o.cmdheight = saved_cmdheight
vim.cmd('redraw!') vim.cmd('redraw!')
if cancelled then if cancelled then
vim.api.nvim_echo({ { 'cancelled!', 'Normal' } }, false, {}) echo('cancelled!')
end end
end end