Skip to content

Fix wrong formatting of multiline string literal #1623

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

Conversation

kimdv
Copy link
Contributor

@kimdv kimdv commented May 1, 2023

Fixes #1603

I tried you comment here: #1616 (comment)

But it didn't work for me.
What I figured out what because if it have an open """ but not a closing """ then the formatter will only format on the closing one.
It sounds to me that works as intended. Only visit the tokens we fix.

@kimdv kimdv requested a review from ahoppen as a code owner May 1, 2023 15:15
@kimdv kimdv force-pushed the kimdv/fix-newline-for-multiline-string-literal branch 2 times, most recently from e248934 to e49abe0 Compare May 1, 2023 15:16
@kimdv kimdv force-pushed the kimdv/fix-newline-for-multiline-string-literal branch from e49abe0 to 06e701c Compare May 2, 2023 08:23
@kimdv kimdv force-pushed the kimdv/fix-newline-for-multiline-string-literal branch from 06e701c to e021732 Compare May 2, 2023 19:25
Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure if it changes anything test-wise but since you are adding a trailing newline, that should also be reflected in previousTokenWillEndWithWhitespace. I.e. the return there should be replaced by the following (slightly refactored to improve readability.

      if previousToken.trailingTrivia.endsWithWhitespace {
        return true
      }
      if isMutable(previousToken) && (requiresWhitespace(between: previousToken, and: token) || requiresTrailingNewline(previousToken)) {
        return true
      }
      return false

@kimdv kimdv force-pushed the kimdv/fix-newline-for-multiline-string-literal branch 3 times, most recently from e375328 to d84ceb3 Compare May 3, 2023 14:24
@kimdv kimdv changed the title Fix wrong foratting of multiline string literal Fix wrong formatting of multiline string literal May 3, 2023
Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. This ended up being a pretty small change after all 😆

Unrelated to this PR, I think there’s still an error where we don’t insert the newline after the opening """:

assertBuildResult(
  StringLiteralExprSyntax(openQuote: .multilineStringQuoteToken(), content: "a", closeQuote: .multilineStringQuoteToken()),
  #"""
  """
  a
  """
  """#
)

Would you like to look into that as well? If you don’t want to look at it straight away, I will file an issue for it.

@ahoppen
Copy link
Member

ahoppen commented May 3, 2023

@swift-ci Please test

@kimdv kimdv force-pushed the kimdv/fix-newline-for-multiline-string-literal branch from d84ceb3 to 74aa602 Compare May 4, 2023 04:59
@kimdv
Copy link
Contributor Author

kimdv commented May 4, 2023

@swift-ci please test

@kimdv
Copy link
Contributor Author

kimdv commented May 4, 2023

@swift-ci please test linux

@kimdv
Copy link
Contributor Author

kimdv commented May 4, 2023

@swift-ci please test Windows

@kimdv kimdv force-pushed the kimdv/fix-newline-for-multiline-string-literal branch from c51b0d6 to fc2e999 Compare May 7, 2023 17:35
@kimdv
Copy link
Contributor Author

kimdv commented May 7, 2023

@swift-ci please test

@kimdv kimdv requested a review from ahoppen May 7, 2023 18:45
@kimdv
Copy link
Contributor Author

kimdv commented May 8, 2023

@swift-ci please test

@kimdv
Copy link
Contributor Author

kimdv commented May 8, 2023

I don't understand why it says I haven't generated files.
When running locally there is nothing new ? 🤨

@kimdv kimdv force-pushed the kimdv/fix-newline-for-multiline-string-literal branch from fc2e999 to 3fccbe7 Compare May 8, 2023 17:30
@kimdv
Copy link
Contributor Author

kimdv commented May 8, 2023

I don't understand why it says I haven't generated files.
When running locally there is nothing new ? 🤨

Found the reason.
The reason that is that I had build and run code generation before. It did not look for a never version.
Looks a bit like a bug to me.

Tried to resolve and update but got those errors

kimdevos@dk-kimdevos12-8752 CodeGeneration % swift package resolve
Updating /Users/kimdevos/repository/kimdv/swift-src/swift-syntax
Updated /Users/kimdevos/repository/kimdv/swift-src/swift-syntax (0.47s)
error: could not find a branch named ‘HEAD’ in /Users/kimdevos/repository/kimdv/swift-src/swift-syntax
kimdevos@dk-kimdevos12-8752 CodeGeneration % swift package update 
Updating /Users/kimdevos/repository/kimdv/swift-src/swift-syntax
Updated /Users/kimdevos/repository/kimdv/swift-src/swift-syntax (0.17s)
error: could not find a branch named ‘HEAD’ in /Users/kimdevos/repository/kimdv/swift-src/swift-syntax

I deleted the .build and re-run the swift package resolve and it worked fine.

Looks a bit like a bug in SPM?

@kimdv
Copy link
Contributor Author

kimdv commented May 8, 2023

@swift-ci please test

@kimdv
Copy link
Contributor Author

kimdv commented May 8, 2023

@swift-ci please test windows

@ahoppen
Copy link
Member

ahoppen commented May 8, 2023

Found the reason. The reason that is that I had build and run code generation before. It did not look for a never version. Looks a bit like a bug to me.

I think that’s intentional. Unless you run swift package update, it won’t update your package version. Could you add a comment to our docs where we suggest swift run --package-path CodeGeneration generate-swiftsyntax Sources to run swift package update beforehand?

I deleted the .build and re-run the swift package resolve and it worked fine.

Looks a bit like a bug in SPM?

I was able to reproduce this as well but then couldn’t reproduce it again by creating a new commit and running swift package update. If you’ve got steps to reliably reproduce this, could you file an issue for SwiftPM?

@kimdv kimdv force-pushed the kimdv/fix-newline-for-multiline-string-literal branch 2 times, most recently from 124adcc to 6fb0cb9 Compare May 10, 2023 13:52
@kimdv kimdv force-pushed the kimdv/fix-newline-for-multiline-string-literal branch 5 times, most recently from 758daa8 to 08fecd9 Compare May 17, 2023 11:54
@kimdv kimdv requested a review from ahoppen May 17, 2023 12:34
@kimdv
Copy link
Contributor Author

kimdv commented May 17, 2023

@swift-ci please test

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wohooo. Nice. This looks really good now. I only have a few remarks mostly about naming and documentation.

}
if let ancestorsParent = ancestor.parent, childrenSeparatedByNewline(ancestorsParent) {
} else if let second {
if second.requiresLeadingNewline {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely not part of this PR, but eventually I would like to remove requiresLeadingNewline from CodeGen altogether and just list the tokens that require a leading newline in this function. Same for childrenSeparatedByNewline. I don’t think we necessarily need to CodeGen it, it could just be implemented manually in this file.

@kimdv kimdv force-pushed the kimdv/fix-newline-for-multiline-string-literal branch 2 times, most recently from 8b61a40 to 2521ba5 Compare May 18, 2023 08:12
@kimdv kimdv force-pushed the kimdv/fix-newline-for-multiline-string-literal branch from 2521ba5 to ac5dcb4 Compare May 18, 2023 12:20
@kimdv
Copy link
Contributor Author

kimdv commented May 18, 2023

@swift-ci please test

@kimdv
Copy link
Contributor Author

kimdv commented May 18, 2023

@swift-ci please test windows

@kimdv kimdv enabled auto-merge May 18, 2023 12:24
@kimdv kimdv disabled auto-merge May 18, 2023 13:26
@kimdv kimdv force-pushed the kimdv/fix-newline-for-multiline-string-literal branch from ac5dcb4 to 6f41221 Compare May 19, 2023 07:15
@kimdv
Copy link
Contributor Author

kimdv commented May 19, 2023

@swift-ci please test

@kimdv
Copy link
Contributor Author

kimdv commented May 19, 2023

@swift-ci please test windows

@kimdv kimdv merged commit 0711d21 into swiftlang:main May 19, 2023
@kimdv kimdv deleted the kimdv/fix-newline-for-multiline-string-literal branch May 19, 2023 13:07
kimdv added a commit to kimdv/swift-syntax that referenced this pull request May 20, 2023
kimdv added a commit to kimdv/swift-syntax that referenced this pull request May 23, 2023
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.

""" should be inserted on a newline
2 participants