Skip to content

Commit 212fa29

Browse files
Merge #5197
5197: SSR internal refactorings r=davidlattimore a=davidlattimore - Extract error code out to a separate module - Improve error reporting when a test fails - Refactor matching code - Update tests so that all paths in search patterns can be resolved Co-authored-by: David Lattimore <[email protected]>
2 parents fe16e1d + a354e5b commit 212fa29

File tree

5 files changed

+243
-191
lines changed

5 files changed

+243
-191
lines changed

crates/ra_ssr/src/errors.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//! Code relating to errors produced by SSR.
2+
3+
/// Constructs an SsrError taking arguments like the format macro.
4+
macro_rules! _error {
5+
($fmt:expr) => {$crate::SsrError::new(format!($fmt))};
6+
($fmt:expr, $($arg:tt)+) => {$crate::SsrError::new(format!($fmt, $($arg)+))}
7+
}
8+
pub(crate) use _error as error;
9+
10+
/// Returns from the current function with an error, supplied by arguments as for format!
11+
macro_rules! _bail {
12+
($($tokens:tt)*) => {return Err(crate::errors::error!($($tokens)*))}
13+
}
14+
pub(crate) use _bail as bail;
15+
16+
#[derive(Debug, PartialEq)]
17+
pub struct SsrError(pub(crate) String);
18+
19+
impl std::fmt::Display for SsrError {
20+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
21+
write!(f, "Parse error: {}", self.0)
22+
}
23+
}
24+
25+
impl SsrError {
26+
pub(crate) fn new(message: impl Into<String>) -> SsrError {
27+
SsrError(message.into())
28+
}
29+
}

crates/ra_ssr/src/lib.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
mod matching;
77
mod parsing;
88
mod replacing;
9+
#[macro_use]
10+
mod errors;
911
#[cfg(test)]
1012
mod tests;
1113

14+
pub use crate::errors::SsrError;
1215
pub use crate::matching::Match;
1316
use crate::matching::{record_match_fails_reasons_scope, MatchFailureReason};
1417
use hir::Semantics;
@@ -41,9 +44,6 @@ pub struct SsrPattern {
4144
pattern: Option<SyntaxNode>,
4245
}
4346

44-
#[derive(Debug, PartialEq)]
45-
pub struct SsrError(String);
46-
4747
#[derive(Debug, Default)]
4848
pub struct SsrMatches {
4949
pub matches: Vec<Match>,
@@ -201,9 +201,8 @@ impl<'db> MatchFinder<'db> {
201201
);
202202
}
203203
}
204-
} else {
205-
self.output_debug_for_nodes_at_range(&node, range, restrict_range, out);
206204
}
205+
self.output_debug_for_nodes_at_range(&node, range, restrict_range, out);
207206
}
208207
}
209208
}
@@ -216,33 +215,28 @@ pub struct MatchDebugInfo {
216215
matched: Result<Match, MatchFailureReason>,
217216
}
218217

219-
impl std::fmt::Display for SsrError {
220-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
221-
write!(f, "Parse error: {}", self.0)
222-
}
223-
}
224-
225218
impl std::fmt::Debug for MatchDebugInfo {
226219
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
227-
write!(f, "========= PATTERN ==========\n")?;
220+
match &self.matched {
221+
Ok(_) => writeln!(f, "Node matched")?,
222+
Err(reason) => writeln!(f, "Node failed to match because: {}", reason.reason)?,
223+
}
224+
writeln!(
225+
f,
226+
"============ AST ===========\n\
227+
{:#?}",
228+
self.node
229+
)?;
230+
writeln!(f, "========= PATTERN ==========")?;
228231
match &self.pattern {
229232
Ok(pattern) => {
230-
write!(f, "{:#?}", pattern)?;
233+
writeln!(f, "{:#?}", pattern)?;
231234
}
232235
Err(err) => {
233-
write!(f, "{}", err.reason)?;
236+
writeln!(f, "{}", err.reason)?;
234237
}
235238
}
236-
write!(
237-
f,
238-
"\n============ AST ===========\n\
239-
{:#?}\n============================\n",
240-
self.node
241-
)?;
242-
match &self.matched {
243-
Ok(_) => write!(f, "Node matched")?,
244-
Err(reason) => write!(f, "Node failed to match because: {}", reason.reason)?,
245-
}
239+
writeln!(f, "============================")?;
246240
Ok(())
247241
}
248242
}

0 commit comments

Comments
 (0)