use super::inheritance::try_extract_type_declaration;
use crate::models::{ParsedEntity, ReferenceIntent};
use std::iter::Peekable;
use std::str::CharIndices;
const GROOVY_KEYWORDS: [&str; 40] = [
"if",
"else",
"while",
"for",
"return",
"new",
"throw",
"catch",
"switch",
"case",
"import",
"package",
"class",
"interface",
"trait",
"enum",
"def",
"try",
"finally",
"assert",
"println",
"void",
"int",
"String",
"boolean",
"double",
"float",
"long",
"byte",
"short",
"char",
"public",
"private",
"protected",
"static",
"final",
"abstract",
"synchronized",
"volatile",
"transient",
];
fn skip_string_literal(chars: &mut Peekable<CharIndices<'_>>, quote: char) {
while let Some((_, nc)) = chars.next() {
if nc == quote {
break;
}
if nc == '\\' {
let _ = chars.next();
}
}
}
fn read_word<'a>(
trimmed: &'a str,
start: usize,
first_char: char,
chars: &mut Peekable<CharIndices<'a>>,
) -> (&'a str, &'a str) {
let mut end = start + first_char.len_utf8();
while let Some(&(i, nc)) = chars.peek() {
if nc.is_alphanumeric() || nc == '_' {
end = i + nc.len_utf8();
chars.next();
} else {
break;
}
}
let word = &trimmed[start..end];
let after_word = &trimmed[end..];
(word, after_word)
}
fn argument_count(text: &str) -> Option<usize> {
let body = text.strip_prefix('(')?;
let mut depth = 0usize;
let mut args: Vec<&str> = Vec::new();
let mut arg_start = 0;
let mut chars = body.char_indices().peekable();
let mut close = None;
while let Some((i, c)) = chars.next() {
match c {
'"' | '\'' => skip_string_literal(&mut chars, c),
'(' | '[' | '{' => depth += 1,
')' | ']' | '}' if depth > 0 => depth -= 1,
')' => {
close = Some(i);
break;
}
',' if depth == 0 => {
args.push(&body[arg_start..i]);
arg_start = i + 1;
}
_ => {}
}
}
let close = close?;
let last = &body[arg_start..close];
if !(args.is_empty() && last.trim().is_empty()) {
args.push(last);
}
if args.iter().any(|arg| is_named_or_spread_argument(arg)) {
return None;
}
let trailing_closure = body[close + 1..].trim_start().starts_with('{');
Some(args.len() + usize::from(trailing_closure))
}
fn is_named_or_spread_argument(arg: &str) -> bool {
let arg = arg.trim();
if arg.starts_with('*') {
return true;
}
let key_end = match arg.chars().next() {
Some(q @ ('"' | '\'')) => arg[1..].find(q).map(|i| i + 2),
Some(c) if c.is_alphabetic() || c == '_' => Some(
arg.find(|c: char| !c.is_alphanumeric() && c != '_')
.unwrap_or(arg.len()),
),
_ => None,
};
key_end.is_some_and(|end| {
let rest = arg[end..].trim_start();
rest.starts_with(':') && !rest.starts_with("::")
})
}
fn receiver_call<'a>(
word: &'a str,
after_trimmed: &'a str,
line_num: usize,
) -> Option<(ReferenceIntent, &'a str)> {
let mut path = vec![word];
let mut after = after_trimmed;
loop {
let dot_trimmed = after.strip_prefix('.')?.trim_start();
let (next_word, rest) = split_identifier(dot_trimmed)?;
let after_next = rest.trim_start();
if after_next.starts_with('(') || after_next.starts_with('{') {
let arg_count = if after_next.starts_with('{') {
Some(1)
} else {
argument_count(after_next)
};
let call = ReferenceIntent::Call {
method: next_word.to_string(),
receiver: Some(path.join(".")),
line: line_num,
arg_count,
};
return Some((call, rest));
}
path.push(next_word);
after = after_next;
}
}
fn bare_call(word: &str, after_trimmed: &str, line_num: usize) -> Option<ReferenceIntent> {
if GROOVY_KEYWORDS.contains(&word) || word.len() <= 1 {
return None;
}
if after_trimmed.starts_with('(') {
return Some(ReferenceIntent::Call {
method: word.to_string(),
receiver: None,
line: line_num,
arg_count: argument_count(after_trimmed),
});
}
if after_trimmed.starts_with('{') {
return Some(ReferenceIntent::Call {
method: word.to_string(),
receiver: None,
line: line_num,
arg_count: Some(1),
});
}
if !after_trimmed.is_empty()
&& !after_trimmed.starts_with('.')
&& !after_trimmed.starts_with('=')
&& !after_trimmed.starts_with(')')
&& !after_trimmed.starts_with(':')
&& !after_trimmed.starts_with(';')
{
let first_arg_char = after_trimmed.chars().next()?;
if first_arg_char == '"'
|| first_arg_char == '\''
|| first_arg_char.is_alphabetic()
|| first_arg_char == '$'
{
return Some(ReferenceIntent::Call {
method: word.to_string(),
receiver: None,
line: line_num,
arg_count: None,
});
}
}
None
}
pub(super) fn scan_line_for_calls(trimmed: &str, line_num: usize, refs: &mut Vec<ReferenceIntent>) {
let is_type_declaration = try_extract_type_declaration(trimmed).is_some();
let mut previous_word = None;
let mut chars = trimmed.char_indices().peekable();
while let Some((i, c)) = chars.next() {
if c == '"' || c == '\'' {
skip_string_literal(&mut chars, c);
continue;
}
if !c.is_alphabetic() && c != '_' {
continue;
}
let (word, after_word) = read_word(trimmed, i, c, &mut chars);
if GROOVY_KEYWORDS.contains(&word) {
previous_word = Some(word);
continue;
}
let after_trimmed = after_word.trim_start();
if let Some((r, rest)) = receiver_call(word, after_trimmed, line_num) {
refs.push(r);
let method_end = trimmed.len() - rest.len();
while chars.next_if(|&(j, _)| j < method_end).is_some() {}
} else if let Some(r) = bare_call(word, after_trimmed, line_num) {
let is_type_body = (is_type_declaration || previous_word == Some("new"))
&& after_trimmed.starts_with('{');
if !is_type_body {
refs.push(r);
}
}
previous_word = Some(word);
}
}
pub(super) fn extract_method_calls(
source: &str,
_entities: &[ParsedEntity],
) -> Vec<ReferenceIntent> {
let mut refs = Vec::new();
for (line_idx, line) in source.lines().enumerate() {
let line_num = line_idx + 1;
let trimmed = line.trim();
if trimmed.starts_with("//")
|| trimmed.starts_with("/*")
|| trimmed.starts_with("*")
|| trimmed.starts_with("package ")
|| trimmed.starts_with("import ")
{
continue;
}
scan_line_for_calls(trimmed, line_num, &mut refs);
}
refs
}
fn closing_index(text: &str, open: char, close: char) -> Option<usize> {
let mut depth = 0usize;
let mut chars = text.char_indices().peekable();
while let Some((i, c)) = chars.next() {
match c {
'"' | '\'' => skip_string_literal(&mut chars, c),
c if c == open => depth += 1,
c if c == close => {
depth = depth.checked_sub(1)?;
if depth == 0 {
return Some(i);
}
}
_ => {}
}
}
None
}
pub(super) fn body_on_declaration_line<'a>(line: &'a str, name: &str) -> Option<&'a str> {
let is_word = |c: char| c.is_alphanumeric() || c == '_';
let open_paren = line.match_indices(name).find_map(|(start, _)| {
if line[..start].chars().next_back().is_some_and(is_word) {
return None;
}
let after = start + name.len();
let rest = line[after..].trim_start_matches(['"', '\'']).trim_start();
rest.starts_with('(').then(|| line.len() - rest.len())
})?;
let close_paren = open_paren + closing_index(&line[open_paren..], '(', ')')?;
let open_brace = close_paren + line[close_paren..].find('{')?;
let body_end =
closing_index(&line[open_brace..], '{', '}').map_or(line.len(), |i| open_brace + i);
Some(&line[open_brace + 1..body_end])
}
fn split_identifier(s: &str) -> Option<(&str, &str)> {
let first = s.chars().next()?;
if !first.is_alphabetic() && first != '_' {
return None;
}
let end = s
.find(|c: char| !c.is_alphanumeric() && c != '_')
.unwrap_or(s.len());
Some((&s[..end], &s[end..]))
}