From 3da309e1b2ccd223e61124f94021f50799b41c09 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 3 Mar 2024 22:40:18 +0800 Subject: [PATCH] feat(git): add vim cmdline parser function --- bundle/git.vim/lua/git/init.lua | 12 ++++++-- bundle/git.vim/plugin/git.vim | 2 +- lua/spacevim/api/vim/argv.lua | 51 +++++++++++++++++++++++++++++++++ test/lua/api/vim/argv.vader | 8 ++++++ 4 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 lua/spacevim/api/vim/argv.lua create mode 100644 test/lua/api/vim/argv.vader diff --git a/bundle/git.vim/lua/git/init.lua b/bundle/git.vim/lua/git/init.lua index 49e33cd7c..9eb0de60d 100644 --- a/bundle/git.vim/lua/git/init.lua +++ b/bundle/git.vim/lua/git/init.lua @@ -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 diff --git a/bundle/git.vim/plugin/git.vim b/bundle/git.vim/plugin/git.vim index 8af8b2262..789f50299 100644 --- a/bundle/git.vim/plugin/git.vim +++ b/bundle/git.vim/plugin/git.vim @@ -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() + command! -nargs=+ -complete=custom,git#complete Git lua require('git').run() else "" " Run git command asynchronously diff --git a/lua/spacevim/api/vim/argv.lua b/lua/spacevim/api/vim/argv.lua new file mode 100644 index 000000000..bb8906ae7 --- /dev/null +++ b/lua/spacevim/api/vim/argv.lua @@ -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 diff --git a/test/lua/api/vim/argv.vader b/test/lua/api/vim/argv.vader new file mode 100644 index 000000000..ec22e51fc --- /dev/null +++ b/test/lua/api/vim/argv.vader @@ -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 + +