Don't show entire context when completing

This commit is contained in:
Tony Chen
2020-11-30 08:33:52 -05:00
committed by chentau
parent 901dd79f6a
commit d95a465b43
6 changed files with 63 additions and 42 deletions

View File

@@ -3682,7 +3682,6 @@ const char * set_one_cmd_context(
case CMD_lua: case CMD_lua:
xp->xp_context = EXPAND_LUA; xp->xp_context = EXPAND_LUA;
xp->xp_pattern = (char_u *)arg;
break; break;
default: default:

View File

@@ -3946,6 +3946,12 @@ nextwild (
p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len), p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
use_options, type); use_options, type);
xfree(p1); 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. // Longest match: make sure it is not shorter, happens with :help.
if (p2 != NULL && type == WILD_LONGEST) { if (p2 != NULL && type == WILD_LONGEST) {
for (j = 0; (size_t)j < xp->xp_pattern_len; j++) { for (j = 0; (size_t)j < xp->xp_pattern_len; j++) {
@@ -3961,7 +3967,7 @@ nextwild (
} }
if (p2 != NULL && !got_int) { 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) { if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen) {
realloc_cmdbuff(ccline.cmdlen + difflen + 4); realloc_cmdbuff(ccline.cmdlen + difflen + 4);
xp->xp_pattern = ccline.cmdbuff + i; xp->xp_pattern = ccline.cmdbuff + i;
@@ -5109,7 +5115,7 @@ ExpandFromContext (
} }
if (xp->xp_context == EXPAND_LUA) { if (xp->xp_context == EXPAND_LUA) {
ILOG("PAT %s", pat); 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); regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);

View File

@@ -1292,7 +1292,7 @@ static void nlua_add_treesitter(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
lua_setfield(lstate, -2, "_ts_parse_query"); 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(); lua_State *const lstate = nlua_enter();
int ret = OK; 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 ] // [ vim, vim._log_keystroke, buf ]
lua_pushlstring(lstate, (const char *)pat, STRLEN(pat)); 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( nlua_error(
lstate, lstate,
_("Error executing vim._expand_pat: %.*s")); _("Error executing vim._expand_pat: %.*s"));
return FAIL;
} }
Error err = ERROR_INIT; Error err = ERROR_INIT;
@@ -1318,12 +1319,18 @@ int nlua_expand_pat(char_u *pat, int *num_results, char_u ***results)
*num_results = 0; *num_results = 0;
*results = NULL; *results = NULL;
Array completions = nlua_pop_Array(lstate, &err); int prefix_len = (int)nlua_pop_Integer(lstate, &err);
if (ERROR_SET(&err)) { if (ERROR_SET(&err)) {
ret = FAIL; ret = FAIL;
goto cleanup; goto cleanup;
} }
Array completions = nlua_pop_Array(lstate, &err);
if (ERROR_SET(&err)) {
ret = FAIL;
goto cleanup_array;
}
garray_T result_array; garray_T result_array;
ga_init(&result_array, (int)sizeof(char *), 80); ga_init(&result_array, (int)sizeof(char *), 80);
for (size_t i = 0; i < completions.size; i++) { 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) { if (v.type != kObjectTypeString) {
ret = FAIL; ret = FAIL;
goto cleanup; goto cleanup_array;
} }
GA_APPEND( 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)); vim_strsave((char_u *)v.data.string.data));
} }
xp->xp_pattern += prefix_len;
*results = result_array.ga_data; *results = result_array.ga_data;
*num_results = result_array.ga_len; *num_results = result_array.ga_len;
cleanup: cleanup_array:
api_free_array(completions); api_free_array(completions);
cleanup:
if (ret == FAIL) { if (ret == FAIL) {
ga_clear(&result_array); ga_clear(&result_array);
} }

View File

@@ -547,7 +547,7 @@ function vim._expand_pat(pat, env)
if pat == '' then if pat == '' then
local result = vim.tbl_keys(env) local result = vim.tbl_keys(env)
table.sort(result) table.sort(result)
return result return result, 0
end end
-- TODO: We can handle spaces in [] ONLY. -- TODO: We can handle spaces in [] ONLY.
@@ -558,7 +558,7 @@ function vim._expand_pat(pat, env)
-- Get the last part of the pattern -- Get the last part of the pattern
local last_part = pat:match("[%w.:_%[%]'\"]+$") 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) 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 local final_env = env
for _, part in ipairs(parts) do for _, part in ipairs(parts) do
if type(final_env) ~= 'table' then if type(final_env) ~= 'table' then
return {} return {}, 0
end end
-- Normally, we just have a string -- 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 if type(part) == "string" then
final_env = rawget(final_env, part) final_env = rawget(final_env, part)
else else
@@ -584,32 +584,32 @@ function vim._expand_pat(pat, env)
-- -> _G[MY_VAR] -> "api" -- -> _G[MY_VAR] -> "api"
local result_key = part[1] local result_key = part[1]
if not result_key then if not result_key then
return {} return {}, 0
end end
local result = rawget(env, result_key) local result = rawget(env, result_key)
if result == nil then if result == nil then
return {} return {}, 0
end end
final_env = rawget(final_env, result) final_env = rawget(final_env, result)
end end
if not final_env then if not final_env then
return {} return {}, 0
end end
end end
local result = vim.tbl_map(function(v) local result = vim.tbl_map(function(v)
return prefix_match_pat .. v return v
end, vim.tbl_filter(function(name) end, vim.tbl_filter(function(name)
return string.find(name, match_pat) ~= nil return string.find(name, match_pat) ~= nil
end, vim.tbl_keys(final_env))) end, vim.tbl_keys(final_env)))
table.sort(result) table.sort(result)
return result return result, #prefix_match_pat
end end
vim._expand_pat_get_parts = function(lua_string) vim._expand_pat_get_parts = function(lua_string)

