oxilean-cli 0.1.2

OxiLean command-line interface
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
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

use crate::lsp::{
    analyze_document, DiagnosticSeverity, Document, DocumentStore, JsonValue, Location, Range,
    SymbolKind, TextEdit,
};
use oxilean_kernel::{Environment, Name};
use std::collections::{HashMap, HashSet};

use super::types::{
    AdvDiagnostic, AdvDiagnosticCache, AdvDiagnosticCode, AdvDiagnosticCollector,
    DiagnosticAnnotator, DiagnosticExportFormat, DiagnosticExporter, DiagnosticGroup,
    DiagnosticHeatmap, DiagnosticQuery, DiagnosticRateLimiter, DiagnosticState,
    DiagnosticStateMachine, DiagnosticSummary, DiagnosticSummaryReport, DiagnosticSuppression,
    DiagnosticTrend, FixKind, SeverityConfig, SuppressionRegistry, TrendDirection,
};

/// Helper function.
pub fn closing_for(ch: char) -> char {
    match ch {
        '(' => ')',
        '[' => ']',
        '{' => '}',
        _ => ch,
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    fn make_doc(content: &str) -> Document {
        Document::new("file:///test.lean", 1, content)
    }
    #[test]
    fn test_severity_config_default() {
        let config = SeverityConfig::default();
        assert!(!config.warnings_as_errors);
        assert!(!config.suppress_hints);
    }
    #[test]
    fn test_severity_config_suppress() {
        let mut config = SeverityConfig::new();
        config.suppress("W001");
        assert!(config.is_suppressed("W001"));
        assert!(!config.is_suppressed("W002"));
        config.unsuppress("W001");
        assert!(!config.is_suppressed("W001"));
    }
    #[test]
    fn test_severity_config_override() {
        let mut config = SeverityConfig::new();
        config.set_override("W001", DiagnosticSeverity::Error);
        let effective = config.effective_severity("W001", DiagnosticSeverity::Warning);
        assert_eq!(effective, Some(DiagnosticSeverity::Error));
    }
    #[test]
    fn test_severity_config_warnings_as_errors() {
        let mut config = SeverityConfig::new();
        config.warnings_as_errors = true;
        let effective = config.effective_severity("W999", DiagnosticSeverity::Warning);
        assert_eq!(effective, Some(DiagnosticSeverity::Error));
    }
    #[test]
    fn test_adv_diagnostic_code_str() {
        assert_eq!(AdvDiagnosticCode::LexError.as_str(), "E001");
        assert_eq!(AdvDiagnosticCode::UnusedVariable.as_str(), "W001");
        assert_eq!(AdvDiagnosticCode::StyleNaming.as_str(), "S001");
    }
    #[test]
    fn test_adv_diagnostic_collector_lex_errors() {
        let env = Environment::new();
        let collector = AdvDiagnosticCollector::new(&env);
        let doc = make_doc("def x := 42");
        let diags = collector.collect_lex_errors(&doc);
        assert!(diags.is_empty());
    }
    #[test]
    fn test_adv_diagnostic_collector_style() {
        let env = Environment::new();
        let mut collector = AdvDiagnosticCollector::new(&env);
        collector.set_max_line_length(10);
        let doc = make_doc("def x := this_is_a_very_long_line_that_exceeds_limits");
        let diags = collector.collect_style_warnings(&doc);
        assert!(!diags.is_empty());
    }
    #[test]
    fn test_adv_diagnostic_collector_trailing_whitespace() {
        let env = Environment::new();
        let collector = AdvDiagnosticCollector::new(&env);
        let doc = make_doc("def x := 1   \ndef y := 2");
        let diags = collector.collect_style_warnings(&doc);
        let trailing = diags
            .iter()
            .filter(|d| d.code == AdvDiagnosticCode::StyleTrailingWhitespace)
            .count();
        assert!(trailing > 0);
    }
    #[test]
    fn test_diagnostic_summary() {
        let summary = DiagnosticSummary {
            errors: 3,
            warnings: 5,
            info: 2,
            hints: 1,
            files_with_issues: 2,
            total_files: 4,
        };
        assert!(summary.has_errors());
        assert_eq!(summary.total(), 11);
        let formatted = summary.format();
        assert!(formatted.contains("3 error(s)"));
    }
    #[test]
    fn test_adv_diagnostic_cache() {
        let mut cache = AdvDiagnosticCache::new();
        assert!(cache.is_empty());
        cache.store("file:///a.lean", 1, Vec::new());
        assert_eq!(cache.len(), 1);
        assert!(cache.get("file:///a.lean", 1).is_some());
        assert!(cache.get("file:///a.lean", 2).is_none());
        cache.invalidate("file:///a.lean");
        assert!(cache.is_empty());
    }
    #[test]
    fn test_fix_kind_str() {
        assert_eq!(FixKind::QuickFix.as_str(), "quickfix");
        assert_eq!(FixKind::OrganizeImports.as_str(), "source.organizeImports");
    }
    #[test]
    fn test_adv_diagnostic_to_json() {
        let diag = AdvDiagnostic {
            code: AdvDiagnosticCode::TypeError,
            severity: DiagnosticSeverity::Error,
            range: Range::single_line(0, 0, 5),
            message: "type mismatch".to_string(),
            related: Vec::new(),
            fixes: Vec::new(),
            tags: Vec::new(),
            uri: "file:///test.lean".to_string(),
        };
        let json = diag.to_json();
        let msg = json.get("message").and_then(|v| v.as_str());
        assert_eq!(msg, Some("type mismatch"));
    }
}
/// A composable filter for advanced diagnostics.
#[allow(dead_code)]
pub trait DiagnosticFilter: Send + Sync {
    /// Return true if this diagnostic passes the filter.
    fn accepts(&self, diag: &AdvDiagnostic) -> bool;
}
/// Return the diagnostics_adv module version.
#[allow(dead_code)]
pub fn diagnostics_adv_version() -> &'static str {
    "0.1.1"
}
#[cfg(test)]
mod adv_extra_tests {
    use super::*;
    use crate::lsp::diagnostics_adv::*;
    use crate::lsp::DiagnosticSeverity;
    fn make_diag(line: u32, msg: &str) -> AdvDiagnostic {
        AdvDiagnostic {
            code: AdvDiagnosticCode::TypeError,
            severity: DiagnosticSeverity::Error,
            range: Range::single_line(line, 0, 5),
            message: msg.to_string(),
            related: vec![],
            fixes: vec![],
            tags: vec![],
            uri: "file:///test.lean".to_string(),
        }
    }
    #[test]
    fn test_diagnostic_trend() {
        let mut trend = DiagnosticTrend::new("file:///a.lean", 5);
        trend.record(3);
        trend.record(4);
        trend.record(5);
        assert_eq!(trend.direction(), TrendDirection::Increasing);
        assert!((trend.moving_average() - 4.0).abs() < 0.01);
    }
    #[test]
    fn test_diagnostic_trend_decreasing() {
        let mut trend = DiagnosticTrend::new("file:///a.lean", 5);
        trend.record(10);
        trend.record(5);
        trend.record(2);
        assert_eq!(trend.direction(), TrendDirection::Decreasing);
    }
    #[test]
    fn test_suppression_registry() {
        let mut reg = SuppressionRegistry::new();
        reg.add(DiagnosticSuppression::new(
            "file:///test.lean",
            0,
            10,
            vec![AdvDiagnosticCode::TypeError],
            "intentional",
        ));
        let d = make_diag(5, "msg");
        assert!(reg.is_suppressed(&d));
        let d2 = make_diag(20, "msg");
        assert!(!reg.is_suppressed(&d2));
    }
    #[test]
    fn test_diagnostic_annotator() {
        let d = make_diag(0, "boom");
        let mut ann = DiagnosticAnnotator::new();
        ann.add_from_diagnostic(&d);
        let src = "let x : Nat := 1";
        let rendered = ann.render(src);
        assert!(rendered.contains("boom"));
        assert!(rendered.contains('^'));
    }
    #[test]
    fn test_diagnostic_exporter_plain() {
        let d = make_diag(0, "test error");
        let out = DiagnosticExporter::export(&[d], DiagnosticExportFormat::Plain);
        assert!(out.contains("[ERROR]"));
        assert!(out.contains("test error"));
    }
    #[test]
    fn test_diagnostic_exporter_csv() {
        let d = make_diag(2, "test");
        let csv = DiagnosticExporter::export(&[d], DiagnosticExportFormat::Csv);
        assert!(csv.contains("uri,line"));
        assert!(csv.contains("error"));
    }
    #[test]
    fn test_diagnostic_exporter_sarif() {
        let d = make_diag(0, "test");
        let sarif = DiagnosticExporter::export(&[d], DiagnosticExportFormat::Sarif);
        assert!(sarif.contains("2.1.0"));
        assert!(sarif.contains("oxilean"));
    }
    #[test]
    fn test_diagnostic_exporter_json() {
        let d = make_diag(0, "test");
        let json = DiagnosticExporter::export(&[d], DiagnosticExportFormat::Json);
        assert!(json.starts_with('['));
        assert!(json.contains("message"));
    }
    #[test]
    fn test_diagnostic_heatmap() {
        let diags = vec![make_diag(0, "e1"), make_diag(0, "e2"), make_diag(5, "w1")];
        let heatmap = DiagnosticHeatmap::build("file:///a.lean", &diags, 10);
        assert_eq!(heatmap.hottest_line(), Some(0));
        let bars = heatmap.render_bars(10);
        assert!(bars.contains("##"));
    }
    #[test]
    fn test_summary_report() {
        let report = DiagnosticSummaryReport {
            total_errors: 3,
            total_warnings: 5,
            total_info: 1,
            total_hints: 0,
            files_with_errors: 2,
            files_checked: 10,
            top_error_codes: vec![],
        };
        assert_eq!(report.total(), 9);
        assert!(report.has_errors());
        let rendered = report.render();
        assert!(rendered.contains("Errors:"));
    }
    #[test]
    fn test_diagnostics_adv_version() {
        assert!(!diagnostics_adv_version().is_empty());
    }
}
/// Deduplicates diagnostics by URI+range+message key.
#[allow(dead_code)]
pub fn dedup_diagnostics(diagnostics: Vec<AdvDiagnostic>) -> Vec<AdvDiagnostic> {
    let mut seen = std::collections::HashSet::new();
    diagnostics
        .into_iter()
        .filter(|d| {
            let key = format!(
                "{}:{}:{}:{}",
                d.uri, d.range.start.line, d.range.start.character, d.message
            );
            seen.insert(key)
        })
        .collect()
}
/// Assigns priority scores to diagnostics for display ordering.
#[allow(dead_code)]
pub fn prioritize_diagnostics(diagnostics: &mut Vec<AdvDiagnostic>) {
    diagnostics.sort_by(|a, b| {
        let sev_rank = |s: &DiagnosticSeverity| match s {
            DiagnosticSeverity::Error => 0,
            DiagnosticSeverity::Warning => 1,
            DiagnosticSeverity::Information => 2,
            DiagnosticSeverity::Hint => 3,
        };
        sev_rank(&a.severity)
            .cmp(&sev_rank(&b.severity))
            .then(a.range.start.line.cmp(&b.range.start.line))
    });
}
/// Group diagnostics by URI.
#[allow(dead_code)]
pub fn group_diagnostics_by_file(diagnostics: Vec<AdvDiagnostic>) -> Vec<DiagnosticGroup> {
    let mut map: std::collections::HashMap<String, Vec<AdvDiagnostic>> =
        std::collections::HashMap::new();
    for d in diagnostics {
        map.entry(d.uri.clone()).or_default().push(d);
    }
    let mut groups: Vec<DiagnosticGroup> = map
        .into_iter()
        .map(|(uri, ds)| DiagnosticGroup {
            uri,
            diagnostics: ds,
        })
        .collect();
    groups.sort_by(|a, b| a.uri.cmp(&b.uri));
    groups
}
#[cfg(test)]
mod adv_dedup_tests {
    use super::*;
    fn make_d(line: u32, msg: &str) -> AdvDiagnostic {
        AdvDiagnostic {
            code: AdvDiagnosticCode::TypeError,
            severity: DiagnosticSeverity::Error,
            range: Range::single_line(line, 0, 5),
            message: msg.to_string(),
            related: vec![],
            fixes: vec![],
            tags: vec![],
            uri: "file:///a.lean".to_string(),
        }
    }
    #[test]
    fn test_dedup_diagnostics() {
        let diags = vec![
            make_d(0, "error A"),
            make_d(0, "error A"),
            make_d(1, "error B"),
        ];
        let deduped = dedup_diagnostics(diags);
        assert_eq!(deduped.len(), 2);
    }
    #[test]
    fn test_prioritize_diagnostics() {
        let mut diags = vec![make_d(3, "late error"), make_d(0, "early error")];
        prioritize_diagnostics(&mut diags);
        assert_eq!(diags[0].range.start.line, 0);
    }
    #[test]
    fn test_group_diagnostics_by_file() {
        let mut d2 = make_d(1, "b");
        d2.uri = "file:///b.lean".to_string();
        let diags = vec![make_d(0, "a"), d2];
        let groups = group_diagnostics_by_file(diags);
        assert_eq!(groups.len(), 2);
    }
    #[test]
    fn test_diagnostic_state_machine() {
        let d = make_d(0, "test");
        let mut sm = DiagnosticStateMachine::new();
        sm.update(&[d.clone()]);
        sm.update(&[d.clone()]);
        let state = sm.state_of(&d);
        assert_eq!(state, DiagnosticState::Persistent);
        sm.update(&[]);
        let state2 = sm.state_of(&d);
        assert_eq!(state2, DiagnosticState::Resolved);
    }
    #[test]
    fn test_diagnostic_group_counts() {
        let d1 = make_d(0, "e1");
        let mut d2 = make_d(1, "w1");
        d2.severity = super::DiagnosticSeverity::Warning;
        let group = DiagnosticGroup {
            uri: "file:///a.lean".to_string(),
            diagnostics: vec![d1, d2],
        };
        assert_eq!(group.error_count(), 1);
        assert_eq!(group.warning_count(), 1);
    }
}
/// Return the version string for this module.
#[allow(dead_code)]
pub fn diagnostics_adv_ext_version() -> &'static str {
    "0.1.1"
}
#[cfg(test)]
mod rate_limiter_tests {
    use super::*;
    #[test]
    fn test_rate_limiter_allows_first() {
        let mut limiter = DiagnosticRateLimiter::new(1000);
        assert!(limiter.should_publish("file:///a.lean"));
    }
    #[test]
    fn test_rate_limiter_blocks_second() {
        let mut limiter = DiagnosticRateLimiter::new(10000);
        limiter.should_publish("file:///a.lean");
        assert!(!limiter.should_publish("file:///a.lean"));
    }
    #[test]
    fn test_rate_limiter_reset() {
        let mut limiter = DiagnosticRateLimiter::new(10000);
        limiter.should_publish("file:///a.lean");
        limiter.reset("file:///a.lean");
        assert!(limiter.should_publish("file:///a.lean"));
    }
}
#[cfg(test)]
mod query_tests {
    use super::*;
    fn make_err(msg: &str) -> AdvDiagnostic {
        AdvDiagnostic {
            code: AdvDiagnosticCode::TypeError,
            severity: DiagnosticSeverity::Error,
            range: Range::single_line(0, 0, 5),
            message: msg.to_string(),
            related: vec![],
            fixes: vec![],
            tags: vec![],
            uri: "file:///a.lean".to_string(),
        }
    }
    #[test]
    fn test_query_all() {
        let d = make_err("test");
        assert!(DiagnosticQuery::All.matches(&d));
    }
    #[test]
    fn test_query_errors() {
        let d = make_err("test");
        assert!(DiagnosticQuery::Errors.matches(&d));
        assert!(!DiagnosticQuery::Warnings.matches(&d));
    }
    #[test]
    fn test_query_keyword() {
        let d = make_err("type mismatch found");
        assert!(DiagnosticQuery::HasKeyword("mismatch".to_string()).matches(&d));
        assert!(!DiagnosticQuery::HasKeyword("unify".to_string()).matches(&d));
    }
    #[test]
    fn test_query_and_or() {
        let d = make_err("type error");
        let q = DiagnosticQuery::And(
            Box::new(DiagnosticQuery::Errors),
            Box::new(DiagnosticQuery::HasKeyword("type".to_string())),
        );
        assert!(q.matches(&d));
        let q2 = DiagnosticQuery::Or(
            Box::new(DiagnosticQuery::Warnings),
            Box::new(DiagnosticQuery::HasKeyword("type".to_string())),
        );
        assert!(q2.matches(&d));
    }
    #[test]
    fn test_query_not() {
        let d = make_err("test");
        let q = DiagnosticQuery::Not(Box::new(DiagnosticQuery::Warnings));
        assert!(q.matches(&d));
    }
}