From 901dd79f6a5ee78a55d726abca868bebff117ca9 Mon Sep 17 00:00:00 2001 From: TJ DeVries Date: Tue, 24 Nov 2020 23:24:52 -0500 Subject: feat: add completion to ':lua' --- .../lua/command_line_completion_spec.lua | 167 +++++++++++++++++++++ test/functional/viml/completion_spec.lua | 42 +++++- 2 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 test/functional/lua/command_line_completion_spec.lua (limited to 'test') diff --git a/test/functional/lua/command_line_completion_spec.lua b/test/functional/lua/command_line_completion_spec.lua new file mode 100644 index 0000000000..9056f80fb5 --- /dev/null +++ b/test/functional/lua/command_line_completion_spec.lua @@ -0,0 +1,167 @@ +local helpers = require('test.functional.helpers')(after_each) + +local clear = helpers.clear +local eq = helpers.eq +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) +end + +local get_compl_parts = function(parts) + return funcs.luaeval("{vim._expand_pat_get_parts(_A)}", parts) +end + +before_each(clear) + +describe('nlua_expand_pat', function() + it('should complete exact matches', function() + eq({'exact'}, get_completions('exact', { exact = true })) + end) + + it('should return empty table when nothing matches', function() + eq({}, 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 })) + end) + + it('should return keys for nested dictionaries', function() + eq( + { + 'vim.api.nvim_buf_set_lines', + 'vim.api.nvim_buf_set_option' + }, + get_completions('vim.api.nvim_buf_', { + vim = { + api = { + nvim_buf_set_lines = true, + nvim_buf_set_option = true, + nvim_win_doesnt_match = true, + }, + other_key = true, + } + }) + ) + end) + + it('it should work with colons', function() + eq( + { + 'MyClass:bawr', + 'MyClass:baz', + }, + get_completions('MyClass:b', { + MyClass = { + baz = true, + bawr = true, + foo = false, + } + }) + ) + end) + + it('should return keys for string reffed dictionaries', function() + eq( + { + 'vim["api"].nvim_buf_set_lines', + 'vim["api"].nvim_buf_set_option' + }, + get_completions('vim["api"].nvim_buf_', { + vim = { + api = { + nvim_buf_set_lines = true, + nvim_buf_set_option = true, + nvim_win_doesnt_match = true, + }, + other_key = true, + } + }) + ) + end) + + it('should return keys for string reffed dictionaries', function() + eq( + { + 'vim["nested"]["api"].nvim_buf_set_lines', + 'vim["nested"]["api"].nvim_buf_set_option' + }, + get_completions('vim["nested"]["api"].nvim_buf_', { + vim = { + nested = { + api = { + nvim_buf_set_lines = true, + nvim_buf_set_option = true, + nvim_win_doesnt_match = true, + }, + }, + other_key = true, + } + }) + ) + end) + + it('should be able to interpolate globals', function() + eq( + { + 'vim[MY_VAR].nvim_buf_set_lines', + 'vim[MY_VAR].nvim_buf_set_option' + }, + get_completions('vim[MY_VAR].nvim_buf_', { + MY_VAR = "api", + vim = { + api = { + nvim_buf_set_lines = true, + nvim_buf_set_option = true, + nvim_win_doesnt_match = true, + }, + other_key = true, + } + }) + ) + end) + + it('should return everything if the input is of length 0', function() + eq({"other", "vim"}, get_completions('', { vim = true, other = true })) + end) + + describe('get_parts', function() + it('should return an empty list for no separators', function() + eq({{}, 1}, get_compl_parts("vim")) + end) + + it('just the first item before a period', function() + eq({{"vim"}, 5}, get_compl_parts("vim.ap")) + end) + + it('should return multiple parts just for period', function() + eq({{"vim", "api"}, 9}, get_compl_parts("vim.api.nvim_buf")) + end) + + it('should be OK with colons', function() + eq({{"vim", "api"}, 9}, get_compl_parts("vim:api.nvim_buf")) + end) + + it('should work for just one string ref', function() + eq({{"vim", "api"}, 12}, get_compl_parts("vim['api'].nvim_buf")) + end) + + it('should work for just one string ref, with double quote', function() + eq({{"vim", "api"}, 12}, get_compl_parts('vim["api"].nvim_buf')) + end) + + it('should allows back-to-back string ref', function() + eq({{"vim", "nested", "api"}, 22}, get_compl_parts('vim["nested"]["api"].nvim_buf')) + end) + + it('should allows back-to-back string ref with spaces before and after', function() + eq({{"vim", "nested", "api"}, 25}, get_compl_parts('vim[ "nested" ]["api"].nvim_buf')) + end) + + it('should allow VAR style loolup', function() + eq({{"vim", {"NESTED"}, "api"}, 20}, get_compl_parts('vim[NESTED]["api"].nvim_buf')) + end) + end) +end) diff --git a/test/functional/viml/completion_spec.lua b/test/functional/viml/completion_spec.lua index 01fc50289d..d59a1a3306 100644 --- a/test/functional/viml/completion_spec.lua +++ b/test/functional/viml/completion_spec.lua @@ -26,6 +26,7 @@ describe('completion', function() [7] = {foreground = Screen.colors.White, background = Screen.colors.Red}, [8] = {reverse = true}, [9] = {bold = true, reverse = true}, + [10] = {foreground = Screen.colors.Grey0, background = Screen.colors.Yellow}, }) end) @@ -895,8 +896,47 @@ describe('completion', function() ]]) end) - describe('from the commandline window', function() + describe('lua completion', function() + it('expands when there is only one match', function() + feed(':lua CURRENT_TESTING_VAR = 1') + feed(':lua CURRENT_TESTING_') + screen:expect{grid=[[ + | + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + :lua CURRENT_TESTING_VAR^ | + ]]} + end) + it('expands when there is only one match', function() + feed(':lua CURRENT_TESTING_FOO = 1') + feed(':lua CURRENT_TESTING_BAR = 1') + feed(':lua CURRENT_TESTING_') + screen:expect{ grid = [[ + | + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {10:CURRENT_TESTING_BAR}{9: CURRENT_TESTING_FOO }| + :lua CURRENT_TESTING_BAR^ | + ]], unchanged = true } + 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'})) + end) + end) + + describe('from the commandline window', function() it('is cleared after CTRL-C', function () feed('q:') feed('ifoo faa fee f') -- cgit From d95a465b4399c3c10b83925935ec5f4807d65b60 Mon Sep 17 00:00:00 2001 From: Tony Chen Date: Mon, 30 Nov 2020 08:33:52 -0500 Subject: Don't show entire context when completing --- .../lua/command_line_completion_spec.lua | 47 ++++++++++++---------- test/functional/viml/completion_spec.lua | 9 +++-- 2 files changed, 31 insertions(+), 25 deletions(-) (limited to 'test') 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) -- cgit From f0ccac0ba4e386071f2c7581082edca3536360a4 Mon Sep 17 00:00:00 2001 From: chentau Date: Tue, 26 Jan 2021 16:45:36 -0800 Subject: lint --- test/functional/lua/command_line_completion_spec.lua | 1 - 1 file changed, 1 deletion(-) (limited to 'test') diff --git a/test/functional/lua/command_line_completion_spec.lua b/test/functional/lua/command_line_completion_spec.lua index 31cd5cf02f..3ba7e1589f 100644 --- a/test/functional/lua/command_line_completion_spec.lua +++ b/test/functional/lua/command_line_completion_spec.lua @@ -2,7 +2,6 @@ local helpers = require('test.functional.helpers')(after_each) local clear = helpers.clear local eq = helpers.eq -local funcs = helpers.funcs local exec_lua = helpers.exec_lua local get_completions = function(input, env) -- cgit