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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/// <summary>
/// Comprehensive C# test file for Codanna parser validation.
/// Tests all major C# language features and relationship tracking.
///
/// This file is designed to validate:
/// - Symbol extraction (classes, interfaces, methods, properties, fields, enums)
/// - Relationship tracking (implements, calls, callers)
/// - Documentation comment parsing
/// - Method call resolution
/// - Interface implementation detection
/// </summary>

// External alias directive (rarely used, for resolving conflicts between assemblies)
extern alias CoreLib;

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Codanna.Examples.CSharp
{
    // === TEST SCENARIO 1: Clear Interface Implementation ===

    /// <summary>
    /// Service interface for data processing.
    /// Tests: Interface symbol extraction, implementer detection
    /// </summary>
    public interface IDataProcessor
    {
        /// <summary>
        /// Process data with the given options.
        /// </summary>
        /// <param name="data">The data to process</param>
        /// <param name="options">Processing options</param>
        /// <returns>Processed result</returns>
        string ProcessData(string data, ProcessingOptions options);

        /// <summary>
        /// Validate data before processing.
        /// </summary>
        bool ValidateData(string data);
    }

    /// <summary>
    /// Concrete implementation of IDataProcessor.
    /// Tests: Class-to-interface relationship, method definitions
    /// </summary>
    public class DataProcessorService : IDataProcessor
    {
        private readonly ILogger _logger;
        private readonly IValidator _validator;

        /// <summary>
        /// Initializes a new instance of the DataProcessorService class.
        /// </summary>
        /// <param name="logger">Logger for diagnostic traces</param>
        /// <param name="validator">Validator for data validation</param>
        public DataProcessorService(ILogger logger, IValidator validator)
        {
            _logger = logger;
            _validator = validator;
        }

        /// <inheritdoc />
        public string ProcessData(string data, ProcessingOptions options)
        {
            // CALLS: ValidateData (should show in get_calls)
            if (!ValidateData(data))
            {
                // CALLS: ILogger.LogError
                _logger.LogError("Invalid data provided");
                throw new ArgumentException("Data validation failed", nameof(data));
            }

            // CALLS: Transform (should show internal call)
            var transformed = Transform(data, options);

            // CALLS: ILogger.LogInfo
            _logger.LogInfo("Data processed successfully");

            return transformed;
        }

        /// <inheritdoc />
        public bool ValidateData(string data)
        {
            // CALLS: IValidator.Validate
            return _validator.Validate(data);
        }

        /// <summary>
        /// Internal transformation method.
        /// Tests: Method call from ProcessData
        /// </summary>
        private string Transform(string data, ProcessingOptions options)
        {
            // CALLS: ProcessingOptions.Apply
            return options.Apply(data);
        }
    }

    // === TEST SCENARIO 2: Base Class and Inheritance ===

    /// <summary>
    /// Base service class providing common functionality.
    /// Tests: Base class extraction, inheritance relationships
    /// </summary>
    public abstract class BaseService
    {
        protected readonly Guid ActivityId;

        protected BaseService()
        {
            ActivityId = Guid.NewGuid();
        }

        /// <summary>
        /// Log a diagnostic message.
        /// Tests: Protected method visibility
        /// </summary>
        protected virtual void LogDiagnostic(string message)
        {
            Console.WriteLine($"[{ActivityId}] {message}");
        }

        /// <summary>
        /// Abstract method to be implemented by derived classes.
        /// </summary>
        public abstract void Execute();
    }

    /// <summary>
    /// Concrete service inheriting from BaseService.
    /// Tests: Inheritance, abstract method implementation
    /// </summary>
    public class ConcreteService : BaseService
    {
        /// <inheritdoc />
        public override void Execute()
        {
            // CALLS: BaseService.LogDiagnostic
            LogDiagnostic("Executing concrete service");
        }
    }

    // === TEST SCENARIO 3: Properties and Fields ===

    /// <summary>
    /// Configuration options class.
    /// Tests: Property extraction, auto-properties, fields
    /// </summary>
    public class ProcessingOptions
    {
        /// <summary>
        /// Gets or sets the timeout in seconds.
        /// </summary>
        public int TimeoutSeconds { get; set; }

        /// <summary>
        /// Gets or sets whether to enable compression.
        /// </summary>
        public bool EnableCompression { get; set; }

        /// <summary>
        /// Gets the configuration name (read-only).
        /// </summary>
        public string Name { get; }

        private readonly DateTime _createdAt;

        /// <summary>
        /// Initializes a new instance with default values.
        /// </summary>
        public ProcessingOptions()
        {
            TimeoutSeconds = 30;
            EnableCompression = false;
            Name = "Default";
            _createdAt = DateTime.UtcNow;
        }

        /// <summary>
        /// Apply options to transform data.
        /// Tests: Method called from other class
        /// </summary>
        public string Apply(string data)
        {
            return EnableCompression ? Compress(data) : data;
        }

        private string Compress(string data)
        {
            return $"compressed[{data}]";
        }
    }

    // === TEST SCENARIO 4: Enums and Constants ===

    /// <summary>
    /// Processing status enumeration.
    /// Tests: Enum extraction, enum member detection
    /// </summary>
    public enum ProcessingStatus
    {
        /// <summary>
        /// Processing is pending.
        /// </summary>
        Pending = 0,

        /// <summary>
        /// Processing is in progress.
        /// </summary>
        InProgress = 1,

        /// <summary>
        /// Processing completed successfully.
        /// </summary>
        Completed = 2,

        /// <summary>
        /// Processing failed with errors.
        /// </summary>
        Failed = 3
    }

    /// <summary>
    /// Constants for configuration values.
    /// Tests: Constant field extraction
    /// </summary>
    public static class Constants
    {
        /// <summary>
        /// Maximum allowed data size in bytes.
        /// </summary>
        public const int MaxDataSize = 1048576; // 1MB

        /// <summary>
        /// Default processing timeout.
        /// </summary>
        public const int DefaultTimeout = 30;

        /// <summary>
        /// Application name constant.
        /// </summary>
        public const string ApplicationName = "Codanna C# Test Suite";
    }

    // === TEST SCENARIO 5: Interface Dependencies (Multiple Interfaces) ===

    /// <summary>
    /// Logger interface for diagnostic tracing.
    /// Tests: Simple interface, method signatures
    /// </summary>
    public interface ILogger
    {
        /// <summary>
        /// Log an informational message.
        /// </summary>
        void LogInfo(string message);

        /// <summary>
        /// Log an error message.
        /// </summary>
        void LogError(string message);
    }

    /// <summary>
    /// Validator interface for data validation.
    /// Tests: Simple interface detection
    /// </summary>
    public interface IValidator
    {
        /// <summary>
        /// Validate the provided data.
        /// </summary>
        bool Validate(string data);
    }

    // === TEST SCENARIO 6: Generic Classes ===

    /// <summary>
    /// Generic result wrapper class.
    /// Tests: Generic type parameter extraction
    /// </summary>
    /// <typeparam name="T">The type of the result value</typeparam>
    public class Result<T>
    {
        /// <summary>
        /// Gets the result value.
        /// </summary>
        public T Value { get; }

        /// <summary>
        /// Gets whether the operation was successful.
        /// </summary>
        public bool IsSuccess { get; }

        /// <summary>
        /// Gets the error message if operation failed.
        /// </summary>
        public string ErrorMessage { get; }

        private Result(T value, bool isSuccess, string errorMessage)
        {
            Value = value;
            IsSuccess = isSuccess;
            ErrorMessage = errorMessage;
        }

        /// <summary>
        /// Create a successful result.
        /// </summary>
        public static Result<T> Success(T value)
        {
            return new Result<T>(value, true, null);
        }

        /// <summary>
        /// Create a failed result.
        /// </summary>
        public static Result<T> Failure(string errorMessage)
        {
            return new Result<T>(default(T), false, errorMessage);
        }
    }

    // === TEST SCENARIO 7: Async Methods ===

    /// <summary>
    /// Async operations handler.
    /// Tests: Async method detection, Task return types
    /// </summary>
    public class AsyncHandler
    {
        /// <summary>
        /// Process data asynchronously.
        /// Tests: Async keyword, Task return type
        /// </summary>
        public async Task<string> ProcessAsync(string data)
        {
            await Task.Delay(100);
            return data.ToUpper();
        }

        /// <summary>
        /// Process with void return (fire and forget).
        /// </summary>
        public async Task ProcessFireAndForgetAsync(string data)
        {
            await Task.Delay(50);
            Console.WriteLine(data);
        }
    }

    // === TEST SCENARIO 8: Nested Classes ===

    /// <summary>
    /// Container class with nested types.
    /// Tests: Nested class extraction
    /// </summary>
    public class Container
    {
        /// <summary>
        /// Nested configuration class.
        /// Tests: Nested class visibility
        /// </summary>
        public class NestedConfig
        {
            public string Setting { get; set; }
        }

        /// <summary>
        /// Private nested helper class.
        /// </summary>
        private class NestedHelper
        {
            public void Help() { }
        }
    }

    // === TEST SCENARIO 9: Extension Methods ===

    /// <summary>
    /// Extension methods for string operations.
    /// Tests: Static class, extension method detection
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// Reverse the string.
        /// Tests: Extension method with 'this' parameter
        /// </summary>
        public static string Reverse(this string input)
        {
            if (string.IsNullOrEmpty(input))
                return input;

            char[] chars = input.ToCharArray();
            Array.Reverse(chars);
            return new string(chars);
        }

        /// <summary>
        /// Check if string is palindrome.
        /// </summary>
        public static bool IsPalindrome(this string input)
        {
            // CALLS: StringExtensions.Reverse
            return input == Reverse(input);
        }
    }

    // === TEST SCENARIO 10: Delegates and Events ===

    /// <summary>
    /// Delegate declaration for data transformation.
    /// Tests: Standalone delegate declaration
    /// </summary>
    public delegate string DataTransformer(string input);

    /// <summary>
    /// Data processing event arguments.
    /// </summary>
    public class DataProcessedEventArgs : EventArgs
    {
        public string Data { get; set; }
        public DateTime ProcessedAt { get; set; }
    }

    /// <summary>
    /// Class with events.
    /// Tests: Event declaration, delegate types
    /// </summary>
    public class EventPublisher
    {
        /// <summary>
        /// Event raised when data is processed.
        /// Tests: event_field_declaration (simple event field)
        /// </summary>
        public event EventHandler<DataProcessedEventArgs> DataProcessed;

        private EventHandler<DataProcessedEventArgs> _customEvent;

        /// <summary>
        /// Custom event with explicit add/remove accessors.
        /// Tests: event_declaration (explicit event with accessors)
        /// </summary>
        public event EventHandler<DataProcessedEventArgs> CustomEvent
        {
            add
            {
                _customEvent += value;
            }
            remove
            {
                _customEvent -= value;
            }
        }

        /// <summary>
        /// Trigger the data processed event.
        /// </summary>
        protected virtual void OnDataProcessed(string data)
        {
            DataProcessed?.Invoke(this, new DataProcessedEventArgs
            {
                Data = data,
                ProcessedAt = DateTime.UtcNow
            });
            _customEvent?.Invoke(this, new DataProcessedEventArgs
            {
                Data = data,
                ProcessedAt = DateTime.UtcNow
            });
        }
    }

    // === TEST SCENARIO 11: Structs ===

    /// <summary>
    /// Point struct for coordinate representation.
    /// Tests: Struct declaration, value type semantics
    /// </summary>
    public struct Point
    {
        public int X { get; set; }
        public int Y { get; set; }

        public Point(int x, int y)
        {
            X = x;
            Y = y;
        }

        /// <summary>
        /// Calculate distance to another point.
        /// </summary>
        public double DistanceTo(Point other)
        {
            int dx = X - other.X;
            int dy = Y - other.Y;
            return Math.Sqrt(dx * dx + dy * dy);
        }
    }

    // === TEST SCENARIO 12: Records ===

    /// <summary>
    /// Record for immutable person data.
    /// Tests: Record declaration (C# 9.0+)
    /// </summary>
    public record Person(string FirstName, string LastName, int Age)
    {
        /// <summary>
        /// Get the full name.
        /// </summary>
        public string FullName => $"{FirstName} {LastName}";
    }

    /// <summary>
    /// Record class with additional members.
    /// </summary>
    public record Employee(string FirstName, string LastName, int Age, string Department)
        : Person(FirstName, LastName, Age);

    // === TEST SCENARIO 13: Indexers ===

    /// <summary>
    /// Collection class with indexer.
    /// Tests: Indexer declaration
    /// </summary>
    public class StringCollection
    {
        private string[] _items = new string[100];

        /// <summary>
        /// Indexer to access items by index.
        /// Tests: Indexer with get/set
        /// </summary>
        public string this[int index]
        {
            get { return _items[index]; }
            set { _items[index] = value; }
        }

        /// <summary>
        /// Named indexer for key-based access.
        /// </summary>
        public string this[string key]
        {
            get { return _items[key.GetHashCode() % 100]; }
            set { _items[key.GetHashCode() % 100] = value; }
        }
    }

    // === TEST SCENARIO 14: Operator Overloading ===

    /// <summary>
    /// Complex number with operator overloading.
    /// Tests: Operator declarations
    /// </summary>
    public struct Complex
    {
        public double Real { get; set; }
        public double Imaginary { get; set; }

        public Complex(double real, double imaginary)
        {
            Real = real;
            Imaginary = imaginary;
        }

        /// <summary>
        /// Addition operator.
        /// Tests: Binary operator overload
        /// </summary>
        public static Complex operator +(Complex a, Complex b)
        {
            return new Complex(a.Real + b.Real, a.Imaginary + b.Imaginary);
        }

        /// <summary>
        /// Subtraction operator.
        /// </summary>
        public static Complex operator -(Complex a, Complex b)
        {
            return new Complex(a.Real - b.Real, a.Imaginary - b.Imaginary);
        }

        /// <summary>
        /// Implicit conversion from double to Complex.
        /// Tests: Conversion operator declaration
        /// </summary>
        public static implicit operator Complex(double real)
        {
            return new Complex(real, 0);
        }

        /// <summary>
        /// Explicit conversion from Complex to double.
        /// </summary>
        public static explicit operator double(Complex c)
        {
            return c.Real;
        }
    }

    // === TEST SCENARIO 15: Destructors ===

    /// <summary>
    /// Resource manager with destructor.
    /// Tests: Destructor/finalizer declaration
    /// </summary>
    public class ResourceManager
    {
        private IntPtr _handle;

        public ResourceManager(IntPtr handle)
        {
            _handle = handle;
        }

        /// <summary>
        /// Destructor/Finalizer.
        /// Tests: Destructor declaration
        /// </summary>
        ~ResourceManager()
        {
            // Cleanup resources
            if (_handle != IntPtr.Zero)
            {
                _handle = IntPtr.Zero;
            }
        }
    }
}