feat: python language server working

This commit is contained in:
Anup Sebastian 2025-10-31 14:31:57 -05:00
parent 686c298c44
commit 1f2f8249ec
3 changed files with 97 additions and 54 deletions

View File

@ -811,7 +811,7 @@ require('lazy').setup({
require('mason-tool-installer').setup { ensure_installed = ensure_installed } require('mason-tool-installer').setup { ensure_installed = ensure_installed }
require('mason-lspconfig').setup { require('mason-lspconfig').setup {
ensure_installed = {}, -- explicitly set to an empty table (Kickstart populates installs via mason-tool-installer) ensure_installed = {},
automatic_installation = false, automatic_installation = false,
handlers = { handlers = {
function(server_name) function(server_name)
@ -822,16 +822,20 @@ require('lazy').setup({
server.capabilities = vim.tbl_deep_extend('force', {}, capabilities, server.capabilities or {}) server.capabilities = vim.tbl_deep_extend('force', {}, capabilities, server.capabilities or {})
-- Use new vim.lsp.config API for Neovim 0.11+ -- Use new vim.lsp.config API for Neovim 0.11+
local lspconfig_servers = require('lspconfig.configs') -- Load the server config from lspconfig
local config = lspconfig_servers[server_name] local ok, lspconfig_server = pcall(require, 'lspconfig.server_configurations.' .. server_name)
if config then if ok and lspconfig_server.default_config then
local config = lspconfig_server.default_config
vim.lsp.config(server_name, { vim.lsp.config(server_name, {
cmd = server.cmd or config.default_config.cmd, cmd = server.cmd or config.cmd,
filetypes = server.filetypes or config.default_config.filetypes, filetypes = server.filetypes or config.filetypes,
root_markers = config.default_config.root_dir, root_markers = config.root_dir,
capabilities = server.capabilities, capabilities = server.capabilities,
settings = server.settings, settings = server.settings,
}) })
-- Enable the LSP for the configured filetypes
vim.lsp.enable(server_name)
end end
end, end,
}, },
@ -1144,5 +1148,52 @@ require('lazy').setup({
}, },
}) })
-- ========================================================================
-- LANGUAGE-SPECIFIC LSP SETUP (for lazy-loaded profiles)
-- ========================================================================
-- Python LSP - starts when opening .py files
vim.api.nvim_create_autocmd('FileType', {
pattern = 'python',
once = false,
callback = function(args)
-- Check if pyright is already attached
local clients = vim.lsp.get_clients({ bufnr = args.buf, name = 'pyright' })
if #clients > 0 then
return
end
-- Get capabilities from blink.cmp
local capabilities = require('blink.cmp').get_lsp_capabilities()
local root_dir = vim.fs.root(args.buf, {
'pyproject.toml',
'setup.py',
'setup.cfg',
'requirements.txt',
'Pipfile',
'pyrightconfig.json',
'.git'
})
vim.lsp.start({
name = 'pyright',
cmd = { vim.fn.stdpath('data') .. '/mason/bin/pyright-langserver', '--stdio' },
root_dir = root_dir or vim.fn.getcwd(),
capabilities = capabilities,
settings = {
python = {
analysis = {
typeCheckingMode = 'basic',
autoImportCompletions = true,
autoSearchPaths = true,
useLibraryCodeForTypes = true,
diagnosticMode = 'openFilesOnly',
},
},
},
})
end,
})
-- The line beneath this is called `modeline`. See `:help modeline` -- The line beneath this is called `modeline`. See `:help modeline`
-- vim: ts=2 sts=2 sw=2 et -- vim: ts=2 sts=2 sw=2 et

View File

@ -11,16 +11,16 @@
"gitsigns.nvim": { "branch": "main", "commit": "20ad4419564d6e22b189f6738116b38871082332" }, "gitsigns.nvim": { "branch": "main", "commit": "20ad4419564d6e22b189f6738116b38871082332" },
"guess-indent.nvim": { "branch": "main", "commit": "84a4987ff36798c2fc1169cbaff67960aed9776f" }, "guess-indent.nvim": { "branch": "main", "commit": "84a4987ff36798c2fc1169cbaff67960aed9776f" },
"lazy.nvim": { "branch": "main", "commit": "f0f5bbb9e5bfae5e6468f9359ffea3d151418176" }, "lazy.nvim": { "branch": "main", "commit": "f0f5bbb9e5bfae5e6468f9359ffea3d151418176" },
"lazydev.nvim": { "branch": "main", "commit": "faf46237f0df43a29e12abd143ff1a0bbac27b7e" }, "lazydev.nvim": { "branch": "main", "commit": "371cd7434cbf95606f1969c2c744da31b77fcfa6" },
"mason-lspconfig.nvim": { "branch": "main", "commit": "3590d66effccc7376d8c3dbe45e8291f9fed2843" }, "mason-lspconfig.nvim": { "branch": "main", "commit": "d7b5feb6e769e995f7fcf44d92f49f811c51d10c" },
"mason-tool-installer.nvim": { "branch": "main", "commit": "517ef5994ef9d6b738322664d5fdd948f0fdeb46" }, "mason-tool-installer.nvim": { "branch": "main", "commit": "517ef5994ef9d6b738322664d5fdd948f0fdeb46" },
"mason.nvim": { "branch": "main", "commit": "ad7146aa61dcaeb54fa900144d768f040090bff0" }, "mason.nvim": { "branch": "main", "commit": "ad7146aa61dcaeb54fa900144d768f040090bff0" },
"mini.nvim": { "branch": "main", "commit": "ee4a4a4abed25e3d108d985b0553c5271f2f71aa" }, "mini.nvim": { "branch": "main", "commit": "e96ef335c7c68d2bc6359cdb7cf0941889df9be0" },
"neo-tree.nvim": { "branch": "main", "commit": "8cdd6b1940f333c1dd085526a9c45b30fb2dbf50" }, "neo-tree.nvim": { "branch": "main", "commit": "8cdd6b1940f333c1dd085526a9c45b30fb2dbf50" },
"nui.nvim": { "branch": "main", "commit": "de740991c12411b663994b2860f1a4fd0937c130" }, "nui.nvim": { "branch": "main", "commit": "de740991c12411b663994b2860f1a4fd0937c130" },
"nvim-dap": { "branch": "master", "commit": "6782b097af2417a4c3e33849b0a26ae2188bd7ea" }, "nvim-dap": { "branch": "master", "commit": "6782b097af2417a4c3e33849b0a26ae2188bd7ea" },
"nvim-dap-ui": { "branch": "master", "commit": "cf91d5e2d07c72903d052f5207511bf7ecdb7122" }, "nvim-dap-ui": { "branch": "master", "commit": "cf91d5e2d07c72903d052f5207511bf7ecdb7122" },
"nvim-lspconfig": { "branch": "master", "commit": "c8c9420b7676caf14e1e1d96caed204a81ba860f" }, "nvim-lspconfig": { "branch": "master", "commit": "e25994a1c2373784364852cd904cb39b6d75f227" },
"nvim-nio": { "branch": "master", "commit": "21f5324bfac14e22ba26553caf69ec76ae8a7662" }, "nvim-nio": { "branch": "master", "commit": "21f5324bfac14e22ba26553caf69ec76ae8a7662" },
"nvim-treesitter": { "branch": "master", "commit": "42fc28ba918343ebfd5565147a42a26580579482" }, "nvim-treesitter": { "branch": "master", "commit": "42fc28ba918343ebfd5565147a42a26580579482" },
"plenary.nvim": { "branch": "master", "commit": "b9fd5226c2f76c951fc8ed5923d85e4de065e509" }, "plenary.nvim": { "branch": "master", "commit": "b9fd5226c2f76c951fc8ed5923d85e4de065e509" },

View File

@ -20,58 +20,50 @@
return { return {
-- ======================================================================== -- ========================================================================
-- PYTHON LSP - Language Server Protocol for Python -- MASON TOOL INSTALLER - Install Python tools on-demand
-- ========================================================================
-- Provides intelligent code completion, go-to-definition, type checking,
-- and more for Python files using pyright (fast, feature-rich LSP)
-- ======================================================================== -- ========================================================================
{ {
'neovim/nvim-lspconfig', 'WhoIsSethDaniel/mason-tool-installer.nvim',
ft = 'python', -- Only load when opening Python files ft = 'python',
dependencies = { dependencies = { 'williamboman/mason.nvim' },
'WhoIsSethDaniel/mason-tool-installer.nvim', -- For installing pyright
},
config = function() config = function()
-- Get shared LSP capabilities from blink.cmp -- Wait for Mason registry to be ready
local capabilities = require('blink.cmp').get_lsp_capabilities() local function install_tools()
local registry_ok, registry = pcall(require, 'mason-registry')
if not registry_ok then
vim.notify('Mason registry not ready, retrying...', vim.log.levels.WARN)
vim.defer_fn(install_tools, 100)
return
end
-- Setup pyright LSP server using new vim.lsp.config API (Neovim 0.11+) -- Refresh registry and install tools
local pyright_config = require('lspconfig.configs').pyright registry.refresh(function()
if pyright_config then local tools = { 'pyright', 'ruff' }
vim.lsp.config('pyright', { for _, tool in ipairs(tools) do
cmd = pyright_config.default_config.cmd, local ok, package = pcall(registry.get_package, tool)
filetypes = pyright_config.default_config.filetypes, if ok and not package:is_installed() then
root_markers = pyright_config.default_config.root_dir, vim.notify('Installing ' .. tool .. '...', vim.log.levels.INFO)
capabilities = capabilities, package:install()
settings = { end
python = { end
analysis = { end)
-- Type checking mode: "off", "basic", or "strict"
typeCheckingMode = 'basic',
-- Auto-import completions
autoImportCompletions = true,
-- Automatically search for stubs
autoSearchPaths = true,
-- Use library code for types
useLibraryCodeForTypes = true,
-- Diagnostic mode: "openFilesOnly" or "workspace"
diagnosticMode = 'openFilesOnly',
},
},
},
})
end end
-- Install Python tools via Mason -- Start installation after a short delay
require('mason-tool-installer').setup { vim.defer_fn(install_tools, 200)
ensure_installed = {
'pyright', -- LSP server for type checking and completions
'ruff', -- Fast formatter, linter, and import organizer (replaces black, isort, flake8)
},
}
end, end,
}, },
-- ========================================================================
-- PYTHON LSP - Language Server Protocol for Python
-- ========================================================================
-- Note: Pyright LSP setup is in init.lua (after lazy.setup) because
-- nvim-lspconfig is already loaded there, and we can't re-trigger its
-- config function with ft='python'. The autocmd in init.lua will start
-- pyright when you open a .py file.
-- ========================================================================
-- ======================================================================== -- ========================================================================
-- PYTHON FORMATTERS - Auto-format Python code -- PYTHON FORMATTERS - Auto-format Python code
-- ======================================================================== -- ========================================================================