Skip to content

Commit 1f2f824

Browse files
Anup SebastianAnup Sebastian
authored andcommitted
feat: python language server working
1 parent 686c298 commit 1f2f824

File tree

3 files changed

+97
-54
lines changed

3 files changed

+97
-54
lines changed

init.lua

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ require('lazy').setup({
811811
require('mason-tool-installer').setup { ensure_installed = ensure_installed }
812812

813813
require('mason-lspconfig').setup {
814-
ensure_installed = {}, -- explicitly set to an empty table (Kickstart populates installs via mason-tool-installer)
814+
ensure_installed = {},
815815
automatic_installation = false,
816816
handlers = {
817817
function(server_name)
@@ -822,16 +822,20 @@ require('lazy').setup({
822822
server.capabilities = vim.tbl_deep_extend('force', {}, capabilities, server.capabilities or {})
823823

824824
-- Use new vim.lsp.config API for Neovim 0.11+
825-
local lspconfig_servers = require('lspconfig.configs')
826-
local config = lspconfig_servers[server_name]
827-
if config then
825+
-- Load the server config from lspconfig
826+
local ok, lspconfig_server = pcall(require, 'lspconfig.server_configurations.' .. server_name)
827+
if ok and lspconfig_server.default_config then
828+
local config = lspconfig_server.default_config
828829
vim.lsp.config(server_name, {
829-
cmd = server.cmd or config.default_config.cmd,
830-
filetypes = server.filetypes or config.default_config.filetypes,
831-
root_markers = config.default_config.root_dir,
830+
cmd = server.cmd or config.cmd,
831+
filetypes = server.filetypes or config.filetypes,
832+
root_markers = config.root_dir,
832833
capabilities = server.capabilities,
833834
settings = server.settings,
834835
})
836+
837+
-- Enable the LSP for the configured filetypes
838+
vim.lsp.enable(server_name)
835839
end
836840
end,
837841
},
@@ -1144,5 +1148,52 @@ require('lazy').setup({
11441148
},
11451149
})
11461150

1151+
-- ========================================================================
1152+
-- LANGUAGE-SPECIFIC LSP SETUP (for lazy-loaded profiles)
1153+
-- ========================================================================
1154+
-- Python LSP - starts when opening .py files
1155+
vim.api.nvim_create_autocmd('FileType', {
1156+
pattern = 'python',
1157+
once = false,
1158+
callback = function(args)
1159+
-- Check if pyright is already attached
1160+
local clients = vim.lsp.get_clients({ bufnr = args.buf, name = 'pyright' })
1161+
if #clients > 0 then
1162+
return
1163+
end
1164+
1165+
-- Get capabilities from blink.cmp
1166+
local capabilities = require('blink.cmp').get_lsp_capabilities()
1167+
1168+
local root_dir = vim.fs.root(args.buf, {
1169+
'pyproject.toml',
1170+
'setup.py',
1171+
'setup.cfg',
1172+
'requirements.txt',
1173+
'Pipfile',
1174+
'pyrightconfig.json',
1175+
'.git'
1176+
})
1177+
1178+
vim.lsp.start({
1179+
name = 'pyright',
1180+
cmd = { vim.fn.stdpath('data') .. '/mason/bin/pyright-langserver', '--stdio' },
1181+
root_dir = root_dir or vim.fn.getcwd(),
1182+
capabilities = capabilities,
1183+
settings = {
1184+
python = {
1185+
analysis = {
1186+
typeCheckingMode = 'basic',
1187+
autoImportCompletions = true,
1188+
autoSearchPaths = true,
1189+
useLibraryCodeForTypes = true,
1190+
diagnosticMode = 'openFilesOnly',
1191+
},
1192+
},
1193+
},
1194+
})
1195+
end,
1196+
})
1197+
11471198
-- The line beneath this is called `modeline`. See `:help modeline`
11481199
-- vim: ts=2 sts=2 sw=2 et

lazy-lock.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
"gitsigns.nvim": { "branch": "main", "commit": "20ad4419564d6e22b189f6738116b38871082332" },
1212
"guess-indent.nvim": { "branch": "main", "commit": "84a4987ff36798c2fc1169cbaff67960aed9776f" },
1313
"lazy.nvim": { "branch": "main", "commit": "f0f5bbb9e5bfae5e6468f9359ffea3d151418176" },
14-
"lazydev.nvim": { "branch": "main", "commit": "faf46237f0df43a29e12abd143ff1a0bbac27b7e" },
15-
"mason-lspconfig.nvim": { "branch": "main", "commit": "3590d66effccc7376d8c3dbe45e8291f9fed2843" },
14+
"lazydev.nvim": { "branch": "main", "commit": "371cd7434cbf95606f1969c2c744da31b77fcfa6" },
15+
"mason-lspconfig.nvim": { "branch": "main", "commit": "d7b5feb6e769e995f7fcf44d92f49f811c51d10c" },
1616
"mason-tool-installer.nvim": { "branch": "main", "commit": "517ef5994ef9d6b738322664d5fdd948f0fdeb46" },
1717
"mason.nvim": { "branch": "main", "commit": "ad7146aa61dcaeb54fa900144d768f040090bff0" },
18-
"mini.nvim": { "branch": "main", "commit": "ee4a4a4abed25e3d108d985b0553c5271f2f71aa" },
18+
"mini.nvim": { "branch": "main", "commit": "e96ef335c7c68d2bc6359cdb7cf0941889df9be0" },
1919
"neo-tree.nvim": { "branch": "main", "commit": "8cdd6b1940f333c1dd085526a9c45b30fb2dbf50" },
2020
"nui.nvim": { "branch": "main", "commit": "de740991c12411b663994b2860f1a4fd0937c130" },
2121
"nvim-dap": { "branch": "master", "commit": "6782b097af2417a4c3e33849b0a26ae2188bd7ea" },
2222
"nvim-dap-ui": { "branch": "master", "commit": "cf91d5e2d07c72903d052f5207511bf7ecdb7122" },
23-
"nvim-lspconfig": { "branch": "master", "commit": "c8c9420b7676caf14e1e1d96caed204a81ba860f" },
23+
"nvim-lspconfig": { "branch": "master", "commit": "e25994a1c2373784364852cd904cb39b6d75f227" },
2424
"nvim-nio": { "branch": "master", "commit": "21f5324bfac14e22ba26553caf69ec76ae8a7662" },
2525
"nvim-treesitter": { "branch": "master", "commit": "42fc28ba918343ebfd5565147a42a26580579482" },
2626
"plenary.nvim": { "branch": "master", "commit": "b9fd5226c2f76c951fc8ed5923d85e4de065e509" },

lua/custom/plugins/python.lua

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,58 +20,50 @@
2020

2121
return {
2222
-- ========================================================================
23-
-- PYTHON LSP - Language Server Protocol for Python
24-
-- ========================================================================
25-
-- Provides intelligent code completion, go-to-definition, type checking,
26-
-- and more for Python files using pyright (fast, feature-rich LSP)
23+
-- MASON TOOL INSTALLER - Install Python tools on-demand
2724
-- ========================================================================
2825
{
29-
'neovim/nvim-lspconfig',
30-
ft = 'python', -- Only load when opening Python files
31-
dependencies = {
32-
'WhoIsSethDaniel/mason-tool-installer.nvim', -- For installing pyright
33-
},
26+
'WhoIsSethDaniel/mason-tool-installer.nvim',
27+
ft = 'python',
28+
dependencies = { 'williamboman/mason.nvim' },
3429
config = function()
35-
-- Get shared LSP capabilities from blink.cmp
36-
local capabilities = require('blink.cmp').get_lsp_capabilities()
30+
-- Wait for Mason registry to be ready
31+
local function install_tools()
32+
local registry_ok, registry = pcall(require, 'mason-registry')
33+
if not registry_ok then
34+
vim.notify('Mason registry not ready, retrying...', vim.log.levels.WARN)
35+
vim.defer_fn(install_tools, 100)
36+
return
37+
end
3738

38-
-- Setup pyright LSP server using new vim.lsp.config API (Neovim 0.11+)
39-
local pyright_config = require('lspconfig.configs').pyright
40-
if pyright_config then
41-
vim.lsp.config('pyright', {
42-
cmd = pyright_config.default_config.cmd,
43-
filetypes = pyright_config.default_config.filetypes,
44-
root_markers = pyright_config.default_config.root_dir,
45-
capabilities = capabilities,
46-
settings = {
47-
python = {
48-
analysis = {
49-
-- Type checking mode: "off", "basic", or "strict"
50-
typeCheckingMode = 'basic',
51-
-- Auto-import completions
52-
autoImportCompletions = true,
53-
-- Automatically search for stubs
54-
autoSearchPaths = true,
55-
-- Use library code for types
56-
useLibraryCodeForTypes = true,
57-
-- Diagnostic mode: "openFilesOnly" or "workspace"
58-
diagnosticMode = 'openFilesOnly',
59-
},
60-
},
61-
},
62-
})
39+
-- Refresh registry and install tools
40+
registry.refresh(function()
41+
local tools = { 'pyright', 'ruff' }
42+
for _, tool in ipairs(tools) do
43+
local ok, package = pcall(registry.get_package, tool)
44+
if ok and not package:is_installed() then
45+
vim.notify('Installing ' .. tool .. '...', vim.log.levels.INFO)
46+
package:install()
47+
end
48+
end
49+
end)
6350
end
6451

65-
-- Install Python tools via Mason
66-
require('mason-tool-installer').setup {
67-
ensure_installed = {
68-
'pyright', -- LSP server for type checking and completions
69-
'ruff', -- Fast formatter, linter, and import organizer (replaces black, isort, flake8)
70-
},
71-
}
52+
-- Start installation after a short delay
53+
vim.defer_fn(install_tools, 200)
7254
end,
7355
},
7456

57+
-- ========================================================================
58+
-- PYTHON LSP - Language Server Protocol for Python
59+
-- ========================================================================
60+
-- Note: Pyright LSP setup is in init.lua (after lazy.setup) because
61+
-- nvim-lspconfig is already loaded there, and we can't re-trigger its
62+
-- config function with ft='python'. The autocmd in init.lua will start
63+
-- pyright when you open a .py file.
64+
-- ========================================================================
65+
66+
7567
-- ========================================================================
7668
-- PYTHON FORMATTERS - Auto-format Python code
7769
-- ========================================================================

0 commit comments

Comments
 (0)