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
/**
 * Comprehensive Kotlin test file for parser maturity assessment
 * Tests all major Kotlin language features and constructs
 */

package com.example.comprehensive

import kotlin.collections.List
import kotlin.collections.Map
import java.util.*

// === TEST SCENARIO: Clear Relationship Testing ===

/**
 * Test service for demonstrating clear relationships
 */
class TestService(
    val name: String,
    private val config: Config
) {
    /**
     * Create a new test service with default config
     */
    constructor(name: String) : this(name, Config.default())

    /**
     * Process data using config - this should show calls
     */
    fun process(): String {
        val result = getConfigName()  // CALLS: TestService.getConfigName
        return "Processing: $result"
    }

    /**
     * Helper method that will be called by process
     */
    private fun getConfigName(): String {
        return config.getDisplayName()  // CALLS: Config.getDisplayName
    }

    /**
     * Nested class example
     */
    class NestedClass {
        fun doSomething() {}
    }

    /**
     * Inner class example
     */
    inner class InnerClass {
        fun accessOuter() = name
    }

    companion object {
        /**
         * Factory method in companion object
         */
        fun create(name: String): TestService {
            return TestService(name)
        }

        const val DEFAULT_NAME = "default"
    }
}

// === DATA CLASSES ===

/**
 * Data class for configuration
 */
data class Config(
    val displayName: String,
    val enabled: Boolean = true,
    var retries: Int = 3
) {
    fun getDisplayName(): String = displayName

    companion object {
        fun default(): Config = Config("Default Config")
    }
}

/**
 * User data class with multiple properties
 */
data class User(
    val id: Long,
    val email: String,
    val name: String?
)

// === INTERFACES ===

/**
 * Repository interface
 */
interface Repository<T> {
    fun save(item: T): Boolean
    fun findById(id: Long): T?
    fun findAll(): List<T>
}

/**
 * Named entity interface
 */
interface Named {
    val name: String
}

/**
 * Auditable interface with default implementation
 */
interface Auditable {
    fun audit(): String = "Audited"
}

// === INTERFACE IMPLEMENTATION ===

/**
 * User repository implementing Repository interface
 */
class UserRepository : Repository<User>, Auditable {
    override fun save(item: User): Boolean {
        // Implementation
        return true
    }

    override fun findById(id: Long): User? {
        return null
    }

    override fun findAll(): List<User> {
        return emptyList()
    }
}

// === ENUM CLASSES ===

/**
 * Status enum for tracking state
 */
enum class Status {
    ACTIVE,
    INACTIVE,
    PENDING,
    ARCHIVED;

    fun isActive(): Boolean = this == ACTIVE
}

/**
 * Priority enum with properties
 */
enum class Priority(val level: Int, val label: String) {
    LOW(1, "Low Priority"),
    MEDIUM(2, "Medium Priority"),
    HIGH(3, "High Priority"),
    CRITICAL(4, "Critical Priority");

    companion object {
        fun fromLevel(level: Int): Priority? {
            return values().find { it.level == level }
        }
    }
}

// === SEALED CLASSES ===

/**
 * Sealed class for result types
 */
sealed class Result<out T> {
    data class Success<T>(val data: T) : Result<T>()
    data class Error(val message: String, val code: Int) : Result<Nothing>()
    object Loading : Result<Nothing>()
}

/**
 * Sealed interface for events
 */
sealed interface Event {
    data class Click(val x: Int, val y: Int) : Event
    data class KeyPress(val key: String) : Event
    object Refresh : Event
}

// === OBJECT DECLARATIONS ===

/**
 * Singleton configuration manager
 */
object ConfigManager {
    private val configs = mutableMapOf<String, Config>()

    fun register(key: String, config: Config) {
        configs[key] = config
    }

    fun get(key: String): Config? = configs[key]
}

/**
 * Logger singleton
 */
object Logger {
    fun log(message: String) {
        println("[LOG] $message")
    }
}

// === EXTENSION FUNCTIONS ===

/**
 * Extension function for String
 */
fun String.toTitleCase(): String {
    return this.split(" ").joinToString(" ") {
        it.replaceFirstChar { c -> c.uppercase() }
    }
}

/**
 * Extension function for List
 */
fun <T> List<T>.secondOrNull(): T? {
    return if (this.size > 1) this[1] else null
}

// === TYPE ALIASES ===

typealias StringMap = Map<String, String>
typealias UserList = List<User>
typealias ResultCallback<T> = (Result<T>) -> Unit

// === GENERIC CLASSES ===

/**
 * Generic box class
 */
class Box<T>(val value: T) {
    fun unwrap(): T = value

    fun <R> map(transform: (T) -> R): Box<R> {
        return Box(transform(value))
    }
}

/**
 * Generic repository with constraints
 */
class CachingRepository<T : Any>(
    private val delegate: Repository<T>
) : Repository<T> {
    private val cache = mutableMapOf<Long, T>()

    override fun save(item: T): Boolean {
        return delegate.save(item)
    }

    override fun findById(id: Long): T? {
        return cache[id] ?: delegate.findById(id)?.also { cache[id] = it }
    }

    override fun findAll(): List<T> {
        return delegate.findAll()
    }
}

// === ANNOTATIONS ===

/**
 * Custom annotation
 */
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class Documented(val author: String, val date: String)

/**
 * Deprecated function example
 */
