seq-lsp 5.6.3

Language Server Protocol implementation for Seq
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
//! Parse, type-check, and lint orchestration for the LSP, plus conversion
//! from compiler/lint results into LSP diagnostics and code actions.

use crate::includes::IncludeResolution;
use seqc::ast::{Program, QuotationSpan, Statement};
use seqc::types::Type;
use seqc::{Parser, TypeChecker, lint};
use std::borrow::Cow;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use tower_lsp::lsp_types::{
    CodeAction, CodeActionKind, Diagnostic, DiagnosticSeverity, Position, Range, TextEdit, Url,
    WorkspaceEdit,
};
use tracing::{debug, warn};

/// Information about a quotation for LSP hover support
#[derive(Debug, Clone)]
pub(crate) struct QuotationInfo {
    /// The quotation's source span
    pub(crate) span: QuotationSpan,
    /// The inferred type (Quotation or Closure with effect)
    pub(crate) inferred_type: Type,
}

/// Strip shebang line from source if present.
///
/// Replaces the first line with a comment if it starts with `#!`
/// so that line numbers in error messages remain correct.
fn strip_shebang(source: &str) -> Cow<'_, str> {
    if source.starts_with("#!") {
        // Replace shebang with comment of same length to preserve line numbers
        if let Some(newline_pos) = source.find('\n') {
            let mut result = String::with_capacity(source.len());
            result.push('#');
            result.push_str(&" ".repeat(newline_pos - 1));
            result.push_str(&source[newline_pos..]);
            Cow::Owned(result)
        } else {
            // Single line file with just shebang
            Cow::Borrowed("#")
        }
    } else {
        Cow::Borrowed(source)
    }
}

/// Resolve the path used for lint/error-flag diagnostics; defaults to `source.seq`
/// when the document has no on-disk path yet (e.g. untitled buffer).
fn default_lint_path(file_path: Option<&Path>) -> PathBuf {
    file_path
        .map(|p| p.to_path_buf())
        .unwrap_or_else(|| PathBuf::from("source.seq"))
}

/// Build a `WorkspaceEdit` that changes a single range in a single file.
fn single_file_workspace_edit(uri: &Url, range: Range, new_text: String) -> WorkspaceEdit {
    let edit = TextEdit { range, new_text };
    let mut changes = HashMap::new();
    changes.insert(uri.clone(), vec![edit]);
    WorkspaceEdit {
        changes: Some(changes),
        document_changes: None,
        change_annotations: None,
    }
}

/// Collect all quotation spans from a program
fn collect_quotation_spans(program: &Program) -> HashMap<usize, QuotationSpan> {
    let mut spans = HashMap::new();
    for word in &program.words {
        collect_quotations_from_statements(&word.body, &mut spans);
    }
    spans
}

fn collect_quotations_from_statements(
    stmts: &[Statement],
    spans: &mut HashMap<usize, QuotationSpan>,
) {
    for stmt in stmts {
        match stmt {
            Statement::Quotation { id, body, span } => {
                if let Some(s) = span {
                    spans.insert(*id, s.clone());
                }
                collect_quotations_from_statements(body, spans);
            }
            Statement::If {
                then_branch,
                else_branch,
                span: _,
            } => {
                collect_quotations_from_statements(then_branch, spans);
                if let Some(else_stmts) = else_branch {
                    collect_quotations_from_statements(else_stmts, spans);
                }
            }
            Statement::Match { arms, span: _ } => {
                for arm in arms {
                    collect_quotations_from_statements(&arm.body, spans);
                }
            }
            _ => {}
        }
    }
}

/// Check a document for parse and type errors, returning LSP diagnostics.
///
/// This version doesn't know about included words - use `check_document_with_quotations`
/// for include-aware diagnostics.
#[cfg(test)]
pub fn check_document(source: &str) -> Vec<Diagnostic> {
    let (diagnostics, _quotations) =
        check_document_with_quotations(source, &IncludeResolution::default(), None);
    diagnostics
}

