add serverity to vscode diagnostics

This commit is contained in:
gfreezy 2018-12-24 00:39:33 +08:00
parent 000aacafda
commit 346638c809
5 changed files with 32 additions and 12 deletions

View file

@ -3,7 +3,7 @@ use std::{
sync::Arc,
};
use ra_editor::{self, find_node_at_offset, FileSymbol, LineIndex, LocalEdit};
use ra_editor::{self, find_node_at_offset, FileSymbol, LineIndex, LocalEdit, Severity};
use ra_syntax::{
ast::{self, ArgListOwner, Expr, NameOwner},
AstNode, SourceFileNode,
@ -364,6 +364,7 @@ impl AnalysisImpl {
.map(|d| Diagnostic {
range: d.range,
message: d.msg,
severity: d.severity,
fix: None,
})
.collect::<Vec<_>>();
@ -385,6 +386,7 @@ impl AnalysisImpl {
Diagnostic {
range: name_node.range(),
message: "unresolved module".to_string(),
severity: Some(Severity::Error),
fix: Some(fix),
}
}
@ -407,6 +409,7 @@ impl AnalysisImpl {
Diagnostic {
range: name_node.range(),
message: "can't declare module at this location".to_string(),
severity: Some(Severity::Error),
fix: Some(fix),
}
}

View file

@ -34,6 +34,7 @@ pub use crate::{
};
pub use ra_editor::{
FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode,
Severity
};
pub use hir::FnSignatureInfo;
@ -198,6 +199,7 @@ pub struct Diagnostic {
pub message: String,
pub range: TextRange,
pub fix: Option<SourceChange>,
pub severity: Option<Severity>,
}
#[derive(Debug)]

View file

@ -82,7 +82,8 @@ fn test_unresolved_module_diagnostic() {
label: "create module",
source_file_edits: [],
file_system_edits: [CreateFile { source_root: SourceRootId(0), path: "foo.rs" }],
cursor_position: None }) }]"#,
cursor_position: None }),
severity: Some(Error) }]"#,
&diagnostics,
);
}

View file

@ -34,14 +34,16 @@ pub struct HighlightedRange {
#[derive(Debug, Copy, Clone)]
pub enum Severity {
Error,
Warning
Warning,
Information,
Hint,
}
#[derive(Debug)]
pub struct Diagnostic {
pub range: TextRange,
pub msg: String,
pub severity: Severity,
pub severity: Option<Severity>,
}
#[derive(Debug)]
@ -104,12 +106,13 @@ pub fn diagnostics(file: &SourceFileNode) -> Vec<Diagnostic> {
}
}
let mut errors: Vec<Diagnostic> = file.errors()
let mut errors: Vec<Diagnostic> = file
.errors()
.into_iter()
.map(|err| Diagnostic {
range: location_to_range(err.location()),
msg: format!("Syntax Error: {}", err),
severity: Severity::Error,
severity: Some(Severity::Error),
})
.collect();
@ -127,7 +130,7 @@ fn check_unnecessary_braces_in_use_statement(file: &SourceFileNode) -> Vec<Diagn
diagnostics.push(Diagnostic {
range: use_tree_list.syntax().range(),
msg: format!("Unnecessary braces in use statement"),
severity: Severity::Warning,
severity: Some(Severity::Warning),
})
}
}
@ -249,9 +252,9 @@ fn main() {}
);
let diagnostics = check_unnecessary_braces_in_use_statement(&file);
assert_eq_dbg(
r#"[Diagnostic { range: [12; 15), msg: "Unnecessary braces in use statement", severity: Warning },
Diagnostic { range: [24; 27), msg: "Unnecessary braces in use statement", severity: Warning },
Diagnostic { range: [61; 64), msg: "Unnecessary braces in use statement", severity: Warning }]"#,
r#"[Diagnostic { range: [12; 15), msg: "Unnecessary braces in use statement", severity: Some(Warning) },
Diagnostic { range: [24; 27), msg: "Unnecessary braces in use statement", severity: Some(Warning) },
Diagnostic { range: [61; 64), msg: "Unnecessary braces in use statement", severity: Some(Warning) }]"#,
&diagnostics,
)
}

View file

@ -8,7 +8,7 @@ use languageserver_types::{
PrepareRenameResponse, RenameParams, SymbolInformation, TextDocumentIdentifier, TextEdit,
WorkspaceEdit, ParameterInformation, ParameterLabel, SignatureInformation, Hover, HoverContents,
};
use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FilePosition};
use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FilePosition, Severity};
use ra_syntax::{TextUnit, text_utils::intersect};
use ra_text_edit::text_utils::contains_offset_nonstrict;
use rustc_hash::FxHashMap;
@ -650,7 +650,7 @@ pub fn publish_diagnostics(
.into_iter()
.map(|d| Diagnostic {
range: d.range.conv_with(&line_index),
severity: Some(DiagnosticSeverity::Error),
severity: d.severity.map(to_diagnostic_severity),
code: None,
source: Some("rust-analyzer".to_string()),
message: d.message,
@ -684,3 +684,14 @@ fn highlight(world: &ServerWorld, file_id: FileId) -> Result<Vec<Decoration>> {
.collect();
Ok(res)
}
fn to_diagnostic_severity(severity: Severity) -> DiagnosticSeverity {
use ra_analysis::Severity::*;
match severity {
Error => DiagnosticSeverity::Error,
Warning => DiagnosticSeverity::Warning,
Information => DiagnosticSeverity::Information,
Hint => DiagnosticSeverity::Hint,
}
}