MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / extract_fields

Method extract_fields

atomic-semantic/src/parsers/java.rs:355–400  ·  view source on GitHub ↗

Extract field declarations from a class body. A single `field_declaration` in Java can declare multiple variables: `private int x, y, z;`

(
        &self,
        node: &Node,
        source: &str,
        file_path: &str,
        entities: &mut Vec<Entity>,
    )

Source from the content-addressed store, hash-verified

353 /// A single `field_declaration` in Java can declare multiple variables:
354 /// `private int x, y, z;`
355 fn extract_fields(
356 &self,
357 node: &Node,
358 source: &str,
359 file_path: &str,
360 entities: &mut Vec<Entity>,
361 ) {
362 let line = node.start_position().row as u32 + 1;
363 let end_line = node.end_position().row as u32 + 1;
364
365 let is_static = self.has_modifier(node, source, "static");
366 let is_final = self.has_modifier(node, source, "final");
367 let exported = self.has_modifier(node, source, "public");
368
369 let kind = if is_static && is_final {
370 EntityKind::Const
371 } else {
372 EntityKind::Variable
373 };
374
375 // Walk children to find variable_declarator nodes
376 let mut cursor = node.walk();
377 for child in node.children(&mut cursor) {
378 if child.kind() == "variable_declarator" {
379 if let Some(name_node) = child.child_by_field_name("name") {
380 let name = self.node_text(&name_node, source);
381
382 let sig_text = self.node_text(node, source);
383 let sig = sig_text
384 .lines()
385 .next()
386 .map(|l| l.trim().trim_end_matches(';').trim().to_string());
387
388 let mut entity = Entity::new(name, kind, file_path, line, end_line);
389 if let Some(s) = sig {
390 entity = entity.with_signature(s);
391 }
392 if exported {
393 entity.exported = true;
394 }
395
396 entities.push(entity);
397 }
398 }
399 }
400 }
401
402 /// Extract an import declaration.
403 fn extract_import(&self, node: &Node, source: &str, file_path: &str) -> Option<Entity> {

Callers 1

walk_treeMethod · 0.80

Calls 9

has_modifierMethod · 0.80
walkMethod · 0.80
childrenMethod · 0.80
with_signatureMethod · 0.80
kindMethod · 0.45
node_textMethod · 0.45
nextMethod · 0.45
linesMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected