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
use libslide::diagnostics::{AssociatedDiagnostic, Diagnostic, DiagnosticKind};
use annotate_snippets::{
display_list::{DisplayList, FormatOptions},
snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
};
pub fn sanitize_source_for_diagnostics(source: &str) -> String {
let source = source.to_owned();
source + " "
}
pub fn emit_slide_diagnostics(
file: Option<&str>,
source: &str,
diagnostics: &[Diagnostic],
color: bool,
) -> String {
if diagnostics.is_empty() {
return String::new();
}
let last_i = diagnostics.len() - 1;
let mut emitted_diagnostics = String::new();
for (i, diagnostic) in diagnostics.iter().enumerate() {
let main_annotation_type = convert_diagnostic_kind(&diagnostic.kind);
let mut annotations = Vec::with_capacity(diagnostic.associated_diagnostics.len() + 1);
let label = diagnostic.msg.clone().unwrap_or_default();
annotations.push(SourceAnnotation {
label: &label,
annotation_type: main_annotation_type,
range: diagnostic.span.into(),
});
for associated_diagnostic in diagnostic.associated_diagnostics.iter() {
annotations.push(SourceAnnotation {
label: &associated_diagnostic.msg,
annotation_type: convert_diagnostic_kind(&associated_diagnostic.kind),
range: associated_diagnostic.span.into(),
});
}
let mut footer = Vec::with_capacity(1);
for associated_diagnostic in diagnostic.unspanned_associated_diagnostics.iter() {
footer.push(convert_associated_diagnostic(associated_diagnostic));
}
let snippet = Snippet {
title: Some(Annotation {
label: Some(&diagnostic.title),
id: Some(diagnostic.code),
annotation_type: main_annotation_type,
}),
footer,
slices: vec![Slice {
source: &source,
line_start: 1,
origin: file,
fold: true,
annotations,
}],
opt: FormatOptions {
color,
..Default::default()
},
};
let suffix = if i != last_i { "\n" } else { "" };
emitted_diagnostics.push_str(&format!("{}\n{}", DisplayList::from(snippet), suffix));
}
emitted_diagnostics
}
fn convert_associated_diagnostic(diagnostic: &AssociatedDiagnostic) -> Annotation {
Annotation {
label: Some(&diagnostic.msg),
id: None,
annotation_type: convert_diagnostic_kind(&diagnostic.kind),
}
}
fn convert_diagnostic_kind(diagnostic_kind: &DiagnosticKind) -> AnnotationType {
match diagnostic_kind {
DiagnosticKind::Error => AnnotationType::Error,
DiagnosticKind::Warning => AnnotationType::Warning,
DiagnosticKind::Note => AnnotationType::Note,
DiagnosticKind::Help => AnnotationType::Help,
}
}