-
Notifications
You must be signed in to change notification settings - Fork 248
Ignore discretionary newlines in a number of new locations. #143
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 all commits
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 |
---|---|---|
|
@@ -510,7 +510,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor { | |
after(node.inKeyword, tokens: .space) | ||
|
||
if let typeAnnotation = node.typeAnnotation { | ||
after(typeAnnotation.colon, tokens: .break(.open(kind: .continuation))) | ||
after( | ||
typeAnnotation.colon, | ||
tokens: .break(.open(kind: .continuation), newlines: .elective(ignoresDiscretionary: true))) | ||
after(typeAnnotation.lastToken, tokens: .break(.close(mustBreak: false), size: 0)) | ||
} | ||
|
||
|
@@ -695,7 +697,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor { | |
/// - Parameter node: The tuple expression element to be arranged. | ||
private func arrangeAsTupleExprElement(_ node: TupleExprElementSyntax) { | ||
before(node.firstToken, tokens: .open) | ||
after(node.colon, tokens: .break) | ||
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true))) | ||
after(node.lastToken, tokens: .close) | ||
} | ||
|
||
|
@@ -756,13 +758,13 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor { | |
} | ||
|
||
override func visit(_ node: DictionaryTypeSyntax) -> SyntaxVisitorContinueKind { | ||
after(node.colon, tokens: .break) | ||
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true))) | ||
return .visitChildren | ||
} | ||
|
||
override func visit(_ node: DictionaryElementSyntax) -> SyntaxVisitorContinueKind { | ||
before(node.firstToken, tokens: .open) | ||
after(node.colon, tokens: .break) | ||
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true))) | ||
after(node.lastToken, tokens: .close) | ||
return .visitChildren | ||
} | ||
|
@@ -855,7 +857,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor { | |
// If we have an open delimiter following the colon, use a space instead of a continuation | ||
// break so that we don't awkwardly shift the delimiter down and indent it further if it | ||
// wraps. | ||
let tokenAfterColon: Token = startsWithOpenDelimiter(Syntax(node.expression)) ? .space : .break | ||
let tokenAfterColon: Token = | ||
startsWithOpenDelimiter(Syntax(node.expression)) | ||
? .space | ||
: .break(.continue, newlines: .elective(ignoresDiscretionary: true)) | ||
|
||
after(node.colon, tokens: tokenAfterColon) | ||
|
||
if let trailingComma = node.trailingComma { | ||
|
@@ -1028,8 +1034,10 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor { | |
override func visit(_ node: FunctionParameterSyntax) -> SyntaxVisitorContinueKind { | ||
before(node.firstToken, tokens: .open) | ||
arrangeAttributeList(node.attributes) | ||
after(node.colon, tokens: .break) | ||
before(node.secondName, tokens: .break) | ||
before( | ||
node.secondName, | ||
tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true))) | ||
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true))) | ||
|
||
if let trailingComma = node.trailingComma { | ||
after(trailingComma, tokens: .close, .break(.same)) | ||
|
@@ -1240,9 +1248,10 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor { | |
|
||
override func visit(_ node: TupleTypeElementSyntax) -> SyntaxVisitorContinueKind { | ||
before(node.firstToken, tokens: .open) | ||
after(node.colon, tokens: .break) | ||
after(node.inOut, tokens: .break) | ||
before(node.secondName, tokens: .break) | ||
before( | ||
node.secondName, | ||
tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true))) | ||
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true))) | ||
|
||
if let trailingComma = node.trailingComma { | ||
after(trailingComma, tokens: .close, .break(.same)) | ||
|
@@ -1303,7 +1312,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor { | |
|
||
override func visit(_ node: AvailabilityLabeledArgumentSyntax) -> SyntaxVisitorContinueKind { | ||
before(node.label, tokens: .open) | ||
after(node.colon, tokens: .break(.continue, size: 1)) | ||
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true))) | ||
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. This break had a 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. The default size for a break is 1 (https://github.com/apple/swift-format/blob/master/Sources/SwiftFormatPrettyPrint/Token.swift#L210), so I was cleaning up the unnecessary specification while editing here. We probably ought to do that throughout at some point. I also think we ought to make the break kind be explicit instead of defaulting to |
||
after(node.value.lastToken, tokens: .close) | ||
return .visitChildren | ||
} | ||
|
@@ -1578,7 +1587,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor { | |
var closeAfterToken: TokenSyntax? = nil | ||
|
||
if let typeAnnotation = node.typeAnnotation { | ||
after(typeAnnotation.colon, tokens: .break(.open(kind: .continuation))) | ||
after( | ||
typeAnnotation.colon, | ||
tokens: .break(.open(kind: .continuation), newlines: .elective(ignoresDiscretionary: true))) | ||
closesNeeded += 1 | ||
closeAfterToken = typeAnnotation.lastToken | ||
} | ||
|
@@ -1664,7 +1675,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor { | |
|
||
override func visit(_ node: AttributedTypeSyntax) -> SyntaxVisitorContinueKind { | ||
arrangeAttributeList(node.attributes) | ||
after(node.specifier, tokens: .break) | ||
after( | ||
node.specifier, | ||
tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true))) | ||
return .visitChildren | ||
} | ||
|
||
|
@@ -1751,7 +1764,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor { | |
|
||
override func visit(_ node: GenericParameterSyntax) -> SyntaxVisitorContinueKind { | ||
before(node.firstToken, tokens: .open) | ||
after(node.colon, tokens: .break) | ||
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true))) | ||
if let trailingComma = node.trailingComma { | ||
after(trailingComma, tokens: .close, .break(.same)) | ||
} else { | ||
|
@@ -1958,7 +1971,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor { | |
after(node.letOrVarKeyword, tokens: .break) | ||
|
||
if let typeAnnotation = node.typeAnnotation { | ||
after(typeAnnotation.colon, tokens: .break(.open(kind: .continuation))) | ||
after( | ||
typeAnnotation.colon, | ||
tokens: .break(.open(kind: .continuation), newlines: .elective(ignoresDiscretionary: true))) | ||
after(typeAnnotation.lastToken, tokens: .break(.close(mustBreak: false), size: 0)) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this break after the
inOut
token?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
inOut
token here is legacy, I believe, from when the attribute used to come before the variable name instead of the type (e.g.,inout foo: Bar
instead offoo: inout Bar
). The parser doesn't accept that syntax anymore (I tried it in the syntax explorer), so I don't think this will ever be non-nil. When using the new syntax (foo: inout Bar
), the type comes across as anAttributedTypeSyntax
, and we add a break after thespecifier
(which might beinout
,__shared
, etc.).