cqs 1.25.0

Code intelligence and RAG for AI agents. Semantic search, call graphs, impact analysis, type dependencies, and smart context assembly — in single tool calls. 54 languages + L5X/L5K PLC exports, 91.2% Recall@1 (BGE-large), 0.951 MRR (296 queries). Local ML, GPU-accelerated.
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
//! Field and method name extraction from code chunks.

use crate::parser::{FieldStyle, Language};

use super::fts::tokenize_identifier;

/// Returns true if a trimmed line should be skipped during field extraction.
/// Universal skips (comments, braces) apply to all languages. Language-specific
/// skips (struct/class/enum headers, decorators) come from `LanguageDef::skip_line_prefixes`.
fn should_skip_line(trimmed: &str, lang: Language) -> bool {
    // Universal skips (all languages)
    if trimmed.is_empty()
        || trimmed.starts_with("//")
        || trimmed.starts_with('#')
        || trimmed.starts_with("/*")
        || trimmed.starts_with('*')
        || trimmed == "{"
        || trimmed == "}"
    {
        return true;
    }
    // Language-specific skips
    let lang_def = lang.def();
    for prefix in lang_def.skip_line_prefixes {
        if trimmed.starts_with(prefix) {
            return true;
        }
    }
    false
}

/// Validates and returns a field name, or `None` if it looks like a keyword,
/// variant with data, or is too short.
fn validate_field_name(name: Option<&str>) -> Option<&str> {
    let name = name?.trim();
    if name.is_empty()
        || name.len() <= 1
        || name.contains('(')
        || name.contains('{')
        || !name.starts_with(|c: char| c.is_alphabetic() || c == '_')
    {
        return None;
    }
    Some(name)
}

/// Strip space-separated prefixes from a line.
/// Each prefix in `prefixes` (split on whitespace) is tried with a trailing
/// space. Longer prefixes are tried first to avoid partial matches (e.g.,
/// "pub" matching inside "pub(crate)").
fn strip_prefixes<'a>(line: &'a str, prefixes: &str) -> &'a str {
    let mut result = line;
    // Sort prefixes longest-first so "pub(crate)" is tried before "pub"
    let mut plist: Vec<String> = prefixes
        .split_whitespace()
        .map(|p| format!("{} ", p))
        .collect();
    plist.sort_by_key(|s| std::cmp::Reverse(s.len()));
    // Apply repeatedly — a line like "public static final int x" needs multiple passes
    let mut changed = true;
    let mut iters = 0;
    while changed && iters < 20 {
        iters += 1;
        changed = false;
        for with_space in &plist {
            if let Some(rest) = result.strip_prefix(with_space.as_str()) {
                result = rest;
                changed = true;
                break; // restart from longest prefix
            }
        }
    }
    result
}

/// Extract field/variant names from struct, enum, or class content.
/// Uses `FieldStyle` from the language definition to determine extraction
/// strategy. Supports `NameFirst` (name before separator) and `TypeFirst`
/// (type before name) patterns across all 51 languages.
pub(super) fn extract_field_names(content: &str, language: Language) -> Vec<String> {
    let _span = tracing::debug_span!("extract_field_names", %language).entered();

    let field_style = language.def().field_style;
    if field_style == FieldStyle::None {
        return Vec::new();
    }

    let mut fields = Vec::new();
    for line in content.lines() {
        let trimmed = line.trim();
        if should_skip_line(trimmed, language) {
            continue;
        }

        let field = match field_style {
            FieldStyle::NameFirst {
                separators,
                strip_prefixes: prefixes,
            } => {
                let clean = strip_prefixes(trimmed, prefixes);
                let sep_chars: Vec<char> = separators.chars().collect();
                clean
                    .split(sep_chars.as_slice())
                    .next()
                    .map(|s| s.trim().trim_end_matches(','))
            }
            FieldStyle::TypeFirst {
                strip_prefixes: prefixes,
            } => {
                let clean = strip_prefixes(trimmed, prefixes);
                // Split on terminators, take first segment: "int maxSize" from "int maxSize;"
                let before_term = clean
                    .split([';', ',', '=', '{'])
                    .next()
                    .unwrap_or("")
                    .trim();
                // Last whitespace-delimited token is the field name
                let name = before_term.rsplit_once(char::is_whitespace).map(|(_, n)| n);
                // Strip pointer/reference markers (C/C++)
                name.map(|n| n.trim_start_matches(['*', '&']))
            }
            FieldStyle::None => unreachable!(),
        };

        if let Some(name) = validate_field_name(field) {
            let tokenized = tokenize_identifier(name).join(" ");
            if !tokenized.is_empty() {
                fields.push(tokenized);
            }
        }

        if fields.len() >= 15 {
            break;
        }
    }

    if fields.is_empty() && !content.is_empty() {
        tracing::trace!(%language, "No fields extracted from content");
    }

    fields
}

