Skip to content

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

Merged
merged 1 commit into from
Feb 10, 2020

Conversation

allevato
Copy link
Member

@allevato allevato commented Feb 8, 2020

  • 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.

- 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.
@allevato
Copy link
Member Author

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)
Copy link
Contributor

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?

Copy link
Member Author

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)))
Copy link
Contributor

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?

Copy link
Member Author

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.

@allevato allevato merged commit 0266e97 into swiftlang:master Feb 10, 2020
@allevato allevato deleted the no-discretionary-colons branch February 10, 2020 20:08
dylansturg added a commit to dylansturg/swift-format that referenced this pull request Mar 9, 2020
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
dylansturg added a commit to dylansturg/swift-format that referenced this pull request Mar 9, 2020
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
dylansturg added a commit to dylansturg/swift-format that referenced this pull request Mar 9, 2020
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
dylansturg added a commit to dylansturg/swift-format that referenced this pull request Mar 9, 2020
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
allevato added a commit that referenced this pull request Mar 10, 2020
…iscretionary

Restore some discretionary newlines from PR #143
aaditya-chandrasekhar pushed a commit to val-verde/swift-format that referenced this pull request May 20, 2021
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants