Skip to content

Commit 23c5a81

Browse files
add comments
1 parent bb37cc3 commit 23c5a81

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

lua/magenta/utils.lua

+12-2
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,32 @@ local visual_commands = {
5353

5454
M.command_complete = function(_, line)
5555
local commands = normal_commands
56+
-- line is whatever the user has typed on the cmdline. Like :Magenta start-inl
57+
-- start splitting by whitespace
5658
local parts = vim.split(vim.trim(line), "%s+")
59+
-- check if runnning command from visual mode
60+
-- and remove command prefix from parts
5761
if vim.startswith("'<,'>Magenta", parts[1]) then
5862
commands = visual_commands
5963
table.remove(parts, 1)
6064
elseif vim.startswith("Magenta", parts[1]) then
6165
table.remove(parts, 1)
6266
end
67+
-- this ensures #parts == 1 if the user has typed :Magenta<SPACE>
68+
-- and #parts == 2 if the user has typed :Magenta command<SPACE>
6369
if line:sub(-1) == " " then
6470
parts[#parts + 1] = ""
6571
end
66-
local prefix, args = table.remove(parts, 1) or "", parts
72+
-- splits command and args
73+
-- because of above, #parts >=1 as soon as user has entered<SPACE> after command
74+
local command, args = table.remove(parts, 1) or "", parts
75+
-- disable autocompletion if tyiping beyond command
6776
if #args > 0 then
6877
return nil
6978
end
79+
-- filter commands by types prefix
7080
return vim.tbl_filter(function(key)
71-
return key:find(prefix, 1, true) == 1
81+
return key:find(command, 1, true) == 1
7282
end, commands)
7383
end
7484

0 commit comments

Comments
 (0)