mason-tool-installer.nvim

repository·main·Indexed 20 days ago

https://github.com/whoissethdaniel/mason-tool-installer.nvim

A Neovim plugin that automates the installation and updating of LSPs, linters, and formatters using Mason. It allows users to declare a desired state of tools via the ensure_installed configuration and provides commands for installing, updating, and cleaning tools.

Tokens
1.2K
Snippets
3
Records
4
Agent score
23%

What's inside mason-tool-installer.nvim

  1. Configure mason-tool-installer.nvim

    main

    Use the setup function to define which tools should be managed. By default, it expects Mason package names. If you have specific integration plugins installed, you can use their respective naming conventions (e.g., lspconfig names via mason-lspconfig).

    Key configuration options:

    • ensure_installed: A list of tools. Each tool can be a string or a table specifying version, auto_update, target (architecture), or a condition function.
    • auto_update: If true, checks and updates tools automatically (default: false).
    • run_on_start: If true, runs installation/updates on startup (default: true).
    • start_delay: Delay in milliseconds before starting installation if run_on_start is true.
    • debounce_hours: Minimum hours elapsed since last install/update before running again (relevant for run_on_start).
    • integrations: A table to enable/disable support for alternative naming via mason-lspconfig, mason-null-ls, or mason-nvim-dap.
    require('mason-tool-installer').setup {
      ensure_installed = {
        -- pin a version
        { 'golangci-lint', version = 'v1.47.0' },
    
        -- toggle auto_update per tool
        { 'bash-language-server', auto_update = true },
    
        -- set specific architecture
        { 'terraformls', target = "win_arm64" },
        { 'lua_ls', target = "win_x64" },
    
        -- conditional installation
        { 'gopls', condition = function() return vim.fn.executable('go') == 1 end },
    
        -- simple string for standard Mason names
        'lua-language-server',
        'stylua',
      },
    
      auto_update = false,
      run_on_start = true,
      start_delay = 3000,
      debounce_hours = 5,
      integrations = {
        ['mason-lspconfig'] = true,
        ['mason-null-ls'] = true,
        ['mason-nvim-dap'] = true,
      },
    }
  2. Handle mason-tool-installer installation events

    main

    The plugin emits User events that you can hook into using autocmd:

    1. MasonToolsStartingInstall: Emitted once at the very beginning of an installation process (only if there are packages to install).
    2. MasonToolsUpdateCompleted: Emitted after any installation or update finishes. If using Neovim 0.8+, the e.data argument contains a table of the programs that were just installed or updated.
    -- Listen for the start of installation
    vim.api.nvim_create_autocmd('User', {
      pattern = 'MasonToolsStartingInstall',
      callback = function()
        vim.schedule(function()
          print 'mason-tool-installer is starting'
        end)
      end,
    })
    
    -- Listen for completion and inspect updated tools
    vim.api.nvim_create_autocmd('User', {
      pattern = 'MasonToolsUpdateCompleted',
      callback = function(e)
        vim.schedule(function()
          print(vim.inspect(e.data)) -- e.data contains the list of installed/updated programs
        end)
      end,
    })
  3. Use mason-tool-installer commands

    main

    You can manually manage your tools using the following commands:

    • :MasonToolsInstall: Installs missing tools or tools with incorrect versions.
    • :MasonToolsInstallSync: Blocking version of :MasonToolsInstall (useful for headless Neovim).
    • :MasonToolsUpdate: Installs missing tools AND updates existing ones.
    • :MasonToolsUpdateSync: Blocking version of :MasonToolsUpdate.
    • :MasonToolsClean: Removes installed packages that are not present in your ensure_installed list.