Hacker News new | past | comments | ask | show | jobs | submit login
CGrep: a context-aware grep for source codes (awgn.github.io)
109 points by psibi on June 20, 2014 | hide | past | favorite | 35 comments



This looks like a cool idea. I think a couple of (neat!) examples, stuff you can't easily do with normal grep, will go a very long way. The list of features is secondary in my opinion.


You basically had this with the Smalltalk refactoring Browser, where each search popped up displayed in a browser window. Then you could easily AND such searches together by executing a script in the browser window. Slightly more sophisticated combinations were possible with just a few lines of scripting.

On top of that, everything above applied to syntax-driven rewrites and refactorings.


Taking this idea further, you could perhaps also have examples of use for each language.


Seconded


blah.c:

  void foo(item *x)
  {
      x->blah(1, 2, 3);
      x->blah (4, 5,
            6);
      blah (7, 5, 1);
      foo (4, 5, 6);
      x->blah (4,
            5, 6);
  }
  
  
  void foo2(item y, item *x)
  {
      y.blah(1, 2, 3);
      y.blah (4, 5,
            6);
      blah (7, 5, 1);
      foo (4, 5, 6);
      x->blah (4,
            5, 6);
  }
search in blah.c for a call to blah() with 5 as 2nd argument from any structure or pointer.

  cgrep --code --semantic '_1 . OR -> blah ( _2 , 5, _3 )' blah.c 

output:

  blah.c:6:    x->blah (4, 5,
  blah.c:7:          6);
  blah.c:10:    x->blah (4,
  blah.c:11:          5, 6);
  blah.c:18:    y.blah (4, 5,
  blah.c:19:          6);
  blah.c:22:    x->blah (4,
  blah.c:23:          5, 6);


I've thought about something like this before, and I'm really excited to see how fully-featured it is. Written in Haskell is also a big plus.


Funny, there was a bell-labs project with same name. It seems to be dead[0], but when we travel the time a few years back, we get a wonderful project homepage[1].

[0] http://www1.bell-labs.com/project/wwexptools/cgrep/

[1] https://web.archive.org/web/20080513225010/http://www1.bell-...


I'm not sure where the documentation is.


It's following the illustrious Haskell tradition of barely-documented code.

(Non-snarky follow-up: Haskell code is often woefully under-documented, for whatever reason. Possibly because people think that the type signatures are sufficient, or possibly because Haskell code lends itself to concision and documentation runs counter to that, or possibly because Haskell projects are often the result of one person's work, diminishing the need to explain oneself. In any case, it's an unfortunate fact of many Haskell apps and libraries.)


Yeah, I struggled with that as well. The landing page of any software project like this should at the least have a lot of example syntax with output, plus a glaringly obvious link to full documentation.


It's in the README at the repo: https://github.com/awgn/cgrep


Any examples?


I was curious, but had no idea how to use this on Ubuntu

sudo apt-get install haskell-platform

download and extract the tarball

cd awgn-cgrep-8af1beb

cabal cgrep.cabal

Unfortunately what stops me using it on a large codebase is that I can't exclude directories like .repo (not sure about .git).


Darn, no SQL support. I often find myself wishing something like this existed for SQL, so I could, say, find all places where field X in table Y is updated, or referenced in a WHERE clause predicate, etc.


interesting request; I've wanted something like this before but never enough to really look for it.

I'm surprised there isn't a tool that can do this already.. perhaps under the guise of refactoring?


a binary for os x would be nice, since installing cabal/ghc currently is a bit of a pain


Isn't that just a brew install away?


It is a package install away: http://www.haskell.org/platform/mac.html

On my machine it is significantly slower than `ag` (Silver Searcher) for basic searches, but it should install and run.


ok thats a way. i usually like to install stuff with homebrew.


How do I build this for Windows? I think I have something Haskell related installed because I see lambda as an icon on the .hs files.


What is the difference between this and ack?

http://beyondgrep.com


This is aware of some basic language semantics like literals, identifiers, etc. Or so it seems. Peek at the github page https://github.com/awgn/cgrep for a little more info.


Would love a UI for this and an editor -- I still use TextWrangler/BBEdit daily to grep large chunks of source code and then edit in place (e.g. for refactoring). Just getting the search results requires opening up each file individually then editing, which is just too slow.


JSON output is incredibly useful in combination with jq[1] for processing the output. Some things become much easier than with standard *nix tools IMO.

[1] http://stedolan.github.io/jq/


Have you tried https://github.com/zeux/qgrep ??? Fast regular expression grep for source code with incremental index updates


Any examples or reasons why I should use this over the_silver_searcher?


How does this compare with Ack?


can it be integrated with sublime text ?


Examples or it didn't happen.


e.g. $ cgrep -r --header "stdio.h" to search recursively for headers

$cgrep -r --identifier VARname to search for a variable

etc..


It really sucks that 15 languages that people use less than C# are supported before anyone thinks of adding support for C#.

Why does everyone hate Microsoft's languages and love Apples?


The most reasonable explanation is that this is developed by (an) Apple user(s) and adding support for Apple's languages is simply easier and more valuable than adding languages for a platform they don't use. If it's worth it to you or other C# users, I'm sure they wouldn't mind you submitting a pull request.


They actually do support C#, but I missed it because I read the list too quickly.


I've not tried it, but the linked article claims c# is supported. Quote:

    Languages support for Awk, C, Cpp, Chapel, Csharp, Css,


You know - I totally missed it because of the way it was written. Sorry for complaining! Thanks so much :)




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

Search: