sqry-core 12.1.2

Core library for sqry - semantic code search engine
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
//! Core node materialization and seed lookup contracts.
//!
//! These helpers operate on `GraphSnapshot` and produce crate-agnostic output
//! types. Consumer crates (LSP, MCP, CLI) build their protocol-specific types
//! from the `MaterializedNode` output.

use super::concurrent::GraphSnapshot;
use super::node::id::NodeId;
use super::resolution::{
    FileScope, ResolutionMode, SymbolCandidateOutcome, SymbolQuery, display_graph_qualified_name,
};
use super::storage::StringInterner;
use super::storage::arena::NodeEntry;
use super::storage::registry::FileRegistry;

/// Crate-agnostic representation of a fully resolved graph node.
///
/// Consumers (LSP, MCP, CLI) convert this into their protocol-specific types
/// without reimplementing resolution or formatting logic.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MaterializedNode {
    /// Graph node identity — stable within a snapshot for index-based edge linking.
    pub node_id: NodeId,
    /// Simple (unqualified) name of the symbol.
    pub name: String,
    /// Language-aware qualified name (e.g., `MyApp.User.GetName` for C#).
    pub qualified_name: String,
    /// Lowercased debug representation of the `NodeKind` variant.
    pub kind: String,
    /// Lowercased language name derived from the file registry.
    pub language: String,
    /// Display path of the file containing the symbol.
    pub file_path: String,
    /// One-indexed start line of the symbol span.
    pub start_line: u32,
    /// One-indexed end line of the symbol span.
    pub end_line: u32,
}

/// Build a language-aware display qualified name for a `NodeEntry`.
///
/// This is the **canonical** implementation. Consumer crates (LSP, MCP, CLI)
/// should call this instead of maintaining their own copy.
///
/// Resolves the entry's `qualified_name` through the string interner, applies
/// language-specific display normalization (e.g., `::` → `.` for C#), and falls
/// back to `fallback_name` when no qualified name is stored.
#[must_use]
pub fn display_entry_qualified_name(
    entry: &NodeEntry,
    strings: &StringInterner,
    files: &FileRegistry,
    fallback_name: &str,
) -> String {
    entry
        .qualified_name
        .and_then(|qn_id| strings.resolve(qn_id))
        .map_or_else(
            || fallback_name.to_string(),
            |qualified| {
                files.language_for_file(entry.file).map_or_else(
                    || qualified.to_string(),
                    |language| {
                        display_graph_qualified_name(
                            language,
                            qualified.as_ref(),
                            entry.kind,
                            entry.is_static,
                        )
                    },
                )
            },
        )
}

/// Resolve one symbol query into ordered candidate seeds.
///
/// Uses exact lookup first, then falls back to `FileScope::Any` and
/// `ResolutionMode::AllowSuffixCandidates` when there are no exact matches.
/// This keeps qualified display keys deterministic while preserving broad
/// suffix lookup for traversal entry points that receive abbreviated names.
/// Dot- and Ruby-`#` qualified display names try the graph-canonical `::`
/// form only when the literal display lookup has no candidates. This preserves
/// exact display keys such as Kotlin `function.T` when a Rust
/// `function::T` also exists.
#[must_use]
pub fn find_nodes_by_name(snapshot: &GraphSnapshot, name: &str) -> Vec<NodeId> {
    let exact_matches = snapshot.find_by_exact_name(name);
    if !exact_matches.is_empty() {
        return exact_matches;
    }

    let mut matches = find_nodes_by_graph_name(snapshot, name);
    if matches.is_empty()
        && let Some(canonical_name) = display_name_to_graph_fallback(name)
    {
        matches.extend(find_nodes_by_graph_name(snapshot, &canonical_name));
        matches.sort_unstable();
        matches.dedup();
    }
    matches
}

fn find_nodes_by_graph_name(snapshot: &GraphSnapshot, name: &str) -> Vec<NodeId> {
    match snapshot.find_symbol_candidates(&SymbolQuery {
        symbol: name,
        file_scope: FileScope::Any,
        mode: ResolutionMode::AllowSuffixCandidates,
    }) {
        SymbolCandidateOutcome::Candidates(matches) => matches,
        SymbolCandidateOutcome::NotFound | SymbolCandidateOutcome::FileNotIndexed => Vec::new(),
    }
}

