Less stubs

This commit is contained in:
Kirill Bulatov 2020-07-27 22:30:55 +03:00
parent 21e5224484
commit a61f2445cb
4 changed files with 28 additions and 31 deletions

View file

@ -121,7 +121,7 @@ fn add_vis_to_referenced_record_field(acc: &mut Assists, ctx: &AssistContext) ->
Some(cap) => match current_visibility { Some(cap) => match current_visibility {
Some(current_visibility) => builder.replace_snippet( Some(current_visibility) => builder.replace_snippet(
cap, cap,
dbg!(current_visibility.syntax()).text_range(), current_visibility.syntax().text_range(),
format!("$0{}", missing_visibility), format!("$0{}", missing_visibility),
), ),
None => builder.insert_snippet(cap, offset, format!("$0{} ", missing_visibility)), None => builder.insert_snippet(cap, offset, format!("$0{} ", missing_visibility)),

View file

@ -16,13 +16,16 @@
use std::{any::Any, fmt}; use std::{any::Any, fmt};
use ra_syntax::{SyntaxNode, SyntaxNodePtr}; use ra_syntax::SyntaxNodePtr;
use crate::{db::AstDatabase, InFile}; use crate::{db::AstDatabase, InFile};
pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static { pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
fn message(&self) -> String; fn message(&self) -> String;
fn source(&self) -> InFile<SyntaxNodePtr>; fn source(&self) -> InFile<SyntaxNodePtr>;
fn highlighting_source(&self) -> InFile<SyntaxNodePtr> {
self.source()
}
fn as_any(&self) -> &(dyn Any + Send + 'static); fn as_any(&self) -> &(dyn Any + Send + 'static);
fn is_experimental(&self) -> bool { fn is_experimental(&self) -> bool {
false false
@ -35,12 +38,6 @@ pub trait AstDiagnostic {
} }
impl dyn Diagnostic { 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> { pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> {
self.as_any().downcast_ref() self.as_any().downcast_ref()
} }

View file

@ -9,7 +9,7 @@ use hir_def::DefWithBodyId;
use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink}; use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink};
use hir_expand::{db::AstDatabase, name::Name, HirFileId, InFile}; use hir_expand::{db::AstDatabase, name::Name, HirFileId, InFile};
use ra_prof::profile; use ra_prof::profile;
use ra_syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr}; use ra_syntax::{ast, AstNode, AstPtr, SyntaxNodePtr};
use stdx::format_to; use stdx::format_to;
use crate::db::HirDatabase; use crate::db::HirDatabase;
@ -64,16 +64,6 @@ pub struct MissingFields {
pub list_parent_path: Option<AstPtr<ast::Path>>, 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 { impl Diagnostic for MissingFields {
fn message(&self) -> String { fn message(&self) -> String {
let mut buf = String::from("Missing structure fields:\n"); let mut buf = String::from("Missing structure fields:\n");
@ -85,16 +75,25 @@ impl Diagnostic for MissingFields {
fn source(&self) -> InFile<SyntaxNodePtr> { fn source(&self) -> InFile<SyntaxNodePtr> {
InFile { file_id: self.file, value: self.field_list.clone().into() } InFile { file_id: self.file, value: self.field_list.clone().into() }
} }
fn as_any(&self) -> &(dyn Any + Send + 'static) { fn as_any(&self) -> &(dyn Any + Send + 'static) {
self 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 { impl AstDiagnostic for MissingFields {
type AST = ast::RecordExprFieldList; type AST = ast::RecordExprFieldList;
fn ast(&self, db: &dyn AstDatabase) -> Self::AST { 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)] #[cfg(test)]
mod tests { mod tests {
use hir_def::{db::DefDatabase, AssocItemId, ModuleDefId}; 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_db::{fixture::WithFixture, FileId, SourceDatabase, SourceDatabaseExt};
use ra_syntax::{TextRange, TextSize}; use ra_syntax::{TextRange, TextSize};
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
@ -307,7 +309,9 @@ mod tests {
db.diagnostics(|d| { db.diagnostics(|d| {
// FXIME: macros... // FXIME: macros...
let file_id = d.source().file_id.original_file(&db); 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(); let message = d.message().to_owned();
actual.entry(file_id).or_default().push((range, message)); actual.entry(file_id).or_default().push((range, message));
}); });
@ -345,7 +349,7 @@ struct Beefy {
} }
fn baz() { fn baz() {
let zz = Beefy { let zz = Beefy {
//^^^^^... Missing structure fields: //^^^^^ Missing structure fields:
// | - seven // | - seven
one: (), one: (),
two: (), two: (),
@ -370,8 +374,8 @@ struct S { foo: i32, bar: () }
impl S { impl S {
fn new() -> S { fn new() -> S {
S { S {
//^... Missing structure fields: //^ Missing structure fields:
//| - bar //| - bar
foo: 92, foo: 92,
baz: 62, baz: 62,
//^^^^^^^ no such field //^^^^^^^ no such field

View file

@ -69,7 +69,6 @@ pub(crate) fn diagnostics(
}) })
}) })
.on::<hir::diagnostics::MissingFields, _>(|d| { .on::<hir::diagnostics::MissingFields, _>(|d| {
let range = sema.diagnostics_range(d).range;
// Note that although we could add a diagnostics to // Note that although we could add a diagnostics to
// fill the missing tuple field, e.g : // fill the missing tuple field, e.g :
// `struct A(usize);` // `struct A(usize);`
@ -95,15 +94,12 @@ pub(crate) fn diagnostics(
}; };
Some(( Some((
Fix::new("Fill struct fields", SourceFileEdit { file_id, edit }.into()), Fix::new("Fill struct fields", SourceFileEdit { file_id, edit }.into()),
range, sema.diagnostics_range(d).range,
)) ))
}; };
res.borrow_mut().push(Diagnostic { res.borrow_mut().push(Diagnostic {
range: d range: d.highlighting_source().file_syntax(db).text_range(),
.list_parent_ast(db)
.map(|path| path.syntax().text_range())
.unwrap_or(range),
message: d.message(), message: d.message(),
severity: Severity::Error, severity: Severity::Error,
fix, fix,