Skip to content

Commit c00a944

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 c00a944

File tree

5 files changed

+211
-1
lines changed

5 files changed

+211
-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+
* `spacesBeforeLineComments` _(number)_: The number of spaces between the last
35+
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.spacesBeforeLineComments = 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 spacesBeforeLineComments
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 spacesBeforeLineComments: 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.spacesBeforeLineComments =
233+
try container.decodeIfPresent(Int.self, forKey: .spacesBeforeLineComments)
234+
?? defaults.spacesBeforeLineComments
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(spacesBeforeLineComments, forKey: .spacesBeforeLineComments)
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.spacesBeforeLineComments, 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: 198 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,127 @@ final class CommentTests: PrettyPrintTestCase {
199200
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
200201
}
201202

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

399+
func testContainerLineCommentsWithCustomLeadingSpaces() {
400+
let input =
401+
"""
402+
// Array comment
403+
let a = [456, // small comment
404+
789]
405+
406+
// Dictionary comment
407+
let b = ["abc": 456, // small comment
408+
"def": 789]
409+
410+
// Trailing comment
411+
let c = [123, 456 // small comment
412+
]
413+
414+
// Multiline comment
415+
let d = [123,
416+
// comment line 1
417+
// comment line 2
418+
456
419+
]
420+
421+
/* Array comment */
422+
let a = [456, /* small comment */
423+
789]
424+
425+
/* Dictionary comment */
426+
let b = ["abc": 456, /* small comment */
427+
"def": 789]
428+
"""
429+
430+
let expected =
431+
"""
432+
// Array comment
433+
let a = [
434+
456, // small comment
435+
789,
436+
]
437+
438+
// Dictionary comment
439+
let b = [
440+
"abc": 456, // small comment
441+
"def": 789,
442+
]
443+
444+
// Trailing comment
445+
let c = [
446+
123, 456, // small comment
447+
]
448+
449+
// Multiline comment
450+
let d = [
451+
123,
452+
// comment line 1
453+
// comment line 2
454+
456,
455+
]
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+
"""
470+
var config = Configuration.forTesting
471+
config.spacesBeforeLineComments = 1
472+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80, configuration: config)
473+
}
474+
277475
func testDocumentationBlockComments() {
278476
let input =
279477
"""

0 commit comments

Comments
 (0)