nvim-treesitter / playground

Treesitter playground integrated into Neovim
Apache License 2.0
895 stars 47 forks source link
hacktoberfest neovim nvim-treesitter tree-sitter

Neovim Treesitter Playground

View treesitter information directly in Neovim!

demo

Deprecation notice

This plugin is deprecated since the functionality is included in Neovim: Use

Requirements

Setup

Install the plugin (vim-plug shown):

Plug 'nvim-treesitter/nvim-treesitter'
Plug 'nvim-treesitter/playground'

It's also recommended that you install the query parser for query editor highlighting. Run this after installing the above plugins.

:TSInstall query

The configuration is like any other nvim-treesitter module.

require "nvim-treesitter.configs".setup {
  playground = {
    enable = true,
    disable = {},
    updatetime = 25, -- Debounced time for highlighting nodes in the playground from source code
    persist_queries = false, -- Whether the query persists across vim sessions
    keybindings = {
      toggle_query_editor = 'o',
      toggle_hl_groups = 'i',
      toggle_injected_languages = 't',
      toggle_anonymous_nodes = 'a',
      toggle_language_display = 'I',
      focus_language = 'f',
      unfocus_language = 'F',
      update = 'R',
      goto_node = '<cr>',
      show_help = '?',
    },
  }
}

Usage

The tree can be toggled using the command :TSPlaygroundToggle.

Keybindings

Query Editor

Press o to show the query editor. Write your query like (node) @capture, put the cursor under the capture to highlight the matches.

Completions

When you are on a query buffer, you can get a list of suggestions with Ctrl-X Ctrl-O. See :h 'omnifunc'.

Query Linter

The playground can lint query files for you. For that, you need to activate the query_linter module:

require "nvim-treesitter.configs".setup {
  query_linter = {
    enable = true,
    use_virtual_text = true,
    lint_events = {"BufWrite", "CursorHold"},
  },
}

Note: Query linter assumes certain directory structure to identify which language queries belong to. It expect query files to be under ./queries/<language_name>

image

Show treesitter and syntax highlight groups under the cursor

The playground comes with :TSHighlightCapturesUnderCursor that shows any treesitter or syntax highlight groups under the cursor.

Show treesitter node under the cursor

If you only wish to view information about the node your cursor is currently on (without having to open up the full tree), you can use :TSNodeUnderCursor instead. A floating window containing information about the parser, node name and row/col ranges will be shown.