Skip to content

ydkulks/AST.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AST.nvim

AST implementation for neovim text editor.

Motivation

  • Abstract Syntax Tree (AST), is a tree that represents the code in an abstract way, which will help you understand the code better.
  • Especially when you are working on someone else's codebase
  • It's also helpful to move around in a file quickly

Demo

Telescope

Telescope search with preview

Plenary popup

Popup

Quick Fix List (Fallback)

Quick fix list

Requirements

Quick Start

Using your favorite plugin manager, install this plugin with its dependencies.

Here is an example for packer package manager.

use{
  "ydkulks/AST.nvim",
  requires = {
    "nvim-treesitter/nvim-treesitter",
    "nvim-lua/plenary.nvim"
  },
  config = function()
    require("AST")
  end
}

Usage

Toggle AST Window

Type :ASTToggle in vim command-line to open the AST window. If Plenary is available, it opens a popup window; otherwise, it falls back to the quickfix list.

Popup Mode (with Plenary)

  • Navigate to the node you want to jump to and press <CR>
  • Press q to close the window

Quickfix Mode (fallback)

  • Navigate to the node you want to jump to and press <CR>
  • This jumps to the code location and closes the quickfix window
  • Use :cclose to close without jumping

Search AST Nodes

Type :ASTSearch to populate the quickfix list and open Telescope's fuzzy search (if available) or the standard quickfix window.

  • Use Telescope's search to filter and select nodes
  • Press <CR> to jump to the selected node

Defaults

You can view the defaults here and customize them according to your needs

Customization

The plugin can be configured using the setup() function. Available options:

  • displayNodeNames: true to show node names (e.g., function names), false to show node types (default: true)
  • nodeTypeRequired: Buffer type (key) and a table of nodes and their icons

Example configuration:

require("AST").setup({
  displayNodeNames = true,
  nodeTypeRequired = {
    rust = {
      { "function_declaration", "󰡱 " },
      { "if_statement", "" },
      { "for_statement", "" },
      { "while_statement", "" }
    }
  }
})

Note: You can get the node type value from inspecting the buffer using :InspectTree or by installing treesitter's playground plugin.

User Mappings

Map the commands to keys for quick access.

-- For example:
-- Mapped <leader>t to toggle the AST window
vim.keymap.set("n","<leader>t",":ASTToggle<CR>")
-- Mapped <leader>s to search AST nodes
vim.keymap.set("n","<leader>s",":ASTSearch<CR>")

Highlight Groups

In this file, some highlight groups have been created. Which can be changed in your configuration.

-- Here is an example to configure highlight groups
vim.api.nvim_set_hl(0, 'ASTIcon', { fg = "#98C379", ctermfg = 114 })

Contribute

  • Submissions are welcome. Just open a pull-request under a branch named after your name.
  • DO NOT MAKE CHANGES TO MAIN BRANCH
  • If you find a bug, please create an issue.

About

AST.nvim is Abstract Syntax Tree (AST) implementation for neovim

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages