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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
--
-- 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
}
-- 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 })
-- Jump between snippet placeholders, falling back to native behavior when no snippet is active
vim.keymap.set({ "i", "s" }, "<C-n>", function()
return vim.snippet.active({ direction = 1 }) and "<Cmd>lua vim.snippet.jump(1)<CR>" or "<C-n>"
end, { expr = true })
vim.keymap.set({ "i", "s" }, "<C-p>", function()
return vim.snippet.active({ direction = -1 }) and "<Cmd>lua vim.snippet.jump(-1)<CR>" or "<C-p>"
end, { expr = true })
------------------------------------------------------------------------------------------------------------------------
-- Snippet expansion fallback
------------------------------------------------------------------------------------------------------------------------
-- Work around Neovim's snippet grammar rejecting placeholder defaults that mix text with nested tabstops (e.g. lua_ls's
-- `${1:pairs(${2:t})}`): the `any_or_text` rule matches a single node, not a `(any + text)^1` sequence. On parse failure,
-- strip the snippet markup and insert it as plain text instead of throwing. Remove once fixed upstream.
-- The `@as table` cast retypes the module here so overriding `expand` isn't flagged as a duplicate field set.
local snippet = vim.snippet --[[@as table]]
local orig_expand = snippet.expand
-- Reduce LSP snippet syntax to plain text: keep placeholder/variable defaults, the first choice, drop bare tabstops.
local function strip_snippet(s)
local prev
repeat
prev = s
s = s:gsub("%$(%b{})", function(group)
local body = group:sub(2, -2)
local choice = body:match("^%d+|(.*)|$")
if choice then
return (choice:gsub(",.*$", "")) -- first choice
end
local default = body:match("^[%w_]+:(.*)$")
if default then
return default -- ${n:default} / ${VAR:default}
end
return "" -- bare ${n} / ${VAR}
end)
until s == prev
s = s:gsub("%$[%w_]+", "") -- bare $n / $VAR
s = s:gsub("\\([%$}{|,\\])", "%1") -- unescape
return s
end
local function insert_plain(text)
local win = vim.api.nvim_get_current_win()
local row, col = unpack(vim.api.nvim_win_get_cursor(win))
local indent = vim.api.nvim_get_current_line():match("^%s*") or ""
local lines = vim.split(text, "\n", { plain = true })
for i = 2, #lines do
lines[i] = indent .. lines[i]
end
vim.api.nvim_buf_set_text(0, row - 1, col, row - 1, col, lines)
local last = #lines
local new_row = row - 1 + (last - 1)
local new_col = last == 1 and col + #lines[1] or #lines[last]
vim.api.nvim_win_set_cursor(win, { new_row + 1, new_col })
end
snippet.expand = function(input)
local ok = pcall(orig_expand, input)
if not ok then
insert_plain(strip_snippet(input))
vim.notify("Snippet parse failed, inserted as plain text: " .. vim.inspect(input), vim.log.levels.WARN)
end
end
------------------------------------------------------------------------------------------------------------------------
-- 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.fn.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.pumborder = "rounded"
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 })
|