Skip to content

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

Conversation

AppAppWorks
Copy link
Contributor

@AppAppWorks AppAppWorks commented Jul 3, 2024

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 FormatRawStringLiteral when performing this conversion.

@AppAppWorks AppAppWorks requested a review from ahoppen as a code owner July 3, 2024 21:14
@AppAppWorks AppAppWorks marked this pull request as draft July 3, 2024 21:14
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.

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.

}

fileprivate extension ExprListSyntax {
func preflight() -> (componentsOnly: [ExprListSyntax.Element], commonPounds: TokenSyntax?)? {
Copy link
Member

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
Copy link
Member

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.

Copy link
Contributor Author

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?

uri: [
TextEdit(
range: positions["1️⃣"]..<positions["5️⃣"],
newText: "##\"[\\##(key): \\##(d) \\##(value)]\"##"
Copy link
Member

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

@AppAppWorks AppAppWorks force-pushed the convert-string-concatenation-to-string-interpolation branch from 56081b7 to 1d64ead Compare July 4, 2024 09:09
@AppAppWorks
Copy link
Contributor Author

@ahoppen please check again :)

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.

Very nice

@AppAppWorks
Copy link
Contributor Author

Could a follow-up action be converting concatenation of (some CustomStringConvertible).description into string interpolation?

e.g. 1.description + false.description -> "\(1)\(false)"

@AppAppWorks AppAppWorks force-pushed the convert-string-concatenation-to-string-interpolation branch 2 times, most recently from 37f0d8b to babf856 Compare July 8, 2024 20:55
@AppAppWorks AppAppWorks marked this pull request as ready for review July 9, 2024 23:39
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.

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.

@ahoppen
Copy link
Member

ahoppen commented Jul 16, 2024

Could a follow-up action be converting concatenation of (some CustomStringConvertible).description into string interpolation?

e.g. 1.description + false.description -> "\(1)\(false)"

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.

@AppAppWorks AppAppWorks force-pushed the convert-string-concatenation-to-string-interpolation branch from e0ce0ba to 50bb915 Compare July 16, 2024 21:59
@AppAppWorks
Copy link
Contributor Author

All done, please have a look.

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 catch with the multi-line comments.

@AppAppWorks AppAppWorks force-pushed the convert-string-concatenation-to-string-interpolation branch from 50bb915 to b19e0a9 Compare July 16, 2024 22:59
@AppAppWorks
Copy link
Contributor Author

Revised, please check again!

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.

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.

@ahoppen
Copy link
Member

ahoppen commented Jul 17, 2024

@swift-ci Please test

@ahoppen ahoppen enabled auto-merge July 17, 2024 17:57
@ahoppen
Copy link
Member

ahoppen commented Jul 17, 2024

@swift-ci Please test Windows

@@ -31,6 +31,7 @@ target_sources(SourceKitLSP PRIVATE
Swift/CodeActions/AddDocumentation.swift
Swift/CodeActions/ConvertIntegerLiteral.swift
Swift/CodeActions/ConvertJSONToCodableStruct.swift
Swift/CodeActions/ConvertStringConcatenationToStringInterpolation.swift,
Copy link
Member

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.

Copy link
Contributor Author

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`
auto-merge was automatically disabled July 17, 2024 21:57

Head branch was pushed to by a user without write access

@AppAppWorks AppAppWorks force-pushed the convert-string-concatenation-to-string-interpolation branch from b19e0a9 to 17e33a6 Compare July 17, 2024 21:57
@ahoppen
Copy link
Member

ahoppen commented Jul 17, 2024

@swift-ci Please test

@ahoppen ahoppen enabled auto-merge July 17, 2024 22:13
@ahoppen
Copy link
Member

ahoppen commented Jul 17, 2024

@swift-ci Please test Windows

@ahoppen ahoppen merged commit e4cb1bd into swiftlang:main Jul 18, 2024
3 checks passed
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.

Code action to replace string concatenation with string interpolation
2 participants