use correct file for diagnostics

closes #1475
This commit is contained in:
Aleksey Kladov 2019-07-05 19:40:02 +03:00
parent fd30ec406b
commit 0b5e399190
2 changed files with 22 additions and 9 deletions

View file

@ -1,6 +1,6 @@
use std::{any::Any, fmt}; use std::{any::Any, fmt};
use ra_syntax::{ast, AstPtr, SyntaxNode, SyntaxNodePtr, TextRange, TreeArc}; use ra_syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, TextRange, TreeArc};
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use crate::{HirDatabase, HirFileId, Name}; use crate::{HirDatabase, HirFileId, Name};
@ -27,11 +27,17 @@ pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
fn as_any(&self) -> &(dyn Any + Send + 'static); fn as_any(&self) -> &(dyn Any + Send + 'static);
} }
pub trait AstDiagnostic {
type AST;
fn ast(&self, db: &impl HirDatabase) -> Self::AST;
}
impl dyn Diagnostic { impl dyn Diagnostic {
pub fn syntax_node(&self, db: &impl HirDatabase) -> TreeArc<SyntaxNode> { pub fn syntax_node(&self, db: &impl HirDatabase) -> TreeArc<SyntaxNode> {
let node = db.parse_or_expand(self.file()).unwrap(); let node = db.parse_or_expand(self.file()).unwrap();
self.syntax_node_ptr().to_node(&*node).to_owned() self.syntax_node_ptr().to_node(&*node).to_owned()
} }
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()
} }
@ -135,3 +141,13 @@ impl Diagnostic for MissingFields {
self self
} }
} }
impl AstDiagnostic for MissingFields {
type AST = TreeArc<ast::NamedFieldList>;
fn ast(&self, db: &impl HirDatabase) -> Self::AST {
let root = db.parse_or_expand(self.file()).unwrap();
let node = self.syntax_node_ptr().to_node(&*root);
ast::NamedFieldList::cast(&node).unwrap().to_owned()
}
}

View file

@ -1,7 +1,7 @@
use std::cell::RefCell; use std::cell::RefCell;
use hir::{ use hir::{
diagnostics::{Diagnostic as _, DiagnosticSink}, diagnostics::{AstDiagnostic, Diagnostic as _, DiagnosticSink},
source_binder, source_binder,
}; };
use itertools::Itertools; use itertools::Itertools;
@ -9,7 +9,7 @@ use ra_assists::ast_editor::{AstBuilder, AstEditor};
use ra_db::SourceDatabase; use ra_db::SourceDatabase;
use ra_prof::profile; use ra_prof::profile;
use ra_syntax::{ use ra_syntax::{
ast::{self, AstNode, NamedField, NamedFieldList}, ast::{self, AstNode, NamedField},
Location, SyntaxNode, TextRange, T, Location, SyntaxNode, TextRange, T,
}; };
use ra_text_edit::{TextEdit, TextEditBuilder}; use ra_text_edit::{TextEdit, TextEditBuilder};
@ -34,9 +34,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
fix: None, fix: None,
})); }));
let source_file = parse.tree; for node in parse.tree.syntax().descendants() {
for node in source_file.syntax().descendants() {
check_unnecessary_braces_in_use_statement(&mut res, file_id, node); check_unnecessary_braces_in_use_statement(&mut res, file_id, node);
check_struct_shorthand_initialization(&mut res, file_id, node); check_struct_shorthand_initialization(&mut res, file_id, node);
} }
@ -61,9 +59,8 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
}) })
}) })
.on::<hir::diagnostics::MissingFields, _>(|d| { .on::<hir::diagnostics::MissingFields, _>(|d| {
let syntax_node = d.syntax_node_ptr(); let node = d.ast(db);
let node = NamedFieldList::cast(syntax_node.to_node(source_file.syntax())).unwrap(); let mut ast_editor = AstEditor::new(&*node);
let mut ast_editor = AstEditor::new(node);
for f in d.missed_fields.iter() { for f in d.missed_fields.iter() {
ast_editor.append_field(&AstBuilder::<NamedField>::from_name(f)); ast_editor.append_field(&AstBuilder::<NamedField>::from_name(f));
} }