syster-base 0.2.0-alpha

Core library for SysML v2 and KerML parsing, AST, and semantic analysis
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
//! Hover information implementation.

use std::sync::Arc;

use crate::base::FileId;
use crate::hir::{HirSymbol, ResolveResult, SymbolIndex, SymbolKind, TypeRef};

/// Result of a hover request.
#[derive(Clone, Debug)]
pub struct HoverResult {
    /// The hover content (markdown).
    pub contents: String,
    /// Qualified name of the hovered symbol (for reference lookup).
    pub qualified_name: Option<Arc<str>>,
    /// Whether this is a definition (for determining if we should show references).
    pub is_definition: bool,
    /// Start line of the hovered range (0-indexed).
    pub start_line: u32,
    /// Start column (0-indexed).
    pub start_col: u32,
    /// End line (0-indexed).
    pub end_line: u32,
    /// End column (0-indexed).
    pub end_col: u32,
}

impl HoverResult {
    /// Create a new hover result.
    pub fn new(contents: String, symbol: &HirSymbol) -> Self {
        Self {
            contents,
            qualified_name: Some(symbol.qualified_name.clone()),
            is_definition: symbol.kind.is_definition(),
            start_line: symbol.start_line,
            start_col: symbol.start_col,
            end_line: symbol.end_line,
            end_col: symbol.end_col,
        }
    }
}

/// Get hover information for a position.
///
/// # Arguments
/// * `index` - The symbol index to search
/// * `file` - The file containing the cursor
/// * `line` - Cursor line (0-indexed)
/// * `col` - Cursor column (0-indexed)
///
/// # Returns
/// Hover information, or None if nothing to show.
pub fn hover(index: &SymbolIndex, file: FileId, line: u32, col: u32) -> Option<HoverResult> {
    // First, check if cursor is on a type reference (e.g., ::>, :, :>)
    if let Some((_target_name, type_ref, _containing_symbol)) =
        find_type_ref_at_position(index, file, line, col)
    {
        // Use pre-resolved target if available (computed during semantic analysis)
        let target_symbol = if let Some(resolved) = &type_ref.resolved_target {
            index.lookup_qualified(resolved).cloned()
        } else {
            // Fallback: try to resolve at hover time (for backwards compatibility)
            let scope = _containing_symbol
                .map(|s| s.qualified_name.as_ref())
                .unwrap_or("");
            let resolver = index.resolver_for_scope(scope);

            match resolver.resolve(&_target_name) {
                ResolveResult::Found(sym) => Some(sym),
                ResolveResult::Ambiguous(syms) => syms.into_iter().next(),
                ResolveResult::NotFound => {
                    // Try qualified name directly

                    index.lookup_qualified(&_target_name).cloned()
                }
            }
        };

        if let Some(target_symbol) = target_symbol {
            let contents = build_hover_content(&target_symbol, index);
            // Return with the type_ref's span (where the cursor is)
            return Some(HoverResult {
                contents,
                qualified_name: Some(target_symbol.qualified_name.clone()),
                is_definition: target_symbol.kind.is_definition(),
                start_line: type_ref.start_line,
                start_col: type_ref.start_col,
                end_line: type_ref.end_line,
                end_col: type_ref.end_col,
            });
        }
    }

    // Otherwise, find the symbol at the cursor position
    let symbol = find_symbol_at_position(index, file, line, col)?;

    // Build hover content
    let contents = build_hover_content(symbol, index);

    Some(HoverResult::new(contents, symbol))
}

/// Build markdown hover content for a symbol.
fn build_hover_content(symbol: &HirSymbol, index: &SymbolIndex) -> String {
    let mut content = String::new();

    // Symbol signature
    content.push_str("```sysml\n");
    content.push_str(&build_signature(symbol));
    content.push_str("\n```\n");

    // Documentation
    if let Some(ref doc) = symbol.doc {
        content.push_str("\n---\n\n");
        content.push_str(doc);
        content.push('\n');
    }

    // Type information for usages
    if symbol.kind.is_usage() && !symbol.supertypes.is_empty() {
        content.push_str("\n**Typed by:** ");
        content.push_str(&symbol.supertypes.join(", "));

        // Try to add info about the type
        if let Some(type_symbol) = index.lookup_definition(&symbol.supertypes[0]) {
            if let Some(ref doc) = type_symbol.doc {
                content.push_str("\n\n*");
                // First sentence of doc
                let first_sentence = doc.split('.').next().unwrap_or(doc);
                content.push_str(first_sentence.trim());
                content.push('*');
            }
        }
        content.push('\n');
    }

    // Qualified name for context
    content.push_str("\n**Qualified Name:** `");
    content.push_str(&symbol.qualified_name);
    content.push_str("`\n");

    // Note: "Referenced by:" section is added at the LSP layer
    // because it needs file path resolution for clickable links.

    content
}

