mirror of
https://github.com/pratiktri/dotfiles.git
synced 2026-02-04 16:41:43 +05:30
feat(Neovim): Sensible keymaps, prune plugins, Kickstart.nvim LSP code
This commit is contained in:
@@ -1,81 +1,172 @@
|
||||
-- This function gets run when an LSP connects to a particular buffer.
|
||||
-- Define mappings specific for LSP related items.
|
||||
-- It sets the mode, buffer and description for us each time.
|
||||
local on_attach = function(_, bufnr)
|
||||
local nmap = function(keys, func, desc)
|
||||
if desc then
|
||||
desc = "LSP: " .. desc
|
||||
end
|
||||
|
||||
vim.keymap.set("n", keys, func, { buffer = bufnr, desc = desc })
|
||||
end
|
||||
|
||||
nmap("<leader>cr", vim.lsp.buf.rename, "Rename Symbol")
|
||||
nmap("<leader>ca", vim.lsp.buf.code_action, "Code Action")
|
||||
nmap("<C-.>", vim.lsp.buf.code_action, "Code Action: VSCode Style")
|
||||
-- See `:help K` for why this keymap
|
||||
-- nmap("K", vim.lsp.buf.hover, "Hover Documentation")
|
||||
-- nmap("<C-k>", vim.lsp.buf.signature_help, "Signature Documentation")
|
||||
|
||||
-- Lesser used LSP functionality
|
||||
nmap("<leader>cS", require("telescope.builtin").lsp_dynamic_workspace_symbols, "Search Workspace Symbols")
|
||||
nmap("<leader>clf", function()
|
||||
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
|
||||
end, "Workspace List Folders")
|
||||
|
||||
-- Create a command `:Format` local to the LSP buffer
|
||||
vim.api.nvim_buf_create_user_command(bufnr, "Format", function(_)
|
||||
vim.lsp.buf.format()
|
||||
end, { desc = "Format current buffer with LSP" })
|
||||
end
|
||||
|
||||
return {
|
||||
{
|
||||
-- LSP Configuration & Plugins
|
||||
-- TODO: Disable it for VSCode
|
||||
--
|
||||
-- Main LSP Configuration
|
||||
"neovim/nvim-lspconfig",
|
||||
cond = require("config.util").is_not_vscode(),
|
||||
dependencies = {
|
||||
-- Automatically install LSPs to stdpath for neovim
|
||||
{ "williamboman/mason.nvim", config = true },
|
||||
{ "williamboman/mason-lspconfig.nvim" },
|
||||
{ "folke/neodev.nvim" },
|
||||
{ "williamboman/mason.nvim", config = true }, -- NOTE: Must be loaded before dependants
|
||||
"williamboman/mason-lspconfig.nvim",
|
||||
"WhoIsSethDaniel/mason-tool-installer.nvim",
|
||||
|
||||
-- Useful status updates for LSP
|
||||
{ "j-hui/fidget.nvim", opts = {} },
|
||||
-- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})`
|
||||
{ "j-hui/fidget.nvim", opts = {} },
|
||||
|
||||
-- Allows extra capabilities provided by nvim-cmp
|
||||
'hrsh7th/cmp-nvim-lsp',
|
||||
"hrsh7th/cmp-nvim-lsp",
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
"williamboman/mason-lspconfig.nvim",
|
||||
cond = require("config.util").is_not_vscode(),
|
||||
config = function()
|
||||
-- Configure LSP
|
||||
-- Every time a new file is opened that is associated with
|
||||
-- an lsp this function will be executed to configure the current buffer
|
||||
vim.api.nvim_create_autocmd("LspAttach", {
|
||||
group = vim.api.nvim_create_augroup("lsp-attach", { clear = true }),
|
||||
callback = function(event)
|
||||
local map = function(keys, func, desc, mode)
|
||||
mode = mode or "n"
|
||||
vim.keymap.set(mode, keys, func, { buffer = event.buf, desc = "LSP: " .. desc })
|
||||
end
|
||||
|
||||
-- mason-lspconfig requires that these setup functions are called in this order
|
||||
-- BEFORE setting up the servers.
|
||||
require("mason").setup()
|
||||
local mason_lspconfig = require("mason-lspconfig")
|
||||
mason_lspconfig.setup()
|
||||
-- Lspsaga Keymaps
|
||||
-- Hover Documentation
|
||||
-- Press K 2 times to jump into the hover window
|
||||
-- Place cursor on "View Documentation" gx -> Open the docs on browser
|
||||
map("K", "<cmd>Lspsaga hover_doc<cr>", "Hover Documentation")
|
||||
map("<F2>", vim.lsp.buf.rename, "Rename Symbol")
|
||||
map("<C-.>", "<cmd>Lspsaga code_action<cr>", "Code Actions")
|
||||
map("<leader>ca", "<cmd>Lspsaga code_action<cr>", "Code Actions")
|
||||
-- e to jump to the symbol under cursor; q to quit
|
||||
map("<leader>o", "<cmd>Lspsaga outline<cr>", "Outline Panel on Left")
|
||||
map("<leader>cr", "<cmd>Lspsaga finder<cr>", "Goto References")
|
||||
map("<leader>cpf", "<cmd>Lspsaga peek_definition<cr>", "Peek definition: Function")
|
||||
map("<leader>cpt", "<cmd>Lspsaga peek_type_definition<cr>", "Peek definition: Class")
|
||||
map("<leader>cpi", "<cmd>Lspsaga finder imp<cr>", "Peek: Implementations")
|
||||
|
||||
-- Jump to the definition of the word under your cursor.
|
||||
-- This is where a variable was first declared, or where a function is defined, etc.
|
||||
-- To jump back, press <C-t>.
|
||||
map("gd", require("telescope.builtin").lsp_definitions, "[G]oto [D]efinition")
|
||||
map("<F12>", require("telescope.builtin").lsp_definitions, "[G]oto [D]efinition")
|
||||
map("gD", vim.lsp.buf.declaration, "[G]oto [D]eclaration")
|
||||
map("gr", require("telescope.builtin").lsp_references, "[G]oto [R]eferences")
|
||||
map("gI", require("telescope.builtin").lsp_implementations, "[G]oto [I]mplementation")
|
||||
|
||||
-- Fuzzy find all the symbols in your current document.
|
||||
-- Symbols are things like variables, functions, types, etc.
|
||||
map("<leader>cs", require("telescope.builtin").lsp_document_symbols, "Search Document Symbols")
|
||||
map("<leader>cS", require("telescope.builtin").lsp_dynamic_workspace_symbols, "Search Workspace Symbols")
|
||||
map("<leader>ci", require("telescope.builtin").lsp_implementations, "Goto Implementation")
|
||||
map("<leader>ct", require("telescope.builtin").lsp_type_definitions, "Goto Type Definition")
|
||||
|
||||
map("<leader>cd", require("telescope.builtin").diagnostics, "List Diagnostics")
|
||||
|
||||
map("<leader>nf", function()
|
||||
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
|
||||
end, "Workspace Folders on Notification")
|
||||
|
||||
-- The following two autocommands are used to highlight references of the
|
||||
-- word under your cursor when your cursor rests there for a little while.
|
||||
-- When you move your cursor, the highlights will be cleared
|
||||
local client = vim.lsp.get_client_by_id(event.data.client_id)
|
||||
if client and client.supports_method(vim.lsp.protocol.Methods.textDocument_documentHighlight) then
|
||||
local highlight_augroup = vim.api.nvim_create_augroup("kickstart-lsp-highlight", { clear = false })
|
||||
vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
|
||||
buffer = event.buf,
|
||||
group = highlight_augroup,
|
||||
callback = vim.lsp.buf.document_highlight,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
|
||||
buffer = event.buf,
|
||||
group = highlight_augroup,
|
||||
callback = vim.lsp.buf.clear_references,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd("LspDetach", {
|
||||
group = vim.api.nvim_create_augroup("kickstart-lsp-detach", { clear = true }),
|
||||
callback = function(event2)
|
||||
vim.lsp.buf.clear_references()
|
||||
vim.api.nvim_clear_autocmds({ group = "kickstart-lsp-highlight", buffer = event2.buf })
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
-- TODO: Make inlay_hint enabled by default
|
||||
-- Change this keymap
|
||||
--
|
||||
-- The following code creates a keymap to toggle inlay hints
|
||||
if client and client.supports_method(vim.lsp.protocol.Methods.textDocument_inlayHint) then
|
||||
map("<leader>ni", function()
|
||||
vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({ bufnr = event.buf }))
|
||||
end, "Toggle Inlay Hints")
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
-- Change diagnostic symbols in the sign column (gutter)
|
||||
if vim.g.have_nerd_font then
|
||||
local signs = { ERROR = "", WARN = "", INFO = "", HINT = "" }
|
||||
local diagnostic_signs = {}
|
||||
for type, icon in pairs(signs) do
|
||||
diagnostic_signs[vim.diagnostic.severity[type]] = icon
|
||||
end
|
||||
vim.diagnostic.config({ signs = { text = diagnostic_signs } })
|
||||
end
|
||||
|
||||
-- LSP servers and clients are able to communicate to each other what features they support.
|
||||
-- By default, Neovim doesn't support everything that is in the LSP specification.
|
||||
-- When you add nvim-cmp, luasnip, etc. Neovim now has *more* capabilities.
|
||||
-- Here, we broadcast the new capabilities to the servers.
|
||||
local capabilities = vim.lsp.protocol.make_client_capabilities()
|
||||
capabilities = vim.tbl_deep_extend("force", capabilities, require("cmp_nvim_lsp").default_capabilities())
|
||||
|
||||
-- Enable the following language servers
|
||||
-- Add any additional override configuration in the following tables. They will be passed to the `settings` field of the server config
|
||||
-- If you want to override the default filetypes that your language server will attach to you can define the property 'filetypes' to the map in question.
|
||||
-- Add any additional override configuration in the following tables. Available keys are:
|
||||
-- - cmd (table): Override the default command used to start the server
|
||||
-- - filetypes (table): Override the default list of associated filetypes for the server
|
||||
-- - capabilities (table): Override fields in capabilities. Can be used to disable certain LSP features.
|
||||
-- - settings (table): Override the default settings passed when initializing the server.
|
||||
-- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/
|
||||
local servers = {
|
||||
html = { filetypes = { "html", "twig", "hbs" } },
|
||||
cssls = {},
|
||||
jsonls = {},
|
||||
|
||||
bashls = { filetypes = { "sh", "bash", "zsh" } },
|
||||
pylsp = {},
|
||||
ts_ls = {},
|
||||
lua_ls = {
|
||||
Lua = {
|
||||
workspace = { checkThirdParty = false },
|
||||
telemetry = { enable = false },
|
||||
completion = { callSnippet = "Replace" },
|
||||
hint = { enable = true },
|
||||
-- NOTE: toggle below to ignore Lua_LS's noisy `missing-fields` warnings
|
||||
-- diagnostics = { disable = { 'missing-fields' } },
|
||||
-- cmd = { ... },
|
||||
-- filetypes = { ... },
|
||||
-- capabilities = {},
|
||||
settings = {
|
||||
Lua = {
|
||||
workspace = { checkThirdParty = false },
|
||||
telemetry = { enable = false },
|
||||
completion = { callSnippet = "Replace" },
|
||||
hint = { enable = true },
|
||||
diagnostics = { disable = { "missing-fields" } },
|
||||
},
|
||||
},
|
||||
},
|
||||
bashls = { filetypes = { "sh", "bash", "zsh" } },
|
||||
html = { filetypes = { "html", "twig", "hbs" } },
|
||||
omnisharp = {
|
||||
settings = {
|
||||
DotNet = {
|
||||
enablePackageRestore = true,
|
||||
FormattingOptions = { OrganizeImports = true },
|
||||
RoslynExtensionOptions = {
|
||||
EnableAnalyzerSupport = true,
|
||||
EnableImportCompletion = true,
|
||||
},
|
||||
Sdk = { IncludePrereleases = false },
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
sqlls = {},
|
||||
dockerls = {},
|
||||
docker_compose_language_service = {},
|
||||
|
||||
marksman = {},
|
||||
ltex = {
|
||||
filetypes = { "markdown", "text" },
|
||||
flags = { debounce_text_changes = 3000 },
|
||||
@@ -95,52 +186,35 @@ return {
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
omnisharp = {
|
||||
-- DotNet = {
|
||||
-- enablePackageRestore = true,
|
||||
-- },
|
||||
settings = {
|
||||
FormattingOptions = {
|
||||
OrganizeImports = true,
|
||||
},
|
||||
RoslynExtensionOptions = {
|
||||
EnableAnalyzerSupport = true,
|
||||
EnableImportCompletion = true,
|
||||
},
|
||||
Sdk = {
|
||||
IncludePrereleases = false,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
-- https://github.com/joe-re/sql-language-server?tab=readme-ov-file#configuration
|
||||
sqlls = {},
|
||||
marksman = {},
|
||||
cssls = {},
|
||||
dockerls = {},
|
||||
docker_compose_language_service = {},
|
||||
jsonls = {},
|
||||
pylsp = {},
|
||||
}
|
||||
|
||||
-- nvim-cmp supports additional completion capabilities, so broadcast that to servers
|
||||
local capabilities = vim.lsp.protocol.make_client_capabilities()
|
||||
capabilities = require("cmp_nvim_lsp").default_capabilities(capabilities)
|
||||
-- Ensure the servers and tools above are installed
|
||||
require("mason").setup()
|
||||
|
||||
mason_lspconfig.setup({
|
||||
ensure_installed = vim.tbl_keys(servers),
|
||||
-- Add other tools here that you want Mason to install for you
|
||||
local ensure_installed = vim.tbl_keys(servers or {})
|
||||
vim.list_extend(ensure_installed, {
|
||||
"stylua",
|
||||
"codespell",
|
||||
"bash-language-server",
|
||||
"marksman",
|
||||
"html-lsp",
|
||||
"css-lsp",
|
||||
"dockerfile-language-server",
|
||||
"python-lsp-server",
|
||||
})
|
||||
require("mason-tool-installer").setup({ ensure_installed = ensure_installed })
|
||||
|
||||
mason_lspconfig.setup_handlers({
|
||||
function(server_name)
|
||||
require("lspconfig")[server_name].setup({
|
||||
inlay_hints = { enabled = true },
|
||||
capabilities = capabilities,
|
||||
on_attach = on_attach,
|
||||
settings = servers[server_name],
|
||||
filetypes = (servers[server_name] or {}).filetypes,
|
||||
diagnostics = {
|
||||
require("mason-lspconfig").setup({
|
||||
handlers = {
|
||||
function(server_name)
|
||||
local server = servers[server_name] or {}
|
||||
-- This handles overriding only values explicitly passed
|
||||
-- by the server configuration above. Useful when disabling
|
||||
-- certain features of an LSP (for example, turning off formatting for ts_ls)
|
||||
server.capabilities = vim.tbl_deep_extend("force", {}, capabilities, server.capabilities or {})
|
||||
server.inlay_hints = { enabled = true }
|
||||
server.diagnostics = {
|
||||
underline = true,
|
||||
update_in_insert = false,
|
||||
virtual_text = {
|
||||
@@ -149,17 +223,10 @@ return {
|
||||
prefix = "●",
|
||||
},
|
||||
severity_sort = true,
|
||||
signs = {
|
||||
text = {
|
||||
[vim.diagnostic.severity.ERROR] = require("config.util").icons.diagnostics.Error,
|
||||
[vim.diagnostic.severity.WARN] = require("config.util").icons.diagnostics.Warn,
|
||||
[vim.diagnostic.severity.HINT] = require("config.util").icons.diagnostics.Hint,
|
||||
[vim.diagnostic.severity.INFO] = require("config.util").icons.diagnostics.Info,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
end,
|
||||
}
|
||||
require("lspconfig")[server_name].setup(server)
|
||||
end,
|
||||
},
|
||||
})
|
||||
end,
|
||||
},
|
||||
@@ -169,11 +236,11 @@ return {
|
||||
cond = require("config.util").is_not_vscode(),
|
||||
opts = {
|
||||
progress = {
|
||||
poll_rate = 1, -- How and when to poll for progress messages
|
||||
suppress_on_insert = true, -- Suppress new messages while in insert mode
|
||||
ignore_done_already = true, -- Ignore new tasks that are already complete
|
||||
poll_rate = 1, -- How and when to poll for progress messages
|
||||
suppress_on_insert = true, -- Suppress new messages while in insert mode
|
||||
ignore_done_already = true, -- Ignore new tasks that are already complete
|
||||
ignore_empty_message = true, -- Ignore new tasks that don't contain a message
|
||||
ignore = {}, -- List of LSP servers to ignore
|
||||
ignore = {}, -- List of LSP servers to ignore
|
||||
|
||||
display = {
|
||||
render_limit = 1, -- How many LSP messages to show at once
|
||||
@@ -182,7 +249,7 @@ return {
|
||||
},
|
||||
|
||||
notification = {
|
||||
poll_rate = 2, -- How often to udate and render notifications
|
||||
poll_rate = 2, -- How often to udate and render notifications
|
||||
filter = vim.log.levels.WARN, -- Minimum notifications level
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user