mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-02-02 22:30:04 +08:00
feat(git): add vim cmdline parser function
This commit is contained in:
parent
e341ee2971
commit
3da309e1b2
@ -1,6 +1,7 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local log = require('git.log')
|
local log = require('git.log')
|
||||||
|
local argv_parser = require('spacevim.api.vim.argv')
|
||||||
|
|
||||||
local cmds = {
|
local cmds = {
|
||||||
'add',
|
'add',
|
||||||
@ -35,7 +36,14 @@ end
|
|||||||
update_cmd()
|
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
|
if not supported_commands[command] then
|
||||||
vim.api.nvim_echo(
|
vim.api.nvim_echo(
|
||||||
@ -45,7 +53,7 @@ function M.run(command, ...)
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local argv = { ... }
|
|
||||||
local ok, cmd = pcall(require, 'git.command.' .. command)
|
local ok, cmd = pcall(require, 'git.command.' .. command)
|
||||||
if ok then
|
if ok then
|
||||||
if type(cmd.run) == 'function' then
|
if type(cmd.run) == 'function' then
|
||||||
|
@ -10,7 +10,7 @@ let g:loaded_git = 1
|
|||||||
|
|
||||||
|
|
||||||
if has('nvim-0.9.0')
|
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
|
else
|
||||||
""
|
""
|
||||||
" Run git command asynchronously
|
" Run git command asynchronously
|
||||||
|
51
lua/spacevim/api/vim/argv.lua
Normal file
51
lua/spacevim/api/vim/argv.lua
Normal 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
|
8
test/lua/api/vim/argv.vader
Normal file
8
test/lua/api/vim/argv.vader
Normal 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
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user