fn display_name_to_graph_fallback(name: &str) -> Option<String> {
    if name.contains("::") {
        return None;
    }

    if name.contains('#') {
        Some(name.replace('#', "::"))
    } else if name.contains('.') {
        Some(name.replace('.', "::"))
    } else {
        None
    }
}

/// Resolve several symbol queries into a stable, deduplicated seed set.
///
/// Calls [`find_nodes_by_name`] for each symbol, then sorts and deduplicates
/// the combined result for deterministic downstream processing.
#[must_use]
pub fn collect_symbol_seeds(snapshot: &GraphSnapshot, symbols: &[String]) -> Vec<NodeId> {
    let mut seeds: Vec<NodeId> = Vec::new();
    for symbol in symbols {
        seeds.extend(find_nodes_by_name(snapshot, symbol));
    }
    seeds.sort_unstable();
    seeds.dedup();
    seeds
}

/// Resolve a graph node id to its display qualified name.
///
/// Returns `None` if the node does not exist or has an empty qualified name
/// after resolution.
#[must_use]
pub fn qualified_node_name(snapshot: &GraphSnapshot, node_id: NodeId) -> Option<String> {
    let strings = snapshot.strings();
    let files = snapshot.files();
    let entry = snapshot.get_node(node_id)?;

    let name = strings
        .resolve(entry.name)
        .map(|value| value.to_string())
        .unwrap_or_default();
    let qualified_name = display_entry_qualified_name(entry, strings, files, &name);

    (!qualified_name.is_empty()).then_some(qualified_name)
}

/// Materialize a graph node into a crate-agnostic `MaterializedNode`.
///
/// Returns `None` if the node does not exist or has an empty qualified name
/// after resolution. Consumers build protocol-specific types from the returned
/// value.
#[must_use]
pub fn materialize_node(snapshot: &GraphSnapshot, node_id: NodeId) -> Option<MaterializedNode> {
    let strings = snapshot.strings();
    let files = snapshot.files();
    let entry = snapshot.get_node(node_id)?;

    let name = strings
        .resolve(entry.name)
        .map(|value| value.to_string())
        .unwrap_or_default();

    let qualified_name = display_entry_qualified_name(entry, strings, files, &name);
    if qualified_name.is_empty() {
        return None;
    }

    let kind = format!("{:?}", entry.kind).to_lowercase();
    let language = files
        .language_for_file(entry.file)
        .map_or("unknown".to_string(), |lang| {
            lang.to_string().to_ascii_lowercase()
        });
    let file_path = files
        .resolve(entry.file)
        .map(|path| path.display().to_string())
        .unwrap_or_default();

    Some(MaterializedNode {
        node_id,
        name,
        qualified_name,
        kind,
        language,
        file_path,
        start_line: entry.start_line,
        end_line: entry.end_line,
    })
}

#[cfg(test)]
mod tests {
    use std::path::{Path, PathBuf};

    use crate::graph::node::Language;
    use crate::graph::unified::concurrent::CodeGraph;
    use crate::graph::unified::node::id::NodeId;
    use crate::graph::unified::node::kind::NodeKind;
    use crate::graph::unified::storage::arena::NodeEntry;

    use super::{
        MaterializedNode, collect_symbol_seeds, find_nodes_by_name, materialize_node,
        qualified_node_name,
    };

    struct TestNode {
        node_id: NodeId,
    }

    fn abs_path(relative: &str) -> PathBuf {
        PathBuf::from("/materialize-tests").join(relative)
    }

    trait NodeEntryExt {
        fn with_qualified_name_opt(
            self,
            qualified_name: Option<crate::graph::unified::string::id::StringId>,
        ) -> Self;
    }

    impl NodeEntryExt for NodeEntry {
        fn with_qualified_name_opt(
            mut self,
            qualified_name: Option<crate::graph::unified::string::id::StringId>,
        ) -> Self {
            self.qualified_name = qualified_name;
            self
        }
    }

    fn add_node(
        graph: &mut CodeGraph,
        kind: NodeKind,
        name: &str,
        qualified_name: Option<&str>,
        file_path: &Path,
        language: Option<Language>,
        start_line: u32,
        end_line: u32,
    ) -> TestNode {
        let name_id = graph.strings_mut().intern(name).unwrap();
        let qualified_name_id =
            qualified_name.map(|value| graph.strings_mut().intern(value).unwrap());
        let file_id = graph
            .files_mut()
            .register_with_language(file_path, language)
            .unwrap();

        let entry = NodeEntry::new(kind, name_id, file_id)
            .with_qualified_name_opt(qualified_name_id)
            .with_location(start_line, 0, end_line, 0);

        let node_id = graph.nodes_mut().alloc(entry).unwrap();
        graph
            .indices_mut()
            .add(node_id, kind, name_id, qualified_name_id, file_id);

        TestNode { node_id }
    }

