Rewrite MarkEditWritingTools in pure Swift#1265
Conversation
|
|
||
| static func shouldReselect(with tool: WritingTool) -> Bool { | ||
| // Compose mode can start without text selections | ||
| return tool != .compose |
There was a problem hiding this comment.
@copilot Why not use implicit return when you have Swift?
Try making the entire write more "Swifty", you should always seek for best practices.
There was a problem hiding this comment.
Applied implicit return in shouldReselect(with:) to match the codebase convention (e.g., AppTheme.current, AppDesign.modernTitleBar). The other functions in the file require explicit return due to guard statements and multi-expression bodies. Fixed in 46139b9.
- Replace NSInvocation with method(for:) + unsafeBitCast pattern - Replace NSSelectorFromString with sel_getUid - Use class_getClassMethod + method_getImplementation for class methods - Define WritingTool as a Swift enum with Int raw value - Remove ObjC files (.h, .m), bridging header, and ObjC directory - Update project.pbxproj: remove ObjC group, add Swift file to Main group, remove SWIFT_OBJC_BRIDGING_HEADER build setting Co-authored-by: cyanzhong <6745066+cyanzhong@users.noreply.github.com>
Co-authored-by: cyanzhong <6745066+cyanzhong@users.noreply.github.com>
Co-authored-by: cyanzhong <6745066+cyanzhong@users.noreply.github.com>
cad3342 to
8453f6c
Compare
|
@copilot I reviewed and trust you that the current rewrite is correct. Now, let's focus on improving the implementation, the goals are:
Interfaces exposed shouldn't be changed, functionality should be 100% preserved. |
…Map+first(where:), simplify writingToolsInstance Co-authored-by: cyanzhong <6745066+cyanzhong@users.noreply.github.com>
Implemented in 1cedb1d:
|
| @available(macOS 15.1, *) | ||
| private extension AppWritingTools { | ||
| /// Invokes a selector on a target and returns the result as `NSObject?`. | ||
| static func invoke(_ target: NSObject, selector name: String) -> NSObject? { |
There was a problem hiding this comment.
@copilot Can you further combine two invoke methods by leveraging generic types?
There was a problem hiding this comment.
Combined into a single generic invoke<Result> with a fallback parameter — the compiler infers Result from the fallback value (nil → NSObject?, 0 → Int). Fixed in bbaa48e.
…h fallback parameter Co-authored-by: cyanzhong <6745066+cyanzhong@users.noreply.github.com>
|
|
||
| return unsafeBitCast( | ||
| target.method(for: selector), | ||
| to: (@convention(c) (NSObject, Selector) -> Result).self |
There was a problem hiding this comment.
@copilot It looks like
Generic parameter 'U' could not be inferred
'(NSObject, Selector) -> Result' is not representable in Objective-C, so it cannot be used with '@convention(c)'
There was a problem hiding this comment.
Reverted to two concrete overloads in 976c800. @convention(c) requires concrete types — generic type parameters are not representable in the C calling convention, so the two overloads are the minimum needed here.
…c) requires concrete types Co-authored-by: cyanzhong <6745066+cyanzhong@users.noreply.github.com>
24d7e3a to
223ddb2
Compare
|
@copilot I noticed |
Confirmed — no unused code remains. All public interfaces are referenced externally: |
223ddb2 to
ea74b35
Compare
ea74b35 to
133e035
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
MarkEditWritingTools.swiftinMarkEditMac/Sources/Main/with pure Swift implementationWritingToolenum matching the ObjCNS_ENUM(long long, WritingTool)requestedToolusingmethod(for:)+unsafeBitCastinstead ofNSInvocationaffordanceIconin pure SwiftshouldReselect(withItem:)andshouldReselect(with:)MarkEditWritingTools.h,.m, bridging header, and theObjC/directory)project.pbxprojto reflect the file changes (remove ObjC refs, add Swift file to Main group, remove bridging header setting)unsafeBitCastblockinvokehelpers (NSObject?andIntoverloads) for the repeatedunsafeBitCast+responds(to:)pattern — two concrete overloads are required because@convention(c)does not support generic type parameterscompactMap+first(where:)inrequestedToolwritingToolsInstancehelperOriginal prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.