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

View file

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

View file

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

View file

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

View file

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