]> git.lizzy.rs Git - rust.git/commitdiff
Less stubs
authorKirill Bulatov <mail4score@gmail.com>
Mon, 27 Jul 2020 19:30:55 +0000 (22:30 +0300)
committerKirill Bulatov <mail4score@gmail.com>
Tue, 11 Aug 2020 12:09:08 +0000 (15:09 +0300)
crates/ra_assists/src/handlers/fix_visibility.rs
crates/ra_hir_expand/src/diagnostics.rs
crates/ra_hir_ty/src/diagnostics.rs
crates/ra_ide/src/diagnostics.rs

index 1aefa79cc3052b9f4a617b1717ff125769b5bdf9..a19dbf33f6bc85cfc129fd8f4f5789c2f621f9bf 100644 (file)
@@ -121,7 +121,7 @@ fn add_vis_to_referenced_record_field(acc: &mut Assists, ctx: &AssistContext) ->
             Some(cap) => match current_visibility {
                 Some(current_visibility) => builder.replace_snippet(
                     cap,
-                    dbg!(current_visibility.syntax()).text_range(),
+                    current_visibility.syntax().text_range(),
                     format!("$0{}", missing_visibility),
                 ),
                 None => builder.insert_snippet(cap, offset, format!("$0{} ", missing_visibility)),
index e889f070fad702365733484611a918f577df025e..ffeca5e82726f771c9d62ecda0b551a016e87fc0 100644 (file)
 
 use std::{any::Any, fmt};
 
-use ra_syntax::{SyntaxNode, SyntaxNodePtr};
+use ra_syntax::SyntaxNodePtr;
 
 use crate::{db::AstDatabase, InFile};
 
 pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
     fn message(&self) -> String;
     fn source(&self) -> InFile<SyntaxNodePtr>;
+    fn highlighting_source(&self) -> InFile<SyntaxNodePtr> {
+        self.source()
+    }
     fn as_any(&self) -> &(dyn Any + Send + 'static);
     fn is_experimental(&self) -> bool {
         false
@@ -35,12 +38,6 @@ pub trait AstDiagnostic {
 }
 
 impl dyn Diagnostic {
-    pub fn syntax_node(&self, db: &impl AstDatabase) -> SyntaxNode {
-        let source = self.source();
-        let node = db.parse_or_expand(source.file_id).unwrap();
-        source.value.to_node(&node)
-    }
-
     pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> {
         self.as_any().downcast_ref()
     }
index a5b00ed4859c1573c804bc749fdef8e183580278..73d2414343d311df349125700e1316ec24908ab3 100644 (file)
@@ -9,7 +9,7 @@
 use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink};
 use hir_expand::{db::AstDatabase, name::Name, HirFileId, InFile};
 use ra_prof::profile;
-use ra_syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr};
+use ra_syntax::{ast, AstNode, AstPtr, SyntaxNodePtr};
 use stdx::format_to;
 
 use crate::db::HirDatabase;
@@ -64,16 +64,6 @@ pub struct MissingFields {
     pub list_parent_path: Option<AstPtr<ast::Path>>,
 }
 
-impl MissingFields {
-    fn root(&self, db: &dyn AstDatabase) -> SyntaxNode {
-        db.parse_or_expand(self.file).unwrap()
-    }
-
-    pub fn list_parent_ast(&self, db: &dyn AstDatabase) -> Option<ast::Path> {
-        self.list_parent_path.as_ref().map(|path| path.to_node(&self.root(db)))
-    }
-}
-
 impl Diagnostic for MissingFields {
     fn message(&self) -> String {
         let mut buf = String::from("Missing structure fields:\n");
@@ -85,16 +75,25 @@ fn message(&self) -> String {
     fn source(&self) -> InFile<SyntaxNodePtr> {
         InFile { file_id: self.file, value: self.field_list.clone().into() }
     }
+
     fn as_any(&self) -> &(dyn Any + Send + 'static) {
         self
     }
+
+    fn highlighting_source(&self) -> InFile<SyntaxNodePtr> {
+        self.list_parent_path
+            .clone()
+            .map(|path| InFile { file_id: self.file, value: path.into() })
+            .unwrap_or_else(|| self.source())
+    }
 }
 
 impl AstDiagnostic for MissingFields {
     type AST = ast::RecordExprFieldList;
 
     fn ast(&self, db: &dyn AstDatabase) -> Self::AST {
-        self.field_list.to_node(&self.root(db))
+        let root = db.parse_or_expand(self.file).unwrap();
+        self.field_list.to_node(&root)
     }
 }
 
@@ -260,7 +259,10 @@ fn ast(&self, db: &dyn AstDatabase) -> Self::AST {
 #[cfg(test)]
 mod tests {
     use hir_def::{db::DefDatabase, AssocItemId, ModuleDefId};
-    use hir_expand::diagnostics::{Diagnostic, DiagnosticSinkBuilder};
+    use hir_expand::{
+        db::AstDatabase,
+        diagnostics::{Diagnostic, DiagnosticSinkBuilder},
+    };
     use ra_db::{fixture::WithFixture, FileId, SourceDatabase, SourceDatabaseExt};
     use ra_syntax::{TextRange, TextSize};
     use rustc_hash::FxHashMap;
@@ -307,7 +309,9 @@ pub(crate) fn check_diagnostics(ra_fixture: &str) {
         db.diagnostics(|d| {
             // FXIME: macros...
             let file_id = d.source().file_id.original_file(&db);
-            let range = d.syntax_node(&db).text_range();
+            let highlighting_source = d.highlighting_source();
+            let node = db.parse_or_expand(highlighting_source.file_id).unwrap();
+            let range = highlighting_source.value.to_node(&node).text_range();
             let message = d.message().to_owned();
             actual.entry(file_id).or_default().push((range, message));
         });
@@ -345,7 +349,7 @@ struct Beefy {
 }
 fn baz() {
     let zz = Beefy {
-           //^^^^^... Missing structure fields:
+           //^^^^^ Missing structure fields:
            //    |    - seven
         one: (),
         two: (),
@@ -370,8 +374,8 @@ struct S { foo: i32, bar: () }
 impl S {
     fn new() -> S {
         S {
-        //^... Missing structure fields:
-        //|    - bar
+      //^ Missing structure fields:
+      //|    - bar
             foo: 92,
             baz: 62,
           //^^^^^^^ no such field
index 7ae4bda0b8e99914fa38137651166a98d068818d..e847df6ea9cb4ac94b250d0979370af91008faa3 100644 (file)
@@ -69,7 +69,6 @@ pub(crate) fn diagnostics(
             })
         })
         .on::<hir::diagnostics::MissingFields, _>(|d| {
-            let range = sema.diagnostics_range(d).range;
             // Note that although we could add a diagnostics to
             // fill the missing tuple field, e.g :
             // `struct A(usize);`
@@ -95,15 +94,12 @@ pub(crate) fn diagnostics(
                 };
                 Some((
                     Fix::new("Fill struct fields", SourceFileEdit { file_id, edit }.into()),
-                    range,
+                    sema.diagnostics_range(d).range,
                 ))
             };
 
             res.borrow_mut().push(Diagnostic {
-                range: d
-                    .list_parent_ast(db)
-                    .map(|path| path.syntax().text_range())
-                    .unwrap_or(range),
+                range: d.highlighting_source().file_syntax(db).text_range(),
                 message: d.message(),
                 severity: Severity::Error,
                 fix,