rustledger-lsp 0.21.0

Language Server Protocol implementation for Beancount
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
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
//! Import review integration for the LSP.
//!
//! Scans transactions for `import-*` metadata (produced by the enriched
//! import pipeline) and provides:
//!
//! - **Diagnostics**: hints/warnings for imported transactions based on confidence
//! - **Code actions**: accept categorization, change account, batch accept
//! - **Code lens**: summary counts of imported/pending/duplicate transactions

use lsp_types::{
    CodeAction, CodeActionKind, CodeLens, Command, Diagnostic, DiagnosticSeverity, Position, Range,
};
use rustledger_core::{Directive, MetaValue};
use rustledger_parser::Spanned;

use super::utils::{LineIndex, PositionEncoding, ranges_overlap};

/// Convert a byte offset to an LSP Position using a LineIndex.
fn to_position(line_index: &LineIndex, offset: usize) -> Position {
    let (line, col) = line_index.offset_to_position(offset);
    Position {
        line,
        character: col,
    }
}

/// Metadata key for import confidence (set by enriched import pipeline).
const META_CONFIDENCE: &str = "import-confidence";
/// Metadata key for import method (rule, merchant-dict, ml, llm, default).
const META_METHOD: &str = "import-method";

/// Generate diagnostics for imported transactions based on their confidence.
///
/// - confidence > 0.9 → Hint ("Imported via rule: Expenses:Groceries")
/// - confidence 0.5–0.9 → Information ("Review: Expenses:Dining (72%)")
/// - confidence < 0.5 → Warning ("Low confidence: Expenses:Unknown")
pub fn import_diagnostics(
    directives: &[Spanned<Directive>],
    source: &str,
    encoding: PositionEncoding,
) -> Vec<Diagnostic> {
    let line_index = LineIndex::new(source, encoding);
    let mut diagnostics = Vec::new();

    for spanned in directives {
        let Directive::Transaction(txn) = &spanned.value else {
            continue;
        };

        let Some(confidence) = txn.meta.get(META_CONFIDENCE).and_then(meta_to_f64) else {
            continue;
        };

        let method = txn
            .meta
            .get(META_METHOD)
            .and_then(meta_to_str)
            .unwrap_or("unknown");

        // Find the contra-account (the categorized posting, not the bank account)
        let contra_account = txn
            .postings
            .get(1)
            .map(|p| p.account.as_str())
            .unwrap_or("unknown");

        let start = to_position(&line_index, spanned.span.start);
        let end = to_position(&line_index, spanned.span.end.min(source.len()));
        let range = Range { start, end };

        let (severity, message) = if confidence > 0.9 {
            (
                DiagnosticSeverity::HINT,
                format!("Imported ({method}): {contra_account}"),
            )
        } else if confidence >= 0.5 {
            (
                DiagnosticSeverity::INFORMATION,
                format!(
                    "Review ({method}, {:.0}%): {contra_account}",
                    confidence * 100.0
                ),
            )
        } else {
            (
                DiagnosticSeverity::WARNING,
                format!(
                    "Low confidence ({:.0}%): {contra_account} — consider recategorizing",
                    confidence * 100.0
                ),
            )
        };

        diagnostics.push(Diagnostic {
            range,
            severity: Some(severity),
            source: Some("rustledger-import".to_string()),
            message,
            ..Default::default()
        });
    }

    diagnostics
}

