modify rewrite_literal

This commit is contained in:
ding-young 2024-07-22 13:17:39 +09:00 committed by Yacin Tmimi
parent cf352a766f
commit 5325b9e98c
2 changed files with 23 additions and 16 deletions

View File

@ -11,7 +11,7 @@ use crate::config::IndentStyle;
use crate::expr::rewrite_literal;
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
use crate::overflow;
use crate::rewrite::{Rewrite, RewriteContext, RewriteErrorExt};
use crate::rewrite::{Rewrite, RewriteContext, RewriteErrorExt, RewriteResult};
use crate::shape::Shape;
use crate::source_map::SpanUtils;
use crate::types::{rewrite_path, PathContext};
@ -244,8 +244,14 @@ fn rewrite_initial_doc_comments(
impl Rewrite for ast::NestedMetaItem {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
self.rewrite_result(context, shape).ok()
}
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
match self {
ast::NestedMetaItem::MetaItem(ref meta_item) => meta_item.rewrite(context, shape),
ast::NestedMetaItem::MetaItem(ref meta_item) => {
meta_item.rewrite_result(context, shape)
}
ast::NestedMetaItem::Lit(ref l) => {
rewrite_literal(context, l.as_token_lit(), l.span, shape)
}
@ -277,11 +283,7 @@ impl Rewrite for ast::MetaItem {
self.rewrite_result(context, shape).ok()
}
fn rewrite_result(
&self,
context: &RewriteContext<'_>,
shape: Shape,
) -> crate::rewrite::RewriteResult {
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
Ok(match self.kind {
ast::MetaItemKind::Word => {
rewrite_path(context, PathContext::Type, &None, &self.path, shape)?
@ -317,7 +319,7 @@ impl Rewrite for ast::MetaItem {
// is longer than the max width and continue on formatting.
// See #2479 for example.
let value = rewrite_literal(context, lit.as_token_lit(), lit.span, lit_shape)
.unwrap_or_else(|| context.snippet(lit.span).to_owned());
.unwrap_or_else(|_| context.snippet(lit.span).to_owned());
format!("{path} = {value}")
}
})

View File

@ -82,7 +82,7 @@ pub(crate) fn format_expr(
)
.ok(),
ast::ExprKind::Lit(token_lit) => {
if let Some(expr_rw) = rewrite_literal(context, token_lit, expr.span, shape) {
if let Ok(expr_rw) = rewrite_literal(context, token_lit, expr.span, shape) {
Some(expr_rw)
} else {
if let LitKind::StrRaw(_) = token_lit.kind {
@ -1262,7 +1262,7 @@ pub(crate) fn rewrite_literal(
token_lit: token::Lit,
span: Span,
shape: Shape,
) -> Option<String> {
) -> RewriteResult {
match token_lit.kind {
token::LitKind::Str => rewrite_string_lit(context, span, shape),
token::LitKind::Integer => rewrite_int_lit(context, token_lit, span, shape),
@ -1270,11 +1270,12 @@ pub(crate) fn rewrite_literal(
context.snippet(span).to_owned(),
context.config.max_width(),
shape,
),
)
.max_width_error(shape.width, span),
}
}
fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) -> Option<String> {
fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) -> RewriteResult {
let string_lit = context.snippet(span);
if !context.config.format_strings() {
@ -1284,9 +1285,10 @@ fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) ->
.all(|line| line.ends_with('\\'))
&& context.config.style_edition() >= StyleEdition::Edition2024
{
return Some(string_lit.to_owned());
return Ok(string_lit.to_owned());
} else {
return wrap_str(string_lit.to_owned(), context.config.max_width(), shape);
return wrap_str(string_lit.to_owned(), context.config.max_width(), shape)
.max_width_error(shape.width, span);
}
}
@ -1298,6 +1300,7 @@ fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) ->
&StringFormat::new(shape.visual_indent(0), context.config),
shape.width.saturating_sub(2),
)
.max_width_error(shape.width, span) // - 2 ?
}
fn rewrite_int_lit(
@ -1305,7 +1308,7 @@ fn rewrite_int_lit(
token_lit: token::Lit,
span: Span,
shape: Shape,
) -> Option<String> {
) -> RewriteResult {
let symbol = token_lit.symbol.as_str();
if let Some(symbol_stripped) = symbol.strip_prefix("0x") {
@ -1323,7 +1326,8 @@ fn rewrite_int_lit(
),
context.config.max_width(),
shape,
);
)
.max_width_error(shape.width, span);
}
}
@ -1332,6 +1336,7 @@ fn rewrite_int_lit(
context.config.max_width(),
shape,
)
.max_width_error(shape.width, span)
}
fn choose_separator_tactic(context: &RewriteContext<'_>, span: Span) -> Option<SeparatorTactic> {