use std::collections::HashMap;
use std::path::{Path, PathBuf};
use super::annotation::StringConstants;
use super::{extract_string_constants, normalize_path, rename_endpoint};
use crate::models::{EntityKind, ImportScope, ParsedEntity};
type ConstantsCache = HashMap<PathBuf, Option<StringConstants>>;
pub(crate) fn resolve_cross_file_constants(entities: &mut [ParsedEntity], repo_root: &Path) {
let type_files: HashMap<String, String> = entities
.iter()
.filter(|e| is_jvm_type(&e.kind))
.map(|e| (e.fqn.clone(), e.file_path.clone()))
.collect();
let mut cache = ConstantsCache::new();
for entity in entities.iter_mut() {
if entity.kind != EntityKind::HttpEndpoint || !entity.name.contains('<') {
continue;
}
let Some((verb, path)) = entity.name.split_once(' ') else {
continue;
};
let lookup = OwnerLookup {
endpoint: entity,
type_files: &type_files,
repo_root,
};
let resolved = normalize_path(&substitute_placeholders(path, |expr| {
lookup.constant(expr, &mut cache)
}));
if resolved != path {
*entity = rename_endpoint(entity, &format!("{verb} {resolved}"));
}
}
}
fn is_jvm_type(kind: &EntityKind) -> bool {
matches!(
kind,
EntityKind::Class
| EntityKind::Interface
| EntityKind::Enum
| EntityKind::KotlinClass
| EntityKind::KotlinInterface
| EntityKind::KotlinObject
)
}
fn substitute_placeholders(path: &str, mut resolve: impl FnMut(&str) -> Option<String>) -> String {
let mut out = String::with_capacity(path.len());
let mut rest = path;
while let Some(open) = rest.find('<') {
let Some(close) = rest[open..].find('>').map(|i| open + i) else {
break;
};
out.push_str(&rest[..open]);
let expr = &rest[open + 1..close];
match resolve(expr) {
Some(value) => out.push_str(&value),
None => out.push_str(&rest[open..=close]),
}
rest = &rest[close + 1..];
}
out.push_str(rest);
out
}
struct OwnerLookup<'a> {
endpoint: &'a ParsedEntity,
type_files: &'a HashMap<String, String>,
repo_root: &'a Path,
}
impl OwnerLookup<'_> {
fn constant(&self, expr: &str, cache: &mut ConstantsCache) -> Option<String> {
let (owner, name) = expr.rsplit_once('.')?;
let owner_simple = owner.rsplit('.').next()?;
self.owner_fqns(owner)
.into_iter()
.filter_map(|fqn| self.owner_file(&fqn))
.find_map(|file| {
let constants = cache
.entry(file.clone())
.or_insert_with(|| read_constants(&file))
.as_ref()?;
constants.get(&format!("{owner_simple}.{name}")).cloned()
})
}
fn owner_fqns(&self, owner: &str) -> Vec<String> {
if owner.contains('.') {
return vec![owner.to_string()];
}
let Some(scope) = self.endpoint.import_scope.as_deref() else {
return Vec::new();
};
match scope.imported(owner) {
Some(fqn) => vec![fqn.to_string()],
None => scope.implicit_candidates(owner).collect(),
}
}
fn owner_file(&self, owner_fqn: &str) -> Option<PathBuf> {
if let Some(file) = self.type_files.get(owner_fqn) {
return Some(self.repo_root.join(file));
}
let root = source_root(
&self.endpoint.file_path,
self.endpoint.import_scope.as_deref(),
)?;
let relative = owner_fqn.replace('.', "/");
["java", "kt"]
.iter()
.map(|ext| self.repo_root.join(&root).join(format!("{relative}.{ext}")))
.find(|candidate| candidate.is_file())
}
}
fn source_root(file_path: &str, scope: Option<&ImportScope>) -> Option<PathBuf> {
let package_dir = scope?.package.as_deref()?.replace('.', "/");
let dir = Path::new(file_path).parent()?;
let dir = dir.to_str()?.replace('\\', "/");
let root = dir.strip_suffix(&package_dir)?.trim_end_matches('/');
Some(PathBuf::from(root))
}
fn read_constants(path: &Path) -> Option<StringConstants> {
let source = std::fs::read_to_string(path).ok()?;
let (language, lang_name): (tree_sitter::Language, &str) =
match path.extension().and_then(|e| e.to_str()) {
Some("java") => (tree_sitter_java::LANGUAGE.into(), "java"),
Some("kt") => (tree_sitter_kotlin_ng::LANGUAGE.into(), "kotlin"),
_ => return None,
};
let mut parser = tree_sitter::Parser::new();
parser.set_language(&language).ok()?;
let tree = parser.parse(&source, None)?;
Some(extract_string_constants(
tree.root_node(),
source.as_bytes(),
lang_name,
))
}