Skip to content

Commit 4a99f1c

Browse files
committed
Add option to customize number of spaces leading // comments
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.
1 parent ae1ef61 commit 4a99f1c

File tree

5 files changed

+236
-1
lines changed

5 files changed

+236
-1
lines changed

Documentation/Configuration.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ top-level keys and values:
3131
lines that are allowed to be present in a source file. Any number larger
3232
than this will be collapsed down to the maximum.
3333

34+
* `spacesBeforeEndOfLineComments` _(number)_: The number of spaces between
35+
the last token on a non-empty line and a line comment starting with `//`.
36+
3437
* `respectsExistingLineBreaks` _(boolean)_: Indicates whether or not existing
3538
line breaks in the source code should be honored (if they are valid
3639
according to the style guidelines being enforced). If this settings is

Sources/SwiftFormat/API/Configuration+Default.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extension Configuration {
2626
self.lineLength = 100
2727
self.tabWidth = 8
2828
self.indentation = .spaces(2)
29+
self.spacesBeforeEndOfLineComments = 2
2930
self.respectsExistingLineBreaks = true
3031
self.lineBreakBeforeControlFlowKeywords = false
3132
self.lineBreakBeforeEachArgument = false

Sources/SwiftFormat/API/Configuration.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public struct Configuration: Codable, Equatable {
2828
case version
2929
case maximumBlankLines
3030
case lineLength
31+
case spacesBeforeEndOfLineComments
3132
case tabWidth
3233
case indentation
3334
case respectsExistingLineBreaks
@@ -66,6 +67,9 @@ public struct Configuration: Codable, Equatable {
6667
/// The maximum length of a line of source code, after which the formatter will break lines.
6768
public var lineLength: Int
6869

70+
// Number of spaces that preceeds line comments.
71+
public var spacesBeforeEndOfLineComments: Int
72+
6973
/// The width of the horizontal tab in spaces.
7074
///
7175
/// This value is used when converting indentation types (for example, from tabs into spaces).
@@ -225,6 +229,9 @@ public struct Configuration: Codable, Equatable {
225229
self.lineLength =
226230
try container.decodeIfPresent(Int.self, forKey: .lineLength)
227231
?? defaults.lineLength
232+
self.spacesBeforeEndOfLineComments =
233+
try container.decodeIfPresent(Int.self, forKey: .spacesBeforeEndOfLineComments)
234+
?? defaults.spacesBeforeEndOfLineComments
228235
self.tabWidth =
229236
try container.decodeIfPresent(Int.self, forKey: .tabWidth)
230237
?? defaults.tabWidth
@@ -288,6 +295,7 @@ public struct Configuration: Codable, Equatable {
288295
try container.encode(version, forKey: .version)
289296
try container.encode(maximumBlankLines, forKey: .maximumBlankLines)
290297
try container.encode(lineLength, forKey: .lineLength)
298+
try container.encode(spacesBeforeEndOfLineComments, forKey: .spacesBeforeEndOfLineComments)
291299
try container.encode(tabWidth, forKey: .tabWidth)
292300
try container.encode(indentation, forKey: .indentation)
293301
try container.encode(respectsExistingLineBreaks, forKey: .respectsExistingLineBreaks)

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3171,7 +3171,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
31713171
return (
31723172
true,
31733173
[
3174-
.space(size: 2, flexible: true),
3174+
.space(size: config.spacesBeforeEndOfLineComments, flexible: true),
31753175
.comment(Comment(kind: .line, text: text), wasEndOfLine: true),
31763176
// There must be a break with a soft newline after the comment, but it's impossible to
31773177
// know which kind of break must be used. Adding this newline is deferred until the

Tests/SwiftFormatTests/PrettyPrint/CommentTests.swift

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import _SwiftFormatTestSupport
2+
import SwiftFormat
23

34
final class CommentTests: PrettyPrintTestCase {
45
func testDocumentationComments() {
@@ -199,6 +200,152 @@ final class CommentTests: PrettyPrintTestCase {
199200
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
200201
}
201202

203+
func testLineCommentsWithCustomLeadingSpaces() {
204+
let pairs: [(String, String)] = [
205+
(
206+
"""
207+
// Line Comment0
208+
209+
// Line Comment1
210+
// Line Comment2
211+
let a = 123
212+
let b = "456" // End of line comment
213+
let c = "More content"
214+
215+
""",
216+
"""
217+
// Line Comment0
218+
219+
// Line Comment1
220+
// Line Comment2
221+
let a = 123
222+
let b = "456" // End of line comment
223+
let c = "More content"
224+
225+
"""
226+
),
227+
(
228+
"""
229+
// Comment 3
230+
// Comment 4
231+
232+
let reallyLongVariableName = 123 // This comment should not wrap
233+
// and should not combine with this comment
234+
235+
func MyFun() {
236+
// just a comment
237+
}
238+
""",
239+
"""
240+
// Comment 3
241+
// Comment 4
242+
243+
let reallyLongVariableName = 123 // This comment should not wrap
244+
// and should not combine with this comment
245+
246+
func MyFun() {
247+
// just a comment
248+
}
249+
250+
"""
251+
),
252+
(
253+
"""
254+
func MyFun() {
255+
// Comment 1
256+
// Comment 2
257+
let a = 123
258+
259+
let b = 456 // Comment 3
260+
}
261+
262+
func MyFun() {
263+
let c = 789 // Comment 4
264+
// Comment 5
265+
}
266+
""",
267+
"""
268+
func MyFun() {
269+
// Comment 1
270+
// Comment 2
271+
let a = 123
272+
273+
let b = 456 // Comment 3
274+
}
275+
276+
func MyFun() {
277+
let c = 789 // Comment 4
278+
// Comment 5
279+
}
280+
281+
"""
282+
),
283+
(
284+
"""
285+
let a = myfun(123 // Cmt 7
286+
)
287+
let a = myfun(var1: 123 // Cmt 7
288+
)
289+
290+
guard condition else { return // Cmt 6
291+
}
292+
293+
switch myvar {
294+
case .one, .two, // three
295+
.four:
296+
dostuff()
297+
default: ()
298+
}
299+
300+
""",
301+
"""
302+
let a = myfun(
303+
123 // Cmt 7
304+
)
305+
let a = myfun(
306+
var1: 123 // Cmt 7
307+
)
308+
309+
guard condition else {
310+
return // Cmt 6
311+
}
312+
313+
switch myvar {
314+
case .one, .two, // three
315+
.four:
316+
dostuff()
317+
default: ()
318+
}
319+
320+
"""
321+
),
322+
(
323+
"""
324+
let a = 123 + // comment
325+
b + c
326+
327+
let d = 123
328+
// Trailing Comment
329+
""",
330+
"""
331+
let a =
332+
123 // comment
333+
+ b + c
334+
335+
let d = 123
336+
// Trailing Comment
337+
338+
"""
339+
),
340+
]
341+
342+
var config = Configuration.forTesting
343+
config.spacesBeforeEndOfLineComments = 3
344+
for (input, expected) in pairs {
345+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45, configuration: config)
346+
}
347+
}
348+
202349
func testContainerLineComments() {
203350
let input =
204351
"""
@@ -274,6 +421,82 @@ final class CommentTests: PrettyPrintTestCase {
274421
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
275422
}
276423

424+
func testContainerLineCommentsWithCustomLeadingSpaces() {
425+
let input =
426+
"""
427+
// Array comment
428+
let a = [456, // small comment
429+
789]
430+
431+
// Dictionary comment
432+
let b = ["abc": 456, // small comment
433+
"def": 789]
434+
435+
// Trailing comment
436+
let c = [123, 456 // small comment
437+
]
438+
439+
// Multiline comment
440+
let d = [123,
441+
// comment line 1
442+
// comment line 2
443+
456
444+
]
445+
446+
/* Array comment */
447+
let a = [456, /* small comment */
448+
789]
449+
450+
/* Dictionary comment */
451+
let b = ["abc": 456, /* small comment */
452+
"def": 789]
453+
"""
454+
455+
let expected =
456+
"""
457+
// Array comment
458+
let a = [
459+
456, // small comment
460+
789,
461+
]
462+
463+
// Dictionary comment
464+
let b = [
465+
"abc": 456, // small comment
466+
"def": 789,
467+
]
468+
469+
// Trailing comment
470+
let c = [
471+
123, 456, // small comment
472+
]
473+
474+
// Multiline comment
475+
let d = [
476+
123,
477+
// comment line 1
478+
// comment line 2
479+
456,
480+
]
481+
482+
/* Array comment */
483+
let a = [
484+
456, /* small comment */
485+
789,
486+
]
487+
488+
/* Dictionary comment */
489+
let b = [
490+
"abc": 456, /* small comment */
491+
"def": 789,
492+
]
493+
494+
"""
495+
var config = Configuration.forTesting
496+
config.spacesBeforeEndOfLineComments = 1
497+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80, configuration: config)
498+
}
499+
277500
func testDocumentationBlockComments() {
278501
let input =
279502
"""

0 commit comments

Comments
 (0)