pub fn metrics_from_tree(
lang: &LANG,
tree: Tree,
source: Vec<u8>,
path: &Path,
pr: Option<Arc<PreprocResults>>,
options: MetricsOptions,
) -> Result<FuncSpace, MetricsError>Expand description
Returns all function spaces data of a code, reusing a
caller-supplied tree_sitter::Tree instead of running
the bundled parser.
Use this when the caller already drives tree-sitter for
other purposes (e.g. an editor doing incremental
reparsing) and wants the metric walker to share that
parse. The supplied tree must have been produced from
source with the tree_sitter::Language returned by
LANG::get_tree_sitter_language for lang; a mismatch
is not unsafe but yields nonsensical metric values.
Equivalent to get_function_spaces_with_options on the
same (lang, source, path) triple when the same tree is
reproduced internally.
§Examples
use std::path::PathBuf;
use big_code_analysis::{
get_function_spaces, metrics_from_tree, tree_sitter, LANG,
MetricsOptions,
};
let source_code = "fn main() { if true { 1 } else { 2 }; }";
let path = PathBuf::from("foo.rs");
let source = source_code.as_bytes().to_vec();
let mut parser = tree_sitter::Parser::new();
parser
.set_language(
&LANG::Rust
.get_tree_sitter_language()
.expect("rust feature enabled"),
)
.expect("rust grammar pinned to a compatible version");
let tree = parser
.parse(&source, None)
.expect("parser has a language set");
let from_tree = metrics_from_tree(
&LANG::Rust,
tree,
source.clone(),
&path,
None,
MetricsOptions::default(),
)
.unwrap();
let from_bytes =
get_function_spaces(&LANG::Rust, source, &path, None).unwrap();
assert_eq!(
from_tree.metrics.cyclomatic.cyclomatic_sum(),
from_bytes.metrics.cyclomatic.cyclomatic_sum(),
);§Errors
Returns MetricsError::LanguageDisabled when lang’s
per-language Cargo feature is not enabled in this build.
The return type also carries MetricsError::EmptyRoot
for forward compatibility, but the walker does not produce
it today — see the variant doc.