top | item 7920382

CGrep: a context-aware grep for source codes

109 points| psibi | 11 years ago |awgn.github.io | reply

35 comments

order
[+] gingerlime|11 years ago|reply
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.
[+] stcredzero|11 years ago|reply
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.

[+] hunt|11 years ago|reply
Taking this idea further, you could perhaps also have examples of use for each language.
[+] bogolisk|11 years ago|reply
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);
[+] egonschiele|11 years ago|reply
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.
[+] chris_wot|11 years ago|reply
I'm not sure where the documentation is.
[+] thinkpad20|11 years ago|reply
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.)

[+] meowface|11 years ago|reply
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.
[+] edderly|11 years ago|reply
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).

[+] githulhu|11 years ago|reply
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.
[+] mh-|11 years ago|reply
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?

[+] yawniek|11 years ago|reply
a binary for os x would be nice, since installing cabal/ghc currently is a bit of a pain
[+] mattgreenrocks|11 years ago|reply
Isn't that just a brew install away?
[+] cheez|11 years ago|reply
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.
[+] mcrider|11 years ago|reply
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.
[+] michaelmior|11 years ago|reply
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/

[+] res0nat0r|11 years ago|reply
Any examples or reasons why I should use this over the_silver_searcher?
[+] volune|11 years ago|reply
How does this compare with Ack?
[+] kumarishan|11 years ago|reply
can it be integrated with sublime text ?
[+] wangweij|11 years ago|reply
Examples or it didn't happen.
[+] invernomut0|11 years ago|reply
e.g. $ cgrep -r --header "stdio.h" to search recursively for headers

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

etc..

[+] WorldWideWayne|11 years ago|reply
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?

[+] TallGuyShort|11 years ago|reply
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.
[+] djeikyb|11 years ago|reply
I've not tried it, but the linked article claims c# is supported. Quote:

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