/// Extract member method/function names from class/struct/interface content.
/// Scans lines for common method declaration patterns across languages.
/// Returns raw method names (not tokenized) — caller tokenizes for NL.
pub(super) fn extract_member_method_names(content: &str, language: Language) -> Vec<String> {
    let mut methods = Vec::new();
    for line in content.lines() {
        let trimmed = line.trim();
        if let Some(name) = extract_method_name_from_line(trimmed, language) {
            if !name.is_empty() && name.len() > 1 {
                methods.push(name);
            }
            if methods.len() >= 15 {
                break;
            }
        }
    }
    methods
}

/// Try to extract a method name from a single line of code.
fn extract_method_name_from_line(line: &str, language: Language) -> Option<String> {
    // Skip comments, empty, decorators
    if line.is_empty()
        || line.starts_with("//")
        || line.starts_with('#')
        || line.starts_with("/*")
        || line.starts_with('*')
        || line.starts_with('@')
    {
        return None;
    }

    // EX-35: Visibility/modifier prefixes are hardcoded here rather than in LanguageDef
    // because: (1) the set is small and shared across most C-family languages,
    // (2) LanguageDef is macro-generated for 51 languages — adding per-language
    // prefix arrays would bloat the macro for negligible benefit, and (3) these
    // prefixes are only used in this one function. If a new language needs
    // language-specific modifiers not covered here, consider moving to LanguageDef then.
    //
    // Rust: fn name(, pub fn name(, pub(crate) fn name(
    // Go: func (r *T) Name(, func Name(
    // Python: def name(
    // JS/TS: methodName(, async methodName(, public methodName(
    // Java/C#/Kotlin: visibility type methodName(
    // Ruby: def name
    let work = line
        .trim_start_matches("pub(crate) ")
        .trim_start_matches("pub(super) ")
        .trim_start_matches("pub ")
        .trim_start_matches("private ")
        .trim_start_matches("protected ")
        .trim_start_matches("public ")
        .trim_start_matches("internal ")
        .trim_start_matches("override ")
        .trim_start_matches("virtual ")
        .trim_start_matches("abstract ")
        .trim_start_matches("static ")
        .trim_start_matches("async ")
        .trim_start_matches("final ");

    match language {
        Language::Rust => {
            if let Some(rest) = work.strip_prefix("fn ") {
                return rest.split('(').next().map(|s| s.trim().to_string());
            }
        }
        Language::Python | Language::Ruby => {
            if let Some(rest) = work.strip_prefix("def ") {
                return rest
                    .split('(')
                    .next()
                    .or_else(|| rest.split_whitespace().next())
                    .map(|s| s.trim().to_string());
            }
        }
        Language::Go => {
            if let Some(rest) = work.strip_prefix("func ") {
                // func (r *T) Name( or func Name(
                let rest = if rest.starts_with('(') {
                    // Skip receiver: func (r *T) Name(
                    rest.find(") ").map(|i| &rest[i + 2..]).unwrap_or(rest)
                } else {
                    rest
                };
                return rest.split('(').next().map(|s| s.trim().to_string());
            }
        }
        _ => {
            // Generic: look for fn/def/func prefix, or name( pattern
            if let Some(rest) = work.strip_prefix("fn ") {
                return rest.split('(').next().map(|s| s.trim().to_string());
            }
            if let Some(rest) = work.strip_prefix("def ") {
                return rest.split('(').next().map(|s| s.trim().to_string());
            }
            if let Some(rest) = work.strip_prefix("func ") {
                return rest.split('(').next().map(|s| s.trim().to_string());
            }
            if let Some(rest) = work.strip_prefix("fun ") {
                // Kotlin
                return rest.split('(').next().map(|s| s.trim().to_string());
            }
            if let Some(rest) = work.strip_prefix("sub ") {
                // Perl, VB.NET
                return rest.split(['(', ' ']).next().map(|s| s.trim().to_string());
            }
            if let Some(rest) = work.strip_prefix("proc ") {
                // Elixir (defp), Nim, Tcl
                return rest.split('(').next().map(|s| s.trim().to_string());
            }
            if let Some(rest) = work.strip_prefix("method ") {
                // Raku, some OOP
                return rest.split('(').next().map(|s| s.trim().to_string());
            }
            // JS/TS/Java/C#: word( pattern after stripping modifiers
            // But need to distinguish from field declarations, so require (
            if let Some(paren_pos) = work.find('(') {
                let before = work[..paren_pos].trim();
                // Could be "returnType methodName" or just "methodName"
                let name = before.split_whitespace().last().unwrap_or(before);
                if !name.is_empty()
                    && name.starts_with(|c: char| c.is_alphabetic() || c == '_')
                    && !name.contains('{')
                    && !name.contains('}')
                    && !name.contains('=')
                    && name != "if"
                    && name != "for"
                    && name != "while"
                    && name != "switch"
                    && name != "catch"
                    && name != "return"
                    && name != "new"
                    && name != "class"
                    && name != "interface"
                    && name != "struct"
                    && name != "enum"
                {
                    return Some(name.to_string());
                }
            }
        }
    }
    None
}

