Skip to content

Commit 0cf48e4

Browse files
bors[bot]eupn
andcommitted
Merge #1683
1683: Use Source in Diagnostic r=matklad a=eupn Closes #1681. Co-authored-by: Evgenii P <[email protected]>
2 parents d5e8fa6 + 475a930 commit 0cf48e4

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

crates/ra_hir/src/diagnostics.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{any::Any, fmt};
33
use ra_syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, TextRange};
44
use relative_path::RelativePathBuf;
55

6-
use crate::{HirDatabase, HirFileId, Name};
6+
use crate::{HirDatabase, HirFileId, Name, Source};
77

88
/// Diagnostic defines hir API for errors and warnings.
99
///
@@ -19,10 +19,9 @@ use crate::{HirDatabase, HirFileId, Name};
1919
/// instance of `Diagnostic` on demand.
2020
pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
2121
fn message(&self) -> String;
22-
fn file(&self) -> HirFileId;
23-
fn syntax_node_ptr(&self) -> SyntaxNodePtr;
22+
fn source(&self) -> Source<SyntaxNodePtr>;
2423
fn highlight_range(&self) -> TextRange {
25-
self.syntax_node_ptr().range()
24+
self.source().ast.range()
2625
}
2726
fn as_any(&self) -> &(dyn Any + Send + 'static);
2827
}
@@ -34,8 +33,8 @@ pub trait AstDiagnostic {
3433

3534
impl dyn Diagnostic {
3635
pub fn syntax_node(&self, db: &impl HirDatabase) -> SyntaxNode {
37-
let node = db.parse_or_expand(self.file()).unwrap();
38-
self.syntax_node_ptr().to_node(&node)
36+
let node = db.parse_or_expand(self.source().file_id).unwrap();
37+
self.source().ast.to_node(&node)
3938
}
4039

4140
pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> {
@@ -87,12 +86,11 @@ impl Diagnostic for NoSuchField {
8786
fn message(&self) -> String {
8887
"no such field".to_string()
8988
}
90-
fn file(&self) -> HirFileId {
91-
self.file
92-
}
93-
fn syntax_node_ptr(&self) -> SyntaxNodePtr {
94-
self.field.into()
89+
90+
fn source(&self) -> Source<SyntaxNodePtr> {
91+
Source { file_id: self.file, ast: self.field.into() }
9592
}
93+
9694
fn as_any(&self) -> &(dyn Any + Send + 'static) {
9795
self
9896
}
@@ -109,11 +107,8 @@ impl Diagnostic for UnresolvedModule {
109107
fn message(&self) -> String {
110108
"unresolved module".to_string()
111109
}
112-
fn file(&self) -> HirFileId {
113-
self.file
114-
}
115-
fn syntax_node_ptr(&self) -> SyntaxNodePtr {
116-
self.decl.into()
110+
fn source(&self) -> Source<SyntaxNodePtr> {
111+
Source { file_id: self.file, ast: self.decl.into() }
117112
}
118113
fn as_any(&self) -> &(dyn Any + Send + 'static) {
119114
self
@@ -131,11 +126,8 @@ impl Diagnostic for MissingFields {
131126
fn message(&self) -> String {
132127
"fill structure fields".to_string()
133128
}
134-
fn file(&self) -> HirFileId {
135-
self.file
136-
}
137-
fn syntax_node_ptr(&self) -> SyntaxNodePtr {
138-
self.field_list.into()
129+
fn source(&self) -> Source<SyntaxNodePtr> {
130+
Source { file_id: self.file, ast: self.field_list.into() }
139131
}
140132
fn as_any(&self) -> &(dyn Any + Send + 'static) {
141133
self
@@ -146,8 +138,8 @@ impl AstDiagnostic for MissingFields {
146138
type AST = ast::NamedFieldList;
147139

148140
fn ast(&self, db: &impl HirDatabase) -> Self::AST {
149-
let root = db.parse_or_expand(self.file()).unwrap();
150-
let node = self.syntax_node_ptr().to_node(&root);
141+
let root = db.parse_or_expand(self.source().file_id).unwrap();
142+
let node = self.source().ast.to_node(&root);
151143
ast::NamedFieldList::cast(node).unwrap()
152144
}
153145
}

crates/ra_ide_api/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
4848
})
4949
})
5050
.on::<hir::diagnostics::UnresolvedModule, _>(|d| {
51-
let source_root = db.file_source_root(d.file().original_file(db));
51+
let source_root = db.file_source_root(d.source().file_id.original_file(db));
5252
let create_file = FileSystemEdit::CreateFile { source_root, path: d.candidate.clone() };
5353
let fix = SourceChange::file_system_edit("create module", create_file);
5454
res.borrow_mut().push(Diagnostic {

0 commit comments

Comments
 (0)