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
Merged
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
3 changes: 3 additions & 0 deletions Documentation/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ top-level keys and values:
lines that are allowed to be present in a source file. Any number larger
than this will be collapsed down to the maximum.

* `spacesBeforeEndOfLineComments` _(number)_: The number of spaces between
the last token on a non-empty line and a line comment starting with `//`.

* `respectsExistingLineBreaks` _(boolean)_: Indicates whether or not existing
line breaks in the source code should be honored (if they are valid
according to the style guidelines being enforced). If this settings is
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftFormat/API/Configuration+Default.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extension Configuration {
self.lineLength = 100
self.tabWidth = 8
self.indentation = .spaces(2)
self.spacesBeforeEndOfLineComments = 2
self.respectsExistingLineBreaks = true
self.lineBreakBeforeControlFlowKeywords = false
self.lineBreakBeforeEachArgument = false
Expand Down
8 changes: 8 additions & 0 deletions Sources/SwiftFormat/API/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public struct Configuration: Codable, Equatable {
case version
case maximumBlankLines
case lineLength
case spacesBeforeEndOfLineComments
case tabWidth
case indentation
case respectsExistingLineBreaks
Expand Down Expand Up @@ -66,6 +67,9 @@ public struct Configuration: Codable, Equatable {
/// The maximum length of a line of source code, after which the formatter will break lines.
public var lineLength: Int

/// Number of spaces that precede line comments.
public var spacesBeforeEndOfLineComments: Int

/// The width of the horizontal tab in spaces.
///
/// This value is used when converting indentation types (for example, from tabs into spaces).
Expand Down Expand Up @@ -225,6 +229,9 @@ public struct Configuration: Codable, Equatable {
self.lineLength =
try container.decodeIfPresent(Int.self, forKey: .lineLength)
?? defaults.lineLength
self.spacesBeforeEndOfLineComments =
try container.decodeIfPresent(Int.self, forKey: .spacesBeforeEndOfLineComments)
?? defaults.spacesBeforeEndOfLineComments
self.tabWidth =
try container.decodeIfPresent(Int.self, forKey: .tabWidth)
?? defaults.tabWidth
Expand Down Expand Up @@ -288,6 +295,7 @@ public struct Configuration: Codable, Equatable {
try container.encode(version, forKey: .version)
try container.encode(maximumBlankLines, forKey: .maximumBlankLines)
try container.encode(lineLength, forKey: .lineLength)
try container.encode(spacesBeforeEndOfLineComments, forKey: .spacesBeforeEndOfLineComments)
try container.encode(tabWidth, forKey: .tabWidth)
try container.encode(indentation, forKey: .indentation)
try container.encode(respectsExistingLineBreaks, forKey: .respectsExistingLineBreaks)
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3171,7 +3171,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
return (
true,
[
.space(size: 2, flexible: true),
.space(size: config.spacesBeforeEndOfLineComments, flexible: true),
.comment(Comment(kind: .line, text: text), wasEndOfLine: true),
// There must be a break with a soft newline after the comment, but it's impossible to
// know which kind of break must be used. Adding this newline is deferred until the
Expand Down
223 changes: 223 additions & 0 deletions Tests/SwiftFormatTests/PrettyPrint/CommentTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _SwiftFormatTestSupport
import SwiftFormat

final class CommentTests: PrettyPrintTestCase {
func testDocumentationComments() {
Expand Down Expand Up @@ -199,6 +200,152 @@ 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.

let pairs: [(String, String)] = [
(
"""
// Line Comment0

// Line Comment1
// Line Comment2
let a = 123
let b = "456" // End of line comment
let c = "More content"

""",
"""
// Line Comment0

// Line Comment1
// Line Comment2
let a = 123
let b = "456" // End of line comment
let c = "More content"

"""
),
(
"""
// Comment 3
// Comment 4

let reallyLongVariableName = 123 // This comment should not wrap
// and should not combine with this comment

func MyFun() {
// just a comment
}
""",
"""
// Comment 3
// Comment 4

let reallyLongVariableName = 123 // This comment should not wrap
// and should not combine with this comment

func MyFun() {
// just a comment
}

"""
),
(
"""
func MyFun() {
// Comment 1
// Comment 2
let a = 123

let b = 456 // Comment 3
}

func MyFun() {
let c = 789 // Comment 4
// Comment 5
}
""",
"""
func MyFun() {
// Comment 1
// Comment 2
let a = 123

let b = 456 // Comment 3
}

func MyFun() {
let c = 789 // Comment 4
// Comment 5
}

"""
),
(
"""
let a = myfun(123 // Cmt 7
)
let a = myfun(var1: 123 // Cmt 7
)

guard condition else { return // Cmt 6
}

switch myvar {
case .one, .two, // three
.four:
dostuff()
default: ()
}

""",
"""
let a = myfun(
123 // Cmt 7
)
let a = myfun(
var1: 123 // Cmt 7
)

guard condition else {
return // Cmt 6
}

switch myvar {
case .one, .two, // three
.four:
dostuff()
default: ()
}

"""
),
(
"""
let a = 123 + // comment
b + c

let d = 123
// Trailing Comment
""",
"""
let a =
123 // comment
+ b + c

let d = 123
// Trailing Comment

"""
),
]

var config = Configuration.forTesting
config.spacesBeforeEndOfLineComments = 3
for (input, expected) in pairs {
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45, configuration: config)
}
}

func testContainerLineComments() {
let input =
"""
Expand Down Expand Up @@ -274,6 +421,82 @@ final class CommentTests: PrettyPrintTestCase {
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
}

func testContainerLineCommentsWithCustomLeadingSpaces() {
let input =
"""
// Array comment
let a = [456, // small comment
789]

// Dictionary comment
let b = ["abc": 456, // small comment
"def": 789]

// Trailing comment
let c = [123, 456 // small comment
]

// Multiline comment
let d = [123,
// comment line 1
// comment line 2
456
]

/* Array comment */
let a = [456, /* small comment */
789]

/* Dictionary comment */
let b = ["abc": 456, /* small comment */
"def": 789]
"""

let expected =
"""
// Array comment
let a = [
456, // small comment
789,
]

// Dictionary comment
let b = [
"abc": 456, // small comment
"def": 789,
]

// Trailing comment
let c = [
123, 456, // small comment
]

// Multiline comment
let d = [
123,
// comment line 1
// comment line 2
456,
]

/* Array comment */
let a = [
456, /* small comment */
789,
]

/* Dictionary comment */
let b = [
"abc": 456, /* small comment */
"def": 789,
]

"""
var config = Configuration.forTesting
config.spacesBeforeEndOfLineComments = 1
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80, configuration: config)
}

func testDocumentationBlockComments() {
let input =
"""
Expand Down