IDE Level Intelligentce LSP: Go-to-Definition, Hover, Diagnostics & Rename | NeoWin Episode 10
Video: IDE Level Intelligentce LSP: Go-to-Definition, Hover, Diagnostics & Rename | NeoWin Episode 10 by Taught by Celeste AI - AI Coding Coach
Watch full page →IDE Level Intelligence LSP: Go-to-Definition, Hover, Diagnostics & Rename in Neovim
Enhance your Neovim setup with powerful IDE-like features using the built-in Language Server Protocol (LSP) support. This guide walks you through installing Mason for managing language servers, configuring Pyright for Python, and setting up keymaps for common LSP actions such as go-to-definition, hover documentation, rename, and diagnostics navigation—all within the terminal.
Code
-- Install Mason and related plugins using your plugin manager (example with packer.nvim)
use {
"williamboman/mason.nvim",
"williamboman/mason-lspconfig.nvim",
"neovim/nvim-lspconfig"
}
-- Setup Mason
require("mason").setup()
require("mason-lspconfig").setup({
ensure_installed = { "pyright" }, -- Automatically install Pyright
})
-- Configure Pyright language server
local lspconfig = require("lspconfig")
lspconfig.pyright.setup{}
-- Define keymaps when LSP attaches to buffer
vim.api.nvim_create_autocmd("LspAttach", {
callback = function(args)
local buf = args.buf
local opts = { buffer = buf, noremap = true, silent = true }
-- Go to definition
vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts)
-- Hover documentation
vim.keymap.set("n", "K", vim.lsp.buf.hover, opts)
-- Find references
vim.keymap.set("n", "gr", vim.lsp.buf.references, opts)
-- Rename symbol
vim.keymap.set("n", "rn", vim.lsp.buf.rename, opts)
-- Code actions (quick fixes)
vim.keymap.set("n", "ca", vim.lsp.buf.code_action, opts)
-- Show diagnostics in a floating window
vim.keymap.set("n", "d", vim.diagnostic.open_float, opts)
-- Navigate to previous diagnostic
vim.keymap.set("n", "[d", vim.diagnostic.goto_prev, opts)
-- Navigate to next diagnostic
vim.keymap.set("n", "]d", vim.diagnostic.goto_next, opts)
end,
})
Key Points
- Mason.nvim provides a convenient way to install and manage language servers like Pyright.
- nvim-lspconfig simplifies configuring language servers and integrating them with Neovim.
- Keymaps such as gd, K, gr, and <leader>rn enable fast navigation and editing powered by the LSP.
- Diagnostic commands help you view and jump between errors and warnings without leaving the editor.
- The LspAttach autocmd ensures keymaps are only active when a language server is attached to the buffer.