Skip to content

Commit fd2108f

Browse files
committed
combine_strs_with_missing_comments
1 parent 77f5b71 commit fd2108f

File tree

13 files changed

+147
-119
lines changed

13 files changed

+147
-119
lines changed

src/attr.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +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::RewriteErrorExt;
15-
use crate::rewrite::{Rewrite, RewriteContext, RewriteResult};
14+
use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
1615
use crate::shape::Shape;
1716
use crate::source_map::SpanUtils;
1817
use crate::types::{rewrite_path, PathContext};
@@ -218,9 +217,10 @@ fn rewrite_initial_doc_comments(
218217
context: &RewriteContext<'_>,
219218
attrs: &[ast::Attribute],
220219
shape: Shape,
221-
) -> Option<(usize, Option<String>)> {
220+
) -> Result<(usize, Option<String>), RewriteError> {
221+
// TODO Q. should we modify Option<String>
222222
if attrs.is_empty() {
223-
return Some((0, None));
223+
return Ok((0, None));
224224
}
225225
// Rewrite doc comments
226226
let sugared_docs = take_while_with_pred(context, attrs, |a| a.is_doc_comment());
@@ -230,7 +230,7 @@ fn rewrite_initial_doc_comments(
230230
.map(|a| context.snippet(a.span))
231231
.collect::<Vec<_>>()
232232
.join("\n");
233-
return Some((
233+
return Ok((
234234
sugared_docs.len(),
235235
Some(rewrite_doc_comment(
236236
&snippet,
@@ -240,7 +240,7 @@ fn rewrite_initial_doc_comments(
240240
));
241241
}
242242

243-
Some((0, None))
243+
Ok((0, None))
244244
}
245245

246246
impl Rewrite for ast::NestedMetaItem {
@@ -330,6 +330,10 @@ impl Rewrite for ast::MetaItem {
330330

331331
impl Rewrite for ast::Attribute {
332332
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
333+
self.rewrite_result(context, shape).ok()
334+
}
335+
336+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
333337
let snippet = context.snippet(self.span);
334338
if self.is_doc_comment() {
335339
rewrite_doc_comment(snippet, shape.comment(context.config), context.config)
@@ -341,7 +345,7 @@ impl Rewrite for ast::Attribute {
341345
let prefix = attr_prefix(self);
342346

343347
if should_skip || contains_comment(snippet) {
344-
return Some(snippet.to_owned());
348+
return Ok(snippet.to_owned());
345349
}
346350

347351
if let Some(ref meta) = self.meta() {
@@ -366,9 +370,11 @@ impl Rewrite for ast::Attribute {
366370
}
367371

368372
// 1 = `[`
369-
let shape = shape.offset_left(prefix.len() + 1)?;
370-
Some(meta.rewrite(context, shape).map_or_else(
371-
|| snippet.to_owned(),
373+
let shape = shape
374+
.offset_left(prefix.len() + 1)
375+
.max_width_error(shape.width, self.span)?;
376+
Ok(meta.rewrite_result(context, shape).map_or_else(
377+
|_| snippet.to_owned(),
372378
|rw| match &self.kind {
373379
ast::AttrKind::Normal(normal_attr) => match normal_attr.item.unsafety {
374380
// For #![feature(unsafe_attributes)]
@@ -380,16 +386,20 @@ impl Rewrite for ast::Attribute {
380386
},
381387
))
382388
} else {
383-
Some(snippet.to_owned())
389+
Ok(snippet.to_owned())
384390
}
385391
}
386392
}
387393
}
388394

389395
impl Rewrite for [ast::Attribute] {
390396
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
397+
self.rewrite_result(context, shape).ok()
398+
}
399+
400+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
391401
if self.is_empty() {
392-
return Some(String::new());
402+
return Ok(String::new());
393403
}
394404

395405
// The current remaining attributes.
@@ -405,7 +415,7 @@ impl Rewrite for [ast::Attribute] {
405415
// merging derives into a single attribute.
406416
loop {
407417
if attrs.is_empty() {
408-
return Some(result);
418+
return Ok(result);
409419
}
410420

411421
// Handle doc comments.
@@ -444,7 +454,7 @@ impl Rewrite for [ast::Attribute] {
444454
// Handle derives if we will merge them.
445455
if !skip_derives && context.config.merge_derives() && is_derive(&attrs[0]) {
446456
let derives = take_while_with_pred(context, attrs, is_derive);
447-
let derive_str = format_derive(derives, shape, context)?;
457+
let derive_str = format_derive(derives, shape, context).unknown_error()?;
448458
result.push_str(&derive_str);
449459

450460
let missing_span = attrs
@@ -477,7 +487,7 @@ impl Rewrite for [ast::Attribute] {
477487
// If we get here, then we have a regular attribute, just handle one
478488
// at a time.
479489

480-
let formatted_attr = attrs[0].rewrite(context, shape)?;
490+
let formatted_attr = attrs[0].rewrite_result(context, shape)?;
481491
result.push_str(&formatted_attr);
482492

483493
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 {
@@ -293,7 +294,7 @@ impl Rewrite for ChainItem {
293294
),
294295
ChainItemKind::Await => ".await".to_owned(),
295296
ChainItemKind::Comment(ref comment, _) => {
296-
rewrite_comment(comment, false, shape, context.config)?
297+
rewrite_comment(comment, false, shape, context.config).ok()?
297298
}
298299
};
299300
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()? // TODO
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)