kickstart.nvim/lua/keymaps.lua

119 lines
4.3 KiB
Lua

-- [[ Basic Keymaps ]]
-- See `:help vim.keymap.set()`
-- Clear highlights on search when pressing <Esc> in normal mode
-- See `:help hlsearch`
vim.keymap.set('n', '<Esc>', '<cmd>nohlsearch<CR>')
-- Diagnostic keymaps
vim.keymap.set('n', '<leader>q', vim.diagnostic.setloclist, { desc = 'Open diagnostic [Q]uickfix list' })
-- Exit terminal mode in the builtin terminal with a shortcut that is a bit easier
-- for people to discover. Otherwise, you normally need to press <C-\><C-n>, which
-- is not what someone will guess without a bit more experience.
--
-- NOTE: This won't work in all terminal emulators/tmux/etc. Try your own mapping
-- or just use <C-\><C-n> to exit terminal mode
vim.keymap.set('t', '<Esc><Esc>', '<C-\\><C-n>', { desc = 'Exit terminal mode' })
-- Keybinds to make split navigation easier.
-- Use CTRL+<hjkl> to switch between windows
--
-- See `:help wincmd` for a list of all window commands
vim.keymap.set('n', '<C-h>', '<C-w><C-h>', { desc = 'Move focus to the left window' })
vim.keymap.set('n', '<C-l>', '<C-w><C-l>', { desc = 'Move focus to the right window' })
vim.keymap.set('n', '<C-j>', '<C-w><C-j>', { desc = 'Move focus to the lower window' })
vim.keymap.set('n', '<C-k>', '<C-w><C-k>', { desc = 'Move focus to the upper window' })
-- [[ Basic Autocommands ]]
-- See `:help lua-guide-autocommands`
-- Highlight when yanking (copying) text
-- Try it with `yap` in normal mode
-- See `:help vim.highlight.on_yank()`
vim.api.nvim_create_autocmd('TextYankPost', {
desc = 'Highlight when yanking (copying) text',
group = vim.api.nvim_create_augroup('kickstart-highlight-yank', { clear = true }),
callback = function()
vim.highlight.on_yank()
end,
})
-- Disables virtual_text and turns virtual_line on
-- when there is a diagnostic on the current line
-- Taken from :
-- https://www.reddit.com/r/neovim/comments/1jpbc7s/disable_virtual_text_if_there_is_diagnostic_in/
local og_virt_text
local og_virt_line
vim.api.nvim_create_autocmd({ 'CursorMoved', 'DiagnosticChanged' }, {
group = vim.api.nvim_create_augroup('diagnostic_only_virtlines', {}),
callback = function()
if og_virt_line == nil then
og_virt_line = vim.diagnostic.config().virtual_lines
end
-- ignore if virtual_lines.current_line is disabled
if not (og_virt_line and og_virt_line.current_line) then
if og_virt_text then
vim.diagnostic.config { virtual_text = og_virt_text }
og_virt_text = nil
end
return
end
if og_virt_text == nil then
og_virt_text = vim.diagnostic.config().virtual_text
end
local lnum = vim.api.nvim_win_get_cursor(0)[1] - 1
if vim.tbl_isempty(vim.diagnostic.get(0, { lnum = lnum })) then
vim.diagnostic.config { virtual_text = og_virt_text }
else
vim.diagnostic.config { virtual_text = false }
end
end,
})
-- immediately redraw the diagnostics when the mode changes
-- Taken from :
-- https://www.reddit.com/r/neovim/comments/1jpbc7s/disable_virtual_text_if_there_is_diagnostic_in/
vim.api.nvim_create_autocmd('ModeChanged', {
group = vim.api.nvim_create_augroup('diagnostic_redraw', {}),
callback = function()
pcall(vim.diagnostic.show)
end,
})
-- Auto import for gopls
vim.api.nvim_create_autocmd('BufWritePre', {
pattern = '*.go',
callback = function()
local params = vim.lsp.util.make_range_params()
params.context = { only = { 'source.organizeImports' } }
-- buf_request_sync defaults to a 1000ms timeout. Depending on your
-- machine and codebase, you may want longer. Add an additional
-- argument after params if you find that you have to write the file
-- twice for changes to be saved.
-- E.g., vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, 3000)
local result = vim.lsp.buf_request_sync(0, 'textDocument/codeAction', params)
for cid, res in pairs(result or {}) do
for _, r in pairs(res.result or {}) do
if r.edit then
local enc = (vim.lsp.get_client_by_id(cid) or {}).offset_encoding or 'utf-16'
vim.lsp.util.apply_workspace_edit(r.edit, enc)
end
end
end
vim.lsp.buf.format { async = false }
end,
})
-- adding the .slint filetype to the list of filetypes that neovim can recognize
vim.cmd [[
augroup _slint
autocmd!
autocmd BufRead,BufEnter *.slint set filetype=slint
augroup end
]]