llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.81k stars 11.91k forks source link

[clangd] feature request: Add ast matcher API to clangd I would love to have a feature in clangd, which enables editor plugins to extend the search capabilities of that editor with an AST search. #66365

Open Febbe opened 1 year ago

Febbe commented 1 year ago

While refactoring or reviewing code I often find problematic code, which occurs in similar forms in the complete codebase. Searching for those via a regex ist cumbersome and does not work for all cases.

My preferred workflow would be:

  1. Activate AST matching
  2. Select code
  3. Press Ctrl+F / Ctrl+H to retrieve the current AST in the search field
  4. Edit Ast to be more generic
  5. Replace AST with other AST (In case of replacement)

In order to implement this in clangd-plugins, clangd itself requires the functionality to

  1. Gain the AST from a code selection
  2. Gain a view of sourcecode-location-range objects from an AST
  3. Allow AST replacements for a sourcecode-location-range object.
llvmbot commented 1 year ago

@llvm/issue-subscribers-clangd

While refactoring or reviewing code I often find problematic code, which occurs in similar forms in the complete codebase. Searching for those via a regex ist cumbersome and does not work for all cases. My preferred workflow would be: 1. Activate AST matching 1. Select code 2. Press Ctrl+F / Ctrl+H to retrieve the current AST in the search field 3. Edit Ast to be more generic 4. Replace AST with other AST (In case of replacement) In order to implement this in clangd-plugins, clangd itself requires the functionality to 1. Gain the AST from a code selection 2. Gain a view of sourcecode-location-range objects from an AST 3. Allow AST replacements for a sourcecode-location-range object.
torshepherd commented 9 months ago

Unrelated, but have you tried https://ast-grep.github.io/? They recently got support for C++ through tree-sitter-cpp

Febbe commented 8 months ago

The thing is, that there is already a tool from LLVM, which supports this: clang-query. Basically I would like to see clang-query integrated to clangd.

torshepherd commented 8 months ago

So clangd does have support for showing AST, just select some text and right click

Febbe commented 8 months ago

No, not showing the AST, I know, that clangd can print it. I want to search and replace in all ASTs of all files with the help of the Clang AST Matcher DSL, just like I can do it either via clang-query or writing my own libtooling program. As I know clang query only supports single files and does not allow replacements and writing a libtooling parser is most often overkill, since I will never need that parser again.