From 8328e196dd093f85e51420fa27d9d9bcdf65e866 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 8 Feb 2019 14:24:39 +0300 Subject: move diagnostics to a separate file --- crates/ra_ide_api/src/diagnostics.rs | 71 +++++++++++++++++++++++++++ crates/ra_ide_api/src/imp.rs | 93 ------------------------------------ crates/ra_ide_api/src/lib.rs | 21 +++++++- 3 files changed, 90 insertions(+), 95 deletions(-) create mode 100644 crates/ra_ide_api/src/diagnostics.rs delete mode 100644 crates/ra_ide_api/src/imp.rs (limited to 'crates') diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs new file mode 100644 index 000000000..c6ca91af8 --- /dev/null +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -0,0 +1,71 @@ +use hir::{Problem, source_binder}; +use ra_ide_api_light::Severity; +use ra_db::SourceDatabase; + +use crate::{db, Diagnostic, FileId, FileSystemEdit, SourceChange}; + +impl db::RootDatabase { + pub(crate) fn diagnostics(&self, file_id: FileId) -> Vec { + let syntax = self.parse(file_id); + + let mut res = ra_ide_api_light::diagnostics(&syntax) + .into_iter() + .map(|d| Diagnostic { + range: d.range, + message: d.msg, + severity: d.severity, + fix: d.fix.map(|fix| SourceChange::from_local_edit(file_id, fix)), + }) + .collect::>(); + if let Some(m) = source_binder::module_from_file_id(self, file_id) { + for (name_node, problem) in m.problems(self) { + let source_root = self.file_source_root(file_id); + let diag = match problem { + Problem::UnresolvedModule { candidate } => { + let create_file = FileSystemEdit::CreateFile { + source_root, + path: candidate.clone(), + }; + let fix = SourceChange { + label: "create module".to_string(), + source_file_edits: Vec::new(), + file_system_edits: vec![create_file], + cursor_position: None, + }; + Diagnostic { + range: name_node.range(), + message: "unresolved module".to_string(), + severity: Severity::Error, + fix: Some(fix), + } + } + Problem::NotDirOwner { move_to, candidate } => { + let move_file = FileSystemEdit::MoveFile { + src: file_id, + dst_source_root: source_root, + dst_path: move_to.clone(), + }; + let create_file = FileSystemEdit::CreateFile { + source_root, + path: move_to.join(candidate), + }; + let fix = SourceChange { + label: "move file and create module".to_string(), + source_file_edits: Vec::new(), + file_system_edits: vec![move_file, create_file], + cursor_position: None, + }; + Diagnostic { + range: name_node.range(), + message: "can't declare module at this location".to_string(), + severity: Severity::Error, + fix: Some(fix), + } + } + }; + res.push(diag) + } + }; + res + } +} diff --git a/crates/ra_ide_api/src/imp.rs b/crates/ra_ide_api/src/imp.rs deleted file mode 100644 index a351c9373..000000000 --- a/crates/ra_ide_api/src/imp.rs +++ /dev/null @@ -1,93 +0,0 @@ -use hir::{ - self, Problem, source_binder -}; -use ra_ide_api_light::{self, LocalEdit, Severity}; -use ra_db::SourceDatabase; - -use crate::{ - db, Diagnostic, FileId, FilePosition, FileSystemEdit, - SourceChange, SourceFileEdit, -}; - -impl db::RootDatabase { - pub(crate) fn diagnostics(&self, file_id: FileId) -> Vec { - let syntax = self.parse(file_id); - - let mut res = ra_ide_api_light::diagnostics(&syntax) - .into_iter() - .map(|d| Diagnostic { - range: d.range, - message: d.msg, - severity: d.severity, - fix: d.fix.map(|fix| SourceChange::from_local_edit(file_id, fix)), - }) - .collect::>(); - if let Some(m) = source_binder::module_from_file_id(self, file_id) { - for (name_node, problem) in m.problems(self) { - let source_root = self.file_source_root(file_id); - let diag = match problem { - Problem::UnresolvedModule { candidate } => { - let create_file = FileSystemEdit::CreateFile { - source_root, - path: candidate.clone(), - }; - let fix = SourceChange { - label: "create module".to_string(), - source_file_edits: Vec::new(), - file_system_edits: vec![create_file], - cursor_position: None, - }; - Diagnostic { - range: name_node.range(), - message: "unresolved module".to_string(), - severity: Severity::Error, - fix: Some(fix), - } - } - Problem::NotDirOwner { move_to, candidate } => { - let move_file = FileSystemEdit::MoveFile { - src: file_id, - dst_source_root: source_root, - dst_path: move_to.clone(), - }; - let create_file = FileSystemEdit::CreateFile { - source_root, - path: move_to.join(candidate), - }; - let fix = SourceChange { - label: "move file and create module".to_string(), - source_file_edits: Vec::new(), - file_system_edits: vec![move_file, create_file], - cursor_position: None, - }; - Diagnostic { - range: name_node.range(), - message: "can't declare module at this location".to_string(), - severity: Severity::Error, - fix: Some(fix), - } - } - }; - res.push(diag) - } - }; - res - } -} - -impl SourceChange { - pub(crate) fn from_local_edit(file_id: FileId, edit: LocalEdit) -> SourceChange { - let file_edit = SourceFileEdit { - file_id, - edit: edit.edit, - }; - SourceChange { - label: edit.label, - source_file_edits: vec![file_edit], - file_system_edits: vec![], - cursor_position: edit - .cursor_position - .map(|offset| FilePosition { offset, file_id }), - } - } -} diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index f5c1aa036..312b11a82 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -14,7 +14,6 @@ #![recursion_limit = "128"] mod db; -mod imp; pub mod mock_analysis; mod symbol_index; mod navigation_target; @@ -32,6 +31,7 @@ mod parent_module; mod references; mod impls; mod assists; +mod diagnostics; #[cfg(test)] mod marks; @@ -58,7 +58,7 @@ pub use crate::{ change::{AnalysisChange, LibraryData}, }; pub use ra_ide_api_light::{ - Fold, FoldKind, HighlightedRange, Severity, StructureNode, + Fold, FoldKind, HighlightedRange, Severity, StructureNode, LocalEdit, LineIndex, LineCol, translate_offset_with_edit, }; pub use ra_db::{ @@ -399,6 +399,23 @@ impl Analysis { } } +impl SourceChange { + pub(crate) fn from_local_edit(file_id: FileId, edit: LocalEdit) -> SourceChange { + let file_edit = SourceFileEdit { + file_id, + edit: edit.edit, + }; + SourceChange { + label: edit.label, + source_file_edits: vec![file_edit], + file_system_edits: vec![], + cursor_position: edit + .cursor_position + .map(|offset| FilePosition { offset, file_id }), + } + } +} + #[test] fn analysis_is_send() { fn is_send() {} -- cgit v1.2.3