3 ms·
To count the number of functions in a rust file you could run: cat main.rs | grep "^\s*fn\s" | wc -l This both promotes the "one thing well" model, while r
by eesmith 3y ago
To count the number of functions in a rust file you could run:
cat main.rs | grep "^\s*fn\s" | wc -l
This both promotes the "one thing well" model, while revealing its inherent limitations.
How many functions are in this rust file?
/* confuse things
fn fo fp
fn fm fl
*/
fn
main() {
println!("Hello, world!");
}
The above grep reports two, when there is only one.
It can be made to work if everyone agrees to follow conventions, like always formatting the Rust code, and indenting comments which look like Rust code.
But that means pushing complexity onto people, rather than into the tools. Which you could do at the beginning, when complexity is low, but it gets increasingly more difficult over time.
- l0b0 3y agoThis sort of heuristic bugs me too. We should be able to write language server-adjacent tools which could do this sort of thing as easily as grep. You could do a lot of useful stuff with that, for example to implement custom linting rules: - Count non-comment tokens within each scope, to look for complex scopes: `foo --tokens --exclude-comments --group-by=scope` - Count expressions (as opposed to just SLOC) per function: `foo --expressions --group-by=function` piped to `uniq` - Get the functions and their argument count: `foo --arguments --count --group-by=function` - Find very short or long names: `foo --names --sort-by=length` piped to `head`/`tail`
- skydhash 3y agoMost languages are open source, it should not be difficult to extract their parser and do this stuff. As far as I know, only Babel ( JavaScript ) and Java have a plugin ecosystem, but only related to compiling.
- deleted 3y ago[deleted]
- eesmith 3y agoWhile I have not tried them out, I know there are tools along these lines. See Tree-sitter at https://tree-sitter.github.io/tree-sitter/ https://tree-sitter.github.io/tree-sitter/ , and as an example project which uses it to parse different languages in a line-oriented manipulable way, see https://pypi.org/project/code-ast/ https://pypi.org/project/code-ast/ . At the very least, the hard part - parsing the different languages - is done.