codeprism_lang_java/
adapter.rs

1//! Adapter to integrate Java parser with codeprism
2
3use crate::parser::{JavaParser, ParseContext as JavaParseContext};
4use crate::types as java_types;
5
6/// Adapter that implements codeprism's LanguageParser trait
7pub struct JavaLanguageParser {
8    parser: std::sync::Mutex<JavaParser>,
9}
10
11impl JavaLanguageParser {
12    /// Create a new Java language parser adapter
13    pub fn new() -> Self {
14        Self {
15            parser: std::sync::Mutex::new(JavaParser::new()),
16        }
17    }
18}
19
20impl Default for JavaLanguageParser {
21    fn default() -> Self {
22        Self::new()
23    }
24}
25
26// Since we can't import codeprism types directly, we'll need to define a conversion
27// trait that the caller can implement
28pub trait ParseResultConverter {
29    type Node;
30    type Edge;
31    type ParseResult;
32
33    fn convert_node(node: java_types::Node) -> Self::Node;
34    fn convert_edge(edge: java_types::Edge) -> Self::Edge;
35    fn create_parse_result(
36        tree: tree_sitter::Tree,
37        nodes: Vec<Self::Node>,
38        edges: Vec<Self::Edge>,
39    ) -> Self::ParseResult;
40}
41
42/// Parse a file and return the result in our internal types
43pub fn parse_file(
44    parser: &JavaLanguageParser,
45    repo_id: &str,
46    file_path: std::path::PathBuf,
47    content: String,
48    old_tree: Option<tree_sitter::Tree>,
49) -> Result<
50    (
51        tree_sitter::Tree,
52        Vec<java_types::Node>,
53        Vec<java_types::Edge>,
54    ),
55    crate::error::Error,
56> {
57    let context = JavaParseContext {
58        repo_id: repo_id.to_string(),
59        file_path,
60        old_tree,
61        content,
62    };
63
64    let mut parser = parser.parser.lock().unwrap();
65    let result = parser.parse(&context)?;
66
67    Ok((result.tree, result.nodes, result.edges))
68}