/// Generate code actions for imported transactions in the given range.
///
/// Offers:
/// - "Accept categorization" for individual transactions
/// - "Accept all high-confidence imports" for batch operations
pub fn import_code_actions(
    directives: &[Spanned<Directive>],
    source: &str,
    range: Range,
    encoding: PositionEncoding,
) -> Vec<CodeAction> {
    let line_index = LineIndex::new(source, encoding);
    let mut actions = Vec::new();
    let mut high_confidence_count = 0;

    for spanned in directives {
        let Directive::Transaction(txn) = &spanned.value else {
            continue;
        };

        let Some(confidence) = txn.meta.get(META_CONFIDENCE).and_then(meta_to_f64) else {
            continue;
        };

        if confidence > 0.9 {
            high_confidence_count += 1;
        }

        let start = to_position(&line_index, spanned.span.start);
        let end = to_position(&line_index, spanned.span.end.min(source.len()));
        let txn_range = Range { start, end };

        // Only offer actions for transactions that overlap the selection
        if !ranges_overlap(range, txn_range) {
            continue;
        }

        let contra_account = txn
            .postings
            .get(1)
            .map(|p| p.account.to_string())
            .unwrap_or_default();

        actions.push(CodeAction {
            title: format!("Accept import: {contra_account}"),
            kind: Some(CodeActionKind::QUICKFIX),
            command: Some(Command {
                title: "Accept import".to_string(),
                command: "rledger.importAccept".to_string(),
                arguments: Some(vec![serde_json::json!({
                    "line": start.line,
                })]),
            }),
            ..Default::default()
        });
    }

    // Batch accept action
    if high_confidence_count > 1 {
        actions.push(CodeAction {
            title: format!("Accept all {high_confidence_count} high-confidence imports"),
            kind: Some(CodeActionKind::QUICKFIX),
            command: Some(Command {
                title: "Accept all high-confidence".to_string(),
                command: "rledger.importAcceptAll".to_string(),
                arguments: None,
            }),
            ..Default::default()
        });
    }

    actions
}

/// Generate code lens for import summary.
///
/// Shows a summary line above the first imported transaction:
/// "N imported | M need review | K duplicates"
pub fn import_code_lens(
    directives: &[Spanned<Directive>],
    source: &str,
    encoding: PositionEncoding,
) -> Vec<CodeLens> {
    let line_index = LineIndex::new(source, encoding);
    let mut total = 0u32;
    let mut needs_review = 0u32;
    let mut first_import_line: Option<Position> = None;

    for spanned in directives {
        let Directive::Transaction(txn) = &spanned.value else {
            continue;
        };

        let Some(confidence) = txn.meta.get(META_CONFIDENCE).and_then(meta_to_f64) else {
            continue;
        };

        total += 1;
        if confidence < 0.9 {
            needs_review += 1;
        }

        if first_import_line.is_none() {
            first_import_line = Some(to_position(&line_index, spanned.span.start));
        }
    }

    if total == 0 {
        return vec![];
    }

    let Some(pos) = first_import_line else {
        return vec![];
    };

    let title = if needs_review > 0 {
        format!("{total} imported | {needs_review} need review")
    } else {
        format!("{total} imported | all accepted")
    };

    vec![CodeLens {
        range: Range {
            start: Position {
                line: pos.line,
                character: 0,
            },
            end: Position {
                line: pos.line,
                character: 0,
            },
        },
        command: Some(Command {
            title,
            command: "rustledger.importSummary".to_string(),
            arguments: None,
        }),
        data: None,
    }]
}

/// Extract f64 from MetaValue.
fn meta_to_f64(v: &MetaValue) -> Option<f64> {
    match v {
        MetaValue::Number(d) => {
            use rust_decimal::prelude::ToPrimitive;
            d.to_f64()
        }
        _ => None,
    }
}

