Crate function_grep
source ·Expand description
§
§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.
Modules§
- For adding new language support, and some predefined support for certain languages,
Macros§
- Use to more easily make new
SupportedLanguage
s. First provide the name (which is used as the type of the language), followed by the tree sitter languge in parenthesis, next you put the file extensions in brackets with a leading . to specify the query we use ?= variable -> string literal query. In the query you when you want use the variable just do {variable}.
Structs§
- The result of finding function with a given name. Use
Self::search_file
orSelf::search_file_with_name
to do the searching.
Enums§
- Errors that we may give back.
Functions§
- Tries to find the appropiate language for the given file [
file_name
] based on the list of languages [langs
] provided. This works by obtaining the extension from the file path and usingget_file_type_from_file_ext
. - Tries to find the appropiate language for the given file extension [
ext
] based on the list of languages [langs
] provided.