Nvim cleanup, autocmd setup added.
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
-- Terminals should open with insert mode
|
||||
vim.api.nvim_create_autocmd("BufEnter", { pattern = "term://*", callback = function() vim.cmd("startinsert") end })
|
||||
-- Start treesitter
|
||||
vim.api.nvim_create_autocmd('FileType', {
|
||||
pattern = '*',
|
||||
callback = function()
|
||||
pcall(vim.treesitter.start)
|
||||
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
|
||||
end,
|
||||
})
|
||||
-- highlights yanked text
|
||||
vim.api.nvim_create_autocmd("TextYankPost", {
|
||||
callback = function()
|
||||
vim.highlight.on_yank({
|
||||
higroup = "IncSearch",
|
||||
timeout = 200,
|
||||
})
|
||||
end,
|
||||
})
|
||||
-- Set absolute numbers when in insert mode otherwise relativenumbers
|
||||
local nums = vim.api.nvim_create_augroup('smart_numbers', {})
|
||||
vim.api.nvim_create_autocmd('InsertEnter', {
|
||||
group = nums,
|
||||
callback = function()
|
||||
vim.opt.relativenumber = false
|
||||
end,
|
||||
})
|
||||
vim.api.nvim_create_autocmd('InsertLeave', {
|
||||
group = nums,
|
||||
callback = function()
|
||||
vim.opt.relativenumber = true
|
||||
end,
|
||||
})
|
||||
-- removes trailing whitespace on save
|
||||
vim.api.nvim_create_autocmd("BufWritePre", {
|
||||
callback = function()
|
||||
local save_cursor = vim.fn.getpos(".")
|
||||
vim.cmd([[%s/\s\+$//e]])
|
||||
vim.fn.setpos(".", save_cursor)
|
||||
end,
|
||||
})
|
||||
-- Lsp autocomplete
|
||||
vim.api.nvim_create_autocmd('LspAttach', {
|
||||
group = vim.api.nvim_create_augroup('my.lsp', {}),
|
||||
callback = function(ev)
|
||||
local client = vim.lsp.get_client_by_id(ev.data.client_id)
|
||||
if not client then return end
|
||||
|
||||
-- Set completeopt for a better experience
|
||||
vim.opt.completeopt = { 'menu', 'menuone', 'noinsert', 'noselect' }
|
||||
|
||||
-- Enable completion if the server supports it
|
||||
if client:supports_method('textDocument/completion') then
|
||||
vim.lsp.completion.enable(true, client.id, ev.buf, {
|
||||
autotrigger = true, -- false = manual trigger only
|
||||
})
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
-- Format: whole file or visual selection via :Fc
|
||||
local function smart_format()
|
||||
local mode = vim.fn.mode()
|
||||
local in_visual = mode == "v" or mode == "V" or mode == "\22"
|
||||
|
||||
local clients = vim.lsp.get_clients({ bufnr = 0 })
|
||||
local has_formatter = false
|
||||
for _, client in ipairs(clients) do
|
||||
if client.supports_method("textDocument/formatting") then
|
||||
has_formatter = true
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if in_visual then
|
||||
if has_formatter then
|
||||
vim.lsp.buf.format({
|
||||
range = {
|
||||
start = vim.api.nvim_buf_get_mark(0, "<"),
|
||||
["end"] = vim.api.nvim_buf_get_mark(0, ">"),
|
||||
},
|
||||
})
|
||||
else
|
||||
vim.cmd("normal! gv=")
|
||||
end
|
||||
else
|
||||
if has_formatter then
|
||||
vim.lsp.buf.format({ async = false })
|
||||
else
|
||||
local view = vim.fn.winsaveview()
|
||||
vim.cmd("normal! gg=G")
|
||||
vim.fn.winrestview(view)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- :FF (Format Function) works in normal and visual mode
|
||||
vim.api.nvim_create_user_command("FF", smart_format, { range = true })
|
||||
Reference in New Issue
Block a user