-
Notifications
You must be signed in to change notification settings - Fork 302
Report codeActions for fixIts produced by swift syntax #941
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
Report codeActions for fixIts produced by swift syntax #941
Conversation
Having an idea of how to test this is definitely not trivial, so here’s a snippet. You should then be able to write assertions to test // Create a workspace with compile commands that don't contain the file we are getting diagnostics for.
// That way we have a workspace that has a build system (and thus doesn't promote settings from the fallback build
// system to be non-fallback settings) but doesn't have build settings for the file being tested. Thus, we generate
// diagnostics from the built-in SwiftParser.
let ws = try await MultiFileTestWorkspace(files: [
"test.swift": "let var = 2",
"compile_commands.json": "[]"
])
let (uri, _) = try ws.openDocument("test.swift")
let diagnostics = try await ws.testClient.send(DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))) |
And thanks a lot for working on this @joehsieh! |
15158e3
to
8a4bc54
Compare
Package.swift
Outdated
@@ -337,7 +337,8 @@ if useLocalDependencies { | |||
.package(url: "https://github.com/apple/swift-package-manager.git", branch: relatedDependenciesBranch), | |||
.package(url: "https://github.com/apple/swift-tools-support-core.git", branch: relatedDependenciesBranch), | |||
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2"), | |||
.package(url: "https://github.com/apple/swift-syntax.git", branch: relatedDependenciesBranch), | |||
// .package(url: "https://github.com/apple/swift-syntax.git", branch: relatedDependenciesBranch), | |||
.package(url: "https://github.com/joehsieh/swift-syntax", branch: "add-fix-it-extesion"), |
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.
note: this change will be reverted once this PR is merged.
) | ||
} | ||
|
||
private static func generateTitle(with edits: [TextEdit], snapshot: DocumentSnapshot) -> String? { |
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.
Nitpick, I would name this without generate
. Since this is side-effect free, I don’t think the function name needs a result and generate
basically just says that the function is returning something, which is obvious from the result type.
private static func generateTitle(with edits: [TextEdit], snapshot: DocumentSnapshot) -> String? { | |
private static func title(for edits: [TextEdit], in snapshot: DocumentSnapshot) -> String? { |
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.
you're right. let me update it.
edit: WorkspaceEdit( | ||
changes: [ | ||
uri : [ | ||
TextEdit(range: Position(line: 0, utf16index: 9)..<Position(line: 0, utf16index: 15), newText: "Multiident "), |
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 use location markers here instead of hardcoded line-column offsets. We aren’t very good about it in the codebase yet as I only introduced the concept of location markers in the last couple of weeks but it’s generally proffered because of the same reasons that I mentioned in your swift-syntax PR.
Here’s an example of how to use the location markers.
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.
got it, it makes sense to use location markers instead. i'll update the code.
|
||
let expectedCodeActions = [ | ||
CodeAction( | ||
title: "Replace \'Multi \' with \'Multiident \'...", |
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.
You don’t need to escape the '
in Swift string literals.
guard let codeActions = diagnostic.codeActions else { | ||
XCTFail("Expected non-empty codeActions") | ||
return | ||
} |
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.
You could just use XCTUnwrap
guard let codeActions = diagnostic.codeActions else { | |
XCTFail("Expected non-empty codeActions") | |
return | |
} | |
let codeActions = try XCTUnwrap(diagnostic.codeAction) |
|
||
func testCodeActionForFixItsProducedBySwiftSyntax() async throws { | ||
let ws = try await MultiFileTestWorkspace(files: [ | ||
"test.swift": "protocol 0️⃣Multi 1️⃣ident 2️⃣{}", |
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.
Nitpick similar to the PR in swift-syntax: We usually start counting the location markers at 0. Would be nice to be consistent here.
guard let generatedTitle = Self.title(for: textEdits, in: snapshot) else { | ||
return nil | ||
} |
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.
Fix-Its from swift-syntax always have a message (fixIt.message.message
I think) and I think we should be using that instead of synthesizing a title here. I think the extraction of title(for:in:)
makes sense regardless though.
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 tried to use fixIt.message.message
instead of title(for:in:)
, but it failed the test, so we might not be able to do it.
Expected:
"Replace 'Multi ' with 'Multiident '..."
"Replace 'Multi ' with 'MultiIdent '..."
Actual:
"join the identifiers together",
"join the identifiers together with camel-case"
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.
Can’t you just update the test case to expect join the identifiers together
?
Also, it would be good to uppercase the first character of the diagnostic, similar to what I did here: #784
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.
you're right, i put the code protocol Multi ident {}
in a swift playground.
the error message was
Join the identifiers together
Join the identifiers together with camel-case
let me correct it.
5b5e295
to
33b4950
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.
Looks good to me. We just need to wait to get swiftlang/swift-syntax#2314 in and revert the change to the package manifest.
When you do that, could you squash your commits? It makes for a nicer Git history.
Got it, i'll do it. in the meantime, i'm trying to refine below command and put it in a git hook
|
I was thinking about integrating the swift-format command plugin into sourcekit-lsp. That way you should be able to run swift-format e.g. from Xcode. But I haven’t had time to look into it yet. If you want, you could try that. |
thanks for the reminder. i found a vscode plugin for swift-format. for the plugin of Xcode, it looks like there is a good one. https://github.com/nicklockwood/SwiftFormat#what-is-this |
Finetune the code Finetune the code Finetune the code Update the code per comments Use location marker instead of hard coded indexes Update the test to align the code convention Rename a variable Correct unit tests Format the code Update Package.swift
13feeb5
to
a095810
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.
Let’s get this in 🎉
@swift-ci Please test |
@swift-ci Please test Windows |
Windows failure is unrelated to your PR. |
@swift-ci Please test Windows |
1 similar comment
@swift-ci Please test Windows |
still WIP, i'm trying to figure out how to write tests for this change.Implemented an initializer of CodeAction with the FixIt from swift-syntax and added a test for it.