Skip to content

Handle new swift-syntax closure expansion behavior #1831

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
Dec 6, 2024

Conversation

woolsweater
Copy link
Contributor

This resolves #1788, following the discussion of alternatives on https://github.com/swiftlang/sourcekit-lsp/pulls/1789. The bulk of the change updates the translation from SourceKit placeholders to LSP placeholders to handle nesting, which is implemented in swiftlang/swift-syntax#2897

closure-expansion.mov

@woolsweater woolsweater force-pushed the the-trail-is-closed branch 2 times, most recently from 319f8bf to 31fa3e9 Compare November 17, 2024 01:30
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 @woolsweater, great changes!

I only have a few comments, otherwise looks great.

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. I just have two minor comments, otherwise this looks great.

return false
}

return token.keyPathInParent == \ClosureSignatureSyntax.inKeyword
Copy link
Member

Choose a reason for hiding this comment

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

I would move the check for token.keyPathInParent == \ClosureSignatureSyntax.inKeyword to the top of the function because it’s an O(1) check vs the ancestorOrSelf check, which needs to traverse the tree up to the root to determine that a token is not in a ClosureExprSyntax.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point; done!

import SwiftParser
import SwiftSyntax
import Swift
@testable import SourceKitLSP
Copy link
Member

Choose a reason for hiding this comment

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

We build SourceKit-LSP with testability disabled in Swift CI because otherwise we would need to build everything twice: Once with testability enabled for testing and then with testability disabled to produce the binary we include in the toolchain. Could you change this to not use @testable? We use @_spi(Testing) to a similar effect where needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, updated. 🤔 I am not very familiar with how @_spi works, but it seems to require everything including the imports to become public. Let me know if there is another way I should have done this.

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. A small comment that you couldn’t have been aware of regarding the imports.

Comment on lines 13 to 14
public import SwiftSyntax
public import SwiftBasicFormat
Copy link
Member

Choose a reason for hiding this comment

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

We need to support building with a Swift 5.10 compiler as well, which doesn’t support the access level imports.

Suggested change
public import SwiftSyntax
public import SwiftBasicFormat
#if compiler(>=6.0)
public import SwiftSyntax
public import SwiftBasicFormat
#else
import SwiftSyntax
import SwiftBasicFormat
#endif

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

/// lines.
@_spi(Testing)
public class ClosureCompletionFormat: BasicFormat {
public override func requiresNewline(between first: TokenSyntax?, and second: TokenSyntax?) -> Bool {
Copy link
Member

Choose a reason for hiding this comment

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

I would add @_spi(Testing) here as well, just to be clear that it’s not a properly public declaration.

Suggested change
public override func requiresNewline(between first: TokenSyntax?, and second: TokenSyntax?) -> Bool {
@_spi(Testing) public override func requiresNewline(between first: TokenSyntax?, and second: TokenSyntax?) -> Bool {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense; done!

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 good. Thanks for implementing this and following up on my review comments.

@ahoppen
Copy link
Member

ahoppen commented Dec 4, 2024

swiftlang/swift-syntax#2897

@swift-ci Please test

//===----------------------------------------------------------------------===//

import SKTestSupport
@testable import SourceKitLSP
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shoot, I missed this other @testable import last night. Does this need to be changed as well @ahoppen ?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we can’t have @testable imports.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed!

@ahoppen
Copy link
Member

ahoppen commented Dec 4, 2024

swiftlang/swift-syntax#2897

@swift-ci Please test

@ahoppen
Copy link
Member

ahoppen commented Dec 5, 2024

swiftlang/swift-syntax#2897

@swift-ci Please test

@ahoppen
Copy link
Member

ahoppen commented Dec 5, 2024

swiftlang/swift-syntax#2897

@swift-ci Please test Windows

@woolsweater
Copy link
Contributor Author

Updated just now to appease the linter.

@ahoppen
Copy link
Member

ahoppen commented Dec 5, 2024

swiftlang/swift-syntax#2897

@swift-ci Please test

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 this file to CMakeLists.txt. We use CMake to build SourceKit-LSP on Windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

This resolves <swiftlang#1788>,
following the discussion of alternatives on
<https://github.com/swiftlang/sourcekit-lsp/pulls/1789>. The bulk of the
change updates the translation from SourceKit placeholders to LSP
placeholders to handle nesting. The `CodeCompletionSession` also passes
a new custom formatter to the swift-syntax expansion routine, which
disables the transformation to trailing closures.
@ahoppen
Copy link
Member

ahoppen commented Dec 5, 2024

swiftlang/swift-syntax#2897

@swift-ci Please test

@ahoppen
Copy link
Member

ahoppen commented Dec 5, 2024

swiftlang/swift-syntax#2897

@swift-ci Please test Windows

@ahoppen ahoppen merged commit 21dfaf0 into swiftlang:main Dec 6, 2024
3 checks passed
@woolsweater
Copy link
Contributor Author

Thank you for shepherding these changes, @ahoppen ! 🙌

ahoppen added a commit to ahoppen/sourcekit-lsp that referenced this pull request Dec 10, 2024
…is-closed"

This reverts commit 21dfaf0, reversing
changes made to f900b4e.
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.

Refine automatic rewriting of trailing closures
2 participants