2 releases

0.1.1 Mar 20, 2024
0.1.0 Mar 20, 2024

#842 in Development tools

Download history 216/week @ 2024-03-19 8/week @ 2024-03-26 40/week @ 2024-04-02

264 downloads per month

MIT license

22KB
321 lines

crates.io Crates.io msrv

function grep

Find functions with a given name in a file, powered by tree sitter.

Use the latest crates.io by putting function-grep = "0.1.1" in your cargo.toml under [dependencies] section.

cli

To install this as a cli utility run:

cargo install function-grep --example=function-grep.

Then to run it use function-grep <FILE> <NAME>.

To see help run function-grep --help.

Examples

When you know the language

use function_grep::{supported_languages::Rust, ParsedFile};
use tree_sitter::Point;
use tree_sitter::Range;

let results = ParsedFile::search_file("foo", "fn foo() {}\n fn bar()\n", &Rust).unwrap();
println!("{:?}", results.results());
assert_eq!(results.results(), &[Range { start_byte: 0, end_byte: 11, start_point: Point { row: 0, column: 0 }, end_point: Point { row: 0, column: 11 } }]);
assert_eq!(results.to_string(), "1: fn foo() {}".to_string())

When you don't know the language

use function_grep::{supported_languages, ParsedFile};
use tree_sitter::Point;
use tree_sitter::Range;

let results = ParsedFile::search_file_with_name("foo", "fn foo() {}\n fn bar()\n", "test.rs",  supported_languages::predefined_languages()).unwrap();
println!("{:?}", results.results());
assert_eq!(results.results(), &[Range { start_byte: 0, end_byte: 11, start_point: Point { row: 0, column: 0 }, end_point: Point { row: 0, column: 11 } }]);
assert_eq!(results.to_string(), "1: fn foo() {}".to_string())

More Examples

To see a more full blown example, look at the main example. To use this as cli utility see here

Using a custom language

use function_grep::{supported_languages::SupportedLanguage, construct_language, ParsedFile};
use tree_sitter::Point;
use tree_sitter::Range;
use tree_sitter::Language;


#[cfg(feature = "rust")]
construct_language!(Rust(tree_sitter_rust::language()).[rs]?=name->

            "((function_item
  name: (identifier) @method-name)
  @method-definition
(#eq? @method-name {name}))
((let_declaration
  pattern: (identifier) @method-name
  value: (closure_expression)) @method-definition
(#eq? @method-name {name}))
((const_item
  name: (identifier) @method-name
  value: (closure_expression)) @method-definition
(#eq? @method-name {name}))
((static_item
  name: (identifier) @method-name
  value: (closure_expression)) @method-definition
(#eq? @method-name {name}))"
);
let results = ParsedFile::search_file("foo", "fn foo() {}\n fn bar()\n", &Rust).unwrap();
println!("{:?}", results.results());
assert_eq!(results.results(), &[Range { start_byte: 0, end_byte: 11, start_point: Point { row: 0, column: 0 }, end_point: Point { row: 0, column: 11 } }]);
assert_eq!(results.to_string(), "1: fn foo() {}".to_string())

Predefined Languages

Theres is built in support for python, c, rust, ocaml, and java. Each predefined language is a feature thats on by default, use no-default-fatures, to select specific languages only.

Dependencies

~3–24MB
~737K SLoC