update completions logic

This commit is contained in:
♥ Minnie ♥ 2025-09-25 11:20:44 +08:00
parent ef436dbbba
commit b99f7b5386
Signed by: jasmine
GPG key ID: 8563E358D4E8040E

View file

@ -6,47 +6,76 @@
# Options provided to the require('cmp').setup function. # Options provided to the require('cmp').setup function.
settings = { settings = {
# Disable autocompletion by default
completion.autocomplete = false;
# The sources to use # The sources to use
sources = [ sources = [
{ # Language Server Protocol # Language Server Protocol
name = "nvim_lsp"; { name = "nvim_lsp"; }
}
{ # Buffer Words # Buffer Words
name = "buffer"; { name = "buffer"; }
}
{ # Filesystem paths # Filesystem paths
name = "path"; { name = "path"; }
}
{ # Markdown # Markdown
name = "render_markdown"; { name = "render_markdown"; }
}
{ # VimTex # VimTex
name = "vimtex"; { name = "vimtex"; }
}
{ # Neorg # Neorg
name = "neorg"; { name = "neorg"; }
}
]; ];
# Key mappings for the completion menu. # Key mappings for the completion menu.
mapping = { mapping = {
"<esc>" = # Confirm candidate on TAB immediately when there's only one completion entry
# Lua "<Tab>" = /* lua */ ''
"cmp.mapping.close()"; cmp.mapping(function(fallback)
"<Down>" = local line, col = unpack(vim.api.nvim_win_get_cursor(0))
# Lua
"cmp.mapping(cmp.mapping.select_next_item({behavior = cmp.SelectBehavior.Select}), {'i', 's'})"; -- When the selection menu is open select the next item
"<Up>" = if cmp.visible() then
# Lua cmp.select_next_item({behavior = cmp.SelectBehavior.Select})
"cmp.mapping(cmp.mapping.select_prev_item({behavior = cmp.SelectBehavior.Select}), {'i', 's'})"; -- Ensure we are mid word
"<cr>" = elseif col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil then
# Lua -- Open the selection menu
"cmp.mapping.confirm({ select = true })"; cmp.complete()
-- Autocomplete when there is only 1 selection available
if #cmp.get_entries() == 1 then
cmp.confirm({ select = true })
end
-- Select the firt item
cmp.select_next_item({behavior = cmp.SelectBehavior.Select})
-- Keep regular functionality of tab
else
fallback()
end
end, {'i', 's'})
'';
# Confirm selection
"<CR>" = /* lua */ ''
cmp.mapping.confirm({ select = true })
'';
# Select the next item in the menu
"<Down>" = /* lua */ ''
cmp.mapping(cmp.mapping.select_next_item({behavior = cmp.SelectBehavior.Select}), {'i', 's'})
'';
# Select the prev item in the menu
"<Up>" = /* lua */ ''
cmp.mapping(cmp.mapping.select_prev_item({behavior = cmp.SelectBehavior.Select}), {'i', 's'})
'';
# Exit the completion menu
"<esc>" = /* lua */ ''
cmp.mapping.close()
'';
}; };
}; };
}; };