/// Build a signature string for a symbol.
fn build_signature(symbol: &HirSymbol) -> String {
    let kind_str = symbol.kind.display();

    // Build name with short name alias if present
    let name_with_alias = if let Some(ref short) = symbol.short_name {
        if short.as_ref() != symbol.name.as_ref() {
            format!("<{}> {}", short, symbol.name)
        } else {
            symbol.name.to_string()
        }
    } else {
        symbol.name.to_string()
    };

    match symbol.kind {
        // Definitions
        SymbolKind::PartDef
        | SymbolKind::ItemDef
        | SymbolKind::ActionDef
        | SymbolKind::PortDef
        | SymbolKind::AttributeDef
        | SymbolKind::ConnectionDef
        | SymbolKind::InterfaceDef
        | SymbolKind::AllocationDef
        | SymbolKind::RequirementDef
        | SymbolKind::ConstraintDef
        | SymbolKind::StateDef
        | SymbolKind::CalculationDef
        | SymbolKind::UseCaseDef
        | SymbolKind::AnalysisCaseDef
        | SymbolKind::ConcernDef
        | SymbolKind::ViewDef
        | SymbolKind::ViewpointDef
        | SymbolKind::RenderingDef
        | SymbolKind::EnumerationDef => {
            let mut sig = format!("{} {}", kind_str, name_with_alias);
            if !symbol.supertypes.is_empty() {
                sig.push_str(" :> ");
                sig.push_str(&symbol.supertypes.join(", "));
            }
            sig
        }

        // Usages
        SymbolKind::PartUsage
        | SymbolKind::ItemUsage
        | SymbolKind::ActionUsage
        | SymbolKind::PortUsage
        | SymbolKind::AttributeUsage
        | SymbolKind::ConnectionUsage
        | SymbolKind::InterfaceUsage
        | SymbolKind::AllocationUsage
        | SymbolKind::RequirementUsage
        | SymbolKind::ConstraintUsage
        | SymbolKind::StateUsage
        | SymbolKind::CalculationUsage
        | SymbolKind::ReferenceUsage
        | SymbolKind::OccurrenceUsage
        | SymbolKind::FlowUsage => {
            let mut sig = format!("{} {}", kind_str, name_with_alias);
            if !symbol.supertypes.is_empty() {
                sig.push_str(" : ");
                sig.push_str(symbol.supertypes[0].as_ref());
            }
            sig
        }

        // Package
        SymbolKind::Package => format!("package {}", name_with_alias),

        // Import
        SymbolKind::Import => format!("import {}", symbol.name),

        // Alias
        SymbolKind::Alias => {
            if !symbol.supertypes.is_empty() {
                format!("alias {} for {}", name_with_alias, symbol.supertypes[0])
            } else {
                format!("alias {}", name_with_alias)
            }
        }

        // Other
        SymbolKind::Comment | SymbolKind::Other | SymbolKind::Dependency => name_with_alias,
    }
}

/// Find the symbol at a specific position in a file.
fn find_symbol_at_position(
    index: &SymbolIndex,
    file: FileId,
    line: u32,
    col: u32,
) -> Option<&HirSymbol> {
    let symbols = index.symbols_in_file(file);

    // Find smallest symbol containing the position
    let mut best: Option<&HirSymbol> = None;

    for symbol in symbols {
        if contains_position(symbol, line, col) || contains_short_name_position(symbol, line, col) {
            match best {
                None => best = Some(symbol),
                Some(current) => {
                    if symbol_size(symbol) < symbol_size(current) {
                        best = Some(symbol);
                    }
                }
            }
        }
    }

    best
}

fn contains_position(symbol: &HirSymbol, line: u32, col: u32) -> bool {
    let after_start =
        line > symbol.start_line || (line == symbol.start_line && col >= symbol.start_col);
    let before_end = line < symbol.end_line || (line == symbol.end_line && col <= symbol.end_col);
    after_start && before_end
}

/// Check if position is within the symbol's short_name span (for hover on short names).
fn contains_short_name_position(symbol: &HirSymbol, line: u32, col: u32) -> bool {
    // All four span components must be present
    let (Some(start_line), Some(start_col), Some(end_line), Some(end_col)) = (
        symbol.short_name_start_line,
        symbol.short_name_start_col,
        symbol.short_name_end_line,
        symbol.short_name_end_col,
    ) else {
        return false;
    };

    let after_start = line > start_line || (line == start_line && col >= start_col);
    let before_end = line < end_line || (line == end_line && col <= end_col);
    after_start && before_end
}

