tree-sitter-manager.nvim

repository·main·Indexed 21 days ago

https://github.com/romus204/tree-sitter-manager.nvim

A lightweight parser manager for Neovim 0.12+ that provides a TUI and automated workflows for installing, updating, and managing Tree-sitter parsers and their associated queries. It supports automatic installation on new filetypes, custom parser sources via forks, and granular control over Treesitter highlighting through whitelist and blacklist configurations.

Tokens
2.3K
Snippets
7
Records
10
Agent score
25%

What's inside tree-sitter-manager.nvim

  1. Use the parser management commands and TUI

    main

    The plugin provides several commands and a TUI (Text User Interface) for managing parsers.

    Commands

    • :TSManager: Open the parser management interface.
    • :TSInstall <lang>: Install specific parsers.
    • :TSUninstall <lang>: Uninstall specific parsers.
    • :TSUpdate: Update parsers (use ! to force update all).

    TUI Keybindings

    When the :TSManager window is open, use these keys:

    • i: Install parser under cursor
    • x: Remove parser under cursor
    • u: Update parser under cursor
    • r: Refresh installation status
    • q or <Esc>: Close window
  2. Run tests for tree-sitter-manager.nvim

    main

    You can run tests in headless mode using make or interactively within Neovim using MiniTest.

    Headless Mode

    Use make commands to run tests from your terminal:

    • make update: Download or update mini.nvim dependencies.
    • make test: Run all modules on a predefined set of languages.
    • make test_xxx: Run a specific test file (e.g., make test_install).
    • make test python bash: Run tests only for specific languages.
    • make test all: Run all test cases for all languages.

    To control which languages are tested in headless mode, set the LANGUAGES environment variable to a space-separated list.

    Interactive Mode

    To run tests inside Neovim, use make nvim to open the editor, then use the following commands:

    • :lua MiniTest.run(): Run all modules.
    • :lua MiniTest.run_file(): Run the current file.
    # Update dependencies
    make update
    
    # Run all tests for specific languages
    make test python bash
    
    # Run all test cases for all languages
    make test all
  3. Install tree-sitter-manager.nvim

    main

    Install the plugin using your preferred Neovim plugin manager. Note that the tree-sitter CLI must be installed system-wide for the plugin to function.

    lazy.nvim

    {
      "romus204/tree-sitter-manager.nvim",
      dependencies = {},
      config = function()
        require("tree-sitter-manager").setup()
      end,
    }

    vim.pack

    vim.pack.add {
      { src = "https://github.com/romus204/tree-sitter-manager.nvim" }
    }
    
    require("tree-sitter-manager").setup()
  4. Configure automatic parser installation

    main

    To automatically install missing parsers when opening a new filetype, set auto_install = true. You can prevent specific languages from being auto-installed using the noauto_install list.

    require("tree-sitter-manager").setup({
      auto_install = true,
      noauto_install = {
        "c", "lua", "markdown", "markdown_inline", "query", "vim", "vimdoc"
      },
    })
  5. Configure tree-sitter-manager.nvim

    main

    Use the setup() function to configure parser directories, installation behavior, and highlighting.

    Default Options

    require("tree-sitter-manager").setup({
      parser_dir = vim.fn.stdpath("data") .. "/site/parser",
      query_dir = vim.fn.stdpath("data") .. "/site/queries",
      assume_installed = {}, -- blacklist languages
      ensure_installed = {}, -- parsers to install at startup
      auto_install = false, -- auto-install when a new filetype is encountered
      noauto_install = {}, -- blacklist from auto_install
      highlight = true, -- enable treesitter highlighting (use list to whitelist)
      nohighlight = {}, -- blacklist from highlight
      languages = {}, -- override or add new parser sources
      nerdfont = true, -- use Nerd Font icons in the manager UI
      border = "rounded", -- border style for the TUI window
      min_width = 78, -- minimum size of the TUI
      min_height = 40,
    })
    ```lua
    require("tree-sitter-manager").setup({
      parser_dir = vim.fn.stdpath("data") .. "/site/parser",
      query_dir = vim.fn.stdpath("data") .. "/site/queries",
      assume_installed = {},
      ensure_installed = {},
      auto_install = false,
      noauto_install = {},
      highlight = true,
      nohighlight = {},
      languages = {},
      nerdfont = true,
      border = "rounded",
      min_width = 78,
      min_height = 40,
    })
  6. Configure Treesitter highlighting

    main

    You can control how Treesitter highlighting is applied via the highlight and nohighlight options.

    Opt-out approach (Blacklist)

    Use nohighlight to specify languages that should use standard regex highlighting instead of Treesitter.

    require("tree-sitter-manager").setup({
      nohighlight = { "yaml", "zsh" },
    })

    Opt-in approach (Whitelist)

    Use highlight to specify only the languages that should use Treesitter highlighting. If highlight is an empty table {}, all Treesitter highlighting is disabled.

    require("tree-sitter-manager").setup({
      highlight = { "lua", "c" },
    })
  7. Example: Writing a language installation test

    main

    This example demonstrates how to create a test file tests/test_install.lua that verifies the installation and highlighting for a list of languages using the child helper.

    1. Define the target languages.
    2. Use new_set with a pre_once hook to setup the child process and trigger the installer.
    3. Use parametrize to run the test case for each language.
    4. Use child.works(lang, "highlights") to verify highlighting functionality.
    -- list languages you want to test
    local languages = _G.languages or { "bash", "python", "java" }
    
    local T = new_set({
        hooks = {
            -- setup will set a unique parent directory to `parser_dir` and `query_dir`
            pre_once = function()
                child.setup({ highlight = true })
                child.lua("installer.install(" .. vim.inspect(languages) .. ")")
                -- wait until bash finishes installation
                -- if the installation fails within the timeout (default 60.000 ms)
                -- an error is thrown
                child.wait(languages)
            end,
        },
        parametrize = parametrize(languages),
    })
    
    T["test-case"] = function(lang)
        -- verify that highlighting works for each lang
        -- second argument is optional, default: highlights
        child.works(lang, "highlights")
    end
    
    return T
  8. Write tests using the testing helper module

    main

    When writing tests for this project, use the helper module at tests/child.lua to spawn and run tests in an isolated Neovim child process. This is required for testing asynchronous functions.

    Global Variables

    • _G.languages: The list of languages passed via make test ....
    • eq, neq, er, ner: Aliases for MiniTest.expect (equality, no_equality, error, no_error).
    • child: require("tests.child").
    • tsm: require("tree-sitter-manager").
    • config: require("tree-sitter-manager.config").
    • installer: require("tree-sitter-manager.installer").

    Global Functions

    • new_set(opts, tbl): A wrapper for MiniTest.new_set(opts, tbl) that includes default hooks for child.setup() and child.cleanup().
    • parametrize(list): Wraps every item in a list into a singleton table, useful for parameterizing tests.
  9. Add or override language parsers

    main

    You can add new languages or override existing ones (e.g., using a fork) by providing a languages table in the setup() configuration. Each entry requires an install_info object containing the url and revision.

    Override a built-in language with a fork

    require("tree-sitter-manager").setup({
      languages = {
        cpp = {
          install_info = {
            url = "https://github.com/myfork/tree-sitter-cpp",
            revision = "abc1234",
            queries = "queries",
          },
        },
      },
    })

    Add a new language

    require("tree-sitter-manager").setup({
      languages = {
        mylang = {
          install_info = {
            url = "https://github.com/someone/tree-sitter-mylang",
            queries = "queries/subdir",
          },
        },
      },
    })

    Query behavior

    If queries is unset or nil, the plugin symlinks the bundled runtime/queries/<lang>/ to the query_dir. If you specify a queries path, it must be the relative path to the queries directory within the repository (e.g., "queries").