/// Extract meaningful keywords from function body, filtering language noise.
/// Returns up to 10 unique keywords sorted by frequency (descending).
pub fn extract_body_keywords(content: &str, language: Language) -> Vec<String> {
    use std::collections::HashMap;

    let stopwords: &[&str] = language.def().stopwords;

    // Count word frequencies
    let mut freq: HashMap<String, usize> = HashMap::new();
    for token in tokenize_identifier(content) {
        if token.len() >= 3 && !stopwords.contains(&token.as_str()) {
            *freq.entry(token).or_insert(0) += 1;
        }
    }

    // Sort by frequency descending, take top 10
    let mut keywords: Vec<(String, usize)> = freq.into_iter().collect();
    keywords.sort_by(|a, b| b.1.cmp(&a.1));
    keywords.into_iter().take(10).map(|(w, _)| w).collect()
}

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

    // ===== extract_field_names regression tests =====

    #[test]
    fn test_extract_field_names_rust() {
        let content = "pub struct Config {\n    pub name: String,\n    pub(crate) max_size: usize,\n    enabled: bool,\n}";
        let result = extract_field_names(content, Language::Rust);
        assert_eq!(result, vec!["name", "max size", "enabled"]);
    }

    #[test]
    fn test_extract_field_names_go() {
        let content = "type Config struct {\n    Name string\n    MaxSize int\n    Enabled bool\n}";
        let result = extract_field_names(content, Language::Go);
        assert_eq!(result, vec!["name", "max size", "enabled"]);
    }

    #[test]
    fn test_extract_field_names_python() {
        let content = "class Config:\n    name: str\n    max_size: int = 100\n    enabled = True";
        let result = extract_field_names(content, Language::Python);
        assert_eq!(result, vec!["name", "max size", "enabled"]);
    }

    #[test]
    fn test_extract_field_names_typescript() {
        let content = "class Config {\n    public name: string;\n    private maxSize: number;\n    readonly enabled: boolean;\n}";
        let result = extract_field_names(content, Language::TypeScript);
        assert_eq!(result, vec!["name", "max size", "enabled"]);
    }

    #[test]
    fn test_extract_field_names_javascript() {
        let content = "class Config {\n    name = 'default';\n    maxSize = 100;\n}";
        let result = extract_field_names(content, Language::JavaScript);
        assert_eq!(result, vec!["name", "max size"]);
    }

    #[test]
    fn test_extract_field_names_java() {
        // Java fields are `type name;` — TypeFirst extraction strips access modifiers,
        // splits on terminators, and takes the last whitespace token (the field name).
        let content = "class Config {\n    private String name;\n    protected int maxSize;\n    public boolean enabled;\n}";
        let result = extract_field_names(content, Language::Java);
        assert_eq!(result, vec!["name", "max size", "enabled"]);
    }

    #[test]
    fn test_extract_field_names_empty_content() {
        let result = extract_field_names("", Language::Rust);
        assert_eq!(result, Vec::<String>::new());
    }

    #[test]
    fn test_extract_field_names_only_comments() {
        let content = "// this is a comment\n// another comment\n/* block comment */";
        let result = extract_field_names(content, Language::Rust);
        assert_eq!(result, Vec::<String>::new());
    }

    #[test]
    fn test_extract_field_names_header_and_brace_only() {
        let content = "pub struct Empty {\n}";
        let result = extract_field_names(content, Language::Rust);
        assert_eq!(result, Vec::<String>::new());
    }

    #[test]
    fn test_extract_field_names_unicode_no_panic() {
        let content = "class Config {\n    café: string;\n}";
        let result = extract_field_names(content, Language::TypeScript);
        // Just verify no panic; check actual output
        assert_eq!(result, vec!["café"]);
    }

    #[test]
    fn test_extract_field_names_capped_at_15() {
        let mut lines = vec!["pub struct Big {".to_string()];
        for i in 0..20 {
            lines.push(format!("    pub field_{}: i32,", i));
        }
        lines.push("}".to_string());
        let content = lines.join("\n");
        let result = extract_field_names(&content, Language::Rust);
        assert_eq!(result.len(), 15);
    }

    #[test]
    fn test_extract_field_names_unsupported_language() {
        let content = "NAME=\"default\"\nMAX_SIZE=100";
        let result = extract_field_names(content, Language::Bash);
        assert_eq!(result, Vec::<String>::new());
    }

    // ===== extract_field_names: TypeFirst languages =====

    #[test]
    fn test_extract_field_names_c() {
        let content =
            "struct Config {\n    const char *name;\n    int max_size;\n    bool enabled;\n};";
        let result = extract_field_names(content, Language::C);
        // TypeFirst: strips "const", takes last token before ;, strips pointer marker *
        assert_eq!(result, vec!["name", "max size", "enabled"]);
    }

    #[test]
    fn test_extract_field_names_cpp() {
        let content =
            "class Widget {\n    std::string title;\n    int width;\n    bool visible;\n};";
        let result = extract_field_names(content, Language::Cpp);
        assert_eq!(result, vec!["title", "width", "visible"]);
    }

    #[test]
    fn test_extract_field_names_csharp() {
        let content = "class Config {\n    public string Name;\n    private int MaxSize;\n    protected bool Enabled;\n}";
        let result = extract_field_names(content, Language::CSharp);
        // TypeFirst: strips access modifiers, takes last token before ;
        assert_eq!(result, vec!["name", "max size", "enabled"]);
    }

    // ===== extract_field_names: NameFirst languages with keyword prefixes =====

    #[test]
    fn test_extract_field_names_kotlin() {
        let content = "data class Config(\n    val name: String,\n    var maxSize: Int,\n    private val enabled: Boolean\n)";
        let result = extract_field_names(content, Language::Kotlin);
        // NameFirst: strips val/var/private, splits on :, tokenizes camelCase
        assert_eq!(result, vec!["name", "max size", "enabled"]);
    }

    #[test]
    fn test_extract_field_names_swift() {
        let content = "struct Config {\n    let name: String\n    var maxSize: Int\n    weak var delegate: Delegate?\n}";
        let result = extract_field_names(content, Language::Swift);
        // NameFirst: strips let/var/weak, splits on :
        assert_eq!(result, vec!["name", "max size", "delegate"]);
    }

    #[test]
    fn test_extract_field_names_scala() {
        let content = "case class Config(\n    val name: String,\n    var maxSize: Int\n)";
        let result = extract_field_names(content, Language::Scala);
        // NameFirst: strips val/var, splits on :
        assert_eq!(result, vec!["name", "max size"]);
    }

    #[test]
    fn test_extract_field_names_php() {
        // PHP fields use $ prefix which fails validate_field_name (not alphabetic start).
        // This is a known limitation: NameFirst extraction keeps "$name" intact,
        // and validate_field_name rejects it because '$' is not alphabetic or '_'.
        let content =
            "class Config {\n    public $name = 'default';\n    private $maxSize = 100;\n}";
        let result = extract_field_names(content, Language::Php);
        assert_eq!(
            result,
            Vec::<String>::new(),
            "PHP $ fields rejected by validator: {result:?}"
        );
    }

    #[test]
    fn test_extract_field_names_ruby() {
        // Ruby attr_accessor lines yield ":name" after stripping the prefix.
        // The colon prefix fails validate_field_name (not alphabetic start).
        // However, "end" passes validation (alphabetic, len > 1).
        // Known limitation: actual field names are not extracted, only the "end" keyword leaks through.
        let content = "class Config\n  attr_accessor :name\n  attr_reader :max_size\nend";
        let result = extract_field_names(content, Language::Ruby);
        assert!(
            !result
                .iter()
                .any(|f| f.contains("name") || f.contains("max")),
            "Ruby : fields should not extract actual field names: {result:?}"
        );
    }

    // ===== extract_field_names: NameFirst assignment languages =====

    #[test]
    fn test_extract_field_names_lua() {
        // Lua table assignment: "Config.name = ..." extracts "Config.name" as a single token
        // because dot is not a tokenizer delimiter. The table name prefix is included.
        let content = "local Config = {}\nConfig.name = 'default'\nConfig.max_size = 100";
        let result = extract_field_names(content, Language::Lua);
        // First line: strip "local" -> "Config = {}" -> split on = -> "Config" -> "config"
        // Second: "Config.name" -> tokenize -> "config.name" (dot not a delimiter)
        // Third: "Config.max_size" -> tokenize -> "config.max" + "size" (underscore splits)
        assert_eq!(result, vec!["config", "config.name", "config.max size"]);
    }

    #[test]
    fn test_extract_field_names_protobuf() {
        // Protobuf uses "type name = N;" syntax but NameFirst with space separator
        // extracts the first space-delimited token, which is the type name.
        // This is a known limitation: protobuf gets type names instead of field names.
        let content = "message Config {\n    string name = 1;\n    int32 max_size = 2;\n    bool enabled = 3;\n}";
        let result = extract_field_names(content, Language::Protobuf);
        // "message" line not skipped (no skip rule for it), extracts "message"
        // Subsequent lines extract type names: "string", "int32", "bool"
        assert!(
            !result.is_empty(),
            "protobuf should extract something (even if type names): {result:?}"
        );
        // Verify it extracts the type tokens (not field names — known limitation)
        assert!(
            result
                .iter()
                .any(|f| f == "message" || f == "string" || f == "bool"),
            "protobuf extracts type tokens with space separator: {result:?}"
        );
    }

    #[test]
    fn extract_method_name_kotlin_fun() {
        let name = extract_method_name_from_line(
            "fun processData(input: String): Result",
            Language::Kotlin,
        );
        assert_eq!(name.as_deref(), Some("processData"));
    }

    #[test]
    fn extract_method_name_perl_sub() {
        let name = extract_method_name_from_line("sub calculate_total {", Language::Perl);
        assert_eq!(name.as_deref(), Some("calculate_total"));
    }
}