/// Check a document and return both diagnostics and quotation info for hover support.
///
/// Parses the document, type-checks it, and collects quotation
/// spans and their inferred types for LSP hover functionality.
pub(crate) fn check_document_with_quotations(
    source: &str,
    includes: &IncludeResolution,
    file_path: Option<&Path>,
) -> (Vec<Diagnostic>, Vec<QuotationInfo>) {
    let mut diagnostics = Vec::new();
    let mut quotation_info = Vec::new();

    // Strip shebang if present (for script mode files)
    let source = strip_shebang(source);

    // Phase 1: Parse
    let mut parser = Parser::new(&source);
    let mut program = match parser.parse() {
        Ok(prog) => prog,
        Err(err) => {
            debug!("Parse error: {}", err);
            diagnostics.push(error_to_diagnostic(&err, &source));
            return (diagnostics, quotation_info);
        }
    };

    // Phase 1.5: Generate ADT constructors
    if let Err(err) = program.generate_constructors() {
        debug!("Constructor generation error: {}", err);
        diagnostics.push(error_to_diagnostic(&err, &source));
        return (diagnostics, quotation_info);
    }

    // Collect quotation spans before type checking
    let quotation_spans = collect_quotation_spans(&program);

    // Phase 2: Validate word calls
    let included_word_names: Vec<&str> = includes.words.iter().map(|w| w.name.as_str()).collect();
    if let Err(err) = program.validate_word_calls_with_externals(&included_word_names) {
        debug!("Validation error: {}", err);
        diagnostics.push(error_to_diagnostic(&err, &source));
    }

    // Phase 3: Type check
    let mut typechecker = TypeChecker::new();
    let external_unions: Vec<&str> = includes.union_names.iter().map(|s| s.as_str()).collect();
    typechecker.register_external_unions(&external_unions);
    // Filter to words with effects (v2.0 requirement)
    let external_words: Vec<(&str, &seqc::Effect)> = includes
        .words
        .iter()
        .filter_map(|w| w.effect.as_ref().map(|e| (w.name.as_str(), e)))
        .collect();
    typechecker.register_external_words(&external_words);

    if let Err(err) = typechecker.check_program(&program) {
        debug!("Type error: {}", err);
        diagnostics.push(error_to_diagnostic(&err, &source));
    }

    // Get quotation types and combine with spans
    let quotation_types = typechecker.take_quotation_types();
    for (id, span) in quotation_spans {
        if let Some(typ) = quotation_types.get(&id) {
            quotation_info.push(QuotationInfo {
                span,
                inferred_type: typ.clone(),
            });
        }
    }

    // Phase 4: Lint checks
    let lint_file_path = default_lint_path(file_path);
    if let Ok(linter) = lint::Linter::with_defaults() {
        let lint_diagnostics = linter.lint_program(&program, &lint_file_path);
        for lint_diag in lint_diagnostics {
            diagnostics.push(lint_to_diagnostic(&lint_diag, &source));
        }
    }

    // Phase 4b: Error flag tracking (unchecked Bool from fallible operations)
    {
        let mut flag_analyzer = seqc::ErrorFlagAnalyzer::new(&lint_file_path);
        let flag_diagnostics = flag_analyzer.analyze_program(&program);
        for flag_diag in flag_diagnostics {
            diagnostics.push(lint_to_diagnostic(&flag_diag, &source));
        }
    }

    (diagnostics, quotation_info)
}

/// Get code actions for lint diagnostics that overlap with the given range.
///
/// This re-runs the linter to find applicable fixes for the requested range.
pub(crate) fn get_code_actions(
    source: &str,
    range: Range,
    uri: &Url,
    file_path: Option<&Path>,
) -> Vec<CodeAction> {
    let mut actions = Vec::new();

    // Strip shebang if present (for script mode files)
    let source = strip_shebang(source);

    // Parse the source
    let mut parser = Parser::new(&source);
    let Ok(program) = parser.parse() else {
        return actions; // No actions if parse fails
    };

    // Run linter
    let lint_file_path = default_lint_path(file_path);

    let Ok(linter) = lint::Linter::with_defaults() else {
        return actions;
    };

    let lint_diagnostics = linter.lint_program(&program, &lint_file_path);

    // Find lint diagnostics that overlap with the requested range
    for lint_diag in &lint_diagnostics {
        let diag_range = make_lint_range(lint_diag, &source);

        // Check if ranges overlap
        if ranges_overlap(&diag_range, &range) {
            // Only create actions for diagnostics that have a fix
            if let Some(action) = lint_to_code_action(lint_diag, &source, uri, &diag_range) {
                actions.push(action);
            }
        }
    }

    actions
}