fn symbol_size(symbol: &HirSymbol) -> u32 {
    let line_diff = symbol.end_line.saturating_sub(symbol.start_line);
    let col_diff = symbol.end_col.saturating_sub(symbol.start_col);
    line_diff * 1000 + col_diff
}

/// Find a type reference at a specific position in a file.
///
/// Returns the target type name, the TypeRef containing the position,
/// and the symbol that contains this type_ref (for scope resolution).
fn find_type_ref_at_position(
    index: &SymbolIndex,
    file: FileId,
    line: u32,
    col: u32,
) -> Option<(Arc<str>, &TypeRef, Option<&HirSymbol>)> {
    let symbols = index.symbols_in_file(file);

    for symbol in symbols {
        for type_ref_kind in symbol.type_refs.iter() {
            // Debug: print all type_refs for message symbols on line 780
            if cfg!(debug_assertions) && symbol.name.contains("ignitionCmd") && line == 780 {
                eprintln!(
                    "[HOVER DEBUG] Checking symbol '{}' type_ref at line={} col={}",
                    symbol.name, line, col
                );
                for tr in type_ref_kind.as_refs() {
                    eprintln!(
                        "[HOVER DEBUG]   TypeRef: target='{}' span={}:{}-{}:{} contains={} resolved_target={:?}",
                        tr.target,
                        tr.start_line,
                        tr.start_col,
                        tr.end_line,
                        tr.end_col,
                        tr.contains(line, col),
                        tr.resolved_target
                    );
                }
            }

            let contains = type_ref_kind.contains(line, col);

            if contains {
                // Find which part contains the position
                if let Some((_part_idx, tr)) = type_ref_kind.part_at(line, col) {
                    if cfg!(debug_assertions) && symbol.name.contains("ignitionCmd") && line == 780
                    {
                        eprintln!(
                            "[HOVER DEBUG]   FOUND! part_at returned target='{}' resolved_target={:?}",
                            tr.target, tr.resolved_target
                        );
                    }
                    return Some((tr.target.clone(), tr, Some(symbol)));
                } else if cfg!(debug_assertions) && symbol.name.contains("ignitionCmd") {
                    eprintln!("[HOVER DEBUG]   contains=true but part_at returned None!");
                }
            }
        }
    }

    None
}

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

    fn make_symbol(name: &str, qualified: &str, kind: SymbolKind, line: u32) -> HirSymbol {
        HirSymbol {
            name: Arc::from(name),
            short_name: None,
            qualified_name: Arc::from(qualified),
            kind,
            file: FileId::new(0),
            start_line: line,
            start_col: 0,
            end_line: line,
            end_col: 20,
            short_name_start_line: None,
            short_name_start_col: None,
            short_name_end_line: None,
            short_name_end_col: None,
            doc: None,
            supertypes: Vec::new(),
            type_refs: Vec::new(),
            is_public: false,
        }
    }

    #[test]
    fn test_hover_part_def() {
        let mut index = SymbolIndex::new();
        let mut def = make_symbol("Car", "Vehicle::Car", SymbolKind::PartDef, 5);
        def.doc = Some(Arc::from("A car is a vehicle."));
        def.supertypes = vec![Arc::from("Vehicle")];
        index.add_file(FileId::new(0), vec![def]);

        let result = hover(&index, FileId::new(0), 5, 5);

        assert!(result.is_some());
        let hover = result.unwrap();
        assert!(hover.contents.contains("Part def Car"));
        assert!(hover.contents.contains(":> Vehicle"));
        assert!(hover.contents.contains("A car is a vehicle"));
    }

    #[test]
    fn test_hover_usage() {
        let mut index = SymbolIndex::new();
        let mut usage = make_symbol("engine", "Car::engine", SymbolKind::PartUsage, 10);
        usage.supertypes = vec![Arc::from("Engine")];
        index.add_file(FileId::new(0), vec![usage]);

        let result = hover(&index, FileId::new(0), 10, 5);

        assert!(result.is_some());
        let hover = result.unwrap();
        assert!(hover.contents.contains("Part engine"));
        assert!(hover.contents.contains(": Engine"));
    }

    #[test]
    fn test_hover_not_found() {
        let index = SymbolIndex::new();
        let result = hover(&index, FileId::new(0), 0, 0);
        assert!(result.is_none());
    }

    #[test]
    fn test_build_signature_package() {
        let symbol = make_symbol("Vehicle", "Vehicle", SymbolKind::Package, 0);
        let sig = build_signature(&symbol);
        assert_eq!(sig, "package Vehicle");
    }
}