realm / jazzy

Soulful docs for Swift & Objective-C
https://realm.io
MIT License
7.32k stars 413 forks source link

Generate Objective-C interfaces for Swift declarations #136

Open beltex opened 9 years ago

beltex commented 9 years ago

I'm certain this is already known, but thought it be handy to have as a placeholder for reference nonetheless. Feel free to close if not, and of course #56 is a pre-req for this.

Independent of the source of the API (C/Obj-C, Swift), generate the interface (declarations) for both Objectivce-C & Swift (as an option) as seen in Apple's docs already (with the Language option). Of course, not everything translates both ways (complex macros --> Swift, tuples --> Obj-C).

As a side note, for those interested in this from the Swift side, the @objc annotation in Swift would help. Tells the compiler to check if the method in question is compatible with Objective-C. See Using Swift with Cocoa and Objective-C book from Apple for more.

jpsim commented 9 years ago

Thanks for filing this issue.

We could do this for Swift source right now, but we'd first have to identify how Xcode generates the -Swift.h file.

56 will of course be a pre-requisite to generating the Swift version of Objective-C declarations.

Bierrot commented 9 years ago

Same here. It would be very nice to have both declarations (obj-c and swift) in jazzy doc.

jpsim commented 8 years ago

There's a proof of concept PR towards SourceKitten that builds towards this: https://github.com/jpsim/SourceKitten/pull/95

fabb commented 8 years ago

Don't mean to hijack the thread, but maybe you can help: I want to generate the Swift Interface for our ObjC Bridging Header via commandline, is that somehow possible (without generating the full-fledged documentation from jazzy)?

jpsim commented 8 years ago

As of #530, Objective-C declarations also include their Swift equivalent when appropriate.

I haven't found a good way to do it the other way around though, so I'd appreciate any help from contributors here 😄

karagraysen commented 7 years ago

Seems that this issue has been resolved so I'm going to close the issue. Lmk if I'm wrong or if we need to reopen the issue.

1ec5 commented 7 years ago

Are you sure? Last I checked, jazzy generated Swift signatures for Objective-C symbols, but not the other way around.

karagraysen commented 7 years ago

I'll look into it. Re-opening for now.

jpsim commented 7 years ago

I still haven't figured out how to perform the swift->objc translation.

akhilcb commented 6 years ago

Just wondering if there is any progress on this since it has been two years.

johnfairh commented 6 years ago

If you (or anyone!) would like to work on this, the best way I've come up with to do this is:

As PoC, drive from jazzy layer:

  1. Generate the Swift sourcekitten json structures as normal.
  2. Generate bridging header for the Swift module. (This contains ObjC defns for Swift decls marked @objc.) Use swiftc -emit-objc-header-path and build module, or dig out of initial sourcekitten build, experimentation needed for most efficient ways ....
  3. Run sourcekitten doc --objc on the bridging header.
  4. Traverse the bridging header json and spot ObjC declarations that match Swift ones. This is easy because the USR fields match. (Haven't proved this is always true, seems to hold for simple examples though - will need testing and maybe study of Swift's PrintAsObjC code.)
  5. Tweak jazzy's Ruby code to understand that there can be two declarations in Swift mode. Should be light relief after the previous parts.

Fully integrated version would do 1-3 in sourcekitten and could optimise the 2/3 steps by just pointing libclang at the header and iterating over it for USRs without generating the intermediate json.