/// Check if two ranges overlap (or if a point is inside a range)
fn ranges_overlap(a: &Range, b: &Range) -> bool {
    // Special case: if b is a zero-width cursor position, check if it's inside a
    if b.start == b.end {
        let cursor_line = b.start.line;
        let cursor_char = b.start.character;

        // Cursor is inside range a if:
        // - cursor line is within a's line range
        // - if on start line, cursor char >= start char
        // - if on end line, cursor char <= end char
        if cursor_line < a.start.line || cursor_line > a.end.line {
            return false;
        }
        if cursor_line == a.start.line && cursor_char < a.start.character {
            return false;
        }
        if cursor_line == a.end.line && cursor_char > a.end.character {
            return false;
        }
        return true;
    }

    // General case: ranges overlap if neither is entirely before the other
    !(a.end.line < b.start.line
        || (a.end.line == b.start.line && a.end.character <= b.start.character)
        || b.end.line < a.start.line
        || (b.end.line == a.start.line && b.end.character <= a.start.character))
}

/// Create an LSP Range from a lint diagnostic
fn make_lint_range(lint_diag: &lint::LintDiagnostic, source: &str) -> Range {
    let start_line = lint_diag.line as u32;
    let end_line = lint_diag.end_line.map(|l| l as u32).unwrap_or(start_line);

    let start_char = lint_diag.start_column.map(|c| c as u32).unwrap_or(0);

    let end_char = match lint_diag.end_column {
        Some(end) => end as u32,
        None => {
            // Fall back to end of the end line
            let target_line = lint_diag.end_line.unwrap_or(lint_diag.line);
            source
                .lines()
                .nth(target_line)
                .map(|l| l.len() as u32)
                .unwrap_or(0)
        }
    };

    Range {
        start: Position {
            line: start_line,
            character: start_char,
        },
        end: Position {
            line: end_line,
            character: end_char,
        },
    }
}

/// Convert a lint diagnostic to a CodeAction if it has a fix
fn lint_to_code_action(
    lint_diag: &lint::LintDiagnostic,
    _source: &str,
    uri: &Url,
    range: &Range,
) -> Option<CodeAction> {
    // For unchecked-* lint rules, offer "Add error check" instead of "Remove"
    if lint_diag.id.starts_with("unchecked-") {
        return unchecked_error_code_action(lint_diag, uri, range);
    }

    let title = if lint_diag.replacement.is_empty() {
        format!("Remove redundant code ({})", lint_diag.id)
    } else {
        format!("Replace with `{}`", lint_diag.replacement)
    };

    let workspace_edit = single_file_workspace_edit(uri, *range, lint_diag.replacement.clone());

    Some(CodeAction {
        title,
        kind: Some(CodeActionKind::QUICKFIX),
        diagnostics: None,
        edit: Some(workspace_edit),
        command: None,
        is_preferred: Some(true),
        disabled: None,
        data: None,
    })
}

/// Generate a code action for unchecked error flag diagnostics.
///
/// Replaces `op drop` with an `if/else/then` error check skeleton:
/// ```seq
/// op if
///   # success
/// else
///   drop  # handle error
/// then
/// ```
fn unchecked_error_code_action(
    lint_diag: &lint::LintDiagnostic,
    uri: &Url,
    range: &Range,
) -> Option<CodeAction> {
    let title = "Add error check (if/else/then)".to_string();

    // The range covers "op drop" — replace just the "drop" part with the skeleton.
    // The diagnostic range starts at the operation and ends after "drop".
    // We want to replace "drop" (the last word in the range) with the skeleton.
    // Since we can't easily compute the "drop" sub-range, we replace the whole
    // matched pattern. The pattern is "op drop", so we replace with "op if ... then".
    //
    // Extract the operation name from the diagnostic message.
    // Messages follow the pattern: "`op` returns ..."
    let op_name = lint_diag
        .message
        .strip_prefix('`')
        .and_then(|s| s.split('`').next())
        .unwrap_or("op");

    let new_text = format!(
        "{} if\n    # success\n  else\n    drop  # handle {} error\n  then",
        op_name, op_name,
    );

    let workspace_edit = single_file_workspace_edit(uri, *range, new_text);

    Some(CodeAction {
        title,
        kind: Some(CodeActionKind::QUICKFIX),
        diagnostics: None,
        edit: Some(workspace_edit),
        command: None,
        is_preferred: Some(false), // not preferred — user should review
        disabled: None,
        data: None,
    })
}