@Deprecated("Use newFunction instead", ReplaceWith("newFunction()"))
fun oldFunction() {
    println("Old function")
}

@Documented(author = "John Doe", date = "2025-11-02")
fun newFunction() {
    println("New function")
}

// === LAMBDA AND HIGHER-ORDER FUNCTIONS ===

/**
 * Higher-order function example
 */
fun <T, R> List<T>.customMap(transform: (T) -> R): List<R> {
    val result = mutableListOf<R>()
    for (item in this) {
        result.add(transform(item))
    }
    return result
}

/**
 * Function taking lambda parameter
 */
fun processUsers(users: List<User>, handler: (User) -> Unit) {
    users.forEach(handler)
}

// === NULLABLE TYPES ===

/**
 * Service handling nullable types
 */
class NullableService {
    fun findUser(id: Long): User? {
        return null
    }

    fun getUserName(user: User?): String {
        return user?.name ?: "Unknown"
    }

    fun requireUser(user: User?): User {
        return user ?: throw IllegalArgumentException("User required")
    }
}

// === DELEGATION ===

/**
 * Interface for delegation
 */
interface Printer {
    fun print(message: String)
}

/**
 * Concrete printer implementation
 */
class ConsolePrinter : Printer {
    override fun print(message: String) {
        println(message)
    }
}

/**
 * Class using delegation
 */
class LoggingPrinter(printer: Printer) : Printer by printer {
    override fun print(message: String) {
        Logger.log("Printing: $message")
        // Delegate to the wrapped printer would happen here in real impl
    }
}

// === PROPERTY DELEGATES ===

/**
 * Class with lazy property
 */
class LazyService {
    val expensiveValue: String by lazy {
        computeExpensiveValue()
    }

    private fun computeExpensiveValue(): String {
        return "Computed value"
    }
}

// === OPERATOR OVERLOADING ===

/**
 * Point class with operator overloading
 */
data class Point(val x: Int, val y: Int) {
    operator fun plus(other: Point): Point {
        return Point(x + other.x, y + other.y)
    }

    operator fun minus(other: Point): Point {
        return Point(x - other.x, y - other.y)
    }

    operator fun unaryMinus(): Point {
        return Point(-x, -y)
    }
}

// === SUSPEND FUNCTIONS (COROUTINES) ===

/**
 * Suspend function for async operations
 */
suspend fun fetchUser(id: Long): User? {
    // Simulated async operation
    return null
}

/**
 * Repository with suspend functions
 */
class AsyncUserRepository {
    suspend fun save(user: User): Boolean {
        return true
    }

    suspend fun findAll(): List<User> {
        return emptyList()
    }
}

// === INLINE FUNCTIONS ===

/**
 * Inline function for performance
 */
inline fun <T> measureTime(block: () -> T): T {
    val start = System.currentTimeMillis()
    val result = block()
    val end = System.currentTimeMillis()
    println("Took ${end - start}ms")
    return result
}

/**
 * Inline function with reified type
 */
inline fun <reified T> isInstance(value: Any): Boolean {
    return value is T
}

// === VISIBILITY MODIFIERS ===

/**
 * Class demonstrating visibility modifiers
 */
class VisibilityDemo {
    public val publicField: String = "public"
    internal val internalField: String = "internal"
    protected val protectedField: String = "protected"
    private val privateField: String = "private"

    public fun publicMethod() {}
    internal fun internalMethod() {}
    protected fun protectedMethod() {}
    private fun privateMethod() {}
}

// === TOP-LEVEL FUNCTIONS ===

/**
 * Top-level function
 */
fun topLevelFunction(): String {
    return "Top level"
}

/**
 * Top-level function with parameters
 */
fun processData(data: String, config: Config): Result<String> {
    return Result.Success(data)
}

// === TOP-LEVEL PROPERTIES ===

val topLevelProperty: String = "Top level property"
const val CONSTANT_VALUE: Int = 42

// === VARARG AND DEFAULT PARAMETERS ===

/**
 * Function with vararg and default parameters
 */
fun format(separator: String = ", ", vararg items: String): String {
    return items.joinToString(separator)
}

/**
 * Function with multiple default parameters
 */
fun createUser(
    id: Long,
    name: String,
    email: String = "unknown@example.com",
    active: Boolean = true
): User {
    return User(id, email, name)
}

// === INFIX FUNCTIONS ===

/**
 * Infix function example
 */
infix fun Int.times(str: String): String {
    return str.repeat(this)
}

// === CONTEXT RECEIVERS ===

/**
 * Context receiver function (Kotlin 1.6.20+ experimental feature)
 * Tree-sitter parses this as infix_expression, not function_declaration
 */
context(Logger) fun logMessage(message: String) {
    log("Context receiver: $message")
}

// === TAILREC FUNCTIONS ===

/**
 * Tail recursive function
 */
tailrec fun factorial(n: Long, accumulator: Long = 1): Long {
    return if (n <= 1) accumulator else factorial(n - 1, n * accumulator)
}

// === MAIN FUNCTION ===

/**
 * Main entry point
 */
fun main(args: Array<String>) {
    val service = TestService.create("MyService")
    val result = service.process()
    Logger.log(result)

    val user = User(1, "user@example.com", "John")
    val repo = UserRepository()
    repo.save(user)

    val point1 = Point(1, 2)
    val point2 = Point(3, 4)
    val sum = point1 + point2

    println("Comprehensive Kotlin test completed")
}