diff options
author | bfredl <bjorn.linse@gmail.com> | 2022-06-29 09:12:01 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-29 09:12:01 +0200 |
commit | 21a1f1f552c1d79165cdb51e9e1297d4a4fe9eb6 (patch) | |
tree | 820cdbb0a08e643753960fe6be91f47f4e181e6f /src/nvim/api/command.c | |
parent | 01fc5097d3b6921fce5c159e5c772dc1590eb847 (diff) | |
parent | 606ec8b70874095a62289f1df3934eca78625742 (diff) | |
download | rneovim-21a1f1f552c1d79165cdb51e9e1297d4a4fe9eb6.tar.gz rneovim-21a1f1f552c1d79165cdb51e9e1297d4a4fe9eb6.tar.bz2 rneovim-21a1f1f552c1d79165cdb51e9e1297d4a4fe9eb6.zip |
Merge pull request #19133 from famiu/feat/api/cmd_support_filter
feat(api): make `nvim_parse_cmd` and `nvim_cmd` support :filter
Diffstat (limited to 'src/nvim/api/command.c')
-rw-r--r-- | src/nvim/api/command.c | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/src/nvim/api/command.c b/src/nvim/api/command.c index 80a5449d29..e6a055995e 100644 --- a/src/nvim/api/command.c +++ b/src/nvim/api/command.c @@ -49,6 +49,9 @@ /// - bar: (boolean) The "|" character is treated as a command separator and the double /// quote character (\") is treated as the start of a comment. /// - mods: (dictionary) |:command-modifiers|. +/// - filter: (dictionary) |:filter|. +/// - pattern: (string) Filter pattern. Empty string if there is no filter. +/// - force: (boolean) Whether filter is inverted or not. /// - silent: (boolean) |:silent|. /// - emsg_silent: (boolean) |:silent!|. /// - sandbox: (boolean) |:sandbox|. @@ -95,7 +98,6 @@ Dictionary nvim_parse_cmd(String str, Dictionary opts, Error *err) } goto end; } - vim_regfree(cmdinfo.cmdmod.cmod_filter_regmatch.regprog); // Parse arguments Array args = ARRAY_DICT_INIT; @@ -220,6 +222,14 @@ Dictionary nvim_parse_cmd(String str, Dictionary opts, Error *err) PUT(result, "nextcmd", CSTR_TO_OBJ((char *)ea.nextcmd)); Dictionary mods = ARRAY_DICT_INIT; + + Dictionary filter = ARRAY_DICT_INIT; + PUT(filter, "pattern", cmdinfo.cmdmod.cmod_filter_pat + ? CSTR_TO_OBJ(cmdinfo.cmdmod.cmod_filter_pat) + : STRING_OBJ(STATIC_CSTR_TO_STRING(""))); + PUT(filter, "force", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_filter_force)); + PUT(mods, "filter", DICTIONARY_OBJ(filter)); + PUT(mods, "silent", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_SILENT)); PUT(mods, "emsg_silent", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_ERRSILENT)); PUT(mods, "sandbox", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_SANDBOX)); @@ -257,6 +267,8 @@ Dictionary nvim_parse_cmd(String str, Dictionary opts, Error *err) PUT(magic, "file", BOOLEAN_OBJ(cmdinfo.magic.file)); PUT(magic, "bar", BOOLEAN_OBJ(cmdinfo.magic.bar)); PUT(result, "magic", DICTIONARY_OBJ(magic)); + + undo_cmdmod(&cmdinfo.cmdmod); end: xfree(cmdline); return result; @@ -513,6 +525,35 @@ String nvim_cmd(uint64_t channel_id, Dict(cmd) *cmd, Dict(cmd_opts) *opts, Error goto end; } + if (HAS_KEY(mods.filter)) { + if (mods.filter.type != kObjectTypeDictionary) { + VALIDATION_ERROR("'mods.filter' must be a Dictionary"); + } + + Dict(cmd_mods_filter) filter = { 0 }; + + if (!api_dict_to_keydict(&filter, KeyDict_cmd_mods_filter_get_field, + mods.filter.data.dictionary, err)) { + goto end; + } + + if (HAS_KEY(filter.pattern)) { + if (filter.pattern.type != kObjectTypeString) { + VALIDATION_ERROR("'mods.filter.pattern' must be a String"); + } + + OBJ_TO_BOOL(cmdinfo.cmdmod.cmod_filter_force, filter.force, false, "'mods.filter.force'"); + + // "filter! // is not no-op, so add a filter if either the pattern is non-empty or if filter + // is inverted. + if (*filter.pattern.data.string.data != NUL || cmdinfo.cmdmod.cmod_filter_force) { + cmdinfo.cmdmod.cmod_filter_pat = string_to_cstr(filter.pattern.data.string); + cmdinfo.cmdmod.cmod_filter_regmatch.regprog = vim_regcomp(cmdinfo.cmdmod.cmod_filter_pat, + RE_MAGIC); + } + } + } + if (HAS_KEY(mods.tab)) { if (mods.tab.type != kObjectTypeInteger || mods.tab.data.integer < 0) { VALIDATION_ERROR("'mods.tab' must be a non-negative Integer"); |