-
Notifications
You must be signed in to change notification settings - Fork 254
Add option to customize number of spaces leading // comments
#776
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import _SwiftFormatTestSupport | ||
| import SwiftFormat | ||
|
|
||
| final class CommentTests: PrettyPrintTestCase { | ||
| func testDocumentationComments() { | ||
|
|
@@ -199,6 +200,152 @@ final class CommentTests: PrettyPrintTestCase { | |
| assertPrettyPrintEqual(input: input, expected: expected, linelength: 45) | ||
| } | ||
|
|
||
| func testLineCommentsWithCustomLeadingSpaces() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. split!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = | ||
| """ | ||
|
|
@@ -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 = | ||
| """ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.