Skip to content

Conversation

@iMostfa
Copy link
Contributor

@iMostfa iMostfa commented Dec 21, 2025

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

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 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] = [
Copy link
Member

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?

Comment on lines +1006 to +1014
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])
}
Copy link
Member

Choose a reason for hiding this comment

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

Slightly more succinct.

Suggested change
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)")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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(
Copy link
Member

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?

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.

Add Action to Copy Objective-C Selector Name of @objc Swift Method

2 participants