-
Notifications
You must be signed in to change notification settings - Fork 327
Add Copy Objective-C Selector code action #2399
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
base: main
Are you sure you want to change the base?
Conversation
ahoppen
left a comment
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 opening the PR. One thing I’m not entirely convinced about is whether this should be a refactoring actions that modifies the current source file. I would probably have implemented this as a code action that shows a message with the Objective-C selector name to the user via the window/showMessageRequest request if the client supports that request. What do you think about that approach?
| /// List of all of the syntactic code action providers, which can be used | ||
| /// to produce code actions using only the swift-syntax tree of a file. | ||
| let allSyntaxCodeActions: [any SyntaxCodeActionProvider.Type] = { | ||
| var result: [any SyntaxCodeActionProvider.Type] = [ |
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.
Did you accidentally change indentation to 4 spaces here? Could you ensure that you run swift-format on your PR?
| let skreq = sourcekitd.dictionary([:]) | ||
| skreq.set(keys.offset, to: offset) | ||
| skreq.set(keys.sourceFile, to: snapshot.uri.sourcekitdSourceFile) | ||
| if let primaryFile = snapshot.uri.primaryFile?.pseudoPath { | ||
| skreq.set(keys.primaryFile, to: primaryFile) | ||
| } | ||
| if let compilerArgs = compileCommand?.compilerArgs { | ||
| skreq.set(keys.compilerArgs, to: compilerArgs as [any SKDRequestValue]) | ||
| } |
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.
Slightly more succinct.
| let skreq = sourcekitd.dictionary([:]) | |
| skreq.set(keys.offset, to: offset) | |
| skreq.set(keys.sourceFile, to: snapshot.uri.sourcekitdSourceFile) | |
| if let primaryFile = snapshot.uri.primaryFile?.pseudoPath { | |
| skreq.set(keys.primaryFile, to: primaryFile) | |
| } | |
| if let compilerArgs = compileCommand?.compilerArgs { | |
| skreq.set(keys.compilerArgs, to: compilerArgs as [any SKDRequestValue]) | |
| } | |
| let skreq = sourcekitd.dictionary([ | |
| keys.offset: offset, | |
| keys.sourceFile: snapshot.uri.sourcekitdSourceFile, | |
| keys.primaryFile: primaryFile, | |
| keys.compilerArgs: compilerArgs as [any SKDRequestValue] | |
| ]) |
| return CodeAction(title: "Copy Objective-C Selector", kind: .refactor, command: copyCommand) | ||
| } | ||
| } catch { | ||
| logger.debug("CopyObjCSelector: applicability check failed: \(error.localizedDescription)") |
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.
| logger.debug("CopyObjCSelector: applicability check failed: \(error.localizedDescription)") | |
| logger.debug("CopyObjCSelector applicability check failed: \(error.forLogging)") |
This will make sure that the error correctly gets redacted based on the user’s logging sensitivity settings.
| do { | ||
| let dict = try await send(sourcekitdRequest: \.objcSelector, skreq, snapshot: snapshot) | ||
| if dict[keys.text] as String? != nil { | ||
| let copyCommand = CopyObjCSelectorCommand( |
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.
Am I missing something or where is CopyObjCSelectorCommand defined?
Resolves: #2145
along with: swiftlang/swift#86173
example.mov
Add Copy Objective-C Selector code action
Implements a new refactor action that allows users to copy Objective-C
selectors from Swift code. Adds the objcSelector SourceKitD request and
corresponding command handling in the language service.
If the approach is okay, i can go forward with adding/updating tests, lint..etc
Thanks