codanna 0.9.19

Code Intelligence for Large Language Models
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Method call representation and resolution
//!
//! This module provides:
//! - `MethodCall` - Structured representation of method calls with receiver information
//! - `MethodCallResolver` - Per-file storage for method calls and variable types
//!
//! # Architecture
//!
//! ```text
//! Parser
//!   ├─ find_variable_types() ──┐
//!   └─ find_method_calls() ────┼─→ MethodCallResolver (per file)
//!                              │      - variable_types: HashMap<String, String>
//!                              │      - method_calls: Vec<MethodCall>
//!//!//!                       LanguageBehavior::resolve_method_call()
//! ```
//!
//! # Separation of Concerns
//!
//! - `MethodCallResolver` - owns DATA (variable types, method calls)
//! - `LanguageBehavior` - owns LOGIC (language-specific resolution)
//! - Indexer - orchestrates WHEN to call what

use crate::Range;
use std::collections::HashMap;

/// Represents a method call with rich receiver information
///
/// This struct captures the full context of a method call, including:
/// - The calling context (which function contains this call)
/// - The method being called
/// - The receiver (if any) and whether it's a static call
/// - The source location
///
/// # Examples
///
/// ```rust
/// use codanna::parsing::MethodCall;
/// use codanna::Range;
///
/// let range = Range::new(1, 0, 1, 10);
///
/// // Instance method: vec.push(item)
/// let call = MethodCall::new("process_items", "push", range)
///     .with_receiver("vec");
///
/// // Static method: String::new()
/// let call = MethodCall::new("create_string", "new", range)
///     .with_receiver("String")
///     .static_method();
///
/// // Self method: self.validate()
/// let call = MethodCall::new("save", "validate", range)
///     .with_receiver("self");
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct MethodCall {
    /// The function/method making the call
    ///
    /// This is the name of the function where this method call appears.
    /// For example, in a function `fn process()` that calls `vec.push()`,
    /// this would be `"process"`.
    pub caller: String,

    /// The method being called
    ///
    /// Just the method name without any qualification.
    /// For `String::new()`, this would be `"new"`.
    pub method_name: String,

    /// The receiver expression (e.g., "self", "vec", "String")
    ///
    /// - `None` for plain function calls
    /// - `Some("self")` for self method calls
    /// - `Some("vec")` for instance method calls
    /// - `Some("String")` for static method calls (when `is_static` is true)
    pub receiver: Option<String>,

    /// Whether this is a static method call (e.g., String::new)
    ///
    /// Used to distinguish between:
    /// - `string.len()` (instance method, is_static = false)
    /// - `String::new()` (static method, is_static = true)
    pub is_static: bool,

    /// Location of the call in the source file (call site)
    pub range: Range,

    /// Definition range of the calling function/method
    ///
    /// Used for precise symbol lookup during relationship resolution.
    /// When provided, enables exact matching instead of name-only fallback.
    pub caller_range: Option<Range>,
}

impl MethodCall {
    /// Creates a new method call with minimal information
    ///
    /// Use the builder methods to add receiver and type information.
    ///
    /// # Arguments
    ///
    /// * `caller` - The function containing this method call
    /// * `method_name` - The name of the method being called
    /// * `range` - The source location of the call
    pub fn new(caller: &str, method_name: &str, range: Range) -> Self {
        Self {
            caller: caller.to_string(),
            method_name: method_name.to_string(),
            receiver: None,
            is_static: false,
            range,
            caller_range: None,
        }
    }

    /// Sets the receiver for this method call
    ///
    /// # Arguments
    ///
    /// * `receiver` - The receiver expression (e.g., "self", "vec", "String")
    pub fn with_receiver(mut self, receiver: &str) -> Self {
        self.receiver = Some(receiver.to_string());
        self
    }

    /// Marks this as a static method call
    ///
    /// Should be used when the receiver is a type name rather than an instance.
    pub fn static_method(mut self) -> Self {
        self.is_static = true;
        self
    }

    /// Sets the definition range of the calling function
    ///
    /// Used for precise symbol lookup during relationship resolution.
    pub fn with_caller_range(mut self, range: Range) -> Self {
        self.caller_range = Some(range);
        self
    }

    /// Checks if this is a self method call
    #[inline]
    pub fn is_self_call(&self) -> bool {
        self.receiver.as_deref() == Some("self")
    }

    /// Checks if this is a plain function call (no receiver)
    #[inline]
    pub fn is_function_call(&self) -> bool {
        self.receiver.is_none()
    }

    /// Gets the fully qualified method name for display
    ///
    /// # Returns
    ///
    /// - `"Type::method"` for static calls
    /// - `"receiver.method"` for instance calls
    /// - `"method"` for plain function calls
    #[must_use = "The formatted name should be used"]
    pub fn qualified_name(&self) -> String {
        match (&self.receiver, self.is_static) {
            (Some(receiver), true) => format!("{receiver}::{}", self.method_name),
            (Some(receiver), false) => format!("{receiver}.{}", self.method_name),
            (None, _) => self.method_name.clone(),
        }
    }
}

