-
Notifications
You must be signed in to change notification settings - Fork 320
Convert String Concatenation to String Interpolation #1551
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
Convert String Concatenation to String Interpolation #1551
Conversation
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.
Thanks for adding the refactoring @AppAppWorks.
Depending on how you want to approach it, you can add support for multi-line string literals in this PR or make the refactoring unavailable if the sequence expression contains a multi-line string literal and then think about that case in a follow-up PR.
Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift
Show resolved
Hide resolved
| } | ||
|
|
||
| fileprivate extension ExprListSyntax { | ||
| func preflight() -> (componentsOnly: [ExprListSyntax.Element], commonPounds: TokenSyntax?)? { |
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.
Could you add a doc comment about what this method does and what componentsOnly and commonPounds represent?
Also, I would make this a static function on ConvertStringConcatenationToStringInterpolation instead of an extension on ExprListSyntax. I feel like extensions on a type should be generally useful but this preflight method is very specialized to the ConvertStringConcatenationToStringInterpolation refactoring.
| var ret: StringLiteralSegmentListSyntax = [] | ||
| for component in componentsOnly { | ||
| if var stringLiteral = StringLiteralExprSyntax(component) { | ||
| stringLiteral.pounds = commonPounds |
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.
Is this doing anything? We only extract the segments from the string literal, so it shouldn’t make a difference whether we set the pounds here. Also means we could remove the pounds property in the extension below.
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.
Setting the pounds here also sets the pounds for all interpolations inside the literal, maybe I should refactor the setter of StringLiteralExprSyntax.pounds to make the intent clearer?
Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift
Outdated
Show resolved
Hide resolved
Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift
Outdated
Show resolved
Hide resolved
| uri: [ | ||
| TextEdit( | ||
| range: positions["1️⃣"]..<positions["5️⃣"], | ||
| newText: "##\"[\\##(key): \\##(d) \\##(value)]\"##" |
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.
I think this would be easier to read if you made it a multi-line string literal, because then you don’t need to escape the ".
56081b7 to
1d64ead
Compare
|
@ahoppen please check again :) |
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.
Very nice
Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift
Outdated
Show resolved
Hide resolved
Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift
Outdated
Show resolved
Hide resolved
Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift
Outdated
Show resolved
Hide resolved
Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift
Outdated
Show resolved
Hide resolved
Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift
Outdated
Show resolved
Hide resolved
Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift
Outdated
Show resolved
Hide resolved
|
Could a follow-up action be converting concatenation of e.g. |
37f0d8b to
babf856
Compare
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.
Looking great! I have a few minor comments that would make the code more readable in my opinion. After that, we’re ready to go.
Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift
Outdated
Show resolved
Hide resolved
Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift
Outdated
Show resolved
Hide resolved
Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift
Outdated
Show resolved
Hide resolved
Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift
Outdated
Show resolved
Hide resolved
I personally wouldn’t prioritize it because I don’t think it’s a pattern that’s used terribly often but I also wouldn’t object to a PR that implements it. |
e0ce0ba to
50bb915
Compare
|
All done, please have a look. |
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.
Nice catch with the multi-line comments.
Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift
Outdated
Show resolved
Hide resolved
Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift
Show resolved
Hide resolved
Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift
Outdated
Show resolved
Hide resolved
Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift
Outdated
Show resolved
Hide resolved
Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift
Outdated
Show resolved
Hide resolved
Sources/SourceKitLSP/Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift
Outdated
Show resolved
Hide resolved
50bb915 to
b19e0a9
Compare
|
Revised, please check again! |
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.
Thank you for implementing the refactoring @AppAppWorks. Looks great!
Would you mind filing an issue to also support multi-line string literals. Just so we don’t forget about that as a follow-up thing that can be done.
|
@swift-ci Please test |
|
@swift-ci Please test Windows |
Sources/SourceKitLSP/CMakeLists.txt
Outdated
| Swift/CodeActions/AddDocumentation.swift | ||
| Swift/CodeActions/ConvertIntegerLiteral.swift | ||
| Swift/CodeActions/ConvertJSONToCodableStruct.swift | ||
| Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift, |
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.
I think this the Windows build is failing because this shouldn’t have a comma.
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.
My bad, I've pushed a new commit, please test again.
added `ConvertStringConcatenationToStringInterpolation` to convert string concatenation to string interpolation: - the string concatenation must contain at least one string literal - the number of pound symbols in the resulting string interpolation is determined by the highest number of pound symbols among all string literals in the string concatenation - multiline string literals are not supported registered in `SyntaxCodeActions.allSyntaxCodeActions` registered in Sources/SourceKitLSP/CMakeLists created a test in `CodeActionTests`
Head branch was pushed to by a user without write access
b19e0a9 to
17e33a6
Compare
|
@swift-ci Please test |
|
@swift-ci Please test Windows |
fixed #1244
It's only a draft as the support for multiline string literals is not yet implemented and more tests have to be written, I also wonder if it's a good idea to also perform the action in
FormatRawStringLiteralwhen performing this conversion.