Skip to content

Indentation of multiline comments between lhs and rhs #4734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/formatting/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use crate::formatting::{
shape::{Indent, Shape},
string::{rewrite_string, StringFormat},
utils::{
count_newlines, first_line_width, format_code_block, last_line_width, tab_to_spaces,
trim_end_unless_two_whitespaces, trim_left_preserve_layout, trimmed_last_line_width,
unicode_str_width,
count_newlines, first_line_width, format_code_block, is_single_line, last_line_width,
tab_to_spaces, trim_end_unless_two_whitespaces, trim_left_preserve_layout,
trimmed_last_line_width, unicode_str_width,
},
};

Expand Down Expand Up @@ -156,6 +156,18 @@ pub(crate) fn is_last_comment_block(s: &str) -> bool {
s.trim_end().ends_with("*/")
}

/// Returns true if the first line of the passed string starts with a comment
/// that end on that line.
fn is_first_comment_ends_on_first_line(s: &str) -> bool {
if s.starts_with("/*") {
s.lines().next().map_or(false, |l| l.contains("*/"))
} else if s.starts_with("//") {
true
} else {
false
}
}

/// Combine `prev_str` and `next_str` into a single `String`. `span` may contain
/// comments between two strings. If there are such comments, then that will be
/// recovered. If `allow_extend` is true and there is no comment between the two
Expand Down Expand Up @@ -221,7 +233,13 @@ pub(crate) fn combine_strs_with_missing_comments(
Cow::from("")
} else {
let one_line_width = last_line_width(prev_str) + first_line_width(&missing_comment) + 1;
if prefer_same_line && one_line_width <= shape.width {
// First comment of a comments block can be in same line if ends in the first line
// (and meets other conditions)
if prefer_same_line
&& one_line_width <= shape.width
&& (is_single_line(&missing_comment)
|| is_first_comment_ends_on_first_line(&missing_comment))
{
Cow::from(" ")
} else {
indent.to_string_with_newline(config)
Expand All @@ -236,7 +254,7 @@ pub(crate) fn combine_strs_with_missing_comments(
indent.to_string_with_newline(config)
} else {
one_line_width += missing_comment.len() + first_sep.len() + 1;
allow_one_line &= !missing_comment.starts_with("//") && !missing_comment.contains('\n');
allow_one_line &= !missing_comment.contains('\n');
if prefer_same_line && allow_one_line && one_line_width <= shape.width {
Cow::from(" ")
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// "=" Assignemnt - Multiline Block comments
fn main () {
let var = /* Block comment line 1
* Block comment line 2
* Block comment line 3 */ first;
var = /* Block comment line 1
* Block comment line 2
* Block comment line 3 */ second;
var = /* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
* Block comment line 2
* Block comment line 3 */ third;
var = /* Block comment line 1
* Block comment line 2 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
* Block comment line 3 */ forth;
}

// "=" Assignemnt - Multiline Line and Block comments
fn main () {
let var = // Line comment 1
// Line comment 2
// Line comment 3
first;
var = // Line comment
/* Block comment line 1
* Block comment line 2 */ second;
var = /* Block comment single line */
// Line comment
/* Block comment line 1
* Block comment line 2 */ third;
var = /* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
* Block comment line 2 */
// Line comment
forth;
var = // Line comment
/* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
* Block comment line 2 */ fifth;
}

// BinOp Assignemnt - Multiline Block comments
fn main () {
let var = /* Block comment line 1
* Block comment line 2
* Block comment line 3 */ first;
var += /* Block comment line 1
* Block comment line 2
* Block comment line 3 */ second;
var -= /* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
* Block comment line 2
* Block comment line 3 */ third;
var *= /* Block comment line 1
* Block comment line 2 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
* Block comment line 3 */ forth;
}

// BinOp Assignemnt - Multiline Line and Block comments
fn main () {
let var = // Line comment 1
// Line comment 2
// Line comment 3
first;
var += // Line comment
/* Block comment line 1
* Block comment line 2 */ second;
var -= /* Block comment single line */
// Line comment
/* Block comment line 1
* Block comment line 2 */ third;
var *= /* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
* Block comment line 2 */
// Line comment
forth;
var /= // Line comment
/* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
* Block comment line 2 */ fifth;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// "=" Assignemnt
fn main () {
let var = first;
var = second;
}
fn main () {
let var = /* Block comment */ first;
var = /* Block comment */ second;
}
fn main () {
let var = // Line comment
first;
var = // Line comment
second;
}
fn main () {
let var = /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ first;
var = /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ second;
}
fn main () {
let var = /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ first;
var = /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ second;
}

// BinOp Assigment
fn main () {
let var = first;
var += second;
}
fn main () {
let var = /* Block comment */ first;
var -= /* Block comment */ second;
}
fn main () {
let var = // Line comment
first;
var *= // Line comment
second;
}
fn main () {
let var = /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ first;
var /= /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ second;
}
fn main () {
let var = /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ first;
var /= /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ second;
}
76 changes: 0 additions & 76 deletions tests/source/lhs-to-rhs-between-comments/assignment.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// "=" Assignemnt - Multiline Block comments
fn main() {
let var =
/* Block comment line 1
* Block comment line 2
* Block comment line 3 */
first;
var =
/* Block comment line 1
* Block comment line 2
* Block comment line 3 */
second;
var =
/* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
* Block comment line 2
* Block comment line 3 */
third;
var =
/* Block comment line 1
* Block comment line 2 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
* Block comment line 3 */
forth;
}

// "=" Assignemnt - Multiline Line and Block comments
fn main() {
let var = // Line comment 1
// Line comment 2
// Line comment 3
first;
var = // Line comment
/* Block comment line 1
* Block comment line 2 */
second;
var = /* Block comment single line */
// Line comment
/* Block comment line 1
* Block comment line 2 */
third;
var =
/* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
* Block comment line 2 */
// Line comment
forth;
var = // Line comment
/* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
* Block comment line 2 */
fifth;
}

// BinOp Assignemnt - Multiline Block comments
fn main() {
let var =
/* Block comment line 1
* Block comment line 2
* Block comment line 3 */
first;
var +=
/* Block comment line 1
* Block comment line 2
* Block comment line 3 */
second;
var -=
/* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
* Block comment line 2
* Block comment line 3 */
third;
var *=
/* Block comment line 1
* Block comment line 2 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
* Block comment line 3 */
forth;
}

// BinOp Assignemnt - Multiline Line and Block comments
fn main() {
let var = // Line comment 1
// Line comment 2
// Line comment 3
first;
var += // Line comment
/* Block comment line 1
* Block comment line 2 */
second;
var -= /* Block comment single line */
// Line comment
/* Block comment line 1
* Block comment line 2 */
third;
var *=
/* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
* Block comment line 2 */
// Line comment
forth;
var /= // Line comment
/* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
* Block comment line 2 */
fifth;
}
Loading