Better naming and docs

This commit is contained in:
Kirill Bulatov 2020-08-11 17:15:11 +03:00
parent 188ec3459e
commit db12ccee96
6 changed files with 30 additions and 28 deletions

View file

@ -138,8 +138,8 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
self.imp.original_range(node) self.imp.original_range(node)
} }
pub fn diagnostics_presentation_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { pub fn diagnostics_display_range(&self, diagnostics: &dyn Diagnostic) -> FileRange {
self.imp.diagnostics_presentation_range(diagnostics) self.imp.diagnostics_display_range(diagnostics)
} }
pub fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator<Item = SyntaxNode> + '_ { pub fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator<Item = SyntaxNode> + '_ {
@ -369,8 +369,8 @@ impl<'db> SemanticsImpl<'db> {
original_range(self.db, node.as_ref()) original_range(self.db, node.as_ref())
} }
fn diagnostics_presentation_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { fn diagnostics_display_range(&self, diagnostics: &dyn Diagnostic) -> FileRange {
let src = diagnostics.presentation(); let src = diagnostics.display_source();
let root = self.db.parse_or_expand(src.file_id).unwrap(); let root = self.db.parse_or_expand(src.file_id).unwrap();
let node = src.value.to_node(&root); let node = src.value.to_node(&root);
self.cache(root, src.file_id); self.cache(root, src.file_id);

View file

@ -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 presentation(&self) -> InFile<SyntaxNodePtr> { fn display_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) {

View file

@ -22,8 +22,8 @@ use crate::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;
/// A presentation source of the diagnostics, to use in highlighting and similar actions /// Used in highlighting and related purposes
fn presentation(&self) -> InFile<SyntaxNodePtr>; fn display_source(&self) -> InFile<SyntaxNodePtr>;
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

View file

@ -37,7 +37,7 @@ impl Diagnostic for NoSuchField {
"no such field".to_string() "no such field".to_string()
} }
fn presentation(&self) -> InFile<SyntaxNodePtr> { fn display_source(&self) -> InFile<SyntaxNodePtr> {
InFile::new(self.file, self.field.clone().into()) InFile::new(self.file, self.field.clone().into())
} }
@ -63,7 +63,7 @@ impl Diagnostic for MissingFields {
buf buf
} }
fn presentation(&self) -> InFile<SyntaxNodePtr> { fn display_source(&self) -> InFile<SyntaxNodePtr> {
InFile { InFile {
file_id: self.file, file_id: self.file,
value: self value: self
@ -95,13 +95,15 @@ impl Diagnostic for MissingPatFields {
} }
buf buf
} }
fn presentation(&self) -> InFile<SyntaxNodePtr> { fn display_source(&self) -> InFile<SyntaxNodePtr> {
let value = self InFile {
.field_list_parent_path file_id: self.file,
.clone() value: self
.map(SyntaxNodePtr::from) .field_list_parent_path
.unwrap_or_else(|| self.field_list_parent.clone().into()); .clone()
InFile { file_id: self.file, value } .map(SyntaxNodePtr::from)
.unwrap_or_else(|| self.field_list_parent.clone().into()),
}
} }
fn as_any(&self) -> &(dyn Any + Send + 'static) { fn as_any(&self) -> &(dyn Any + Send + 'static) {
self self
@ -119,7 +121,7 @@ impl Diagnostic for MissingMatchArms {
fn message(&self) -> String { fn message(&self) -> String {
String::from("Missing match arm") String::from("Missing match arm")
} }
fn presentation(&self) -> InFile<SyntaxNodePtr> { fn display_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) {
@ -137,7 +139,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 presentation(&self) -> InFile<SyntaxNodePtr> { fn display_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) {
@ -155,7 +157,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 presentation(&self) -> InFile<SyntaxNodePtr> { fn display_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) {
@ -173,7 +175,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 presentation(&self) -> InFile<SyntaxNodePtr> { fn display_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) {
@ -194,7 +196,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 presentation(&self) -> InFile<SyntaxNodePtr> { fn display_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) {
@ -256,12 +258,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| {
let src = d.display_source();
let root = db.parse_or_expand(src.file_id).unwrap();
// FIXME: macros... // FIXME: macros...
let diagnostics_presentation = d.presentation(); let file_id = src.file_id.original_file(&db);
let root = db.parse_or_expand(diagnostics_presentation.file_id).unwrap(); let range = src.value.to_node(&root).text_range();
let file_id = diagnostics_presentation.file_id.original_file(&db);
let range = diagnostics_presentation.value.to_node(&root).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));
}); });

View file

@ -183,7 +183,7 @@ mod tests {
/// Takes a multi-file input fixture with annotated cursor positions, /// Takes a multi-file input fixture with annotated cursor positions,
/// and checks that: /// and checks that:
/// * a diagnostic is produced /// * a diagnostic is produced
/// * this diagnostic fix touches the input cursor position /// * this diagnostic fix trigger range touches the input cursor position
/// * that the contents of the file containing the cursor match `after` after the diagnostic fix is applied /// * that the contents of the file containing the cursor match `after` after the diagnostic fix is applied
fn check_fix(ra_fixture_before: &str, ra_fixture_after: &str) { fn check_fix(ra_fixture_before: &str, ra_fixture_after: &str) {
let after = trim_indent(ra_fixture_after); let after = trim_indent(ra_fixture_after);

View file

@ -1,4 +1,5 @@
//! Provides a way to attach fix actions to the //! Provides a way to attach fixes to the diagnostics.
//! The same module also has all curret custom fixes for the diagnostics implemented.
use crate::Fix; use crate::Fix;
use ast::{edit::IndentLevel, make}; use ast::{edit::IndentLevel, make};
use hir::{ use hir::{