1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-02 20:30:05 +08:00

feat(git): add vim cmdline parser function

This commit is contained in:
Eric Wong 2024-03-03 22:40:18 +08:00
parent e341ee2971
commit 3da309e1b2
4 changed files with 70 additions and 3 deletions

View File

@ -1,6 +1,7 @@
local M = {}
local log = require('git.log')
local argv_parser = require('spacevim.api.vim.argv')
local cmds = {
'add',
@ -35,7 +36,14 @@ end
update_cmd()
function M.run(command, ...)
function M.run(cmdline)
-- log.debug('cmdlien:' .. cmdline)
local argv = argv_parser.parser(cmdline)
-- log.debug('argvs:' .. vim.inspect(argv))
local command = table.remove(argv, 1)
if not supported_commands[command] then
vim.api.nvim_echo(
@ -45,7 +53,7 @@ function M.run(command, ...)
)
return
end
local argv = { ... }
local ok, cmd = pcall(require, 'git.command.' .. command)
if ok then
if type(cmd.run) == 'function' then

View File

@ -10,7 +10,7 @@ let g:loaded_git = 1
if has('nvim-0.9.0')
command! -nargs=+ -complete=custom,git#complete Git lua require('git').run(<f-args>)
command! -nargs=+ -complete=custom,git#complete Git lua require('git').run(<q-args>)
else
""
" Run git command asynchronously

View File

@ -0,0 +1,51 @@
--=============================================================================
-- argv.lua --- cmdline to argv
-- Copyright (c) 2016-2022 Wang Shidong & Contributors
-- Author: Wang Shidong < wsdjeg@outlook.com >
-- URL: https://spacevim.org
-- License: GPLv3
--=============================================================================
local M = {}
local str = require('spacevim.api.data.string')
function M.parser(cmdline)
local argvs = {}
local argv = ''
local escape = false
local isquote = false
for _, c in ipairs(str.string2chars(cmdline)) do
if not escape and not isquote and c == ' ' then
if #argv > 0 then
table.insert(argvs, argv)
argv = ''
end
elseif not escape and isquote and c == '"' then
isquote = false
table.insert(argvs, argv)
argv = ''
elseif not escape and not isquote and c == '"' then
isquote = true
elseif not escape and c == '\\' then
escape = true
elseif escape and c == '"' then
argv = argv .. '"'
escape = false
else
argv = argv .. c
end
end
if argv ~= '' then
table.insert(argvs, argv)
end
return argvs
end
return M

View File

@ -0,0 +1,8 @@
Execute ( SpaceVim lua api: vim.keys.t(str) ):
if has('nvim-0.5.0')
lua spacevim_api_argv = require('spacevim.api').import('vim.argv')
AssertEqual luaeval("spacevim_api_argv.parser('a b \"c d\"')"), ['a', 'b', 'c d']
AssertEqual luaeval("spacevim_api_argv.parser('a b \"c \\\\\" d\"')"), ['a', 'b', 'c " d']
endif