diff --git a/lua/core/settings.lua b/lua/core/settings.lua index 341482fa1..45fb5b776 100644 --- a/lua/core/settings.lua +++ b/lua/core/settings.lua @@ -109,6 +109,22 @@ settings["search_backend"] = "telescope" ---@type boolean settings["lsp_inlayhints"] = false +-- Set to true to enable python experimental LSPs, e.g. `ty`, `pyrefly`. +-- NOTE: By setting the option to true, must set one of the following LSPs in `python_experimental_lsp_deps`, +-- else it won't work alone, and the default `pylsp` LSP will be used. +-- NOTE: By setting this to false, the default `pylsp` LSP will be installed and used. +---@type boolean +settings["use_python_experimental_lsp"] = false + +-- Python Experimental LSPs to install during bootstrap. +-- NOTE: As these LSPs are experimental, they may not be stable or fully functional. +-- NOTE: Both these LSPs don't support formatting, so `ruff` is installed for linting and formatting by default. +---@type string[] +settings["python_experimental_lsp_deps"] = { + -- "pyrefly", + -- "ty", +} + -- LSPs to install during bootstrap. -- Full lsit: https://github.com/neovim/nvim-lspconfig/tree/master/lua/lspconfig/configs ---@type string[] @@ -118,7 +134,6 @@ settings["lsp_deps"] = { "html", "jsonls", "lua_ls", - "pylsp", "gopls", } diff --git a/lua/modules/configs/completion/mason-lspconfig.lua b/lua/modules/configs/completion/mason-lspconfig.lua index 3d25d155a..c89f2a4fe 100644 --- a/lua/modules/configs/completion/mason-lspconfig.lua +++ b/lua/modules/configs/completion/mason-lspconfig.lua @@ -2,10 +2,38 @@ local M = {} M.setup = function() local lsp_deps = require("core.settings").lsp_deps + local use_python_experimental_lsp = require("core.settings").use_python_experimental_lsp + local python_experimental_lsp_deps = require("core.settings").python_experimental_lsp_deps require("lspconfig.ui.windows").default_options.border = "rounded" - require("modules.utils").load_plugin("mason-lspconfig", { - ensure_installed = lsp_deps, + local load_plugin = require("modules.utils").load_plugin + + local lsp_deps_with_python = lsp_deps + local has_python_experimental_lsp_deps = python_experimental_lsp_deps and #python_experimental_lsp_deps > 0 + + if use_python_experimental_lsp and has_python_experimental_lsp_deps then + -- If using experimental Python LSP, add the experimental LSPs to the list of dependencies + lsp_deps_with_python = vim.list_extend(lsp_deps, python_experimental_lsp_deps) + table.insert(lsp_deps_with_python, "ruff") -- ruff is used for linting and formatting + elseif use_python_experimental_lsp and not has_python_experimental_lsp_deps then + -- Experimental LSP desired, but dependencies are missing/empty. + -- Warn and fall back to pylsp. + vim.notify( + [[ +If you want to use the experimental Python LSP, +please set `python_experimental_lsp_deps` (a table of LSP names) in your settings. +Fallback to default `pylsp` now.]], + vim.log.levels.WARN, + { title = "nvim-lspconfig" } + ) + table.insert(lsp_deps_with_python, "pylsp") + else + -- Experimental LSP is not desired. Use default pylsp. + table.insert(lsp_deps_with_python, "pylsp") + end + + load_plugin("mason-lspconfig", { + ensure_installed = lsp_deps_with_python, }) vim.diagnostic.config({ @@ -83,7 +111,7 @@ please REMOVE your LSP configuration (rust_analyzer.lua) from the `servers` dire end end - for _, lsp in ipairs(lsp_deps) do + for _, lsp in ipairs(lsp_deps_with_python) do mason_lsp_handler(lsp) end end