    #[test]
    fn find_nodes_by_name_returns_matching_candidates() {
        let mut graph = CodeGraph::new();
        let path = abs_path("src/lib.rs");
        let node = add_node(
            &mut graph,
            NodeKind::Function,
            "process",
            Some("crate::process"),
            &path,
            Some(Language::Rust),
            1,
            10,
        );

        let snapshot = graph.snapshot();
        let results = find_nodes_by_name(&snapshot, "process");
        assert!(
            results.contains(&node.node_id),
            "expected node_id {:?} in results {:?}",
            node.node_id,
            results
        );
    }

    #[test]
    fn find_nodes_by_name_returns_empty_for_nonexistent() {
        let mut graph = CodeGraph::new();
        let path = abs_path("src/lib.rs");
        let _node = add_node(
            &mut graph,
            NodeKind::Function,
            "existing",
            Some("crate::existing"),
            &path,
            Some(Language::Rust),
            1,
            5,
        );

        let snapshot = graph.snapshot();
        let results = find_nodes_by_name(&snapshot, "nonexistent_symbol_xyz");
        assert!(
            results.is_empty(),
            "expected empty results, got {results:?}"
        );
    }

    #[test]
    fn collect_symbol_seeds_deduplicates() {
        let mut graph = CodeGraph::new();
        let path = abs_path("src/lib.rs");
        let node = add_node(
            &mut graph,
            NodeKind::Function,
            "dedup_target",
            Some("crate::dedup_target"),
            &path,
            Some(Language::Rust),
            1,
            10,
        );

        let snapshot = graph.snapshot();

        // Query the same symbol twice — should deduplicate.
        let symbols = vec!["dedup_target".to_string(), "dedup_target".to_string()];
        let seeds = collect_symbol_seeds(&snapshot, &symbols);

        assert_eq!(
            seeds.iter().filter(|id| **id == node.node_id).count(),
            1,
            "expected exactly one occurrence of node_id {:?}, got seeds {:?}",
            node.node_id,
            seeds
        );
    }

    #[test]
    fn qualified_node_name_returns_language_aware_name() {
        let mut graph = CodeGraph::new();
        let path = abs_path("src/Program.cs");
        let node = add_node(
            &mut graph,
            NodeKind::Method,
            "GetName",
            Some("MyApp::User::GetName"),
            &path,
            Some(Language::CSharp),
            5,
            15,
        );

        let snapshot = graph.snapshot();
        let name = qualified_node_name(&snapshot, node.node_id);

        // C# uses `.` as separator, so `::` should be normalized.
        assert_eq!(name, Some("MyApp.User.GetName".to_string()));
    }

    #[test]
    fn materialize_node_produces_complete_node() {
        let mut graph = CodeGraph::new();
        let path = abs_path("src/main.rs");
        let node = add_node(
            &mut graph,
            NodeKind::Function,
            "main",
            Some("crate::main"),
            &path,
            Some(Language::Rust),
            1,
            20,
        );

        let snapshot = graph.snapshot();
        let materialized = materialize_node(&snapshot, node.node_id);

        let expected = MaterializedNode {
            node_id: node.node_id,
            name: "main".to_string(),
            qualified_name: "crate::main".to_string(),
            kind: "function".to_string(),
            language: "rust".to_string(),
            file_path: path.display().to_string(),
            start_line: 1,
            end_line: 20,
        };

        assert_eq!(materialized, Some(expected));
    }

    #[test]
    fn materialize_node_returns_none_for_empty_qualified_name() {
        let mut graph = CodeGraph::new();
        let path = abs_path("src/lib.rs");
        // Node with no qualified name and an empty simple name — produces an
        // empty qualified name after resolution, so materialization should skip.
        let node = add_node(
            &mut graph,
            NodeKind::Function,
            "",
            None,
            &path,
            Some(Language::Rust),
            1,
            1,
        );

        let snapshot = graph.snapshot();
        let materialized = materialize_node(&snapshot, node.node_id);
        assert!(
            materialized.is_none(),
            "expected None for node with empty qualified name, got {materialized:?}"
        );
    }
}