From a61f2445cba2a48bb7ea6c8477e3198b297f3c67 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 27 Jul 2020 22:30:55 +0300 Subject: Less stubs --- crates/ra_assists/src/handlers/fix_visibility.rs | 2 +- crates/ra_hir_expand/src/diagnostics.rs | 11 +++---- crates/ra_hir_ty/src/diagnostics.rs | 38 +++++++++++++----------- crates/ra_ide/src/diagnostics.rs | 8 ++--- 4 files changed, 28 insertions(+), 31 deletions(-) (limited to 'crates') diff --git a/crates/ra_assists/src/handlers/fix_visibility.rs b/crates/ra_assists/src/handlers/fix_visibility.rs index 1aefa79cc..a19dbf33f 100644 --- a/crates/ra_assists/src/handlers/fix_visibility.rs +++ b/crates/ra_assists/src/handlers/fix_visibility.rs @@ -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)), diff --git a/crates/ra_hir_expand/src/diagnostics.rs b/crates/ra_hir_expand/src/diagnostics.rs index e889f070f..ffeca5e82 100644 --- a/crates/ra_hir_expand/src/diagnostics.rs +++ b/crates/ra_hir_expand/src/diagnostics.rs @@ -16,13 +16,16 @@ 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; + fn highlighting_source(&self) -> InFile { + 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(&self) -> Option<&D> { self.as_any().downcast_ref() } diff --git a/crates/ra_hir_ty/src/diagnostics.rs b/crates/ra_hir_ty/src/diagnostics.rs index a5b00ed48..73d241434 100644 --- a/crates/ra_hir_ty/src/diagnostics.rs +++ b/crates/ra_hir_ty/src/diagnostics.rs @@ -9,7 +9,7 @@ use hir_def::DefWithBodyId; 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>, } -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 { - 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 @@ impl Diagnostic for MissingFields { fn source(&self) -> InFile { 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 { + 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 @@ impl AstDiagnostic for MismatchedArgCount { #[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 @@ mod tests { 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 diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index 7ae4bda0b..e847df6ea 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs @@ -69,7 +69,6 @@ pub(crate) fn diagnostics( }) }) .on::(|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, -- cgit v1.2.3