mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
Better naming
This commit is contained in:
parent
a61f2445cb
commit
ee1586c1ed
5 changed files with 58 additions and 49 deletions
|
@ -145,6 +145,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
|
||||||
self.imp.original_range(node)
|
self.imp.original_range(node)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn diagnostics_fix_range(&self, diagnostics: &dyn Diagnostic) -> FileRange {
|
||||||
|
self.imp.diagnostics_fix_range(diagnostics)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn diagnostics_range(&self, diagnostics: &dyn Diagnostic) -> FileRange {
|
pub fn diagnostics_range(&self, diagnostics: &dyn Diagnostic) -> FileRange {
|
||||||
self.imp.diagnostics_range(diagnostics)
|
self.imp.diagnostics_range(diagnostics)
|
||||||
}
|
}
|
||||||
|
@ -376,6 +380,13 @@ impl<'db> SemanticsImpl<'db> {
|
||||||
original_range(self.db, node.as_ref())
|
original_range(self.db, node.as_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn diagnostics_fix_range(&self, diagnostics: &dyn Diagnostic) -> FileRange {
|
||||||
|
let src = diagnostics.fix_source();
|
||||||
|
let root = self.db.parse_or_expand(src.file_id).unwrap();
|
||||||
|
let node = src.value.to_node(&root);
|
||||||
|
original_range(self.db, src.with_value(&node))
|
||||||
|
}
|
||||||
|
|
||||||
fn diagnostics_range(&self, diagnostics: &dyn Diagnostic) -> FileRange {
|
fn diagnostics_range(&self, diagnostics: &dyn Diagnostic) -> FileRange {
|
||||||
let src = diagnostics.source();
|
let src = diagnostics.source();
|
||||||
let root = self.db.parse_or_expand(src.file_id).unwrap();
|
let root = self.db.parse_or_expand(src.file_id).unwrap();
|
||||||
|
|
|
@ -18,7 +18,7 @@ impl Diagnostic for UnresolvedModule {
|
||||||
fn message(&self) -> String {
|
fn message(&self) -> String {
|
||||||
"unresolved module".to_string()
|
"unresolved module".to_string()
|
||||||
}
|
}
|
||||||
fn source(&self) -> InFile<SyntaxNodePtr> {
|
fn fix_source(&self) -> InFile<SyntaxNodePtr> {
|
||||||
InFile::new(self.file, self.decl.clone().into())
|
InFile::new(self.file, self.decl.clone().into())
|
||||||
}
|
}
|
||||||
fn as_any(&self) -> &(dyn Any + Send + 'static) {
|
fn as_any(&self) -> &(dyn Any + Send + 'static) {
|
||||||
|
|
|
@ -22,9 +22,9 @@ 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 fix_source(&self) -> InFile<SyntaxNodePtr>;
|
||||||
fn highlighting_source(&self) -> InFile<SyntaxNodePtr> {
|
fn source(&self) -> InFile<SyntaxNodePtr> {
|
||||||
self.source()
|
self.fix_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 {
|
||||||
|
|
|
@ -37,7 +37,7 @@ impl Diagnostic for NoSuchField {
|
||||||
"no such field".to_string()
|
"no such field".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn source(&self) -> InFile<SyntaxNodePtr> {
|
fn fix_source(&self) -> InFile<SyntaxNodePtr> {
|
||||||
InFile::new(self.file, self.field.clone().into())
|
InFile::new(self.file, self.field.clone().into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,9 +50,8 @@ impl AstDiagnostic for NoSuchField {
|
||||||
type AST = ast::RecordExprField;
|
type AST = ast::RecordExprField;
|
||||||
|
|
||||||
fn ast(&self, db: &dyn AstDatabase) -> Self::AST {
|
fn ast(&self, db: &dyn AstDatabase) -> Self::AST {
|
||||||
let root = db.parse_or_expand(self.source().file_id).unwrap();
|
let root = db.parse_or_expand(self.file).unwrap();
|
||||||
let node = self.source().value.to_node(&root);
|
self.field.to_node(&root)
|
||||||
ast::RecordExprField::cast(node).unwrap()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,20 +71,20 @@ impl Diagnostic for MissingFields {
|
||||||
}
|
}
|
||||||
buf
|
buf
|
||||||
}
|
}
|
||||||
fn source(&self) -> InFile<SyntaxNodePtr> {
|
fn fix_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 source(&self) -> InFile<SyntaxNodePtr> {
|
||||||
|
self.list_parent_path
|
||||||
|
.clone()
|
||||||
|
.map(|path| InFile { file_id: self.file, value: path.into() })
|
||||||
|
.unwrap_or_else(|| self.fix_source())
|
||||||
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
@ -112,7 +111,7 @@ impl Diagnostic for MissingPatFields {
|
||||||
}
|
}
|
||||||
buf
|
buf
|
||||||
}
|
}
|
||||||
fn source(&self) -> InFile<SyntaxNodePtr> {
|
fn fix_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) {
|
||||||
|
@ -131,7 +130,7 @@ impl Diagnostic for MissingMatchArms {
|
||||||
fn message(&self) -> String {
|
fn message(&self) -> String {
|
||||||
String::from("Missing match arm")
|
String::from("Missing match arm")
|
||||||
}
|
}
|
||||||
fn source(&self) -> InFile<SyntaxNodePtr> {
|
fn fix_source(&self) -> InFile<SyntaxNodePtr> {
|
||||||
InFile { file_id: self.file, value: self.match_expr.clone().into() }
|
InFile { file_id: self.file, value: self.match_expr.clone().into() }
|
||||||
}
|
}
|
||||||
fn as_any(&self) -> &(dyn Any + Send + 'static) {
|
fn as_any(&self) -> &(dyn Any + Send + 'static) {
|
||||||
|
@ -149,7 +148,7 @@ impl Diagnostic for MissingOkInTailExpr {
|
||||||
fn message(&self) -> String {
|
fn message(&self) -> String {
|
||||||
"wrap return expression in Ok".to_string()
|
"wrap return expression in Ok".to_string()
|
||||||
}
|
}
|
||||||
fn source(&self) -> InFile<SyntaxNodePtr> {
|
fn fix_source(&self) -> InFile<SyntaxNodePtr> {
|
||||||
InFile { file_id: self.file, value: self.expr.clone().into() }
|
InFile { file_id: self.file, value: self.expr.clone().into() }
|
||||||
}
|
}
|
||||||
fn as_any(&self) -> &(dyn Any + Send + 'static) {
|
fn as_any(&self) -> &(dyn Any + Send + 'static) {
|
||||||
|
@ -162,8 +161,7 @@ impl AstDiagnostic for MissingOkInTailExpr {
|
||||||
|
|
||||||
fn ast(&self, db: &dyn AstDatabase) -> Self::AST {
|
fn ast(&self, db: &dyn AstDatabase) -> Self::AST {
|
||||||
let root = db.parse_or_expand(self.file).unwrap();
|
let root = db.parse_or_expand(self.file).unwrap();
|
||||||
let node = self.source().value.to_node(&root);
|
self.expr.to_node(&root)
|
||||||
ast::Expr::cast(node).unwrap()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,7 +175,7 @@ impl Diagnostic for BreakOutsideOfLoop {
|
||||||
fn message(&self) -> String {
|
fn message(&self) -> String {
|
||||||
"break outside of loop".to_string()
|
"break outside of loop".to_string()
|
||||||
}
|
}
|
||||||
fn source(&self) -> InFile<SyntaxNodePtr> {
|
fn fix_source(&self) -> InFile<SyntaxNodePtr> {
|
||||||
InFile { file_id: self.file, value: self.expr.clone().into() }
|
InFile { file_id: self.file, value: self.expr.clone().into() }
|
||||||
}
|
}
|
||||||
fn as_any(&self) -> &(dyn Any + Send + 'static) {
|
fn as_any(&self) -> &(dyn Any + Send + 'static) {
|
||||||
|
@ -190,8 +188,7 @@ impl AstDiagnostic for BreakOutsideOfLoop {
|
||||||
|
|
||||||
fn ast(&self, db: &dyn AstDatabase) -> Self::AST {
|
fn ast(&self, db: &dyn AstDatabase) -> Self::AST {
|
||||||
let root = db.parse_or_expand(self.file).unwrap();
|
let root = db.parse_or_expand(self.file).unwrap();
|
||||||
let node = self.source().value.to_node(&root);
|
self.expr.to_node(&root)
|
||||||
ast::Expr::cast(node).unwrap()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,7 +202,7 @@ impl Diagnostic for MissingUnsafe {
|
||||||
fn message(&self) -> String {
|
fn message(&self) -> String {
|
||||||
format!("This operation is unsafe and requires an unsafe function or block")
|
format!("This operation is unsafe and requires an unsafe function or block")
|
||||||
}
|
}
|
||||||
fn source(&self) -> InFile<SyntaxNodePtr> {
|
fn fix_source(&self) -> InFile<SyntaxNodePtr> {
|
||||||
InFile { file_id: self.file, value: self.expr.clone().into() }
|
InFile { file_id: self.file, value: self.expr.clone().into() }
|
||||||
}
|
}
|
||||||
fn as_any(&self) -> &(dyn Any + Send + 'static) {
|
fn as_any(&self) -> &(dyn Any + Send + 'static) {
|
||||||
|
@ -217,9 +214,8 @@ impl AstDiagnostic for MissingUnsafe {
|
||||||
type AST = ast::Expr;
|
type AST = ast::Expr;
|
||||||
|
|
||||||
fn ast(&self, db: &dyn AstDatabase) -> Self::AST {
|
fn ast(&self, db: &dyn AstDatabase) -> Self::AST {
|
||||||
let root = db.parse_or_expand(self.source().file_id).unwrap();
|
let root = db.parse_or_expand(self.file).unwrap();
|
||||||
let node = self.source().value.to_node(&root);
|
self.expr.to_node(&root)
|
||||||
ast::Expr::cast(node).unwrap()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -236,7 +232,7 @@ impl Diagnostic for MismatchedArgCount {
|
||||||
let s = if self.expected == 1 { "" } else { "s" };
|
let s = if self.expected == 1 { "" } else { "s" };
|
||||||
format!("Expected {} argument{}, found {}", self.expected, s, self.found)
|
format!("Expected {} argument{}, found {}", self.expected, s, self.found)
|
||||||
}
|
}
|
||||||
fn source(&self) -> InFile<SyntaxNodePtr> {
|
fn fix_source(&self) -> InFile<SyntaxNodePtr> {
|
||||||
InFile { file_id: self.file, value: self.call_expr.clone().into() }
|
InFile { file_id: self.file, value: self.call_expr.clone().into() }
|
||||||
}
|
}
|
||||||
fn as_any(&self) -> &(dyn Any + Send + 'static) {
|
fn as_any(&self) -> &(dyn Any + Send + 'static) {
|
||||||
|
@ -250,7 +246,7 @@ impl Diagnostic for MismatchedArgCount {
|
||||||
impl AstDiagnostic for MismatchedArgCount {
|
impl AstDiagnostic for MismatchedArgCount {
|
||||||
type AST = ast::CallExpr;
|
type AST = ast::CallExpr;
|
||||||
fn ast(&self, db: &dyn AstDatabase) -> Self::AST {
|
fn ast(&self, db: &dyn AstDatabase) -> Self::AST {
|
||||||
let root = db.parse_or_expand(self.source().file_id).unwrap();
|
let root = db.parse_or_expand(self.file).unwrap();
|
||||||
let node = self.source().value.to_node(&root);
|
let node = self.source().value.to_node(&root);
|
||||||
ast::CallExpr::cast(node).unwrap()
|
ast::CallExpr::cast(node).unwrap()
|
||||||
}
|
}
|
||||||
|
@ -308,12 +304,11 @@ mod tests {
|
||||||
let mut actual: FxHashMap<FileId, Vec<(TextRange, String)>> = FxHashMap::default();
|
let mut actual: FxHashMap<FileId, Vec<(TextRange, String)>> = FxHashMap::default();
|
||||||
db.diagnostics(|d| {
|
db.diagnostics(|d| {
|
||||||
// FXIME: macros...
|
// FXIME: macros...
|
||||||
let file_id = d.source().file_id.original_file(&db);
|
let source = d.source();
|
||||||
let highlighting_source = d.highlighting_source();
|
let root = db.parse_or_expand(source.file_id).unwrap();
|
||||||
let node = db.parse_or_expand(highlighting_source.file_id).unwrap();
|
let range = source.value.to_node(&root).text_range();
|
||||||
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(source.file_id.original_file(&db)).or_default().push((range, message));
|
||||||
});
|
});
|
||||||
|
|
||||||
for (file_id, diags) in actual.iter_mut() {
|
for (file_id, diags) in actual.iter_mut() {
|
||||||
|
|
|
@ -54,18 +54,19 @@ pub(crate) fn diagnostics(
|
||||||
let res = RefCell::new(res);
|
let res = RefCell::new(res);
|
||||||
let mut sink = DiagnosticSinkBuilder::new()
|
let mut sink = DiagnosticSinkBuilder::new()
|
||||||
.on::<hir::diagnostics::UnresolvedModule, _>(|d| {
|
.on::<hir::diagnostics::UnresolvedModule, _>(|d| {
|
||||||
let original_file = d.source().file_id.original_file(db);
|
|
||||||
let fix = Fix::new(
|
let fix = Fix::new(
|
||||||
"Create module",
|
"Create module",
|
||||||
FileSystemEdit::CreateFile { anchor: original_file, dst: d.candidate.clone() }
|
FileSystemEdit::CreateFile {
|
||||||
.into(),
|
anchor: d.file.original_file(db),
|
||||||
|
dst: d.candidate.clone(),
|
||||||
|
}
|
||||||
|
.into(),
|
||||||
);
|
);
|
||||||
let range = sema.diagnostics_range(d).range;
|
|
||||||
res.borrow_mut().push(Diagnostic {
|
res.borrow_mut().push(Diagnostic {
|
||||||
range,
|
range: sema.diagnostics_range(d).range,
|
||||||
message: d.message(),
|
message: d.message(),
|
||||||
severity: Severity::Error,
|
severity: Severity::Error,
|
||||||
fix: Some((fix, range)),
|
fix: Some((fix, sema.diagnostics_fix_range(d).range)),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.on::<hir::diagnostics::MissingFields, _>(|d| {
|
.on::<hir::diagnostics::MissingFields, _>(|d| {
|
||||||
|
@ -94,12 +95,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()),
|
||||||
sema.diagnostics_range(d).range,
|
sema.diagnostics_fix_range(d).range,
|
||||||
))
|
))
|
||||||
};
|
};
|
||||||
|
|
||||||
res.borrow_mut().push(Diagnostic {
|
res.borrow_mut().push(Diagnostic {
|
||||||
range: d.highlighting_source().file_syntax(db).text_range(),
|
range: sema.diagnostics_range(d).range,
|
||||||
message: d.message(),
|
message: d.message(),
|
||||||
severity: Severity::Error,
|
severity: Severity::Error,
|
||||||
fix,
|
fix,
|
||||||
|
@ -110,21 +111,23 @@ pub(crate) fn diagnostics(
|
||||||
let replacement = format!("Ok({})", node.syntax());
|
let replacement = format!("Ok({})", node.syntax());
|
||||||
let edit = TextEdit::replace(node.syntax().text_range(), replacement);
|
let edit = TextEdit::replace(node.syntax().text_range(), replacement);
|
||||||
let source_change = SourceFileEdit { file_id, edit }.into();
|
let source_change = SourceFileEdit { file_id, edit }.into();
|
||||||
let range = sema.diagnostics_range(d).range;
|
|
||||||
res.borrow_mut().push(Diagnostic {
|
res.borrow_mut().push(Diagnostic {
|
||||||
range,
|
range: sema.diagnostics_range(d).range,
|
||||||
message: d.message(),
|
message: d.message(),
|
||||||
severity: Severity::Error,
|
severity: Severity::Error,
|
||||||
fix: Some((Fix::new("Wrap with ok", source_change), range)),
|
fix: Some((
|
||||||
|
Fix::new("Wrap with ok", source_change),
|
||||||
|
sema.diagnostics_fix_range(d).range,
|
||||||
|
)),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.on::<hir::diagnostics::NoSuchField, _>(|d| {
|
.on::<hir::diagnostics::NoSuchField, _>(|d| {
|
||||||
let range = sema.diagnostics_range(d).range;
|
|
||||||
res.borrow_mut().push(Diagnostic {
|
res.borrow_mut().push(Diagnostic {
|
||||||
range,
|
range: sema.diagnostics_range(d).range,
|
||||||
message: d.message(),
|
message: d.message(),
|
||||||
severity: Severity::Error,
|
severity: Severity::Error,
|
||||||
fix: missing_struct_field_fix(&sema, file_id, d).map(|fix| (fix, range)),
|
fix: missing_struct_field_fix(&sema, file_id, d)
|
||||||
|
.map(|fix| (fix, sema.diagnostics_fix_range(d).range)),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
// Only collect experimental diagnostics when they're enabled.
|
// Only collect experimental diagnostics when they're enabled.
|
||||||
|
|
Loading…
Reference in a new issue