/// Convert a lint diagnostic to an LSP diagnostic.
fn lint_to_diagnostic(lint_diag: &lint::LintDiagnostic, source: &str) -> Diagnostic {
    let severity = match lint_diag.severity {
        lint::Severity::Error => DiagnosticSeverity::ERROR,
        lint::Severity::Warning => DiagnosticSeverity::WARNING,
        lint::Severity::Hint => DiagnosticSeverity::HINT,
    };

    let message = if lint_diag.replacement.is_empty() {
        lint_diag.message.clone()
    } else {
        format!(
            "{} (use `{}` instead)",
            lint_diag.message, lint_diag.replacement
        )
    };

    let range = make_lint_range(lint_diag, source);

    Diagnostic {
        range,
        severity: Some(severity),
        code: Some(tower_lsp::lsp_types::NumberOrString::String(
            lint_diag.id.clone(),
        )),
        code_description: None,
        source: Some("seq-lint".to_string()),
        message,
        related_information: None,
        tags: None,
        data: None,
    }
}

/// Convert a compiler error string to an LSP diagnostic.
///
/// The compiler currently returns errors as strings without structured position
/// information. We attempt to extract line numbers from the error message,
/// falling back to line 0 if not found.
fn error_to_diagnostic(error: &str, source: &str) -> Diagnostic {
    let (line, message) = extract_line_info(error, source);

    // Calculate actual line length for proper highlighting
    let line_length = source
        .lines()
        .nth(line)
        .map(|l| l.len() as u32)
        .unwrap_or(0);

    Diagnostic {
        range: Range {
            start: Position {
                line: line as u32,
                character: 0,
            },
            end: Position {
                line: line as u32,
                character: line_length,
            },
        },
        severity: Some(DiagnosticSeverity::ERROR),
        code: Some(tower_lsp::lsp_types::NumberOrString::String(
            "type-error".to_string(),
        )),
        code_description: None,
        source: Some("seqc".to_string()),
        message: message.to_string(),
        related_information: None,
        tags: None,
        data: None,
    }
}

/// Try to extract line number information from an error message.
///
/// Current compiler error formats we try to handle:
/// - "at line N: ..."
/// - "line N: ..."
/// - "Unknown word: 'foo'" (search for 'foo' in source)
/// - "Undefined word 'foo' called in word 'bar'" (search for 'foo' in source)
///
/// Returns (line_number, cleaned_message)
fn extract_line_info<'a>(error: &'a str, source: &str) -> (usize, &'a str) {
    // Try "at line N" pattern
    if let Some(idx) = error.find("at line ") {
        let after = &error[idx + 8..];
        if let Some(end) = after.find(|c: char| !c.is_ascii_digit())
            && let Ok(line) = after[..end].parse::<usize>()
        {
            return (line.saturating_sub(1), error); // LSP uses 0-based lines
        }
    }

    // Try "line N:" pattern
    if let Some(idx) = error.find("line ") {
        let after = &error[idx + 5..];
        if let Some(end) = after.find(|c: char| !c.is_ascii_digit())
            && let Ok(line) = after[..end].parse::<usize>()
        {
            return (line.saturating_sub(1), error);
        }
    }

    // Try to find unknown word in source (old format)
    if let Some(rest) = error.strip_prefix("Unknown word: '")
        && let Some(end) = rest.find('\'')
        && let Some(line) = find_word_line(source, &rest[..end])
    {
        return (line, error);
    }

    // Try to find undefined word in source (new format from validate_word_calls)
    // Format: "Undefined word 'foo' called in word 'bar'"
    if let Some(rest) = error.strip_prefix("Undefined word '")
        && let Some(end) = rest.find('\'')
        && let Some(line) = find_word_line(source, &rest[..end])
    {
        return (line, error);
    }

    // Fallback: report on line 0
    warn!("Could not extract line info from error: {}", error);
    (0, error)
}

/// Find the line number where a word appears in the source.
///
/// Seq words can contain special characters like `-`, `>`, `?`, etc.
/// We need to match whole words accounting for these characters.
fn find_word_line(source: &str, word: &str) -> Option<usize> {
    for (line_num, line) in source.lines().enumerate() {
        if !line.contains(word) {
            continue;
        }

        // Skip comment lines
        let trimmed = line.trim();
        if trimmed.starts_with('#') {
            continue;
        }

        // Check each potential word position
        // Seq words are separated by whitespace, so we can use that
        for token in trimmed.split_whitespace() {
            if token == word {
                return Some(line_num);
            }
        }
    }
    None
}

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

    #[test]
    fn test_parse_error() {
        let source = ": foo 1 2 +";
        let diagnostics = check_document(source);
        // Should error on missing semicolon
        assert!(!diagnostics.is_empty());
    }

    #[test]
    fn test_type_error() {
        let source = ": foo ( -- Int ) \"hello\" ;";
        let diagnostics = check_document(source);
        // Should error on stack effect mismatch
        assert!(!diagnostics.is_empty());
    }

    #[test]
    fn test_undefined_word() {
        let source = ": main ( -- Int ) undefined-word 0 ;";
        let diagnostics = check_document(source);
        // Should error on undefined word
        assert!(!diagnostics.is_empty(), "Expected diagnostics but got none");
        assert!(
            diagnostics[0].message.contains("Undefined word"),
            "Expected 'Undefined word' in message, got: {}",
            diagnostics[0].message
        );
    }

    #[test]
    fn test_valid_program() {
        let source = ": main ( -- Int ) 0 ;";
        let diagnostics = check_document(source);
        assert!(diagnostics.is_empty());
    }

    #[test]
    fn test_find_word_with_special_chars() {
        let source = "string->float\nfile-exists?\nsome-word";
        assert_eq!(find_word_line(source, "string->float"), Some(0));
        assert_eq!(find_word_line(source, "file-exists?"), Some(1));
        assert_eq!(find_word_line(source, "some-word"), Some(2));
    }

    #[test]
    fn test_find_word_skips_comments() {
        let source = "# string->float comment\nstring->float";
        assert_eq!(find_word_line(source, "string->float"), Some(1));
    }

    #[test]
    fn test_builtin_make_variant_recognized() {
        // variant.make-2 should be recognized as a builtin, not flagged as unknown
        // variant.make-2 expects ( field1 field2 Symbol -- V )
        let source = ": main ( -- ) 1 2 :Tag variant.make-2 drop ;";
        let diagnostics = check_document(source);
        // Should have no "Undefined word" errors for variant.make-2
        for d in &diagnostics {
            assert!(
                !d.message.contains("variant.make-2"),
                "variant.make-2 should be recognized as builtin, got: {}",
                d.message
            );
        }
    }

    #[test]
    fn test_adt_constructor_recognized() {
        // Make-Circle should be generated from the union definition
        let source = r#"
union Shape { Circle { radius: Int } Rectangle { width: Int, height: Int } }

: main ( -- Int )
  5 Make-Circle
  drop
  0
;
"#;
        let diagnostics = check_document(source);
        // Should have no errors - Make-Circle is a valid constructor
        for d in &diagnostics {
            assert!(
                !d.message.contains("Make-Circle"),
                "Make-Circle should be recognized as ADT constructor, got: {}",
                d.message
            );
        }
        assert!(
            diagnostics.is_empty(),
            "Expected no diagnostics, got: {:?}",
            diagnostics.iter().map(|d| &d.message).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_lint_swap_drop() {
        // swap drop should trigger a lint hint suggesting nip
        let source = ": main ( -- Int ) 1 2 swap drop ;";
        let diagnostics = check_document(source);
        // Should have a lint hint for prefer-nip
        let lint_diags: Vec<_> = diagnostics
            .iter()
            .filter(|d| d.source.as_deref() == Some("seq-lint"))
            .collect();
        assert!(
            !lint_diags.is_empty(),
            "Expected lint diagnostic for swap drop"
        );
        assert!(
            lint_diags[0].message.contains("nip"),
            "Expected nip suggestion, got: {}",
            lint_diags[0].message
        );
        assert_eq!(lint_diags[0].severity, Some(DiagnosticSeverity::HINT));
    }

    #[test]
    fn test_lint_redundant_swap_swap() {
        // swap swap should trigger a lint warning
        let source = ": main ( -- Int ) 1 2 swap swap drop ;";
        let diagnostics = check_document(source);
        // Should have a lint warning for redundant-swap-swap
        let lint_diags: Vec<_> = diagnostics
            .iter()
            .filter(|d| d.source.as_deref() == Some("seq-lint"))
            .collect();
        assert!(
            lint_diags.iter().any(|d| d.message.contains("cancel out")),
            "Expected swap swap warning, got: {:?}",
            lint_diags.iter().map(|d| &d.message).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_shebang_is_tolerated() {
        // Files with shebang should parse without errors
        let source = "#!/usr/bin/env seqc\n: main ( -- Int ) 0 ;";
        let diagnostics = check_document(source);
        assert!(
            diagnostics.is_empty(),
            "Shebang should be tolerated, got: {:?}",
            diagnostics.iter().map(|d| &d.message).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_if_else_branch_mismatch_reports_correct_line() {
        // If/else with incompatible branch types should report error on the if line
        let source = r#": test ( Bool -- Int )
  if
    42
  else
    "string"
  then
;

: main ( -- )
  true test drop
;
"#;
        let diagnostics = check_document(source);
        assert!(!diagnostics.is_empty(), "Expected type error");
        let diag = &diagnostics[0];
        assert!(
            diag.message.contains("if/else branches have incompatible"),
            "Expected if/else branch mismatch error, got: {}",
            diag.message
        );
        // The 'if' is on line 2 (1-indexed), which is line 1 in 0-indexed LSP coordinates
        assert_eq!(
            diag.range.start.line, 1,
            "Expected error on line 1 (0-indexed), got line {}. Message: {}",
            diag.range.start.line, diag.message
        );
    }

    #[test]
    fn test_diagnostic_structure() {
        // Verify the diagnostic has all required fields for neovim
        let source = r#": test ( Bool -- Int )
  if 42 else "string" then
;
: main ( -- ) true test drop ;
"#;
        let diagnostics = check_document(source);
        assert!(!diagnostics.is_empty(), "Expected diagnostic");
        let diag = &diagnostics[0];

        // These fields must be present for neovim to show file-level diagnostics
        assert!(diag.severity.is_some(), "severity must be set");
        assert!(diag.source.is_some(), "source must be set");

        // Print the diagnostic for debugging
        println!("Diagnostic: {:?}", diag);
        println!("  severity: {:?}", diag.severity);
        println!("  source: {:?}", diag.source);
        println!("  code: {:?}", diag.code);
        println!("  range: {:?}", diag.range);
        println!("  message: {}", diag.message);

        // Verify JSON serialization
        let json = serde_json::to_string_pretty(diag).unwrap();
        println!("JSON:\n{}", json);
        assert!(
            json.contains("\"severity\":"),
            "JSON must contain severity field"
        );
    }

    #[test]
    fn test_stack_type_mismatch_reports_correct_line() {
        // Stack type mismatch should report error on the word call that caused it
        let source = r#"union IntResult {
  Ok { value: Int }
  Err { message: String }
}

: safe-divide ( Int Int -- IntResult )
    dup 0 i.= if
      drop "division by zero" Make-Err
    else
      i./ Make-Ok
    then
;

: main ( -- )
  10 2 safe-divide drop
;
"#;
        let diagnostics = check_document(source);
        assert!(!diagnostics.is_empty(), "Expected type error");
        let diag = &diagnostics[0];
        assert!(
            diag.message.contains("stack type mismatch") || diag.message.contains("Make-Ok"),
            "Expected stack type mismatch error, got: {}",
            diag.message
        );
        // The 'Make-Ok' is on line 10 (1-indexed), which is line 9 in 0-indexed LSP coordinates
        assert_eq!(
            diag.range.start.line, 9,
            "Expected error on line 9 (0-indexed), got line {}. Message: {}",
            diag.range.start.line, diag.message
        );
    }

    #[test]
    fn test_match_arm_mismatch_reports_correct_line() {
        // Match with incompatible arm types should report error on the match line
        let source = r#"union Message {
  Get { value: Int }
  Set { key: Int, value: Int }
}

: handle ( Message -- Int )
  match
    Get -> 42
    Set -> "string"
  end
;

: main ( -- )
  1 Make-Get handle drop
;
"#;
        let diagnostics = check_document(source);
        assert!(!diagnostics.is_empty(), "Expected type error");
        let diag = &diagnostics[0];
        assert!(
            diag.message.contains("match arms have incompatible"),
            "Expected match arms mismatch error, got: {}",
            diag.message
        );
        // The 'match' is on line 7 (1-indexed), which is line 6 in 0-indexed LSP coordinates
        assert_eq!(
            diag.range.start.line, 6,
            "Expected error on line 6 (0-indexed), got line {}. Message: {}",
            diag.range.start.line, diag.message
        );
    }
}