Skip to content
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

Add option to customize number of spaces leading // comments #776

Merged
merged 2 commits into from
Jul 30, 2024

Conversation

dduan
Copy link
Contributor

@dduan dduan commented Jul 21, 2024

Add an option spacesBeforeEndOfLineComments that allows customization of number of spaces between the first non-trivial token and a line comment with the // prefix. Defaults to 2, the historical default value.

@bnbarham bnbarham requested a review from allevato July 22, 2024 17:47
Comment on lines 70 to 71
// Number of spaces that preceeds line comments.
public var spacesBeforeLineComments: Int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this apply to both // and /**/ comments? The test case with /**/ has 1-space in both the input and expected, so wasn't clear from there.

Copy link
Contributor Author

@dduan dduan Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe /* */ is referred to as "block comments" regardless of whether it's on the same line or not. In existing unit tests (adjacent to the ones I added), there are /* */ comments with 1 leading space that aren't affected by the formatting (which ensures 2 spaces). I made sure in the documentation, it explicitly says "comments starting with '//'" to avoid this ambiguity.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, thanks. Could you make the comment here a doc comment (///) and include that here as well? @allevato is it intended that block comments are treated differently in this case? Not sure I have much of an opinion on it either way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of this configuration should be explicit that it's just about "end of line comments", since that traditionally means // comments that specifically follow something else on a line and does not include /* ... */, ///, or /** ... */ comments, nor // comments on their own line. So I'd call this spacesBeforeEndOfLineComments.

I think it's correct to only support this for // EoL comments. Let's imagine we have a situation where we want 2 spaces before those, but only one space around inline block comments, and we have this contrived example:

let x = 5 // something
let y = someValue + x /* meters */ / someOtherValue /* seconds */

When we add another space before the comment on the first line, I don't think it would be right to change the spacing around the block comments on the second line.

@@ -199,6 +200,127 @@ final class CommentTests: PrettyPrintTestCase {
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
}

func testLineCommentsWithCustomLeadingSpaces() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would help to split this test case up - it's quite hard to compare the input/expectation at the moment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a copy of the previous tests. I can try and split of both?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to just have the new tests split, feel free to also clean up the copied-from in another PR though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I'd split them into functions so the failure is super obvious if it happens. Currently you'd have to map the failed input/output back to one of the pairs by looking over them all (since the failure would always be on the same line). You could also add a line number to each test case, but at that point having separate functions seems easier.

Any preference @allevato? I'm also happy to take as is, it's just tests.

Copy link
Member

@simanerush simanerush left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat!

Add an option `spacesBeforeLineComments` that allows customization of
number of spaces between the first non-trivial token and a line comment
with the `//` prefix. Defaults to 2, the historical default value.
@dduan dduan force-pushed the dd/line-comment-leading-spaces branch from b634fd3 to 4a99f1c Compare July 23, 2024 18:06
@dduan dduan requested a review from bnbarham July 23, 2024 18:52
Copy link
Contributor

@bnbarham bnbarham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates!

@@ -199,6 +200,127 @@ final class CommentTests: PrettyPrintTestCase {
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
}

func testLineCommentsWithCustomLeadingSpaces() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I'd split them into functions so the failure is super obvious if it happens. Currently you'd have to map the failed input/output back to one of the pairs by looking over them all (since the failure would always be on the same line). You could also add a line number to each test case, but at that point having separate functions seems easier.

Any preference @allevato? I'm also happy to take as is, it's just tests.

Sources/SwiftFormat/API/Configuration.swift Outdated Show resolved Hide resolved
Co-authored-by: Ben Barham <b.n.barham@gmail.com>
@dduan
Copy link
Contributor Author

dduan commented Jul 28, 2024

@allevato thoughts?

Copy link
Member

@allevato allevato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised folks would have a strong enough opinion about this to want to change the default, but whatever 🙂

@allevato allevato merged commit ffc641e into swiftlang:main Jul 30, 2024
@dduan
Copy link
Contributor Author

dduan commented Jul 30, 2024

Thanks @allevato! FWIW, I personally don't hold any opinions about any of the options. But getting swift-format to adoption for my team requires some finessing such as this :)

@dduan dduan deleted the dd/line-comment-leading-spaces branch July 30, 2024 19:05
@tscholze
Copy link

tscholze commented Oct 9, 2024

Does anyone know when this version of swiftformat will be shipped with Xcode? It seems that the current version of Xcode has not this feature included. Thanks!

@ahoppen
Copy link
Member

ahoppen commented Oct 9, 2024

This change will be included in the swift-format bundled with Swift 6.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants