### Configure mason-lspconfig.nvim Settings Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/doc/mason-lspconfig.txt Configure mason-lspconfig.nvim behavior by passing settings to the setup() function. This example ensures specific servers are installed. ```lua require("mason-lspconfig").setup({ ensure_installed = { "rust_analyzer", "ts_ls" } }) ``` -------------------------------- ### Setup mason-lspconfig.nvim Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/README.md Basic setup for mason-lspconfig.nvim. Ensure mason.nvim and nvim-lspconfig are installed and available in runtimepath before calling setup. ```lua require("mason-lspconfig").setup() ``` -------------------------------- ### Setup with Specific Servers to Install Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/examples-and-patterns.md Configure mason-lspconfig to install and automatically enable a specific list of LSP servers upon initial setup. ```lua require("mason").setup() require("mason-lspconfig").setup({ ensure_installed = { "lua_ls", "rust_analyzer", "pyright" } }) ``` -------------------------------- ### Setup with ensure_installed Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/configuration.md Configures mason-lspconfig to install and automatically enable a predefined list of LSP servers. ```lua require("mason-lspconfig").setup({ ensure_installed = { "lua_ls", "rust_analyzer", "pyright" } }) ``` -------------------------------- ### Import and Setup Plugin Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/index.md Demonstrates how to import and set up the mason-lspconfig plugin with a list of servers to ensure are installed and enable automatic enabling. ```lua require("mason-lspconfig").setup({ ensure_installed = { "lua_ls", "rust_analyzer" }, automatic_enable = true }) ``` -------------------------------- ### lazy.nvim Integration for Mason-LSPConfig Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/examples-and-patterns.md Recommended setup using lazy.nvim, which handles plugin loading and setup automatically. Specify servers to ensure are installed within the plugin's options. ```lua { "mason-org/mason-lspconfig.nvim", opts = { ensure_installed = { "lua_ls", "rust_analyzer" }, }, dependencies = { { "mason-org/mason.nvim", opts = {} }, "neovim/nvim-lspconfig", }, } ``` -------------------------------- ### Example Usage of :PylspInstall Command Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-server-configurations.md Demonstrates how to use the custom :PylspInstall command to install specific pylsp plugins. ```vim :PylspInstall pylsp-mypy python-lsp-black ``` -------------------------------- ### setup Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-main-module.md Sets up the mason-lspconfig plugin. This function should be called after mason.nvim and nvim-lspconfig are initialized. It handles validation, registry refresh, server installation, and automatic enabling. ```APIDOC ## Function: setup ### Description Sets up the mason-lspconfig plugin. Must be called after mason.nvim and nvim-lspconfig are initialized. ### Parameters #### Request Body - **config** (MasonLspconfigSettings) - Optional - Configuration table with settings like `ensure_installed` and `automatic_enable` ### Returns None. This function initializes the plugin and registers event handlers. ### Example ```lua require("mason").setup() require("lspconfig").setup() require("mason-lspconfig").setup({ ensure_installed = { "lua_ls", "rust_analyzer" }, automatic_enable = true }) ``` ``` -------------------------------- ### Command Override Configuration Example Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/types.md Example of overriding the command used to start an LSP server. ```lua { cmd = { "elixir-ls" } } ``` -------------------------------- ### Setup with Selective Automatic Enabling Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/configuration.md Installs multiple LSP servers but only automatically enables a subset, requiring manual enabling for others. ```lua require("mason-lspconfig").setup({ ensure_installed = { "lua_ls", "rust_analyzer", "ts_ls", "json_ls" }, automatic_enable = { exclude = { "ts_ls", "json_ls" } } }) ``` -------------------------------- ### Correct Mason LSP Setup Order Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/errors-and-notifications.md Ensures mason.nvim and mason-lspconfig.nvim are set up in the correct order to prevent installation and setup issues. ```lua -- Correct order require("mason").setup() require("lspconfig.setup") -- Only needed if explicit setup require("mason-lspconfig").setup({ ensure_installed = { "lua_ls" } }) ``` -------------------------------- ### Setup mason-lspconfig.nvim Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/doc/mason-lspconfig.txt Call the setup() function to enable the mason-lspconfig plugin. Ensure mason.nvim is set up and nvim-lspconfig is available in 'runtimepath' beforehand. ```lua require("mason").setup() require("mason-lspconfig").setup() ``` -------------------------------- ### Install Specific LSP Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/configuration.md Configure mason-lspconfig to automatically install a list of specified LSP servers during setup. ```lua require("mason-lspconfig").setup({ ensure_installed = { "lua_ls", "rust_analyzer", "ts_ls" } }) ``` -------------------------------- ### Recommended lazy.nvim Setup Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/README.md Recommended setup for lazy.nvim users. This automatically handles the setup call for mason-lspconfig.nvim. ```lua { "mason-org/mason-lspconfig.nvim", opts = {}, dependencies = { { "mason-org/mason.nvim", opts = {} }, "neovim/nvim-lspconfig", }, } ``` -------------------------------- ### Setup LSP for Minimal Neovim Config Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/examples-and-patterns.md Sets up mason and mason-lspconfig with a minimal configuration, ensuring only the lua_ls server is installed. Also creates a user command for LSP info. ```lua -- plugins/lsp.lua local function setup_lsp() require("mason").setup() require("mason-lspconfig").setup({ ensure_installed = { "lua_ls" } }) end setup_lsp() -- Enable LSP commands vim.api.nvim_create_user_command("LspInfo", function() require("vim.lsp").util.list_buffers_clients() end, {} ) ``` -------------------------------- ### Get Installed Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/index.md Retrieves a list of LSP servers that are currently installed via Mason. ```lua -- Get installed servers local installed = require("mason-lspconfig").get_installed_servers() ``` -------------------------------- ### Testing mason-lspconfig.nvim Setup Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/index.md Use these Vim commands to verify your mason-lspconfig.nvim installation and configuration. They check health, list installed servers, show available servers for the current filetype, and display all mason packages. ```vim " Check health :checkhealth mason-lspconfig " View installed servers :LspInfo " View available servers for current filetype :LspInstall " View all mason packages :Mason ``` -------------------------------- ### Install mason.nvim with packer.nvim Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-health.md Use this snippet to install the mason.nvim plugin if you are using the packer.nvim package manager. Ensure you call the setup function. ```lua use { "mason-org/mason.nvim", config = function() require("mason").setup() end } ``` -------------------------------- ### Get Installed Server Completion Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-commands.md Provides a newline-separated string of all currently installed server names. Used by the :LspUninstall command completion. ```lua _G.mason_lspconfig_completion.installed_server_completion(): string ``` -------------------------------- ### Setup mason-lspconfig Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-main-module.md Initializes the mason-lspconfig plugin with custom settings. Ensure mason.nvim and nvim-lspconfig are set up first. This function handles server installation and automatic enabling. ```lua require("mason").setup() require("lspconfig").setup() require("mason-lspconfig").setup({ ensure_installed = { "lua_ls", "rust_analyzer" }, automatic_enable = true }) ``` -------------------------------- ### Minimal mason-lspconfig Setup Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/configuration.md Basic setup for mason-lspconfig.nvim without any specific configuration options. All servers are enabled automatically. ```lua require("mason-lspconfig").setup() ``` -------------------------------- ### Get Installed LSP Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-main-module.md Retrieves a list of LSP server names currently installed via Mason. Returns an empty list if no servers are installed. ```lua local installed = require("mason-lspconfig").get_installed_servers() for _, server in ipairs(installed) do print(server) -- e.g., "lua_ls", "rust_analyzer" end ``` -------------------------------- ### Example Mason LSP Notifications Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-commands.md These examples show the user notifications that mason-lspconfig.nvim provides for LSP installation and uninstallation processes, including success, failure, and invalid server name cases. ```text [mason-lspconfig.nvim] installing lua_ls [mason-lspconfig.nvim] lua_ls was successfully installed [mason-lspconfig.nvim] failed to install lua_ls. Installation logs are available in :Mason and :MasonLog [mason-lspconfig.nvim] Could not find LSP server "invalid_name". ``` -------------------------------- ### Install First Available Server for Filetype Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/examples-and-patterns.md Installs the first available LSP server detected for a given filetype. Use this to automatically install a suitable server when needed. ```lua local function install_for_filetype(filetype) local available = require("mason-lspconfig").get_available_servers({ filetype = filetype }) if #available > 0 then local server = available[1] -- Get first available vim.cmd("LspInstall " .. server) else vim.notify("No servers found for " .. filetype, vim.log.levels.WARN) end end -- Usage install_for_filetype("python") -- Installs first available Python server ``` -------------------------------- ### Setup with Specific Server Versions Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-ensure-installed.md Installs specific versions of servers like 'rust_analyzer@nightly' or 'pyright@latest', while using default versions for others. This allows for control over server versions. ```lua require("mason-lspconfig").setup({ ensure_installed = { "lua_ls", "rust_analyzer@nightly", "pyright@latest" } }) ``` -------------------------------- ### MasonLspconfigSettings Configuration Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/types.md Defines the configuration object for the setup() function. Use this to specify servers to install and control automatic enabling. ```lua ---@class MasonLspconfigSettings { ensure_installed: string[], automatic_enable: boolean | string[] | { exclude: string[] } } ``` ```lua local config = { ensure_installed = { "lua_ls", "rust_analyzer" }, automatic_enable = true } require("mason-lspconfig").setup(config) ``` -------------------------------- ### Installation Flow: Step 1 - Parse Configuration Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-ensure-installed.md The first step in the installation flow, parsing the server identifier from the `ensure_installed` list to extract the server name and version. ```lua for _, server_identifier in ipairs(settings.current.ensure_installed) do local server_name, version = Package.Parse(server_identifier) ``` -------------------------------- ### Install Servers by Filetype Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/index.md Prompts the user to select and install LSP servers based on the current filetype using the :LspInstall command. ```vim " Install by filetype (prompts user) :LspInstall :LspInstall python ``` -------------------------------- ### Get Available Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/index.md Retrieves a list of LSP servers that are available to be installed via Mason. ```lua -- Get available servers local available = require("mason-lspconfig").get_available_servers() ``` -------------------------------- ### Install Specific Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/index.md Installs one or more specified LSP servers using the :LspInstall command. ```vim " Install specific servers :LspInstall lua_ls rust_analyzer ``` -------------------------------- ### Install Specific LSP Servers via Command Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/examples-and-patterns.md Use the `:LspInstall` command to install one or more specified LSP servers. If a server is already installed, this command will ensure it is present. ```vim :LspInstall lua_ls rust_analyzer pyright ``` -------------------------------- ### Install LSP Servers by Filetype via Command Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/examples-and-patterns.md Install LSP servers associated with a specific filetype using the `:LspInstall` command followed by the filetype name. The user will be prompted to select which server to install if multiple are available. ```vim " Install servers for Python :LspInstall python ``` -------------------------------- ### InstallHandle Callbacks Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/types.md Demonstrates registering callbacks for installation success or failure using the InstallHandle returned from package installation operations. ```lua handle:on_success(fn) -- Register success callback handle:on_failure(fn) -- Register failure callback ``` ```lua local handle = pkg:install({}) handle:on_success(function() print("Installation successful") end) handle:on_failure(function() print("Installation failed") end) ``` -------------------------------- ### Get Available LSP Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-main-module.md Retrieves a list of all installable LSP servers from Mason. Supports filtering by filetype. ```lua -- Get all available servers local all_servers = require("mason-lspconfig").get_available_servers() -- Get servers for Lua local lua_servers = require("mason-lspconfig").get_available_servers({ filetype = "lua" }) -- Get servers for multiple filetypes local multi_servers = require("mason-lspconfig").get_available_servers({ filetype = { "typescript", "typescriptreact" } }) ``` -------------------------------- ### Lazy Load LSP Installation Command Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/examples-and-patterns.md Sets up mason and mason-lspconfig, then defines a user command `LspInstall` that lazily loads the installation logic. Includes custom completion for available servers. ```lua -- main config file require("mason").setup() require("mason-lspconfig").setup({ ensure_installed = { "lua_ls" } }) -- deferred loading of LSP commands vim.api.nvim_create_user_command("LspInstall", function(opts) require("mason-lspconfig.api.command").LspInstall(opts.fargs) end, { nargs = "*", complete = "custom,v:lua.mason_lspconfig_completion.available_server_completion" }) ``` -------------------------------- ### Integration with Automatic Enable Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-ensure-installed.md Combines `ensure_installed` with `automatic_enable = true` to automatically install and then enable specified LSP servers. This streamlines the setup process for language support. ```lua require("mason-lspconfig").setup({ ensure_installed = { "lua_ls", "rust_analyzer" }, automatic_enable = true }) -- Both servers are installed and automatically enabled ``` -------------------------------- ### setup(config) Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/index.md Initializes the mason-lspconfig.nvim plugin with the provided configuration. This function is essential for setting up the plugin and its features. ```APIDOC ## setup(config) ### Description Initializes the mason-lspconfig.nvim plugin with the provided configuration. ### Method `setup` (function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **config** (table) - Required - Configuration table for mason-lspconfig.nvim. ### Request Example ```lua require('mason-lspconfig').setup({ -- your configuration here }) ``` ### Response #### Success Response None (initializes the plugin) #### Response Example None ``` -------------------------------- ### Install LSP Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/doc/mason-lspconfig.txt Use the :LspInstall command to install LSP servers. It accepts nvim-lspconfig server names and can be used with or without arguments to prompt for server selection. ```vim :LspInstall rust_analyzer lua_ls ``` ```vim :LspInstall ``` -------------------------------- ### Setup LSP Servers for Web Development Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/examples-and-patterns.md Configures mason-lspconfig to ensure specific LSP servers for frontend and backend web development are installed. Includes common frontend and backend servers. ```lua require("mason-lspconfig").setup({ ensure_installed = { -- Frontend "ts_ls", "eslint", "stylelint_lsp", "html", "cssls", -- Backend "pyright", -- General "json_ls", "yaml_language_server", "lua_ls" } }) ``` -------------------------------- ### Manual Server Enabling Example Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-automatic-enable.md Demonstrates how to manually enable LSP servers after disabling automatic enabling or when using an exclude list. This example shows enabling a single server and then multiple servers in a loop. ```lua require("mason-lspconfig").setup({ automatic_enable = false }) -- Later, when you want to enable a server vim.lsp.enable("lua_ls") -- Or enable multiple servers for _, server in ipairs({ "lua_ls", "rust_analyzer" }) do vim.lsp.enable(server) end ``` -------------------------------- ### Set Up with Specific Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/index.md Configures the plugin to ensure a specific list of LSP servers are installed. ```lua require("mason-lspconfig").setup({ ensure_installed = { "lua_ls", "rust_analyzer", "pyright", "ts_ls" } }) ``` -------------------------------- ### Setup mason-lspconfig Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/doc/mason-lspconfig.txt Configures mason-lspconfig with custom settings. The `automatic_enable` option controls whether installed LSP servers are automatically enabled. ```APIDOC ## POST /mason-lspconfig/setup ### Description Sets up mason-lspconfig with the provided configuration table. ### Method POST ### Endpoint /mason-lspconfig/setup ### Parameters #### Request Body - **config** (table) - Optional - Configuration table for mason-lspconfig. See |mason-lspconfig-settings|. - **automatic_enable** (boolean | string[] | { exclude: string[] }) - Optional - Controls automatic enabling of installed LSP servers. Defaults to true. ### Request Example ```json { "config": { "automatic_enable": false } } ``` ### Response #### Success Response (200) - **message** (string) - Indicates successful setup. #### Response Example ```json { "message": "mason-lspconfig setup complete." } ``` ``` -------------------------------- ### Internal LspInstall Function Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-commands.md The internal asynchronous implementation for installing LSP servers. It handles parsing server names, resolving mappings, and delegating the installation to mason.api.command.MasonInstall(). ```lua function LspInstall(servers: string[]): void ``` -------------------------------- ### Resolve Package Example Usage Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-ensure-installed.md An example demonstrating how to use the `resolve_package` function. It shows how to handle the `Optional` return value to either process the found package or handle the case where the server is not found. ```lua local Optional = require "mason-core.optional" -- Internal implementation pattern resolve_package("lua_ls") :if_present(function(pkg) -- pkg is the Package object for lua-language-server -- Can check if installed or schedule installation end) :if_not_present(function() -- Server not found in mappings or registry end) ``` -------------------------------- ### Event Handling: Package Install Success Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-automatic-enable.md Illustrates how the `package:install:success` event triggers the `enable_server_scheduled` handler, which automatically enables a newly installed server according to the `automatic_enable` configuration rules. ```lua -- User runs :LspInstall rust_analyzer during a session -- Event fires: package:install:success -- Handler: enable_server_scheduled("rust_analyzer") is called -- Result: rust_analyzer is automatically enabled for the session ``` -------------------------------- ### Install mason.nvim with lazy.nvim Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-health.md Use this snippet to install the mason.nvim plugin if you are using the lazy.nvim package manager. ```lua { "mason-org/mason.nvim", opts = {}, } ``` -------------------------------- ### automatic_enable.init() Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-automatic-enable.md Initializes the automatic enable feature. It sets up event listeners and enables all currently installed LSP servers. This function is automatically called during `setup()` if `automatic_enable` is not explicitly set to `false`. ```APIDOC ## Function: init ### Description Initializes the automatic enable feature. Sets up event listeners and enables all currently installed servers. ### Method `automatic_enable.init()` ### Parameters None ### Returns None ### Behavior - Clears the internal `enabled_servers` tracking table. - Iterates through all installed packages in the registry and enables them using `enable_server()`. - Registers an event listener on the registry for future package installations. - Deregisters any previous listener before registering a new one. ``` -------------------------------- ### Check Installation Status of Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/index.md Compares installed and available LSP servers to determine and print the installation status of each available server. ```lua local installed = require("mason-lspconfig").get_installed_servers() local available = require("mason-lspconfig").get_available_servers() local is_installed = {} for _, server in ipairs(installed) do is_installed[server] = true end for _, server in ipairs(available) do if is_installed[server] then print(server .. " - installed") else print(server .. " - not installed") end end ``` -------------------------------- ### Installation Flow: Step 2 & 3 - Resolve Package and Check Status Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-ensure-installed.md Steps two and three of the installation flow. This snippet shows resolving the package using `resolve_package` and then checking if the package is already installed or being installed before initiating the installation. ```lua resolve_package(server_name) :if_present(function(pkg) -- Step 3 end) :if_not_present(function() -- Handle error end) if not pkg:is_installed() and not pkg:is_installing() then require("mason-lspconfig.install").install(pkg, version) end ``` -------------------------------- ### LspInstall Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-commands.md Installs one or more LSP servers. It supports installing by lspconfig server name, language name, or with version specifiers. If called without arguments, it prompts based on the current buffer's filetype. ```APIDOC ## Command: LspInstall ### Description Installs one or more LSP servers. Supports installing by lspconfig server name, language name, or with version specifiers. If called without arguments, it prompts based on the current buffer's filetype. ### Arguments - **server** (string) - Optional - Server name(s) to install. Can be multiple space-separated server names. Supports both lspconfig server names and language/filetype names. Optionally supports version specification using `@` syntax (e.g., `lua_ls@latest`). ### Behavior - If called with server names: installs the specified servers. - If called without arguments: prompts based on current buffer's filetype. - Can accept both lspconfig server names (e.g., `lua_ls`) and language names (e.g., `typescript`, `java`). - If a language name is provided that maps to multiple servers, prompts user to select which server to install. - After installation, displays the Mason UI showing the installed servers. ### Examples ```vim " Install specific servers by lspconfig name :LspInstall lua_ls rust_analyzer ts_ls " Install without arguments - prompts based on current buffer filetype :LspInstall " Install with version specifier :LspInstall rust_analyzer@nightly " Install by language name (will prompt if multiple servers available) :LspInstall typescript :LspInstall java ``` ### Completion The command supports custom completion showing all available servers from the Mason registry. Available servers are sorted and deduplicated. ``` -------------------------------- ### Installation Flow: Step 4 - Handle Errors Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-ensure-installed.md The error handling step in the installation flow. If a server cannot be resolved, a warning notification is displayed to the user. ```lua :if_not_present(function() notify( ("[mason-lspconfig.nvim] Server %q is not a valid entry in ensure_installed. Make sure to only provide lspconfig server names."):format( server_name ), vim.log.levels.WARN ) end) ``` -------------------------------- ### Install LSP Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-commands.md Installs one or more LSP servers by their lspconfig name, language name, or with version specifiers. If called without arguments, it prompts based on the current buffer's filetype. Supports custom completion for available servers. ```vim :LspInstall lua_ls rust_analyzer ts_ls ``` ```vim :LspInstall ``` ```vim :LspInstall rust_analyzer@nightly ``` ```vim :LspInstall typescript ``` ```vim :LspInstall java ``` -------------------------------- ### Configure Ensure Installed Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/README.md Specify a list of servers to ensure are installed when configuring mason-lspconfig.nvim. ```lua require("mason-lspconfig").setup { ensure_installed = { "lua_ls", "rust_analyzer" }, } ``` -------------------------------- ### Check Installation Status of All Required Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/examples-and-patterns.md Compares a list of required LSP servers against the currently installed servers managed by mason. Returns a list of missing servers. ```lua local function check_all_servers_installed() local ensure_installed = { "lua_ls", "rust_analyzer", "pyright" } local installed = require("mason-lspconfig").get_installed_servers() local installed_set = {} for _, server in ipairs(installed) do installed_set[server] = true end local missing = {} for _, server in ipairs(ensure_installed) do if not installed_set[server] then table.insert(missing, server) end end return missing end -- Usage local missing = check_all_servers_installed() if #missing > 0 then print("Missing servers: " .. table.concat(missing, ", ")) else print("All required servers are installed") end ``` -------------------------------- ### Install LSP Servers with Specific Versions Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/configuration.md Configure mason-lspconfig to install specific LSP servers, optionally with a version tag like '@nightly'. ```lua require("mason-lspconfig").setup({ ensure_installed = { "lua_ls", "rust_analyzer@nightly" } }) ``` -------------------------------- ### Configure mason-lspconfig.nvim with lazy.nvim Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/README.md Use this snippet to set up mason-lspconfig.nvim as a dependency in your lazy.nvim configuration. It ensures mason.nvim and nvim-lspconfig are installed and allows specifying servers to be automatically installed. ```lua { "mason-org/mason-lspconfig.nvim", opts = { ensure_installed = { "lua_ls", "rust_analyzer" }, }, dependencies = { { "mason-org/mason.nvim", opts = {} }, "neovim/nvim-lspconfig", }, } ``` -------------------------------- ### Enable All Installed LSP Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/configuration.md Configure mason-lspconfig to automatically enable all installed LSP servers. This is the default behavior. ```lua require("mason-lspconfig").setup({ automatic_enable = true }) ``` -------------------------------- ### Headless Environment Check for Ensure Installed Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-ensure-installed.md Shows how the `ensure_installed` feature is skipped in headless Neovim instances. This prevents server installations in automation or scripting environments. ```lua if platform.is_headless and #settings.current.ensure_installed > 0 then -- Skip ensure_installed end ``` -------------------------------- ### Auto-Install Missing LSP Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/examples-and-patterns.md Ensures that a list of specified LSP servers are installed. It checks which servers are already installed and then runs `LspInstall` for any that are missing. ```lua local function ensure_servers(servers) local installed = require("mason-lspconfig").get_installed_servers() local installed_set = {} for _, s in ipairs(installed) do installed_set[s] = true end for _, server in ipairs(servers) do if not installed_set[server] then vim.cmd("LspInstall " .. server) end end end -- Usage ensure_servers({ "lua_ls", "rust_analyzer" }) ``` -------------------------------- ### Register Custom Command to List Installed Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/examples-and-patterns.md Creates a custom Neovim command `:LspServers` that lists all currently installed LSP servers. This is useful for quick verification. ```lua local function register_lsp_commands() vim.api.nvim_create_user_command("LspServers", function() local installed = require("mason-lspconfig").get_installed_servers() vim.cmd("enew") vim.fn.append(0, installed) end, {}) end register_lsp_commands() -- Usage: :LspServers ``` -------------------------------- ### Validate Server Availability Before Installation Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/examples-and-patterns.md Checks if a specified LSP server is available in the mason registry before attempting installation. Returns true if available, false otherwise. ```lua local function validate_server_available(server_name) local available = require("mason-lspconfig").get_available_servers() for _, s in ipairs(available) do if s == server_name then return true end end return false end -- Usage if validate_server_available("lua_ls") then vim.notify("lua_ls is available for installation") else vim.notify("lua_ls is not available", vim.log.levels.WARN) end ``` -------------------------------- ### Get Installed LSP Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/doc/mason-lspconfig.txt Retrieves a list of LSP servers that are installed via Mason and supported by lspconfig. ```APIDOC ## GET /mason-lspconfig/get_installed_servers ### Description Returns the installed LSP servers supported by lspconfig. ### Method GET ### Endpoint /mason-lspconfig/get_installed_servers ### Parameters None ### Response #### Success Response (200) - **servers** (string[]) - A list of nvim-lspconfig server names for installed servers. ### Response Example ```json { "servers": [ "lua_ls", "pyright" ] } ``` ``` -------------------------------- ### get_installed_servers() Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/index.md Retrieves a list of all servers that are currently installed via mason.nvim. ```APIDOC ## get_installed_servers() ### Description Retrieves a list of all servers that are currently installed via mason.nvim. ### Method `get_installed_servers` (function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```lua local installed_servers = require('mason-lspconfig').get_installed_servers() print(vim.inspect(installed_servers)) ``` ### Response #### Success Response - **servers** (table) - A table containing information about installed servers. #### Response Example ```json { "servers": [ "pyright", "tsserver" ] } ``` ``` -------------------------------- ### mason_lspconfig_completion.installed_server_completion Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-commands.md Provides completion for currently installed LSP servers. This function is used by the `:LspUninstall` command for completion. ```APIDOC ### Global Completion Function: mason_lspconfig_completion.installed_server_completion #### Description Provides completion for currently installed servers. #### Returns A newline-separated string of all currently installed server names. #### Used by `:LspUninstall` command completion ``` -------------------------------- ### Lua Filetype Mappings Example Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-mappings.md This is an example of how filetypes are mapped to available LSP servers in the auto-generated lua file. It shows a table where keys are filetypes and values are lists of corresponding LSP server names. ```lua lua = { "lua_ls", "luau_lsp" } ``` ```lua rust = { "rust_analyzer" } ``` ```lua python = { "pyright", "pylsp", "basedpyright" } ``` ```lua typescript = { "ts_ls", "denols" } ``` ```lua go = { "gopls", "golangci_lint_ls" } ``` -------------------------------- ### Query Available LSP Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/examples-and-patterns.md Get a count of all LSP servers that are available for installation through Mason. ```lua local available = require("mason-lspconfig").get_available_servers() print("Total available servers: " .. #available) ``` -------------------------------- ### Fixing Setup Order Warning Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/examples-and-patterns.md Demonstrates the correct order for setting up mason, lspconfig, and mason-lspconfig. Ensure `mason.setup()` is called first, followed by `lspconfig` (if used directly), and finally `mason-lspconfig.setup()`. ```lua -- Correct order require("mason").setup() -- FIRST require("lspconfig") -- SECOND (if needed) require("mason-lspconfig").setup() -- THIRD ``` -------------------------------- ### Disable Automatic Server Enabling Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/doc/mason-lspconfig.txt To disable the automatic enabling of installed LSP servers, pass `automatic_enable = false` to the `setup` function. Servers installed outside of Mason will still require manual enabling. ```lua require("mason-lspconfig").setup({ automatic_enable = false }) ``` -------------------------------- ### Get Available LSP Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/doc/mason-lspconfig.txt Retrieves a list of all available LSP servers, including both installed and uninstalled ones. ```APIDOC ## GET /mason-lspconfig/get_available_servers ### Description Returns the available (both installed & uninstalled) LSP servers. ### Method GET ### Endpoint /mason-lspconfig/get_available_servers ### Parameters #### Query Parameters - **filter** (table) - Optional - A table with key-value pairs used to filter the list of server names. - **filetype** (string | string[]) - Optional - Only return servers with matching filetype. ### Response #### Success Response (200) - **servers** (string[]) - A list of nvim-lspconfig server names for available servers. ### Response Example ```json { "servers": [ "lua_ls", "pyright", "tsserver" ] } ``` ``` -------------------------------- ### Disable Automatic Server Enabling Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/README.md Disable the automatic enabling of installed LSP servers by setting `automatic_enable` to `false` in the setup configuration. ```lua require("mason-lspconfig").setup { automatic_enable = false } ``` -------------------------------- ### Correct vs. Incorrect mason.nvim Setup Order Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/errors-and-notifications.md Ensures mason.nvim is set up before mason-lspconfig.nvim to avoid warnings. Call `require("mason").setup()` before `require("mason-lspconfig").setup()`. ```lua -- Wrong order - triggers warning require("mason-lspconfig").setup() require("mason").setup() -- Correct order - no warning require("mason").setup() require("mason-lspconfig").setup() ``` -------------------------------- ### Mason LSP Health Check Success Output Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/errors-and-notifications.md Example of the expected output when the mason-lspconfig.nvim health check passes, indicating a correct setup. ```vim mason-lspconfig.nvim ✓ Neovim v0.11 ✓ mason.nvim v2 ``` -------------------------------- ### Get Mason LSP Status Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/examples-and-patterns.md Retrieves the count of installed and available LSP servers managed by mason-lspconfig. Useful for displaying status information. ```lua local function mason_lspconfig_status() local installed = require("mason-lspconfig").get_installed_servers() local available = require("mason-lspconfig").get_available_servers() return { installed_count = #installed, available_count = #available, installed = installed, } end -- Usage local status = mason_lspconfig_status() print(string.format("Installed: %d/%d servers", status.installed_count, status.available_count)) ``` -------------------------------- ### Package Management with mason-core Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/types.md Shows how to interact with package objects from mason-core, including checking installation status and performing install/uninstall operations. ```lua pkg:is_installed(): boolean -- Check if package is installed pkg:is_installing(): boolean -- Check if installation is in progress pkg:install(opts, callback) -- Install the package pkg:uninstall(callback) -- Uninstall the package pkg.name -- Package name (string) ``` ```lua local registry = require("mason-registry") local pkg = registry.get_package("lua-language-server") if not pkg:is_installed() then pkg:install({}, function(success) -- Install the package if success then print("Installed successfully") end end) end ``` -------------------------------- ### GitHub Actions Workflow for Health Check Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-health.md An example GitHub Actions workflow that automates the installation of Neovim and plugins, then runs the mason-lspconfig health check. ```yaml name: Health Check on: [push] jobs: health: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: rhysd/action-setup-vim@v1 with: neovim: true version: v0.11 - name: Install plugins run: | mkdir -p ~/.config/nvim cp init.lua ~/.config/nvim/ - name: Run health check run: nvim --headless -c "checkhealth mason-lspconfig" -c "qa" ``` -------------------------------- ### Custom Health Checks Example Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-health.md Extends the default health checks by running the core checks and adding a custom check for installed LSP servers. ```lua -- Your custom health check local health = require("mason-lspconfig.health") health.check() -- Additional checks if require("mason-lspconfig").get_installed_servers() then vim.health.ok "LSP servers installed" else vim.health.warn "No LSP servers installed" end ``` -------------------------------- ### Optional Ensure Installed Configuration Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-ensure-installed.md Demonstrates valid configurations for `ensure_installed`, including omitting it entirely, providing an empty list, or specifying a list of servers. The `ensure_installed` feature is entirely optional. ```lua -- Valid - no ensure_installed specified require("mason-lspconfig").setup() -- Valid - empty list require("mason-lspconfig").setup({ ensure_installed = {} }) -- Valid - servers specified require("mason-lspconfig").setup({ ensure_installed = { "lua_ls" } }) ``` -------------------------------- ### Conditional LSP Setup with Executable Check Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/examples-and-patterns.md Conditionally sets up the TypeScript LSP only if the 'node' executable is found in the system's PATH. This prevents errors when Node.js is not installed. ```lua local function setup_lsp() if not vim.fn.executable("node") then print("Node.js not found, skipping TypeScript LSP") return end require("mason-lspconfig").setup({ ensure_installed = { "ts_ls" } }) end setup_lsp() ``` -------------------------------- ### enable_server(mason_pkg) Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-automatic-enable.md Enables a single LSP server based on configuration and available custom configurations. It handles checking inclusion/exclusion lists, applying custom configurations, and finally enabling the server via `vim.lsp.enable()`. ```APIDOC ## Function: enable_server ### Description Enables a single LSP server based on configuration and available custom configurations. It handles checking inclusion/exclusion lists, applying custom configurations, and finally enabling the server via `vim.lsp.enable()`. ### Method `enable_server(mason_pkg: string | Package): void` ### Parameters #### Path Parameters - **mason_pkg** (string | Package) - Required - Mason package name (string) or package object. ### Behavior - Converts package objects to package names if needed. - Looks up the lspconfig server name from the mason package name using mappings. - Skips enabling if the server is already enabled, in the `exclude` list, not in the `include` list (if applicable), or if `automatic_enable` is `false`. - Checks for and applies a custom LSP configuration file at `mason-lspconfig.lsp.` using `vim.lsp.config()`. - Calls `vim.lsp.enable()` to enable the server. - Marks the server as enabled in the `enabled_servers` tracking table. ``` -------------------------------- ### Initialize Automatic Enable Feature Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-automatic-enable.md Initializes the automatic enable feature, setting up event listeners and enabling all currently installed servers. This function is automatically called during setup if the feature is not disabled. ```lua function automatic_enable.init(): void ``` -------------------------------- ### Verify Mason Setup Order Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/examples-and-patterns.md Checks if the 'mason' plugin has been set up by inspecting the `mason.has_setup` variable. This helps verify correct initialization order. ```lua local mason = require("mason") print("mason.has_setup: " .. tostring(mason.has_setup)) -- Should print: mason.has_setup: true ``` -------------------------------- ### Example Custom LSP Configuration Structure Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-automatic-enable.md Demonstrates the structure for custom LSP configurations, showing how to override the default command or configure before_init hooks. These files are located at lua/mason-lspconfig/lsp/.lua. ```lua -- lua/mason-lspconfig/lsp/astro.lua return { before_init = function(_, config) -- Custom initialization logic config.init_options.typescript.serverPath = ... end, } -- lua/mason-lspconfig/lsp/elixirls.lua return { cmd = { "elixir-ls" }, } ``` -------------------------------- ### Direct Configuration with nvim-lspconfig Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/examples-and-patterns.md Sets up mason-lspconfig and then manually configures a specific LSP server (lua_ls) with custom settings. Servers managed by mason-lspconfig are automatically enabled. ```lua -- Set up mason-lspconfig require("mason-lspconfig").setup({ ensure_installed = { "lua_ls", "rust_analyzer" } }) -- Servers are automatically enabled -- But you can still configure them manually vim.lsp.config("lua_ls", { settings = { Lua = { diagnostics = { globals = { "vim" } } } } }) ``` -------------------------------- ### EmmyLua Type Hint Example Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/types.md Example of adding type hints in comments for EmmyLua (Lua Language Server). ```lua ---@type MasonLspconfigSettings local config = { ensure_installed = { "lua_ls" } } ``` -------------------------------- ### resolve_tsdk Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-typescript-utilities.md Resolves the TypeScript SDK path and module path, prioritizing workspace installations and falling back to package-vendored installations. ```APIDOC ## Function: resolve_tsdk ### Description Resolves the TypeScript SDK path and module path, checking both workspace and package installations. It first attempts to use the workspace installation if provided, then falls back to the package's vendored TypeScript. ### Signature ```lua function typescript.resolve_tsdk( package_dir: string, workspace_dir: string? ): string?, string? ``` ### Parameters #### Path Parameters - **package_dir** (string) - Required - Mason package installation directory containing a vendored TypeScript - **workspace_dir** (string) - Optional - Optional workspace directory to check for local TypeScript installation ### Returns - **string?** - Path to TypeScript library directory, or `nil` if resolution fails. - **string?** - Path to the TypeScript server library module, or `nil` if resolution fails. ### Example ```lua local typescript = require("mason-lspconfig.typescript") local package_dir = vim.fn.expand("$MASON/packages/astro-language-server") local workspace_dir = vim.fn.getcwd() local tsdk, tsserver = typescript.resolve_tsdk(package_dir, workspace_dir) if tsdk then print("TypeScript SDK:", tsdk) print("TypeScript Server:", tsserver) else print("Could not resolve TypeScript") end ``` ``` -------------------------------- ### Translate Between Server and Package Names Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/index.md Demonstrates how to use the retrieved mappings to convert between LSP server names and their corresponding Mason package names. ```lua local maps = require("mason-lspconfig").get_mappings() -- Get package name from server name local package = maps.lspconfig_to_package["lua_ls"] -- "lua-language-server" -- Get server name from package name local server = maps.package_to_lspconfig["lua-language-server"] -- "lua_ls" ``` -------------------------------- ### Inspect Mason LSP Settings Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/errors-and-notifications.md Use this snippet to print the current settings of mason-lspconfig.nvim to help diagnose why servers might not be auto-enabling. ```lua local settings = require("mason-lspconfig.settings") print(vim.inspect(settings.current)) ``` -------------------------------- ### get_installed_servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-main-module.md Retrieves a list of LSP server names that are currently installed via Mason. This is useful for checking the current state of installed language servers. ```APIDOC ## Function: get_installed_servers ### Description Returns a list of LSP server names that are currently installed via Mason. ### Parameters None ### Returns A table (array) of lspconfig server names that are currently installed. Returns an empty table if no servers are installed. ### Example ```lua local installed = require("mason-lspconfig").get_installed_servers() for _, server in ipairs(installed) do print(server) -- e.g., "lua_ls", "rust_analyzer" end ``` ``` -------------------------------- ### Correct vs. Incorrect ensure_installed Server Names Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/errors-and-notifications.md Uses lspconfig server names in `ensure_installed`, not mason package names. Check correct names with `:LspInstall`. ```lua -- Wrong - mason package name instead of lspconfig name require("mason-lspconfig").setup({ ensure_installed = { "lua-language-server" } -- Wrong! }) -- Correct - use lspconfig name require("mason-lspconfig").setup({ ensure_installed = { "lua_ls" } -- Correct }) ``` -------------------------------- ### :LspInstall command Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/index.md A Neovim command to install a specified LSP server. This command is exposed directly to the user for interactive use. ```APIDOC ## :LspInstall command ### Description A Neovim command to install a specified LSP server. This command is exposed directly to the user for interactive use. ### Method `:LspInstall` (command) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Usage ```vim :LspInstall ``` ### Example ```vim :LspInstall pyright ``` ### Response None (executes an installation action) ``` -------------------------------- ### Query Installed LSP Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/examples-and-patterns.md Retrieve a list of all LSP servers currently installed via Mason. This is useful for scripting or displaying information about the available servers. ```lua local installed = require("mason-lspconfig").get_installed_servers() print("Installed servers:") for _, server in ipairs(installed) do print(" - " .. server) end ``` -------------------------------- ### Large Project Server Configuration Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-ensure-installed.md A comprehensive list of LSP servers for various development needs (Frontend, Backend, DevOps, General) to be installed via `ensure_installed`. Useful for setting up a development environment with a wide range of language support. ```lua require("mason-lspconfig").setup({ ensure_installed = { -- Frontend "ts_ls", "css_variables", "html", -- Backend "rust_analyzer", "gopls", "pyright", -- DevOps "docker_compose_language_service", "terraform_lsp", "yamlls", -- General "json_ls", "lua_ls", "marksman", "ltex" } }) ``` -------------------------------- ### Enable All Installed Servers Source: https://github.com/mason-org/mason-lspconfig.nvim/blob/main/_autodocs/api-reference-automatic-enable.md Enables all currently installed LSP servers without resetting the tracking state. This is useful during registry refreshes to enable newly discovered servers. ```lua function automatic_enable.enable_all(): void ```