Crates.io | function-grep |
lib.rs | function-grep |
version | 0.1.1 |
source | src |
created_at | 2024-03-20 19:13:56.208607 |
updated_at | 2024-03-20 19:41:08.45895 |
description | find functions in files, powered by tree sitter |
homepage | |
repository | https://github.com/mendelsshop/git_function_history/tree/main/function-grep |
max_upload_size | |
id | 1180879 |
size | 32,582 |
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.
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
.
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())
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())
To see a more full blown example, look at the main example. To use this as cli utility see here
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())
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.