Skip to content

SSR internal refactorings #5197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions crates/ra_ssr/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! Code relating to errors produced by SSR.

/// Constructs an SsrError taking arguments like the format macro.
macro_rules! _error {
($fmt:expr) => {$crate::SsrError::new(format!($fmt))};
($fmt:expr, $($arg:tt)+) => {$crate::SsrError::new(format!($fmt, $($arg)+))}
}
pub(crate) use _error as error;

/// Returns from the current function with an error, supplied by arguments as for format!
macro_rules! _bail {
($($tokens:tt)*) => {return Err(crate::errors::error!($($tokens)*))}
}
pub(crate) use _bail as bail;

#[derive(Debug, PartialEq)]
pub struct SsrError(pub(crate) String);

impl std::fmt::Display for SsrError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Parse error: {}", self.0)
}
}

impl SsrError {
pub(crate) fn new(message: impl Into<String>) -> SsrError {
SsrError(message.into())
}
}
42 changes: 18 additions & 24 deletions crates/ra_ssr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
mod matching;
mod parsing;
mod replacing;
#[macro_use]
mod errors;
#[cfg(test)]
mod tests;

pub use crate::errors::SsrError;
pub use crate::matching::Match;
use crate::matching::{record_match_fails_reasons_scope, MatchFailureReason};
use hir::Semantics;
Expand Down Expand Up @@ -41,9 +44,6 @@ pub struct SsrPattern {
pattern: Option<SyntaxNode>,
}

#[derive(Debug, PartialEq)]
pub struct SsrError(String);

#[derive(Debug, Default)]
pub struct SsrMatches {
pub matches: Vec<Match>,
Expand Down Expand Up @@ -201,9 +201,8 @@ impl<'db> MatchFinder<'db> {
);
}
}
} else {
self.output_debug_for_nodes_at_range(&node, range, restrict_range, out);
}
self.output_debug_for_nodes_at_range(&node, range, restrict_range, out);
}
}
}
Expand All @@ -216,33 +215,28 @@ pub struct MatchDebugInfo {
matched: Result<Match, MatchFailureReason>,
}

impl std::fmt::Display for SsrError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Parse error: {}", self.0)
}
}

impl std::fmt::Debug for MatchDebugInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "========= PATTERN ==========\n")?;
match &self.matched {
Ok(_) => writeln!(f, "Node matched")?,
Err(reason) => writeln!(f, "Node failed to match because: {}", reason.reason)?,
}
writeln!(
f,
"============ AST ===========\n\
{:#?}",
self.node
)?;
writeln!(f, "========= PATTERN ==========")?;
match &self.pattern {
Ok(pattern) => {
write!(f, "{:#?}", pattern)?;
writeln!(f, "{:#?}", pattern)?;
}
Err(err) => {
write!(f, "{}", err.reason)?;
writeln!(f, "{}", err.reason)?;
}
}
write!(
f,
"\n============ AST ===========\n\
{:#?}\n============================\n",
self.node
)?;
match &self.matched {
Ok(_) => write!(f, "Node matched")?,
Err(reason) => write!(f, "Node failed to match because: {}", reason.reason)?,
}
writeln!(f, "============================")?;
Ok(())
}
}
Expand Down
Loading