Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

My muscle memory is still dialed in to `ack`, but I recently built an open source tool called `grep-ast` [0] that serves a similar function to ripgrep, ack, etc. The difference is that it shows matching lines in the context of the functions/methods/classes/etc that contain them.

It uses the abstract syntax tree (AST) of the source code to show how the matching lines fit into the code structure. It shows relevant code from every layer of the AST, above and below the matches.

It feels quite useful when you're grepping to understand how functions, classes, variables etc are used within a non-trivial codebase.

Here's a snippet that shows grep-ast searching the django repo. Notice that it finds `ROOT_URLCONF` and then shows you the method and class that contain the matching line, including a helpful part of the docstring. If you ran this in the terminal, it would also colorize the matches.

  django$ gast ROOT_URLCONF

  middleware/locale.py:

  │from django.conf import settings
  │from django.conf.urls.i18n import is_language_prefix_patterns_used
  │from django.http import HttpResponseRedirect
  ⋮...
  │class LocaleMiddleware(MiddlewareMixin):
  │    """
  │    Parse a request and decide what translation object to install in the
  │    current thread context. This allows pages to be dynamically translated to
  │    the language the user desires (if the language is available).
  ⋮...
  │    def process_request(self, request):
  ▶        urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
[0] https://github.com/paul-gauthier/grep-ast


have you looked at semgrep? This isn't the same thing but is similarly poking at the whole "ASTs are a thing we want to grep at" problem


Thanks for the pointer. There are definitely a few tools that have explored the idea of searching the AST of code. Semgrep seems to do that, as does a tool called ast-grep [0].

Both of them are sort of doing the opposite of my tool. They are letting you specify your search as a chunk of code/AST.

My tool let's you grep a regex as usual, but shows you the matches in a helpful AST aware way.

[0] https://ast-grep.github.io/


Thanks for making this. Immediately useful.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: