logicaffeine-lsp 0.9.16

Language Server Protocol implementation for LogicAffeine
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
use tower_lsp::lsp_types::{
    Diagnostic, DiagnosticRelatedInformation, DiagnosticSeverity, Location, Range, Url,
};

use logicaffeine_base::Interner;
use logicaffeine_language::error::{ParseError, ParseErrorKind, socratic_explanation};
use logicaffeine_language::token::{Token, TokenType};

use crate::index::{find_token_span_for_name_pub, find_keyword_span_before_name};
use crate::line_index::LineIndex;
use crate::pipeline::AnalysisError;

/// Convert a list of parse errors to LSP diagnostics.
pub fn convert_errors(
    errors: &[ParseError],
    interner: &Interner,
    line_index: &LineIndex,
) -> Vec<Diagnostic> {
    errors
        .iter()
        .map(|e| error_to_diagnostic(e, interner, line_index))
        .collect()
}

fn error_to_diagnostic(
    error: &ParseError,
    interner: &Interner,
    line_index: &LineIndex,
) -> Diagnostic {
    let start = line_index.position(error.span.start);
    let end = line_index.position(error.span.end.max(error.span.start + 1));

    let message = socratic_explanation(error, interner);
    let severity = severity_for_kind(&error.kind);
    let code = diagnostic_code_for_kind(&error.kind);

    Diagnostic {
        range: Range { start, end },
        severity: Some(severity),
        code,
        source: Some("logicaffeine".to_string()),
        message,
        ..Default::default()
    }
}

/// Convert analysis errors (escape/ownership) to LSP diagnostics.
///
/// The `uri` parameter is used for `DiagnosticRelatedInformation` locations.
/// Pass `None` to omit related information (e.g., in tests without a real URI).
pub fn convert_analysis_errors(
    errors: &[AnalysisError],
    tokens: &[Token],
    interner: &Interner,
    line_index: &LineIndex,
    uri: Option<&Url>,
) -> Vec<Diagnostic> {
    errors
        .iter()
        .map(|e| analysis_error_to_diagnostic(e, tokens, interner, line_index, uri))
        .collect()
}

fn analysis_error_to_diagnostic(
    error: &AnalysisError,
    tokens: &[Token],
    interner: &Interner,
    line_index: &LineIndex,
    uri: Option<&Url>,
) -> Diagnostic {
    // Resolve the variable name to a span in the token stream
    let (start, end) = if let Some(span) = find_token_span_for_name_pub(tokens, &error.variable, interner) {
        (line_index.position(span.start), line_index.position(span.end))
    } else {
        // Fallback: point to start of document
        let zero = tower_lsp::lsp_types::Position { line: 0, character: 0 };
        (zero, zero)
    };

    // Build related information pointing to the cause (Give statement, Zone entry, etc.)
    let related_information = uri.and_then(|u| build_related_information(error, tokens, interner, line_index, u));

    Diagnostic {
        range: Range { start, end },
        severity: Some(DiagnosticSeverity::ERROR),
        code: Some(tower_lsp::lsp_types::NumberOrString::String(error.code.to_string())),
        source: Some("logicaffeine".to_string()),
        message: error.message.clone(),
        related_information,
        ..Default::default()
    }
}

/// Build `DiagnosticRelatedInformation` linking the error to its cause.
fn build_related_information(
    error: &AnalysisError,
    tokens: &[Token],
    interner: &Interner,
    line_index: &LineIndex,
    uri: &Url,
) -> Option<Vec<DiagnosticRelatedInformation>> {
    let cause_message = error.cause_context.as_ref()?;

    // Determine which keyword token to search for based on error code
    let keyword = match error.code {
        "use-after-move" | "double-move" | "maybe-moved" => TokenType::Give,
        "escape-return" | "escape-assignment" => TokenType::Zone,
        _ => return None,
    };

    let cause_span = find_keyword_span_before_name(tokens, keyword, &error.variable, interner)?;
    let cause_start = line_index.position(cause_span.start);
    let cause_end = line_index.position(cause_span.end);

    Some(vec![DiagnosticRelatedInformation {
        location: Location {
            uri: uri.clone(),
            range: Range {
                start: cause_start,
                end: cause_end,
            },
        },
        message: cause_message.clone(),
    }])
}

/// Return a stable diagnostic code for error kinds that code actions match on.
fn diagnostic_code_for_kind(kind: &ParseErrorKind) -> Option<tower_lsp::lsp_types::NumberOrString> {
    let code = match kind {
        ParseErrorKind::IsValueEquality { .. } => "is-value-equality",
        ParseErrorKind::UseAfterMove { .. } => "use-after-move",
        ParseErrorKind::ZeroIndex => "zero-index",
        ParseErrorKind::GrammarError(_) => "grammar-error",
        ParseErrorKind::UndefinedVariable { .. } => "undefined-variable",
        ParseErrorKind::TypeMismatch { .. } => "type-mismatch",
        _ => return None,
    };
    Some(tower_lsp::lsp_types::NumberOrString::String(code.to_string()))
}

fn severity_for_kind(kind: &ParseErrorKind) -> DiagnosticSeverity {
    match kind {
        // Warnings: style issues that don't prevent compilation
        ParseErrorKind::IsValueEquality { .. } | ParseErrorKind::GrammarError(_) => {
            DiagnosticSeverity::WARNING
        }
        // Information: hints about idiomatic usage
        ParseErrorKind::ZeroIndex => DiagnosticSeverity::INFORMATION,
        // Everything else is an error
        _ => DiagnosticSeverity::ERROR,
    }
}

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

    #[test]
    fn parse_error_produces_diagnostic() {
        let interner = Interner::new();
        let line_index = LineIndex::new("Let x be 5.\nSet x to 10.");

        let error = ParseError {
            kind: ParseErrorKind::ExpectedExpression,
            span: Span::new(12, 15),
        };

        let diagnostics = convert_errors(&[error], &interner, &line_index);
        assert_eq!(diagnostics.len(), 1);
        assert_eq!(diagnostics[0].range.start.line, 1);
        assert_eq!(diagnostics[0].severity, Some(DiagnosticSeverity::ERROR));
        assert!(diagnostics[0].message.contains("expression"));
    }

    #[test]
    fn is_value_equality_is_warning() {
        let interner = Interner::new();
        let line_index = LineIndex::new("x is 5");

        let error = ParseError {
            kind: ParseErrorKind::IsValueEquality {
                variable: "x".to_string(),
                value: "5".to_string(),
            },
            span: Span::new(0, 6),
        };

        let diagnostics = convert_errors(&[error], &interner, &line_index);
        assert_eq!(diagnostics[0].severity, Some(DiagnosticSeverity::WARNING));
    }

    #[test]
    fn grammar_error_is_warning() {
        let interner = Interner::new();
        let line_index = LineIndex::new("bad grammar");

        let error = ParseError {
            kind: ParseErrorKind::GrammarError("test".to_string()),
            span: Span::new(0, 3),
        };

        let diagnostics = convert_errors(&[error], &interner, &line_index);
        assert_eq!(diagnostics[0].severity, Some(DiagnosticSeverity::WARNING));
    }

    #[test]
    fn zero_index_is_information() {
        let interner = Interner::new();
        let line_index = LineIndex::new("list[0]");

        let error = ParseError {
            kind: ParseErrorKind::ZeroIndex,
            span: Span::new(4, 7),
        };

        let diagnostics = convert_errors(&[error], &interner, &line_index);
        assert_eq!(diagnostics[0].severity, Some(DiagnosticSeverity::INFORMATION));
    }

    #[test]
    fn multiple_errors_produce_multiple_diagnostics() {
        let interner = Interner::new();
        let line_index = LineIndex::new("abc\ndef\nghi");

        let errors = vec![
            ParseError {
                kind: ParseErrorKind::ExpectedExpression,
                span: Span::new(0, 3),
            },
            ParseError {
                kind: ParseErrorKind::ExpectedExpression,
                span: Span::new(4, 7),
            },
        ];

        let diagnostics = convert_errors(&errors, &interner, &line_index);
        assert_eq!(diagnostics.len(), 2);
        assert_eq!(diagnostics[0].range.start.line, 0);
        assert_eq!(diagnostics[1].range.start.line, 1);
    }

    #[test]
    fn empty_errors_produce_empty_diagnostics() {
        let interner = Interner::new();
        let line_index = LineIndex::new("fine");
        let diagnostics = convert_errors(&[], &interner, &line_index);
        assert!(diagnostics.is_empty());
    }

    #[test]
    fn use_after_move_is_error_severity() {
        let severity = severity_for_kind(&ParseErrorKind::UseAfterMove {
            name: "x".to_string(),
        });
        assert_eq!(severity, DiagnosticSeverity::ERROR);
    }

    #[test]
    fn diagnostic_range_spans_correct_positions() {
        let interner = Interner::new();
        let line_index = LineIndex::new("abc\ndef");
        let error = ParseError {
            kind: ParseErrorKind::ExpectedExpression,
            span: Span::new(4, 7),
        };
        let diagnostics = convert_errors(&[error], &interner, &line_index);
        assert_eq!(diagnostics[0].range.start.line, 1);
        assert_eq!(diagnostics[0].range.start.character, 0);
        assert_eq!(diagnostics[0].range.end.line, 1);
        assert_eq!(diagnostics[0].range.end.character, 3);
    }

    #[test]
    fn is_value_equality_has_diagnostic_code() {
        let interner = Interner::new();
        let line_index = LineIndex::new("x is 5");

        let error = ParseError {
            kind: ParseErrorKind::IsValueEquality {
                variable: "x".to_string(),
                value: "5".to_string(),
            },
            span: Span::new(0, 6),
        };

        let diagnostics = convert_errors(&[error], &interner, &line_index);
        assert_eq!(
            diagnostics[0].code,
            Some(tower_lsp::lsp_types::NumberOrString::String("is-value-equality".to_string())),
            "IsValueEquality should have diagnostic code"
        );
    }

    #[test]
    fn use_after_move_has_diagnostic_code() {
        let interner = Interner::new();
        let line_index = LineIndex::new("x moved");

        let error = ParseError {
            kind: ParseErrorKind::UseAfterMove {
                name: "x".to_string(),
            },
            span: Span::new(0, 1),
        };

        let diagnostics = convert_errors(&[error], &interner, &line_index);
        assert_eq!(
            diagnostics[0].code,
            Some(tower_lsp::lsp_types::NumberOrString::String("use-after-move".to_string())),
            "UseAfterMove should have diagnostic code"
        );
    }

    #[test]
    fn regular_error_has_no_diagnostic_code() {
        let interner = Interner::new();
        let line_index = LineIndex::new("bad");

        let error = ParseError {
            kind: ParseErrorKind::ExpectedExpression,
            span: Span::new(0, 3),
        };

        let diagnostics = convert_errors(&[error], &interner, &line_index);
        assert_eq!(diagnostics[0].code, None, "ExpectedExpression should have no code");
    }

    #[test]
    fn diagnostic_has_source() {
        let interner = Interner::new();
        let line_index = LineIndex::new("bad");

        let error = ParseError {
            kind: ParseErrorKind::ExpectedExpression,
            span: Span::new(0, 3),
        };

        let diagnostics = convert_errors(&[error], &interner, &line_index);
        assert_eq!(diagnostics[0].source, Some("logicaffeine".to_string()));
    }

    #[test]
    fn escape_error_produces_diagnostic_with_code() {
        let error = AnalysisError {
            message: "Reference 'x' cannot escape zone 'temp'.".to_string(),
            variable: "x".to_string(),
            code: "escape-return",
            cause_context: Some("zone 'temp'".to_string()),
        };
        let interner = Interner::new();
        let line_index = LineIndex::new("Let x be 5.");
        let diagnostics = convert_analysis_errors(&[error], &[], &interner, &line_index, None);
        assert_eq!(diagnostics.len(), 1);
        assert_eq!(
            diagnostics[0].code,
            Some(tower_lsp::lsp_types::NumberOrString::String("escape-return".to_string()))
        );
        assert_eq!(diagnostics[0].severity, Some(DiagnosticSeverity::ERROR));
    }

    #[test]
    fn ownership_error_has_error_severity() {
        let error = AnalysisError {
            message: "Cannot use 'x' after giving it away.".to_string(),
            variable: "x".to_string(),
            code: "use-after-move",
            cause_context: None,
        };
        let interner = Interner::new();
        let line_index = LineIndex::new("Let x be 5.");
        let diagnostics = convert_analysis_errors(&[error], &[], &interner, &line_index, None);
        assert_eq!(diagnostics[0].severity, Some(DiagnosticSeverity::ERROR));
    }

    #[test]
    fn ownership_error_message_is_socratic() {
        let error = AnalysisError {
            message: "Cannot use 'x' after giving it away.\n\nYou transferred ownership.".to_string(),
            variable: "x".to_string(),
            code: "use-after-move",
            cause_context: None,
        };
        let interner = Interner::new();
        let line_index = LineIndex::new("Let x be 5.");
        let diagnostics = convert_analysis_errors(&[error], &[], &interner, &line_index, None);
        assert!(diagnostics[0].message.contains("giving it away"), "Message should be Socratic: {}", diagnostics[0].message);
        assert!(diagnostics[0].message.contains("ownership"), "Message should explain ownership: {}", diagnostics[0].message);
    }

    #[test]
    fn undefined_variable_has_diagnostic_code() {
        let interner = Interner::new();
        let line_index = LineIndex::new("Show z.");

        let error = ParseError {
            kind: ParseErrorKind::UndefinedVariable {
                name: "z".to_string(),
            },
            span: Span::new(5, 6),
        };

        let diagnostics = convert_errors(&[error], &interner, &line_index);
        assert_eq!(
            diagnostics[0].code,
            Some(tower_lsp::lsp_types::NumberOrString::String("undefined-variable".to_string()))
        );
    }

    #[test]
    fn type_mismatch_has_diagnostic_code() {
        let interner = Interner::new();
        let line_index = LineIndex::new("Let x: Int be \"hello\".");

        let error = ParseError {
            kind: ParseErrorKind::TypeMismatch {
                expected: "Int".to_string(),
                found: "Text".to_string(),
            },
            span: Span::new(0, 22),
        };

        let diagnostics = convert_errors(&[error], &interner, &line_index);
        assert_eq!(
            diagnostics[0].code,
            Some(tower_lsp::lsp_types::NumberOrString::String("type-mismatch".to_string()))
        );
    }

    #[test]
    fn use_after_move_diagnostic_has_related_information() {
        // Use the full pipeline to get real tokens with Give
        let source = "## Main\n    Let x be 5.\n    Let y be 0.\n    Give x to y.\n    Show x.\n";
        let result = crate::pipeline::analyze(source);
        let line_index = LineIndex::new(source);
        let uri = Url::parse("file:///test.la").unwrap();

        // Create a move error with cause_context (as pipeline does)
        let error = AnalysisError {
            message: "Cannot use 'x' after giving it away.".to_string(),
            variable: "x".to_string(),
            code: "use-after-move",
            cause_context: Some("'x' was given away here".to_string()),
        };

        let diagnostics = convert_analysis_errors(
            &[error],
            &result.tokens,
            &result.interner,
            &line_index,
            Some(&uri),
        );

        assert_eq!(diagnostics.len(), 1);
        let related = diagnostics[0].related_information.as_ref();
        assert!(
            related.is_some(),
            "Use-after-move diagnostic should have related information"
        );
        let related = related.unwrap();
        assert_eq!(related.len(), 1);
        assert!(
            related[0].message.contains("given away"),
            "Related info should mention giving away: {}",
            related[0].message
        );
        assert_eq!(related[0].location.uri, uri);
    }

    #[test]
    fn related_info_points_to_give_statement() {
        let source = "## Main\n    Let x be 5.\n    Let y be 0.\n    Give x to y.\n    Show x.\n";
        let result = crate::pipeline::analyze(source);
        let line_index = LineIndex::new(source);
        let uri = Url::parse("file:///test.la").unwrap();

        let error = AnalysisError {
            message: "Cannot use 'x' after giving it away.".to_string(),
            variable: "x".to_string(),
            code: "use-after-move",
            cause_context: Some("'x' was given away here".to_string()),
        };

        let diagnostics = convert_analysis_errors(
            &[error],
            &result.tokens,
            &result.interner,
            &line_index,
            Some(&uri),
        );

        let related = diagnostics[0].related_information.as_ref().unwrap();
        // The Give keyword should be on line 3 (0-indexed): "    Give x to y."
        assert_eq!(
            related[0].location.range.start.line, 3,
            "Related info should point to the Give statement on line 3"
        );
    }

    #[test]
    fn no_related_info_without_uri() {
        let source = "## Main\n    Let x be 5.\n    Let y be 0.\n    Give x to y.\n    Show x.\n";
        let result = crate::pipeline::analyze(source);
        let line_index = LineIndex::new(source);

        let error = AnalysisError {
            message: "Cannot use 'x' after giving it away.".to_string(),
            variable: "x".to_string(),
            code: "use-after-move",
            cause_context: Some("'x' was given away here".to_string()),
        };

        let diagnostics = convert_analysis_errors(
            &[error],
            &result.tokens,
            &result.interner,
            &line_index,
            None,
        );

        assert!(
            diagnostics[0].related_information.is_none(),
            "Without URI, related information should be None"
        );
    }

    #[test]
    fn escape_diagnostic_has_related_info_pointing_to_zone() {
        // Escape errors should have related info pointing to the Zone keyword
        let error = AnalysisError {
            message: "Reference 'x' cannot escape zone 'temp'.".to_string(),
            variable: "x".to_string(),
            code: "escape-return",
            cause_context: Some("zone 'temp'".to_string()),
        };

        // Without real tokens containing a Zone keyword, related info won't resolve.
        // This tests the fallback when no matching keyword is found.
        let interner = Interner::new();
        let line_index = LineIndex::new("Zone temp: Let x be 5.");
        let uri = Url::parse("file:///test.la").unwrap();
        let diagnostics = convert_analysis_errors(
            &[error],
            &[],
            &interner,
            &line_index,
            Some(&uri),
        );
        // With empty tokens, the keyword search fails gracefully
        assert_eq!(diagnostics.len(), 1);
        // related_information is None because no tokens matched
        assert!(
            diagnostics[0].related_information.is_none(),
            "With no tokens, related info should be None"
        );
    }
}