Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow finding all nodes and nodes containing a string #545

Merged
merged 1 commit into from
Mar 12, 2021

Conversation

abrown
Copy link
Contributor

@abrown abrown commented Mar 12, 2021

When using the -f option in rust-code-analysis-cli to find nodes, the current behavior is to return all found nodes if the passed filter string is not one of call|comment|error|string|function|<u16>. I found it useful to be able to find nodes that contained a certain string (e.g. expr) so I added this functionality. Because in testing I found that an empty -f never creates the "accept all" filter, I added a keyword "all" to enable this behavior (also quite useful). The filters now accept all|call|comment|error|string|function|<u16>|<string>.

When using the `-f` option in `rust-code-analysis-cli` to find nodes, the current behavior is to return all found nodes if the passed filter string is not one of `call|comment|error|string|function|<u16>`. I found it useful to be able to find nodes that contained a certain string (e.g. `expr`) so I added this functionality. Because in testing I found that an empty `-f` never creates the "accept all" filter, I added a keyword `"all"` to enable this behavior (also quite useful). The filters now accept `all|call|comment|error|string|function|<u16>|<string>`.
Copy link
Collaborator

@calixteman calixteman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Thank you for your patch.

@calixteman calixteman merged commit 55145e0 into mozilla:master Mar 12, 2021
@calixteman
Copy link
Collaborator

calixteman commented Mar 12, 2021

My first goal with this option was mainly to find errors in asts to fix tree-sitter grammars.
I'd like to implement a kind of grep where we could match some strings in the context of some nodes.
What's your use case ?

@abrown abrown deleted the find-containing branch March 12, 2021 17:31
@abrown
Copy link
Contributor Author

abrown commented Mar 12, 2021

Right now I'm just using rust-code-analysis-cli to do some cross-language exploring (BTW, great tool!). I think it would cool to eventually be able to search for tree patterns (e.g. "while loops that contain..."); maybe that's what you meant by "strings in the context of some nodes" and grep. I would assume that I would have to switch over to using the library instead of the CLI but that is fine. Some preliminary questions along these lines:

  • Is there a way to ask the tool to find X and then look inside of X for Y?
  • And is there a way to ask the tool once we've found an identifier Z in expression X, find me all uses/defines of Z outside of X?

@marco-c
Copy link
Collaborator

marco-c commented Mar 12, 2021

@abrown we have thought about something along those lines. Our dream would be to have something like https://www.youtube.com/watch?v=yqi8U8Q0h2g, but cross-language.

@marco-c
Copy link
Collaborator

marco-c commented Mar 12, 2021

Sorry, I forgot to actually answer your questions.

Is there a way to ask the tool to find X and then look inside of X for Y?

I don't think so, but you could use the --dump option to output a tree, and then implement something like that on top of the output.

And is there a way to ask the tool once we've found an identifier Z in expression X, find me all uses/defines of Z outside of X?

Nope, I guess you could build it on top of the --dump option too.

@calixteman might have more ideas, as he's way more expert than me on this project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants