-
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
Conversation
- After the colon in... - ...a type annotation (`x: Int`) - ...a tuple type (`(a: A, b: B) -> Void`) - ...a tuple expression element (`(x: 10, y: 20)`) - ...a dictionary type (`[Key: Value]`) - ...a dictionary element (`["key": "value"]`) - ...a function call argument (`foo(x: 10, y: 20)`) - ...a function declaration parameter (`func foo(x: Int)`) - ...a generic parameter (`struct A<T: P> {}`) - ...an availability attribute (`@attribute(renamed: "foo")`) - Between the first and second name of a parameter in a function declaration (`func foo(firstName secondName: Type)`) - After the specifier in an attributed type (`inout Foo`) We still allow wrapping in all of these locations, but only if the formatter determines that it is necessary to make the contents fit.
cc @dylansturg |
@@ -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) |
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 of foo: 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 an AttributedTypeSyntax
, and we add a break after the specifier
(which might be inout
, __shared
, etc.).
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
This break had a size
of 1 before, to create a space. Do we still want that space when it doesn't fire?
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 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 .continue
.
Reverted `ignoresDiscretionary` breaks include after the colon in... - a dictionary type - a dictionary element - a function call argument - a function decl parameter - a generic parameter - a tuple type - a tuple expr element
Several breaks were changed to disallow discretionary newlines in swiftlang#143. In practice, some of these breaks actually require a more nuanced approach because grouping around the content after the `:` is frequently subjective. Depending on the length of the label/key before the `:` and the actual expr or type, it varies whether a group should be around the content after the `:`. The formatter's current model doesn't support modifying the tokens to add/remove group tokens once the token stream is created. In a future patch, it would be nice to have a way to switch between grouping and not grouping the content after the `:` based on the preceding key or label. Reverted `ignoresDiscretionary` breaks include after the colon in... - a dictionary type - a dictionary element - a function call argument - a function decl parameter - a generic parameter - a tuple type - a tuple expr element
Several breaks were changed to disallow discretionary newlines in swiftlang#143. In practice, some of these breaks actually require a more nuanced approach because grouping around the content after the `:` is frequently subjective. Depending on the length of the label/key before the `:` and the actual expr or type, it varies whether a group should be around the content after the `:`. The formatter's current model doesn't support modifying the tokens to add/remove group tokens once the token stream is created. In a future patch, it would be nice to have a way to switch between grouping and not grouping the content after the `:` based on the preceding key or label. Reverted `ignoresDiscretionary` breaks include after the colon in... - a dictionary type - a dictionary element - a function call argument - a function decl parameter - a generic parameter - a tuple type - a tuple expr element
Several breaks were changed to disallow discretionary newlines in swiftlang#143. In practice, some of these breaks actually require a more nuanced approach because grouping around the content after the `:` is frequently subjective. Depending on the length of the label/key before the `:` and the actual expr or type, it varies whether a group should be around the content after the `:`. The formatter's current model doesn't support modifying the tokens to add/remove group tokens once the token stream is created. In a future patch, it would be nice to have a way to switch between grouping and not grouping the content after the `:` based on the preceding key or label. Reverted `ignoresDiscretionary` breaks include after the colon in... - a dictionary type - a dictionary element - a function call argument - a function decl parameter - a generic parameter - a tuple type - a tuple expr element
…iscretionary Restore some discretionary newlines from PR #143
Bumps [lodash](https://github.com/lodash/lodash) from 4.17.15 to 4.17.19. - [Release notes](https://github.com/lodash/lodash/releases) - [Commits](lodash/lodash@4.17.15...4.17.19) Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Mattt <[email protected]>
x: Int
)(a: A, b: B) -> Void
)(x: 10, y: 20)
)[Key: Value]
)["key": "value"]
)foo(x: 10, y: 20)
)func foo(x: Int)
)struct A<T: P> {}
)@attribute(renamed: "foo")
)declaration (
func foo(firstName secondName: Type)
)inout Foo
)We still allow wrapping in all of these locations, but only if the
formatter determines that it is necessary to make the contents fit.