weggli icon indicating copy to clipboard operation
weggli copied to clipboard

Finding function declarations

Open iulian3144 opened this issue 3 years ago • 4 comments

I would like to find function declarations only but it seems that weggli does not support this at the moment.

What I have tried: $ weggli -R func=myFoo '_ $func(_);' .

If this is indeed not supported, an enhancement would be nice even though this may be a little out of scope for the tool (based on its description).

iulian3144 avatar Apr 20 '22 18:04 iulian3144

If you run weggli with verbosity turned on, your query is probably getting normalized into a compound_statement. If you're looking for content outside of a compound_statement, you might be able to trim out that logic in parse_search_pattern (src/main.rs).

I'm not sure, but you also might need to anchor to a different tree-sitter-c rule which are those things listed in VALID_NODE_KINDS (src/main.rs). Weggli is anchoring to the definitions themselves, so declarations might be out of scope.

arizvisa avatar Jul 20 '22 17:07 arizvisa

Just adding "declaration" in the VALID_NODE_KINDS makes it work. Thanks a lot for the hint, @arizvisa. I can see why this behavior would not be desired by default but maybe it would be nice to at least have a flag that enables this. I think that something like --list-kinds / --kinds (similar to ctags) would work great.

$ weggli --list-kinds
d declaration         [off]
p compound_statement  [on]
f function_definition [on]
s struct_specifier    [on]
e enum_specifier      [on]
u union_specifier     [on]
c class_specifier     [on]

$ weggli --kinds=+d-u --list-kinds
d declaration         [on]
p compound_statement  [on]
f function_definition [on]
s struct_specifier    [on]
e enum_specifier      [on]
u union_specifier     [off]
c class_specifier     [on]

iulian3144 avatar Jul 21 '22 17:07 iulian3144

Yeah, agreed. Having variable granularity on what's considered an anchor point when filtering your query is pretty useful.

Very cool that it worked out. I've had to patch it similarly so that I could chain queries together via sh pipeline ([definition|specifier] -> [declaration|expression]). Something like that anyways.

arizvisa avatar Jul 22 '22 23:07 arizvisa

Thanks for the bug report and sorry for the slow response. I didn't have much time to work on weggli in the last weeks.

I think disabling the VALID_NODE_KINDS check + anchoring based on a flag would solve this. I'll take a stab at this, but it might take a bit before I find the time.

felixwilhelm avatar Jul 28 '22 13:07 felixwilhelm