Skip to content

Commit 43dc254

Browse files
committed
Fix for issue rust-lang#4793 for handling empty code-block in doc comments
1 parent 2b3ca99 commit 43dc254

File tree

3 files changed

+115
-22
lines changed

3 files changed

+115
-22
lines changed

src/formatting/utils.rs

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,13 @@ pub(crate) fn format_code_block(
780780
result
781781
}
782782

783+
if code_snippet.is_empty() {
784+
return Some(FormattedSnippet {
785+
snippet: String::new(),
786+
non_formatted_ranges: vec![],
787+
});
788+
}
789+
783790
// Wrap the given code block with `fn main()` if it does not have one.
784791
let snippet = enclose_in_main_block(code_snippet, config);
785792
let mut result = String::with_capacity(snippet.len());
@@ -805,32 +812,42 @@ pub(crate) fn format_code_block(
805812
.unwrap_or_else(|| formatted.snippet.len());
806813
let mut is_indented = true;
807814
let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config);
808-
for (kind, ref line) in LineClasses::new(&formatted.snippet[FN_MAIN_PREFIX.len()..block_len]) {
809-
if !is_first {
810-
result.push('\n');
811-
} else {
812-
is_first = false;
813-
}
814-
let trimmed_line = if !is_indented {
815-
line
816-
} else if line.len() > indent_str.len() {
817-
// Make sure that the line has leading whitespaces.
818-
if line.starts_with(indent_str.as_ref()) {
819-
let offset = if config.hard_tabs() {
820-
1
815+
816+
if FN_MAIN_PREFIX.len() >= block_len {
817+
// Code block with empty lines
818+
result.push_str("\n\n");
819+
} else {
820+
// Non-empty code block
821+
for (kind, ref line) in
822+
LineClasses::new(&formatted.snippet[FN_MAIN_PREFIX.len()..block_len])
823+
{
824+
if !is_first {
825+
result.push('\n');
826+
} else {
827+
is_first = false;
828+
}
829+
let trimmed_line = if !is_indented {
830+
line
831+
} else if line.len() > indent_str.len() {
832+
// Make sure that the line has leading whitespaces.
833+
if line.starts_with(indent_str.as_ref()) {
834+
let offset = if config.hard_tabs() {
835+
1
836+
} else {
837+
config.tab_spaces()
838+
};
839+
&line[offset..]
821840
} else {
822-
config.tab_spaces()
823-
};
824-
&line[offset..]
841+
line
842+
}
825843
} else {
826844
line
827-
}
828-
} else {
829-
line
830-
};
831-
result.push_str(trimmed_line);
832-
is_indented = indent_next_line(kind);
845+
};
846+
result.push_str(trimmed_line);
847+
is_indented = indent_next_line(kind);
848+
}
833849
}
850+
834851
Some(FormattedSnippet {
835852
snippet: result,
836853
non_formatted_ranges: formatted.non_formatted_ranges,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
3+
/// ```
4+
/// ```
5+
fn foo() {}
6+
7+
/// ```
8+
///Something
9+
/// ```
10+
fn foo() {}
11+
12+
/// ```
13+
///
14+
/// ```
15+
fn foo() {}
16+
17+
fn foo() {
18+
/// ```
19+
///
20+
/// ```
21+
struct bar {}
22+
}
23+
24+
/// ```
25+
/// fn com(){
26+
/// let i = 5;
27+
/// }
28+
/// ```
29+
fn foo() {}
30+
31+
fn foo() {
32+
/// ```
33+
///fn com(){
34+
///let i = 5;
35+
///}
36+
/// ```
37+
struct bar {}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
3+
/// ```
4+
/// ```
5+
fn foo() {}
6+
7+
/// ```
8+
/// Something
9+
/// ```
10+
fn foo() {}
11+
12+
/// ```
13+
///
14+
/// ```
15+
fn foo() {}
16+
17+
fn foo() {
18+
/// ```
19+
///
20+
/// ```
21+
struct bar {}
22+
}
23+
24+
/// ```
25+
/// fn com() {
26+
/// let i = 5;
27+
/// }
28+
/// ```
29+
fn foo() {}
30+
31+
fn foo() {
32+
/// ```
33+
/// fn com() {
34+
/// let i = 5;
35+
/// }
36+
/// ```
37+
struct bar {}
38+
}

0 commit comments

Comments
 (0)