|
20 | 20 |
|
21 | 21 | return { |
22 | 22 | -- ======================================================================== |
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 |
27 | 24 | -- ======================================================================== |
28 | 25 | { |
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' }, |
34 | 29 | 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 |
37 | 38 |
|
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) |
63 | 50 | end |
64 | 51 |
|
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) |
72 | 54 | end, |
73 | 55 | }, |
74 | 56 |
|
| 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 | + |
75 | 67 | -- ======================================================================== |
76 | 68 | -- PYTHON FORMATTERS - Auto-format Python code |
77 | 69 | -- ======================================================================== |
|
0 commit comments