|
| 1 | +use diagnostics::diagnostic::Level; |
| 2 | +use tower_lsp::lsp_types::{ |
| 3 | + Diagnostic, DiagnosticRelatedInformation, DiagnosticSeverity, Location, Position, Range, Url, |
| 4 | +}; |
| 5 | + |
| 6 | +pub fn to_tower_diag(diagnostic: diagnostics::diagnostic::Diagnostic) -> Diagnostic { |
| 7 | + let severity = match diagnostic.level { |
| 8 | + Level::Error => DiagnosticSeverity::ERROR, |
| 9 | + Level::Warning => DiagnosticSeverity::WARNING, |
| 10 | + Level::Note => DiagnosticSeverity::INFORMATION, |
| 11 | + }; |
| 12 | + |
| 13 | + let mut related_info = vec![]; |
| 14 | + |
| 15 | + for span in diagnostic.spans { |
| 16 | + related_info.push(DiagnosticRelatedInformation { |
| 17 | + location: Location { |
| 18 | + uri: Url::parse(&span.start.file_path).unwrap(), |
| 19 | + range: Range { |
| 20 | + start: Position::new(span.start.line as u32, span.start.col as u32), |
| 21 | + end: Position::new(span.start.line as u32, span.start.end_col as u32), |
| 22 | + }, |
| 23 | + }, |
| 24 | + message: span.label.unwrap().into(), |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + let main_range = Range { |
| 29 | + start: Position::new( |
| 30 | + diagnostic.primary_span.start.line as u32, |
| 31 | + diagnostic.primary_span.start.col as u32, |
| 32 | + ), |
| 33 | + end: Position::new( |
| 34 | + diagnostic.primary_span.start.line as u32, |
| 35 | + diagnostic.primary_span.start.end_col as u32, |
| 36 | + ), |
| 37 | + }; |
| 38 | + |
| 39 | + Diagnostic { |
| 40 | + range: main_range, |
| 41 | + message: diagnostic.primary_span.label.unwrap(), |
| 42 | + severity: Some(severity), |
| 43 | + related_information: Some(related_info), |
| 44 | + |
| 45 | + ..Default::default() |
| 46 | + } |
| 47 | +} |
0 commit comments