Skip to content

Commit

Permalink
print original code if external formatter raised an error
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Jan 3, 2024
1 parent 5cdd130 commit e228e6a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
4 changes: 3 additions & 1 deletion dprint_plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ impl SyncPluginHandler<FormatOptions> for MarkupFmtPluginHandler {
match format_result {
Ok(code) => Ok(Some(code.into_bytes())),
Err(FormatError::Syntax(err)) => Err(err.into()),
Err(FormatError::External(err)) => Err(err),
Err(FormatError::External(err, code)) => Err(anyhow::anyhow!(
"[markup_fmt] failed to format code with external formatter: `{code}`:\n{err}"
)),
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions markup_fmt/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ where
pub(crate) in_svg: bool,
pub(crate) indent_level: usize,
pub(crate) external_formatter: F,
pub(crate) external_formatter_error: Option<E>,
pub(crate) external_formatter_error: Option<(E, String)>,
}

impl<'b, 's, E, F> Ctx<'b, 's, E, F>
Expand Down Expand Up @@ -101,7 +101,8 @@ where
.saturating_sub(self.indent_level)
.saturating_sub(2), // this is technically wrong, just workaround
);
let formatted = formatted.trim_end_matches(|c: char| c.is_ascii_whitespace() || c == ';');
let formatted =
formatted.trim_end_matches(|c: char| c.is_ascii_whitespace() || c == ';');
formatted
.strip_prefix("let e =")
.unwrap_or(formatted)
Expand Down Expand Up @@ -189,7 +190,7 @@ where
match (self.external_formatter)(path, code, print_width) {
Ok(code) => code,
Err(e) => {
self.external_formatter_error = Some(e);
self.external_formatter_error = Some((e, code.to_owned()));
code.into()
}
}
Expand Down
7 changes: 5 additions & 2 deletions markup_fmt/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub enum FormatError<E> {
Syntax(SyntaxError),
/// Error from external formatter, for example,
/// there're errors when formatting the `<script>` or `<style>` tag.
External(E),
External(E, String),
}

impl<E> fmt::Display for FormatError<E>
Expand All @@ -99,7 +99,10 @@ where
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FormatError::Syntax(e) => e.fmt(f),
FormatError::External(e) => e.fmt(f),
FormatError::External(e, code) => write!(
f,
"failed to format code with external formatter: `{code}`:\n{e}"
),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions markup_fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ where
};

let doc = ast.doc(&mut ctx);
if let Some(error) = ctx.external_formatter_error {
return Err(FormatError::External(error));
if let Some((error, code)) = ctx.external_formatter_error {
return Err(FormatError::External(error, code));
}

Ok(tiny_pretty::print(
Expand Down

0 comments on commit e228e6a

Please sign in to comment.