/// Owns method call resolution data for a single file
///
/// Collects variable types and method calls during parsing,
/// provides them for resolution in Pass 2.
///
/// # Example
///
/// ```rust
/// use codanna::parsing::{MethodCall, MethodCallResolver};
/// use codanna::Range;
///
/// let mut resolver = MethodCallResolver::new();
///
/// // Register variable types from let bindings
/// resolver.register_variable_type("calc", "Calculator");
///
/// // Add method calls from parsing
/// let call = MethodCall::new("process", "add", Range::new(5, 4, 5, 14))
///     .with_receiver("calc");
/// resolver.add_method_call(call);
///
/// // Later, during resolution:
/// if let Some(mc) = resolver.find_method_call("process", "add") {
///     let receiver_type = resolver.variable_types().get("calc");
///     // Use receiver_type for resolution...
/// }
/// ```
#[derive(Debug, Default)]
pub struct MethodCallResolver {
    /// Variable name to type mappings (e.g., "calc" -> "Calculator")
    variable_types: HashMap<String, String>,
    /// Method calls collected from parsing
    method_calls: Vec<MethodCall>,
}

impl MethodCallResolver {
    /// Creates a new empty resolver
    pub fn new() -> Self {
        Self::default()
    }

    /// Register a variable's type from let binding, parameter, etc.
    ///
    /// # Arguments
    ///
    /// * `var` - The variable name (normalized)
    /// * `type_name` - The type name
    pub fn register_variable_type(&mut self, var: &str, type_name: &str) {
        self.variable_types
            .insert(var.to_string(), type_name.to_string());
    }

    /// Add a method call from parsing
    ///
    /// Returns `true` if added, `false` if duplicate (same caller, method, line, column)
    pub fn add_method_call(&mut self, call: MethodCall) -> bool {
        // Deduplication: check if already exists by (caller, method, line, col)
        let exists = self.method_calls.iter().any(|mc| {
            mc.caller == call.caller
                && mc.method_name == call.method_name
                && mc.range.start_line == call.range.start_line
                && mc.range.start_column == call.range.start_column
        });
        if exists {
            return false;
        }
        self.method_calls.push(call);
        true
    }

    /// Get the variable types map for resolution
    pub fn variable_types(&self) -> &HashMap<String, String> {
        &self.variable_types
    }

    /// Find a method call by caller and method name
    pub fn find_method_call(&self, caller: &str, method_name: &str) -> Option<&MethodCall> {
        self.method_calls
            .iter()
            .find(|mc| mc.caller == caller && mc.method_name == method_name)
    }

    /// Get all method calls
    pub fn method_calls(&self) -> &[MethodCall] {
        &self.method_calls
    }

    /// Check if a method call exists at a specific position
    ///
    /// Used to determine if a function_call is already tracked as a method_call.
    pub fn has_method_call_at(
        &self,
        caller: &str,
        method_name: &str,
        line: u32,
        column: u16,
    ) -> bool {
        self.method_calls.iter().any(|mc| {
            mc.caller == caller
                && mc.method_name == method_name
                && mc.range.start_line == line
                && mc.range.start_column == column
        })
    }

    /// Check if resolver has any data
    pub fn is_empty(&self) -> bool {
        self.method_calls.is_empty() && self.variable_types.is_empty()
    }