/// Extract &str from MetaValue.
fn meta_to_str(v: &MetaValue) -> Option<&str> {
    match v {
        MetaValue::String(s) => Some(s.as_str()),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rust_decimal::Decimal;
    use rustledger_core::{Metadata, Posting, Transaction, naive_date};
    use rustledger_parser::Span;
    use std::str::FromStr;

    fn make_imported_txn(confidence: f64, method: &str, account: &str) -> Spanned<Directive> {
        let date = naive_date(2024, 1, 15).unwrap();
        let mut meta = Metadata::default();
        meta.insert(
            META_CONFIDENCE.to_string(),
            MetaValue::Number(Decimal::from_str(&confidence.to_string()).unwrap()),
        );
        meta.insert(
            META_METHOD.to_string(),
            MetaValue::String(method.to_string()),
        );

        let txn = Transaction {
            date,
            flag: '*',
            payee: None,
            narration: "Test".into(),
            tags: vec![],
            links: vec![],
            meta,
            postings: vec![
                rustledger_core::Spanned::synthesized(Posting::new(
                    "Assets:Bank",
                    rustledger_core::Amount::new(Decimal::from_str("-50").unwrap(), "USD"),
                )),
                rustledger_core::Spanned::synthesized(Posting::new(
                    account,
                    rustledger_core::Amount::new(Decimal::from_str("50").unwrap(), "USD"),
                )),
            ],
            trailing_comments: vec![],
        };

        Spanned {
            value: Directive::Transaction(txn),
            span: Span { start: 0, end: 100 },
            file_id: 0,
        }
    }

    fn make_source() -> String {
        // Source text long enough for the spans
        " ".repeat(200)
    }

    #[test]
    fn diagnostics_high_confidence_is_hint() {
        let source = make_source();
        let directives = vec![make_imported_txn(0.95, "rule", "Expenses:Groceries")];
        let diags = import_diagnostics(&directives, &source, PositionEncoding::Utf16);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].severity, Some(DiagnosticSeverity::HINT));
        assert!(diags[0].message.contains("Expenses:Groceries"));
    }

    #[test]
    fn diagnostics_medium_confidence_is_info() {
        let source = make_source();
        let directives = vec![make_imported_txn(0.72, "ml", "Expenses:Dining")];
        let diags = import_diagnostics(&directives, &source, PositionEncoding::Utf16);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].severity, Some(DiagnosticSeverity::INFORMATION));
        assert!(diags[0].message.contains("72%"));
    }

    #[test]
    fn diagnostics_low_confidence_is_warning() {
        let source = make_source();
        let directives = vec![make_imported_txn(0.3, "default", "Expenses:Unknown")];
        let diags = import_diagnostics(&directives, &source, PositionEncoding::Utf16);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].severity, Some(DiagnosticSeverity::WARNING));
        assert!(diags[0].message.contains("recategorizing"));
    }

    #[test]
    fn diagnostics_skips_non_imported() {
        let source = make_source();
        let date = naive_date(2024, 1, 15).unwrap();
        let txn = Transaction::new(date, "Not imported");
        let directives = vec![Spanned {
            value: Directive::Transaction(txn),
            span: Span { start: 0, end: 50 },
            file_id: 0,
        }];
        let diags = import_diagnostics(&directives, &source, PositionEncoding::Utf16);
        assert!(diags.is_empty());
    }

    #[test]
    fn code_lens_shows_summary() {
        let source = make_source();
        let directives = vec![
            make_imported_txn(0.95, "rule", "Expenses:Groceries"),
            make_imported_txn(0.6, "ml", "Expenses:Dining"),
            make_imported_txn(0.3, "default", "Expenses:Unknown"),
        ];
        let lenses = import_code_lens(&directives, &source, PositionEncoding::Utf16);
        assert_eq!(lenses.len(), 1);
        let title = &lenses[0].command.as_ref().unwrap().title;
        assert!(title.contains("3 imported"));
        assert!(title.contains("2 need review"));
    }

    #[test]
    fn code_lens_all_accepted() {
        let source = make_source();
        let directives = vec![
            make_imported_txn(0.95, "rule", "Expenses:Groceries"),
            make_imported_txn(0.99, "rule", "Expenses:Dining"),
        ];
        let lenses = import_code_lens(&directives, &source, PositionEncoding::Utf16);
        assert_eq!(lenses.len(), 1);
        let title = &lenses[0].command.as_ref().unwrap().title;
        assert!(title.contains("all accepted"));
    }

    #[test]
    fn code_lens_empty_for_no_imports() {
        let source = make_source();
        let directives = vec![];
        let lenses = import_code_lens(&directives, &source, PositionEncoding::Utf16);
        assert!(lenses.is_empty());
    }

    // ===== Code action tests =====

    fn make_imported_txn_at(
        confidence: f64,
        method: &str,
        account: &str,
        start: usize,
        end: usize,
    ) -> Spanned<Directive> {
        let date = naive_date(2024, 1, 15).unwrap();
        let mut meta = Metadata::default();
        meta.insert(
            META_CONFIDENCE.to_string(),
            MetaValue::Number(Decimal::from_str(&confidence.to_string()).unwrap()),
        );
        meta.insert(
            META_METHOD.to_string(),
            MetaValue::String(method.to_string()),
        );

        let txn = Transaction {
            date,
            flag: '*',
            payee: None,
            narration: "Test".into(),
            tags: vec![],
            links: vec![],
            meta,
            postings: vec![
                rustledger_core::Spanned::synthesized(Posting::new(
                    "Assets:Bank",
                    rustledger_core::Amount::new(Decimal::from_str("-50").unwrap(), "USD"),
                )),
                rustledger_core::Spanned::synthesized(Posting::new(
                    account,
                    rustledger_core::Amount::new(Decimal::from_str("50").unwrap(), "USD"),
                )),
            ],
            trailing_comments: vec![],
        };

        Spanned {
            value: Directive::Transaction(txn),
            span: Span { start, end },
            file_id: 0,
        }
    }

    #[test]
    fn code_actions_appear_for_txns_in_range() {
        // Source: 400 chars, two txns at different positions
        let source = " ".repeat(400);
        let directives = vec![
            make_imported_txn_at(0.95, "rule", "Expenses:Groceries", 0, 100),
            make_imported_txn_at(0.6, "ml", "Expenses:Dining", 200, 300),
        ];

        // Request actions covering only the first transaction
        let range = Range {
            start: Position {
                line: 0,
                character: 0,
            },
            end: Position {
                line: 0,
                character: 50,
            },
        };
        let actions = import_code_actions(&directives, &source, range, PositionEncoding::Utf16);

        // Should have action for first txn + batch action (2 high-confidence)
        let accept_actions: Vec<_> = actions
            .iter()
            .filter(|a| a.title.starts_with("Accept import:"))
            .collect();
        assert_eq!(accept_actions.len(), 1);
        assert!(accept_actions[0].title.contains("Expenses:Groceries"));
    }

    #[test]
    fn code_actions_not_outside_range() {
        let source = " ".repeat(400);
        let directives = vec![make_imported_txn_at(
            0.95,
            "rule",
            "Expenses:Groceries",
            200,
            300,
        )];

        // Request actions for range 0..50 which does NOT overlap the txn at 200..300
        // Line index: since source is all spaces on line 0, byte 200 is still line 0, char 200
        // So we need a range that ends before char 200
        let range = Range {
            start: Position {
                line: 0,
                character: 0,
            },
            end: Position {
                line: 0,
                character: 50,
            },
        };
        let actions = import_code_actions(&directives, &source, range, PositionEncoding::Utf16);

        // No accept actions for individual txns (only batch if applicable)
        let accept_actions: Vec<_> = actions
            .iter()
            .filter(|a| a.title.starts_with("Accept import:"))
            .collect();
        assert!(accept_actions.is_empty());
    }

    #[test]
    fn code_actions_batch_accept_count() {
        let source = " ".repeat(600);
        let directives = vec![
            make_imported_txn_at(0.95, "rule", "Expenses:Groceries", 0, 100),
            make_imported_txn_at(0.99, "rule", "Expenses:Dining", 100, 200),
            make_imported_txn_at(0.3, "default", "Expenses:Unknown", 200, 300),
        ];

        // Request actions covering all transactions
        let range = Range {
            start: Position {
                line: 0,
                character: 0,
            },
            end: Position {
                line: 0,
                character: 599,
            },
        };
        let actions = import_code_actions(&directives, &source, range, PositionEncoding::Utf16);

        // Should have batch accept with count = 2 (only the two > 0.9)
        let batch_action = actions
            .iter()
            .find(|a| a.title.contains("Accept all"))
            .expect("batch action should exist");
        assert!(batch_action.title.contains("2"));
    }

    #[test]
    fn code_actions_no_batch_when_single_high_confidence() {
        let source = " ".repeat(400);
        let directives = vec![
            make_imported_txn_at(0.95, "rule", "Expenses:Groceries", 0, 100),
            make_imported_txn_at(0.3, "default", "Expenses:Unknown", 100, 200),
        ];

        let range = Range {
            start: Position {
                line: 0,
                character: 0,
            },
            end: Position {
                line: 0,
                character: 399,
            },
        };
        let actions = import_code_actions(&directives, &source, range, PositionEncoding::Utf16);

        // Only 1 high confidence → no batch action
        let batch_actions: Vec<_> = actions
            .iter()
            .filter(|a| a.title.contains("Accept all"))
            .collect();
        assert!(batch_actions.is_empty());
    }

    #[test]
    fn ranges_overlap_basic() {
        let a = Range {
            start: Position {
                line: 0,
                character: 0,
            },
            end: Position {
                line: 0,
                character: 10,
            },
        };
        let b = Range {
            start: Position {
                line: 0,
                character: 5,
            },
            end: Position {
                line: 0,
                character: 15,
            },
        };
        assert!(ranges_overlap(a, b));
    }

    #[test]
    fn ranges_no_overlap() {
        let a = Range {
            start: Position {
                line: 0,
                character: 0,
            },
            end: Position {
                line: 0,
                character: 5,
            },
        };
        let b = Range {
            start: Position {
                line: 0,
                character: 10,
            },
            end: Position {
                line: 0,
                character: 15,
            },
        };
        assert!(!ranges_overlap(a, b));
    }

    /// LSP half-open `Range.end` semantics: adjacent ranges where
    /// `a.end == b.start` are NOT overlapping. Pre-round-17 the import
    /// module had a private `ranges_overlap` with strict `<` semantics
    /// that returned `true` for this boundary case; the round-17
    /// canonicalization hoisted the helper to `utils::ranges_overlap`
    /// with the spec-correct `<=` semantics. This test pins the new
    /// behavior so a future revert (or accidental re-introduction of
    /// the old `<` predicate) is caught.
    #[test]
    fn ranges_adjacent_are_not_overlapping() {
        let a = Range {
            start: Position::new(0, 0),
            end: Position::new(0, 10),
        };
        let b = Range {
            start: Position::new(0, 10),
            end: Position::new(0, 20),
        };
        assert!(
            !ranges_overlap(a, b),
            "adjacent ranges (a.end == b.start) must not overlap under LSP half-open semantics"
        );
        assert!(
            !ranges_overlap(b, a),
            "adjacency is symmetric — swapping the args must not flip the result"
        );

        // Cross-line adjacency: a ends on line 1 col 0 (= start of
        // line 1), b starts at line 1 col 0. Same not-overlapping.
        let a = Range {
            start: Position::new(0, 0),
            end: Position::new(1, 0),
        };
        let b = Range {
            start: Position::new(1, 0),
            end: Position::new(2, 0),
        };
        assert!(!ranges_overlap(a, b));
    }

    #[test]
    fn diagnostics_source_is_set() {
        let source = make_source();
        let directives = vec![make_imported_txn(0.95, "rule", "Expenses:Groceries")];
        let diags = import_diagnostics(&directives, &source, PositionEncoding::Utf16);
        assert_eq!(diags[0].source.as_deref(), Some("rustledger-import"));
    }
}