Skip to content

Commit 3d468e2

Browse files
ding-youngytmimi
authored andcommitted
update combine_strs_with_missing_comments
1 parent 5325b9e commit 3d468e2

File tree

13 files changed

+148
-126
lines changed

13 files changed

+148
-126
lines changed

src/attr.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::config::IndentStyle;
1111
use crate::expr::rewrite_literal;
1212
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
1313
use crate::overflow;
14-
use crate::rewrite::{Rewrite, RewriteContext, RewriteErrorExt, RewriteResult};
14+
use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
1515
use crate::shape::Shape;
1616
use crate::source_map::SpanUtils;
1717
use crate::types::{rewrite_path, PathContext};
@@ -217,9 +217,9 @@ fn rewrite_initial_doc_comments(
217217
context: &RewriteContext<'_>,
218218
attrs: &[ast::Attribute],
219219
shape: Shape,
220-
) -> Option<(usize, Option<String>)> {
220+
) -> Result<(usize, Option<String>), RewriteError> {
221221
if attrs.is_empty() {
222-
return Some((0, None));
222+
return Ok((0, None));
223223
}
224224
// Rewrite doc comments
225225
let sugared_docs = take_while_with_pred(context, attrs, |a| a.is_doc_comment());
@@ -229,7 +229,7 @@ fn rewrite_initial_doc_comments(
229229
.map(|a| context.snippet(a.span))
230230
.collect::<Vec<_>>()
231231
.join("\n");
232-
return Some((
232+
return Ok((
233233
sugared_docs.len(),
234234
Some(rewrite_doc_comment(
235235
&snippet,
@@ -239,7 +239,7 @@ fn rewrite_initial_doc_comments(
239239
));
240240
}
241241

242-
Some((0, None))
242+
Ok((0, None))
243243
}
244244

245245
impl Rewrite for ast::NestedMetaItem {
@@ -328,6 +328,10 @@ impl Rewrite for ast::MetaItem {
328328

329329
impl Rewrite for ast::Attribute {
330330
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
331+
self.rewrite_result(context, shape).ok()
332+
}
333+
334+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
331335
let snippet = context.snippet(self.span);
332336
if self.is_doc_comment() {
333337
rewrite_doc_comment(snippet, shape.comment(context.config), context.config)
@@ -339,7 +343,7 @@ impl Rewrite for ast::Attribute {
339343
let prefix = attr_prefix(self);
340344

341345
if should_skip || contains_comment(snippet) {
342-
return Some(snippet.to_owned());
346+
return Ok(snippet.to_owned());
343347
}
344348

345349
if let Some(ref meta) = self.meta() {
@@ -364,9 +368,11 @@ impl Rewrite for ast::Attribute {
364368
}
365369

366370
// 1 = `[`
367-
let shape = shape.offset_left(prefix.len() + 1)?;
368-
Some(meta.rewrite(context, shape).map_or_else(
369-
|| snippet.to_owned(),
371+
let shape = shape
372+
.offset_left(prefix.len() + 1)
373+
.max_width_error(shape.width, self.span)?;
374+
Ok(meta.rewrite_result(context, shape).map_or_else(
375+
|_| snippet.to_owned(),
370376
|rw| match &self.kind {
371377
ast::AttrKind::Normal(normal_attr) => match normal_attr.item.unsafety {
372378
// For #![feature(unsafe_attributes)]
@@ -378,16 +384,20 @@ impl Rewrite for ast::Attribute {
378384
},
379385
))
380386
} else {
381-
Some(snippet.to_owned())
387+
Ok(snippet.to_owned())
382388
}
383389
}
384390
}
385391
}
386392

387393
impl Rewrite for [ast::Attribute] {
388394
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
395+
self.rewrite_result(context, shape).ok()
396+
}
397+
398+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
389399
if self.is_empty() {
390-
return Some(String::new());
400+
return Ok(String::new());
391401
}
392402

393403
// The current remaining attributes.
@@ -403,7 +413,7 @@ impl Rewrite for [ast::Attribute] {
403413
// merging derives into a single attribute.
404414
loop {
405415
if attrs.is_empty() {
406-
return Some(result);
416+
return Ok(result);
407417
}
408418

409419
// Handle doc comments.
@@ -442,7 +452,7 @@ impl Rewrite for [ast::Attribute] {
442452
// Handle derives if we will merge them.
443453
if !skip_derives && context.config.merge_derives() && is_derive(&attrs[0]) {
444454
let derives = take_while_with_pred(context, attrs, is_derive);
445-
let derive_str = format_derive(derives, shape, context)?;
455+
let derive_str = format_derive(derives, shape, context).unknown_error()?;
446456
result.push_str(&derive_str);
447457

448458
let missing_span = attrs
@@ -475,7 +485,7 @@ impl Rewrite for [ast::Attribute] {
475485
// If we get here, then we have a regular attribute, just handle one
476486
// at a time.
477487

478-
let formatted_attr = attrs[0].rewrite(context, shape)?;
488+
let formatted_attr = attrs[0].rewrite_result(context, shape)?;
479489
result.push_str(&formatted_attr);
480490

481491
let missing_span = attrs

src/chains.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ impl ChainItemKind {
267267
}
268268

269269
impl Rewrite for ChainItem {
270+
// TODO impl rewrite_result after rebase
270271
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
271272
let shape = shape.sub_width(self.tries)?;
272273
let rewrite = match self.kind {
@@ -294,7 +295,7 @@ impl Rewrite for ChainItem {
294295
),
295296
ChainItemKind::Await => ".await".to_owned(),
296297
ChainItemKind::Comment(ref comment, _) => {
297-
rewrite_comment(comment, false, shape, context.config)?
298+
rewrite_comment(comment, false, shape, context.config).ok()?
298299
}
299300
};
300301
Some(format!("{rewrite}{}", "?".repeat(self.tries)))

src/comment.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use itertools::{multipeek, MultiPeek};
66
use rustc_span::Span;
77

88
use crate::config::Config;
9-
use crate::rewrite::RewriteContext;
9+
use crate::rewrite::{RewriteContext, RewriteErrorExt, RewriteResult};
1010
use crate::shape::{Indent, Shape};
1111
use crate::string::{rewrite_string, StringFormat};
1212
use crate::utils::{
@@ -157,7 +157,7 @@ pub(crate) fn combine_strs_with_missing_comments(
157157
span: Span,
158158
shape: Shape,
159159
allow_extend: bool,
160-
) -> Option<String> {
160+
) -> RewriteResult {
161161
trace!(
162162
"combine_strs_with_missing_comments `{}` `{}` {:?} {:?}",
163163
prev_str, next_str, span, shape
@@ -187,7 +187,7 @@ pub(crate) fn combine_strs_with_missing_comments(
187187
result.push_str(&indent.to_string_with_newline(config))
188188
}
189189
result.push_str(next_str);
190-
return Some(result);
190+
return Ok(result);
191191
}
192192

193193
// We have a missing comment between the first expression and the second expression.
@@ -232,10 +232,10 @@ pub(crate) fn combine_strs_with_missing_comments(
232232
result.push_str(&second_sep);
233233
result.push_str(next_str);
234234

235-
Some(result)
235+
Ok(result)
236236
}
237237

238-
pub(crate) fn rewrite_doc_comment(orig: &str, shape: Shape, config: &Config) -> Option<String> {
238+
pub(crate) fn rewrite_doc_comment(orig: &str, shape: Shape, config: &Config) -> RewriteResult {
239239
identify_comment(orig, false, shape, config, true)
240240
}
241241

@@ -244,7 +244,7 @@ pub(crate) fn rewrite_comment(
244244
block_style: bool,
245245
shape: Shape,
246246
config: &Config,
247-
) -> Option<String> {
247+
) -> RewriteResult {
248248
identify_comment(orig, block_style, shape, config, false)
249249
}
250250

@@ -254,7 +254,7 @@ fn identify_comment(
254254
shape: Shape,
255255
config: &Config,
256256
is_doc_comment: bool,
257-
) -> Option<String> {
257+
) -> RewriteResult {
258258
let style = comment_style(orig, false);
259259

260260
// Computes the byte length of line taking into account a newline if the line is part of a
@@ -346,7 +346,7 @@ fn identify_comment(
346346
let (first_group, rest) = orig.split_at(first_group_ending);
347347
let rewritten_first_group =
348348
if !config.normalize_comments() && has_bare_lines && style.is_block_comment() {
349-
trim_left_preserve_layout(first_group, shape.indent, config)?
349+
trim_left_preserve_layout(first_group, shape.indent, config).unknown_error()?
350350
} else if !config.normalize_comments()
351351
&& !config.wrap_comments()
352352
&& !(
@@ -367,7 +367,7 @@ fn identify_comment(
367367
)?
368368
};
369369
if rest.is_empty() {
370-
Some(rewritten_first_group)
370+
Ok(rewritten_first_group)
371371
} else {
372372
identify_comment(
373373
rest.trim_start(),
@@ -899,7 +899,7 @@ fn rewrite_comment_inner(
899899
shape: Shape,
900900
config: &Config,
901901
is_doc_comment: bool,
902-
) -> Option<String> {
902+
) -> RewriteResult {
903903
let mut rewriter = CommentRewrite::new(orig, block_style, shape, config);
904904

905905
let line_breaks = count_newlines(orig.trim_end());
@@ -933,7 +933,7 @@ fn rewrite_comment_inner(
933933
}
934934
}
935935

936-
Some(rewriter.finish())
936+
Ok(rewriter.finish())
937937
}
938938

939939
const RUSTFMT_CUSTOM_COMMENT_PREFIX: &str = "//#### ";
@@ -998,15 +998,15 @@ pub(crate) fn rewrite_missing_comment(
998998
span: Span,
999999
shape: Shape,
10001000
context: &RewriteContext<'_>,
1001-
) -> Option<String> {
1001+
) -> RewriteResult {
10021002
let missing_snippet = context.snippet(span);
10031003
let trimmed_snippet = missing_snippet.trim();
10041004
// check the span starts with a comment
10051005
let pos = trimmed_snippet.find('/');
10061006
if !trimmed_snippet.is_empty() && pos.is_some() {
10071007
rewrite_comment(trimmed_snippet, false, shape, context.config)
10081008
} else {
1009-
Some(String::new())
1009+
Ok(String::new())
10101010
}
10111011
}
10121012

@@ -1018,13 +1018,13 @@ pub(crate) fn recover_missing_comment_in_span(
10181018
shape: Shape,
10191019
context: &RewriteContext<'_>,
10201020
used_width: usize,
1021-
) -> Option<String> {
1021+
) -> RewriteResult {
10221022
let missing_comment = rewrite_missing_comment(span, shape, context)?;
10231023
if missing_comment.is_empty() {
1024-
Some(String::new())
1024+
Ok(String::new())
10251025
} else {
10261026
let missing_snippet = context.snippet(span);
1027-
let pos = missing_snippet.find('/')?;
1027+
let pos = missing_snippet.find('/').unknown_error()?;
10281028
// 1 = ` `
10291029
let total_width = missing_comment.len() + used_width + 1;
10301030
let force_new_line_before_comment =
@@ -1034,7 +1034,7 @@ pub(crate) fn recover_missing_comment_in_span(
10341034
} else {
10351035
Cow::from(" ")
10361036
};
1037-
Some(format!("{sep}{missing_comment}"))
1037+
Ok(format!("{sep}{missing_comment}"))
10381038
}
10391039
}
10401040

0 commit comments

Comments
 (0)