summaryrefslogtreecommitdiffstats
path: root/.config/nvim/plugin/50-completion.lua
blob: 919f3953e6fb3d4bf4e07842754eaf5c55be8f07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
--
-- Completion configuration plugin
--

------------------------------------------------------------------------------------------------------------------------
-- Insert mode completion
------------------------------------------------------------------------------------------------------------------------
-- See `:help ins-completion-menu`

vim.opt.autocomplete = true -- Show completion menu automatically
vim.opt.completeopt = {
	"noselect", -- No item selected initially
	"fuzzy",
	"menuone", -- Show matches in a menu, even if there's only one match
	"popup", -- Menu items show extra info in the popup window
	"preview", -- Show extra info in the preview window
}
-- Completion sources (in order of priority)
vim.opt.complete = {
	"o", -- 'omnifunc'
}
vim.opt.pumwidth = 25
vim.keymap.set("i", "<Tab>", function()
	return vim.fn.pumvisible() == 1 and "<C-n>" or "<Tab>"
end, { expr = true })
vim.keymap.set("i", "<S-Tab>", function()
	return vim.fn.pumvisible() == 1 and "<C-p>" or "<S-Tab>"
end, { expr = true })

------------------------------------------------------------------------------------------------------------------------
-- Command-line mode completion
------------------------------------------------------------------------------------------------------------------------
-- See `:help cmdline-completion` and `:help cmdline-autocompletion`

-- Show completion menu automatically
vim.api.nvim_create_autocmd({ "CmdlineChanged", "CmdlineEnter" }, {
	desc = "Autocompletion",
	group = vim.g.dotfiles.augroup,
	pattern = "[:\\/\\?]",
	callback = function()
		vim.cmd.call("wildtrigger()")
	end,
})
vim.opt.wildmenu = true -- Show completions in a menu
vim.opt.wildchar = 9 -- Char code assigned to command line wildcard expansion
vim.opt.wildoptions = {
	"exacttext", -- Discard regex artifacts when performing search pattern completion
	"pum", -- Show completions in a popup menu
	"tagfile", -- Show tag kind and file
	"fuzzy", -- Fuzzy matching (doesn't work with files/dirs, see `:help 'wildoptions'`, but patterns do work!)
}
-- Completion modes triggered in order by `wildtrigger()` or 'wildchar'
vim.opt.wildmode = {
	"noselect", -- List matches without inserting
	"full", -- List matches and insert first full match
}
-- Insert unique match
vim.keymap.set("c", string.format("%c", vim.o.wildchar), function()
	local complete_info = vim.fn.cmdcomplete_info()
	if complete_info.pum_visible == 1 then
		return #complete_info.matches == 1 and "<C-n><Space><BS>" or "<C-n>"
	end
	vim.fn.wildtrigger()
end, { expr = true, desc = "Command line wildcard expansion" })
-- Show next completion choices after accepting an entry with `<C-y>`
vim.keymap.set("c", "<C-y>", function()
	local complete_info = vim.fn.cmdcomplete_info()
	if complete_info.pum_visible == 1 then
		return complete_info.selected ~= -1 and "<Space><BS>" or "<C-e>"
	end
	return "<C-y>"
end, { expr = true })