diff options
author | Tony Chen <tchen1998@gmail.com> | 2020-11-30 08:33:52 -0500 |
---|---|---|
committer | chentau <tchen1998@gmail.com> | 2021-01-26 17:04:32 -0800 |
commit | d95a465b4399c3c10b83925935ec5f4807d65b60 (patch) | |
tree | 5a356442adb3cd61483cf0dfb6958731d58b1249 | |
parent | 901dd79f6a5ee78a55d726abca868bebff117ca9 (diff) | |
download | rneovim-d95a465b4399c3c10b83925935ec5f4807d65b60.tar.gz rneovim-d95a465b4399c3c10b83925935ec5f4807d65b60.tar.bz2 rneovim-d95a465b4399c3c10b83925935ec5f4807d65b60.zip |
Don't show entire context when completing
-rw-r--r-- | src/nvim/ex_docmd.c | 1 | ||||
-rw-r--r-- | src/nvim/ex_getln.c | 10 | ||||
-rw-r--r-- | src/nvim/lua/executor.c | 20 | ||||
-rw-r--r-- | src/nvim/lua/vim.lua | 18 | ||||
-rw-r--r-- | test/functional/lua/command_line_completion_spec.lua | 47 | ||||
-rw-r--r-- | test/functional/viml/completion_spec.lua | 9 |
6 files changed, 63 insertions, 42 deletions
diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index 629c7df386..c1f0c09377 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -3682,7 +3682,6 @@ const char * set_one_cmd_context( case CMD_lua: xp->xp_context = EXPAND_LUA; - xp->xp_pattern = (char_u *)arg; break; default: diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 96cab0f110..8d10e98259 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -3946,6 +3946,12 @@ nextwild ( p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len), use_options, type); xfree(p1); + + // xp->xp_pattern might have been modified by ExpandOne (for example, + // in lua completion), so recompute the pattern index and length + i = (int)(xp->xp_pattern - ccline.cmdbuff); + xp->xp_pattern_len = (size_t)ccline.cmdpos - (size_t)i; + // Longest match: make sure it is not shorter, happens with :help. if (p2 != NULL && type == WILD_LONGEST) { for (j = 0; (size_t)j < xp->xp_pattern_len; j++) { @@ -3961,7 +3967,7 @@ nextwild ( } if (p2 != NULL && !got_int) { - difflen = (int)STRLEN(p2) - (int)xp->xp_pattern_len; + difflen = (int)STRLEN(p2) - (int)(xp->xp_pattern_len); if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen) { realloc_cmdbuff(ccline.cmdlen + difflen + 4); xp->xp_pattern = ccline.cmdbuff + i; @@ -5109,7 +5115,7 @@ ExpandFromContext ( } if (xp->xp_context == EXPAND_LUA) { ILOG("PAT %s", pat); - return nlua_expand_pat(pat, num_file, file); + return nlua_expand_pat(xp, pat, num_file, file); } regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 5d75a7dc0f..cefb0ee429 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1292,7 +1292,7 @@ static void nlua_add_treesitter(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL lua_setfield(lstate, -2, "_ts_parse_query"); } -int nlua_expand_pat(char_u *pat, int *num_results, char_u ***results) +int nlua_expand_pat(expand_T *xp, char_u *pat, int *num_results, char_u ***results) { lua_State *const lstate = nlua_enter(); int ret = OK; @@ -1307,10 +1307,11 @@ int nlua_expand_pat(char_u *pat, int *num_results, char_u ***results) // [ vim, vim._log_keystroke, buf ] lua_pushlstring(lstate, (const char *)pat, STRLEN(pat)); - if (lua_pcall(lstate, 1, 1, 0)) { + if (lua_pcall(lstate, 1, 2, 0) != 0) { nlua_error( lstate, _("Error executing vim._expand_pat: %.*s")); + return FAIL; } Error err = ERROR_INIT; @@ -1318,12 +1319,18 @@ int nlua_expand_pat(char_u *pat, int *num_results, char_u ***results) *num_results = 0; *results = NULL; - Array completions = nlua_pop_Array(lstate, &err); + int prefix_len = (int)nlua_pop_Integer(lstate, &err); if (ERROR_SET(&err)) { ret = FAIL; goto cleanup; } + Array completions = nlua_pop_Array(lstate, &err); + if (ERROR_SET(&err)) { + ret = FAIL; + goto cleanup_array; + } + garray_T result_array; ga_init(&result_array, (int)sizeof(char *), 80); for (size_t i = 0; i < completions.size; i++) { @@ -1331,7 +1338,7 @@ int nlua_expand_pat(char_u *pat, int *num_results, char_u ***results) if (v.type != kObjectTypeString) { ret = FAIL; - goto cleanup; + goto cleanup_array; } GA_APPEND( @@ -1340,12 +1347,15 @@ int nlua_expand_pat(char_u *pat, int *num_results, char_u ***results) vim_strsave((char_u *)v.data.string.data)); } + xp->xp_pattern += prefix_len; *results = result_array.ga_data; *num_results = result_array.ga_len; -cleanup: +cleanup_array: api_free_array(completions); +cleanup: + if (ret == FAIL) { ga_clear(&result_array); } diff --git a/src/nvim/lua/vim.lua b/src/nvim/lua/vim.lua index 3e601586ea..49c34c5b89 100644 --- a/src/nvim/lua/vim.lua +++ b/src/nvim/lua/vim.lua @@ -547,7 +547,7 @@ function vim._expand_pat(pat, env) if pat == '' then local result = vim.tbl_keys(env) table.sort(result) - return result + return result, 0 end -- TODO: We can handle spaces in [] ONLY. @@ -558,7 +558,7 @@ function vim._expand_pat(pat, env) -- Get the last part of the pattern local last_part = pat:match("[%w.:_%[%]'\"]+$") - if not last_part then return {} end + if not last_part then return {}, 0 end local parts, search_index = vim._expand_pat_get_parts(last_part) @@ -568,11 +568,11 @@ function vim._expand_pat(pat, env) local final_env = env for _, part in ipairs(parts) do if type(final_env) ~= 'table' then - return {} + return {}, 0 end -- Normally, we just have a string - -- Just attempt to get the string directly from the environment + -- Just attempt to get the string directly from the environment if type(part) == "string" then final_env = rawget(final_env, part) else @@ -584,32 +584,32 @@ function vim._expand_pat(pat, env) -- -> _G[MY_VAR] -> "api" local result_key = part[1] if not result_key then - return {} + return {}, 0 end local result = rawget(env, result_key) if result == nil then - return {} + return {}, 0 end final_env = rawget(final_env, result) end if not final_env then - return {} + return {}, 0 end end local result = vim.tbl_map(function(v) - return prefix_match_pat .. v + return v end, vim.tbl_filter(function(name) return string.find(name, match_pat) ~= nil end, vim.tbl_keys(final_env))) table.sort(result) - return result + return result, #prefix_match_pat end vim._expand_pat_get_parts = function(lua_string) diff --git a/test/functional/lua/command_line_completion_spec.lua b/test/functional/lua/command_line_completion_spec.lua index 9056f80fb5..31cd5cf02f 100644 --- a/test/functional/lua/command_line_completion_spec.lua +++ b/test/functional/lua/command_line_completion_spec.lua @@ -6,33 +6,34 @@ local funcs = helpers.funcs local exec_lua = helpers.exec_lua local get_completions = function(input, env) - return exec_lua("return vim._expand_pat(...)", '^' .. input, env) + return exec_lua("return {vim._expand_pat(...)}", '^' .. input, env) end local get_compl_parts = function(parts) - return funcs.luaeval("{vim._expand_pat_get_parts(_A)}", parts) + return exec_lua("return {vim._expand_pat_get_parts(...)}", parts) end before_each(clear) describe('nlua_expand_pat', function() it('should complete exact matches', function() - eq({'exact'}, get_completions('exact', { exact = true })) + eq({{'exact'}, 0}, get_completions('exact', { exact = true })) end) it('should return empty table when nothing matches', function() - eq({}, get_completions('foo', { bar = true })) + eq({{}, 0}, get_completions('foo', { bar = true })) end) it('should return nice completions with function call prefix', function() - eq({'print(FOO'}, get_completions('print(F', { FOO = true, bawr = true })) + eq({{'FOO'}, 6}, get_completions('print(F', { FOO = true, bawr = true })) end) it('should return keys for nested dictionaries', function() eq( - { - 'vim.api.nvim_buf_set_lines', - 'vim.api.nvim_buf_set_option' + {{ + 'nvim_buf_set_lines', + 'nvim_buf_set_option' + }, 8 }, get_completions('vim.api.nvim_buf_', { vim = { @@ -49,9 +50,10 @@ describe('nlua_expand_pat', function() it('it should work with colons', function() eq( - { - 'MyClass:bawr', - 'MyClass:baz', + {{ + 'bawr', + 'baz', + }, 8 }, get_completions('MyClass:b', { MyClass = { @@ -65,9 +67,10 @@ describe('nlua_expand_pat', function() it('should return keys for string reffed dictionaries', function() eq( - { - 'vim["api"].nvim_buf_set_lines', - 'vim["api"].nvim_buf_set_option' + {{ + 'nvim_buf_set_lines', + 'nvim_buf_set_option' + }, 11 }, get_completions('vim["api"].nvim_buf_', { vim = { @@ -84,9 +87,10 @@ describe('nlua_expand_pat', function() it('should return keys for string reffed dictionaries', function() eq( - { - 'vim["nested"]["api"].nvim_buf_set_lines', - 'vim["nested"]["api"].nvim_buf_set_option' + {{ + 'nvim_buf_set_lines', + 'nvim_buf_set_option' + }, 21 }, get_completions('vim["nested"]["api"].nvim_buf_', { vim = { @@ -105,9 +109,10 @@ describe('nlua_expand_pat', function() it('should be able to interpolate globals', function() eq( - { - 'vim[MY_VAR].nvim_buf_set_lines', - 'vim[MY_VAR].nvim_buf_set_option' + {{ + 'nvim_buf_set_lines', + 'nvim_buf_set_option' + }, 12 }, get_completions('vim[MY_VAR].nvim_buf_', { MY_VAR = "api", @@ -124,7 +129,7 @@ describe('nlua_expand_pat', function() end) it('should return everything if the input is of length 0', function() - eq({"other", "vim"}, get_completions('', { vim = true, other = true })) + eq({{"other", "vim"}, 0}, get_completions('', { vim = true, other = true })) end) describe('get_parts', function() diff --git a/test/functional/viml/completion_spec.lua b/test/functional/viml/completion_spec.lua index d59a1a3306..a4241fe5aa 100644 --- a/test/functional/viml/completion_spec.lua +++ b/test/functional/viml/completion_spec.lua @@ -3,6 +3,7 @@ local Screen = require('test.functional.ui.screen') local clear, feed = helpers.clear, helpers.feed local eval, eq, neq = helpers.eval, helpers.eq, helpers.neq local feed_command, source, expect = helpers.feed_command, helpers.source, helpers.expect +local funcs = helpers.funcs local curbufmeths = helpers.curbufmeths local command = helpers.command local meths = helpers.meths @@ -929,10 +930,10 @@ describe('completion', function() end) it('provides completion from `getcompletion()`', function() - eq({'vim'}, meths.call_function('getcompletion', {'vi', 'lua'})) - eq({'vim.api'}, meths.call_function('getcompletion', {'vim.ap', 'lua'})) - eq({'vim.tbl_filter'}, meths.call_function('getcompletion', {'vim.tbl_fil', 'lua'})) - eq({'print(vim'}, meths.call_function('getcompletion', {'print(vi', 'lua'})) + eq({'vim'}, funcs.getcompletion('vi', 'lua')) + eq({'api'}, funcs.getcompletion('vim.ap', 'lua')) + eq({'tbl_filter'}, funcs.getcompletion('vim.tbl_fil', 'lua')) + eq({'vim'}, funcs.getcompletion('print(vi', 'lua')) end) end) |