View File

@@ -6,33 +6,34 @@ local funcs = helpers.funcs
local exec_lua = helpers.exec_lua local exec_lua = helpers.exec_lua
local get_completions = function(input, env) 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 end
local get_compl_parts = function(parts) 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 end
before_each(clear) before_each(clear)
describe('nlua_expand_pat', function() describe('nlua_expand_pat', function()
it('should complete exact matches', function() it('should complete exact matches', function()
eq({'exact'}, get_completions('exact', { exact = true })) eq({{'exact'}, 0}, get_completions('exact', { exact = true }))
end) end)
it('should return empty table when nothing matches', function() it('should return empty table when nothing matches', function()
eq({}, get_completions('foo', { bar = true })) eq({{}, 0}, get_completions('foo', { bar = true }))
end) end)
it('should return nice completions with function call prefix', function() 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) end)
it('should return keys for nested dictionaries', function() it('should return keys for nested dictionaries', function()
eq( eq(
{ {{
'vim.api.nvim_buf_set_lines', 'nvim_buf_set_lines',
'vim.api.nvim_buf_set_option' 'nvim_buf_set_option'
}, 8
}, },
get_completions('vim.api.nvim_buf_', { get_completions('vim.api.nvim_buf_', {
vim = { vim = {
@@ -49,9 +50,10 @@ describe('nlua_expand_pat', function()
it('it should work with colons', function() it('it should work with colons', function()
eq( eq(
{ {{
'MyClass:bawr', 'bawr',
'MyClass:baz', 'baz',
}, 8
}, },
get_completions('MyClass:b', { get_completions('MyClass:b', {
MyClass = { MyClass = {
@@ -65,9 +67,10 @@ describe('nlua_expand_pat', function()
it('should return keys for string reffed dictionaries', function() it('should return keys for string reffed dictionaries', function()
eq( eq(
{ {{
'vim["api"].nvim_buf_set_lines', 'nvim_buf_set_lines',
'vim["api"].nvim_buf_set_option' 'nvim_buf_set_option'
}, 11
}, },
get_completions('vim["api"].nvim_buf_', { get_completions('vim["api"].nvim_buf_', {
vim = { vim = {
@@ -84,9 +87,10 @@ describe('nlua_expand_pat', function()
it('should return keys for string reffed dictionaries', function() it('should return keys for string reffed dictionaries', function()
eq( eq(
{ {{
'vim["nested"]["api"].nvim_buf_set_lines', 'nvim_buf_set_lines',
'vim["nested"]["api"].nvim_buf_set_option' 'nvim_buf_set_option'
}, 21
}, },
get_completions('vim["nested"]["api"].nvim_buf_', { get_completions('vim["nested"]["api"].nvim_buf_', {
vim = { vim = {
@@ -105,9 +109,10 @@ describe('nlua_expand_pat', function()
it('should be able to interpolate globals', function() it('should be able to interpolate globals', function()
eq( eq(
{ {{
'vim[MY_VAR].nvim_buf_set_lines', 'nvim_buf_set_lines',
'vim[MY_VAR].nvim_buf_set_option' 'nvim_buf_set_option'
}, 12
}, },
get_completions('vim[MY_VAR].nvim_buf_', { get_completions('vim[MY_VAR].nvim_buf_', {
MY_VAR = "api", MY_VAR = "api",
@@ -124,7 +129,7 @@ describe('nlua_expand_pat', function()
end) end)
it('should return everything if the input is of length 0', function() 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) end)
describe('get_parts', function() describe('get_parts', function()

View File

@@ -3,6 +3,7 @@ local Screen = require('test.functional.ui.screen')
local clear, feed = helpers.clear, helpers.feed local clear, feed = helpers.clear, helpers.feed
local eval, eq, neq = helpers.eval, helpers.eq, helpers.neq local eval, eq, neq = helpers.eval, helpers.eq, helpers.neq
local feed_command, source, expect = helpers.feed_command, helpers.source, helpers.expect local feed_command, source, expect = helpers.feed_command, helpers.source, helpers.expect
local funcs = helpers.funcs
local curbufmeths = helpers.curbufmeths local curbufmeths = helpers.curbufmeths
local command = helpers.command local command = helpers.command
local meths = helpers.meths local meths = helpers.meths
@@ -929,10 +930,10 @@ describe('completion', function()
end) end)
it('provides completion from `getcompletion()`', function() it('provides completion from `getcompletion()`', function()
eq({'vim'}, meths.call_function('getcompletion', {'vi', 'lua'})) eq({'vim'}, funcs.getcompletion('vi', 'lua'))
eq({'vim.api'}, meths.call_function('getcompletion', {'vim.ap', 'lua'})) eq({'api'}, funcs.getcompletion('vim.ap', 'lua'))
eq({'vim.tbl_filter'}, meths.call_function('getcompletion', {'vim.tbl_fil', 'lua'})) eq({'tbl_filter'}, funcs.getcompletion('vim.tbl_fil', 'lua'))
eq({'print(vim'}, meths.call_function('getcompletion', {'print(vi', 'lua'})) eq({'vim'}, funcs.getcompletion('print(vi', 'lua'))
end) end)
end) end)