    /// Clear all data (for reuse)
    pub fn clear(&mut self) {
        self.variable_types.clear();
        self.method_calls.clear();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_range() -> Range {
        Range::new(10, 5, 10, 20)
    }

    // === MethodCall tests ===

    #[test]
    fn test_basic_construction() {
        let call = MethodCall::new("main", "process", test_range());
        assert_eq!(call.caller, "main");
        assert_eq!(call.method_name, "process");
        assert_eq!(call.receiver, None);
        assert!(!call.is_static);
    }

    #[test]
    fn test_builder_pattern() {
        let call = MethodCall::new("handler", "clone", test_range()).with_receiver("data");

        assert_eq!(call.receiver, Some("data".to_string()));
        assert!(!call.is_static);
    }

    #[test]
    fn test_static_method() {
        let call = MethodCall::new("main", "new", test_range())
            .with_receiver("HashMap")
            .static_method();

        assert_eq!(call.receiver, Some("HashMap".to_string()));
        assert!(call.is_static);
    }

    #[test]
    fn test_helper_methods() {
        let self_call = MethodCall::new("foo", "bar", test_range()).with_receiver("self");
        assert!(self_call.is_self_call());
        assert!(!self_call.is_function_call());

        let func_call = MethodCall::new("main", "println", test_range());
        assert!(!func_call.is_self_call());
        assert!(func_call.is_function_call());
    }

    #[test]
    fn test_qualified_name() {
        // Static method
        let static_call = MethodCall::new("main", "new", test_range())
            .with_receiver("Vec")
            .static_method();
        assert_eq!(static_call.qualified_name(), "Vec::new");

        // Instance method
        let instance_call = MethodCall::new("process", "push", test_range()).with_receiver("items");
        assert_eq!(instance_call.qualified_name(), "items.push");

        // Function call
        let func_call = MethodCall::new("main", "println", test_range());
        assert_eq!(func_call.qualified_name(), "println");
    }

    #[test]
    fn test_equality() {
        let call1 = MethodCall::new("main", "test", test_range()).with_receiver("obj");
        let call2 = MethodCall::new("main", "test", test_range()).with_receiver("obj");
        let call3 = MethodCall::new("main", "test", test_range()).with_receiver("other");

        assert_eq!(call1, call2);
        assert_ne!(call1, call3);
    }

    // === MethodCallResolver tests ===

    #[test]
    fn test_resolver_new() {
        let resolver = MethodCallResolver::new();
        assert!(resolver.is_empty());
        assert!(resolver.variable_types().is_empty());
        assert!(resolver.method_calls().is_empty());
    }

    #[test]
    fn test_resolver_variable_types() {
        let mut resolver = MethodCallResolver::new();

        resolver.register_variable_type("calc", "Calculator");
        resolver.register_variable_type("items", "Vec<Item>");

        assert_eq!(
            resolver.variable_types().get("calc"),
            Some(&"Calculator".to_string())
        );
        assert_eq!(
            resolver.variable_types().get("items"),
            Some(&"Vec<Item>".to_string())
        );
        assert_eq!(resolver.variable_types().get("unknown"), None);
    }

    #[test]
    fn test_resolver_add_method_call() {
        let mut resolver = MethodCallResolver::new();

        let call1 =
            MethodCall::new("process", "add", Range::new(5, 4, 5, 14)).with_receiver("calc");
        let call2 =
            MethodCall::new("process", "multiply", Range::new(6, 4, 6, 18)).with_receiver("calc");

        assert!(resolver.add_method_call(call1));
        assert!(resolver.add_method_call(call2));

        assert_eq!(resolver.method_calls().len(), 2);
    }

    #[test]
    fn test_resolver_deduplication() {
        let mut resolver = MethodCallResolver::new();

        let call1 =
            MethodCall::new("process", "add", Range::new(5, 4, 5, 14)).with_receiver("calc");
        let call2 =
            MethodCall::new("process", "add", Range::new(5, 4, 5, 14)).with_receiver("calc");
        // Different call with same position
        let call3 =
            MethodCall::new("process", "add", Range::new(5, 4, 5, 14)).with_receiver("other");

        assert!(resolver.add_method_call(call1)); // First add succeeds
        assert!(!resolver.add_method_call(call2)); // Exact duplicate rejected
        assert!(!resolver.add_method_call(call3)); // Same position rejected

        assert_eq!(resolver.method_calls().len(), 1);
    }

    #[test]
    fn test_resolver_find_method_call() {
        let mut resolver = MethodCallResolver::new();

        let call = MethodCall::new("process", "add", test_range()).with_receiver("calc");
        resolver.add_method_call(call);

        assert!(resolver.find_method_call("process", "add").is_some());
        assert!(resolver.find_method_call("process", "subtract").is_none());
        assert!(resolver.find_method_call("other", "add").is_none());
    }

    #[test]
    fn test_resolver_clear() {
        let mut resolver = MethodCallResolver::new();

        resolver.register_variable_type("calc", "Calculator");
        resolver.add_method_call(MethodCall::new("process", "add", test_range()));

        assert!(!resolver.is_empty());

        resolver.clear();

        assert!(resolver.is_empty());
        assert!(resolver.variable_types().is_empty());
        assert!(resolver.method_calls().is_empty());
    }

    #[test]
    fn test_resolver_integration() {
        // Simulates the full flow: parsing → storage → lookup
        let mut resolver = MethodCallResolver::new();

        // 1. During parsing, register variable types
        resolver.register_variable_type("calc", "Calculator");
        resolver.register_variable_type("items", "Vec<i32>");

        // 2. During parsing, add method calls
        let call1 = MethodCall::new("process_numbers", "add", Range::new(7, 4, 7, 14))
            .with_receiver("calc");
        let call2 = MethodCall::new("process_numbers", "multiply", Range::new(8, 4, 8, 18))
            .with_receiver("calc");
        let call3 = MethodCall::new("compute_total", "add", Range::new(16, 8, 16, 18))
            .with_receiver("calc");

        resolver.add_method_call(call1);
        resolver.add_method_call(call2);
        resolver.add_method_call(call3);

        // 3. During resolution, lookup
        let mc = resolver.find_method_call("process_numbers", "add").unwrap();
        assert_eq!(mc.receiver.as_deref(), Some("calc"));

        let receiver_type = resolver.variable_types().get(mc.receiver.as_ref().unwrap());
        assert_eq!(receiver_type, Some(&"Calculator".to_string()));
    }
}