Skip to content

Editor Integration

ast-grep is a command line tool for structural search/replace. But it can be readily integrated into your editors and streamline your workflow.

This page introduces several editors that has ast-grep support.

VSCode

ast-grep has an official VSCode extension in the market place.

To get a feel of what it can do, see the introduction on YouTube!

Features

The ast-grep VSCode is an extension to bridge the power of ast-grep and the beloved editor VSCode. It includes two parts:

  • a UI for ast-grep CLI and
  • a client for ast-grep LSP.

Requirement

You need to install ast-grep CLI locally and optionally set up a linting project.

Use pattern to structural search your codebase.

FeatureScreenshot
Search Pattern
Search In Folder

Structural Replace

Use pattern to replace matching code.

FeatureScreenshot
Replace Preview
Commit Replace

Diagnostics and Code Action

Require LSP setup

Code linting and code actions require setting up sgconfig.yml in your workspace root.

FeatureScreenshot
Code Linting

FAQs


Why LSP diagnostics are not working?

You need several things to set up LSP diagnostics:

  1. Install ast-grep CLI. Make sure it is accessible in VSCode editor.
  2. Set up a linting project in your workspace root. The sgconfig.yml file is required for LSP diagnostics.
  3. The LSP server by default is started in the workspace root. Make sure the sgconfig.yml is in the workspace root.

Why ast-grep VSCode cannot find the CLI?

The extension has a different environment from the terminal. You need to make sure the CLI is accessible in the extension environment. For example, if the CLI is installed in a virtual environment, you need to activate the virtual environment in the terminal where you start VSCode.

Here are a few ways to make the CLI accessible:

  1. Install the CLI globally.
  2. Specify the CLI path in the extension settings astGrep.serverPath.
  3. Check if VSCode has the same PATH as the terminal.

Project Root Detection

By default, ast-grep will only start in the workspace root. If you want to start ast-grep in a subfolder, you can specify the configPath in the extension settings. The configPath is the path to the sgconfig.yml file and is relative to the workspace root.

Neovim

nvim-lspconfig

The recommended setup is using nvim-lspconfig.

lua
local configs = require 'lspconfig.configs'
configs.ast_grep = {
  default_config = {
    cmd = {'ast-grep', 'lsp'};
    single_file_support = false;
    root_dir = nvim_lsp.util.root_pattern('sgconfig.yml');
  };
}

coc.nvim

Please see coc-ast-grep

You need to have coc.nvim installed for this extension to work. e.g. vim-plug:

vim
Plug 'yaegassy/coc-ast-grep', {'do': 'yarn install --frozen-lockfile'}

telescope.nvim

telescope-sg is the ast-grep picker for telescope.nvim.

Usage:

vim
Telescope ast_grep

telescope-ast-grep.nvim is an alternative plugin that provides ast-grep functionality enhancements.

More Editors...

More ast-grep editor integration will be supported by the community! Your contribution is warmly welcome.

Made with ❤️ with Rust