dot/.config/nvim/lua/config/autocmds.lua
2026-02-27 13:36:47 +01:00

68 lines
2 KiB
Lua

vim.o.updatetime = 250
vim.cmd [[autocmd CursorHold,CursorHoldI * lua vim.diagnostic.open_float(nil, {focus=false})]]
-- autocomplete
local completion_timer = nil
local function get_buffer_keywords(prefix)
local matches = {}
local seen = {}
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_loaded(buf) then
for _, line in ipairs(vim.api.nvim_buf_get_lines(buf, 0, -1, false)) do
for word in line:gmatch("[%w_]+") do
if word ~= prefix and word:sub(1, #prefix) == prefix and not seen[word] then
seen[word] = true
table.insert(matches, word)
end
end
end
end
end
return matches
end
vim.api.nvim_create_autocmd("TextChangedI", {
callback = function()
if completion_timer then
completion_timer:stop()
end
completion_timer = vim.uv.new_timer()
completion_timer:start(150, 0, vim.schedule_wrap(function()
if vim.fn.pumvisible() == 1 or vim.api.nvim_get_mode().mode ~= "i" then
return
end
local col = vim.api.nvim_win_get_cursor(0)[2]
local line = vim.api.nvim_get_current_line()
local prefix = line:sub(1, col):match("[%w_]+$")
if not prefix or #prefix < 2 then
return
end
local matches = get_buffer_keywords(prefix)
if #matches > 0 then
vim.fn.complete(col - #prefix + 1, matches)
end
end))
end,
})
if not os.getenv("NVIM_LSP_OMIT_DIR") then
vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("lsp", { clear = true }),
callback = function(args)
-- enable completion
vim.lsp.completion.enable(true, args.data.client_id, args.buf, { autotrigger = true })
-- autoformat, if not disabled
if not os.getenv("NVIM_LSP_NO_AUTOFMT") then
vim.api.nvim_create_autocmd("BufWritePre", {
buffer = args.buf,
callback = function()
vim.lsp.buf.format {}
end,
})
end
end
})
end