knot 1.16.2

Codebase Graph + Vector RAG Indexer for Java, TypeScript, JavaScript, Kotlin, Rust, Python, Groovy, C/C++, C#, Build Systems, and HTML/CSS codebases
Documentation
use super::inheritance::try_extract_type_declaration;
use crate::models::{ParsedEntity, ReferenceIntent};

/// Scans source for method call patterns and returns reference intents.
// Reserved for future reference extraction
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)
}

/// Number of arguments of a parenthesized call, given the text from its `(`
/// to the end of the line. Commas inside strings, closures, lists, maps and
/// nested calls do not separate arguments, and a trailing closure
/// (`each(1) { ... }`) is one more argument.
///
/// `None` whenever the count is uncertain: named arguments (Groovy collects
/// them into a single `Map` parameter), a spread (`*parts`), or an argument
/// list that does not close on this line.
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))
}

/// `name: value` / `'name': value` (a named argument) or `*values` (a spread).
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("::")
    })
}

/// `receiver.method(` → the call, plus the text after `method` so the caller
/// can skip the receiver path and the method name. The receiver keeps its
/// whole dotted path (`StringUtils.INSTANCE.countWords(` → receiver
/// `StringUtils.INSTANCE`).
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);
            // The receiver path and the method name are part of this call:
            // scanned again, the name would be reported as a bare call too
            // (issue #43).
            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) {
            // A type name followed by its body's `{` is a declaration, not a
            // closure-only call (`class Worker {` / `class Foo extends Bar {`).
            // The same applies to an anonymous class body (`new Handler {`).
            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);
    }
}

/// Scans source for method call patterns and returns reference intents.
// Reserved for future reference extraction
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
}
/// Index of the delimiter closing the one `text` opens with (`(` → `)`,
/// `{` → `}`), skipping string literals and nested pairs.
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
}

/// The text inside a method body that opens on the method's declaration
/// line: `String wide(Formatter f) { f.format('x', 80) }` →
/// ` f.format('x', 80) `; up to the end of the line when the body continues
/// on the next ones. `None` when the line does not declare `name(` (or
/// `"name"(`) or the body does not open on it.
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])
}

/// Splits an identifier from the start of `s`, returns (identifier, rest).
// Reserved for future FQN construction
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..]))
}