(
path: &str,
source: &str,
language: Language,
tree: &Tree,
extract: impl FnOnce() -> Vec<Entity>,
)
| 405 | |
| 406 | impl SourceAnalysis { |
| 407 | pub(crate) fn from_tree( |
| 408 | path: &str, |
| 409 | source: &str, |
| 410 | language: Language, |
| 411 | tree: &Tree, |
| 412 | extract: impl FnOnce() -> Vec<Entity>, |
| 413 | ) -> Result<Self, String> { |
| 414 | if tree.root_node().has_error() { |
| 415 | return Err("Source has recovered/missing syntax; semantic facts unavailable".into()); |
| 416 | } |
| 417 | let mut nodes = Vec::new(); |
| 418 | let mut stack = vec![(tree.root_node(), 0)]; |
| 419 | while let Some((node, depth)) = stack.pop() { |
| 420 | if nodes.len() >= 100_000 || depth > 256 { |
| 421 | return Err("Semantic source analysis budget exceeded".into()); |
| 422 | } |
| 423 | nodes.push(node); |
| 424 | stack.extend( |
| 425 | children(node) |
| 426 | .into_iter() |
| 427 | .rev() |
| 428 | .map(|node| (node, depth + 1)), |
| 429 | ); |
| 430 | } |
| 431 | let mut result = Self { |
| 432 | path: path.into(), |
| 433 | language, |
| 434 | entities: extract(), |
| 435 | symbols: vec![], |
| 436 | calls: vec![], |
| 437 | imports: vec![], |
| 438 | modules: vec![], |
| 439 | globals: BTreeMap::new(), |
| 440 | tokens: tokens(tree.root_node(), source, None, false), |
| 441 | outside_tokens: tokens(tree.root_node(), source, None, true), |
| 442 | exports_complete: true, |
| 443 | }; |
| 444 | let mut used = BTreeSet::new(); |
| 445 | let mut declarations = BTreeMap::<(u32, String), Vec<Node<'_>>>::new(); |
| 446 | let mut declarations_by_line = BTreeMap::<u32, Vec<Node<'_>>>::new(); |
| 447 | for node in nodes.iter().filter(|node| callable(**node)) { |
| 448 | declarations_by_line |
| 449 | .entry(node.start_position().row as u32 + 1) |
| 450 | .or_default() |
| 451 | .push(*node); |
| 452 | if let Some(name) = node.child_by_field_name("name") { |
| 453 | declarations |
| 454 | .entry(( |
| 455 | node.start_position().row as u32 + 1, |
| 456 | text(name, source).into(), |
| 457 | )) |
| 458 | .or_default() |
| 459 | .push(*node); |
| 460 | } |
| 461 | } |
| 462 | let mut symbol_nodes = BTreeMap::new(); |
| 463 | let file_conditional = children(tree.root_node()).iter().any(|node| { |
| 464 | node.kind() == "inner_attribute_item" && text(*node, source).contains("cfg") |
nothing calls this directly
no test coverage detected