I'd like a command line tool to find where a symbol is defined. The compiler should know it so it should be a matter of exposing it to user.
I couldn't find how to use nimsuggest for that although its jump to definition should use that I believe.
For a more complete tool these would be nice:
eg usage:
## find location where `parseFoo` is defined
nimfind --symbol:parseFoo --type:definition
foo.bar(10,13): proc parseFoo(): auto =
## likewise, with declaration(s)
nimfind --symbol:parseFoo --type:declaration
## likewise, with usage(s), limit to 100
nimfind --symbol:parseFoo --type:usage --limit:100
## find definition, with a regex
nimfind --symbol:'^parse\w{3}' --type:definition --regex
## find definition, with a specified kind
nimfind --symbol:parseFoo --type:definition --kind:proc|template|iterator
## find definition, filtered by constraints on arguments, eg: 2nd arg of type string, 3rd arg w name foo and output type is void or string
nimfind --symbol:parseFoo --type:definition --arg:'(?, ?:string, foo:?) --out:'void|string'
## find definition, filtered by user defined macro (that can do arbitrary logic)
nimfind --macro:file_containing_macro.nim
NOTE: the search would depend on passed in cfg files or other --define:SYMBOL flags
How complex would it be to build this tool (given that the compiler already must be doing most of this)? I'm guessing a compiler plugin would be the right way to go; is nimsuggest the closest existing tool?
Eg, if there's a way the plugin can intercept the place in the compiler where semantic analysis is completed on a given proc, the filtering logic can be applied there as a macro (builtin the tool or user defined)
Nimsuggest is documented here:
https://nim-lang.org/docs/nimsuggest.html
Unfortuantely it still lacks examples, PRs are welcome.
thanks, indeed examples would help.
here's a start:
echo sug import_all.nim | nimsuggest --hints:off --stdin import_all.nim > all.txt
followed by fast queries:
grep pattern all.txt
this is already very useful but to support queries like in the OP without bloating nimsuggest with lots of functions, maybe we could add to nimsuggest an option for machine readable output (eg json), or expose an API.
Then we can build such a tool (let's call it nimquery) on top of it. I think that would be very useful. Will report back.
NOTE: the closest I can think of is clang-query, eg: https://eli.thegreenplace.net/2014/07/29/ast-matchers-and-clang-refactoring-tools among other uses, it can be used for API exploration, do complex refactorings, etc.