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.
Structural Search
Use pattern to structural search your codebase.
Feature | Screenshot |
---|---|
Search Pattern | |
Search In Folder |
Structural Replace
Use pattern to replace matching code.
Feature | Screenshot |
---|---|
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.
Feature | Screenshot |
---|---|
Code Linting |
FAQs
Why LSP diagnostics are not working?
You need several things to set up LSP diagnostics:
- Install ast-grep CLI. Make sure it is accessible in VSCode editor.
- Set up a linting project in your workspace root. The
sgconfig.yml
file is required for LSP diagnostics. - 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:
- Install the CLI globally.
- Specify the CLI path in the extension settings
astGrep.serverPath
. - 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.
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:
Plug 'yaegassy/coc-ast-grep', {'do': 'yarn install --frozen-lockfile'}
telescope.nvim
telescope-sg is the ast-grep picker for telescope.nvim.
Usage:
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.