jpsim / SourceKitten

An adorable little framework and command line tool for interacting with SourceKit.
MIT License
2.31k stars 226 forks source link

Getting documentation for specific offset in text #592

Open joshbirnholz opened 5 years ago

joshbirnholz commented 5 years ago

I'm using SourceKitten to improve syntax highlighting and add code completion for my app Swift Coder.

I've managed to get code completion suggestions through SourceKitten so far, but it gave me an idea. Seeing the "docBrief" one-line descriptions made me think about implementing something like this:

Screen Shot 2019-05-07 at 12 10 59 AM

Or, basically, Xcode's Quick Help inspector.

I'm not sure where I should be looking to get this information through SourceKit, or if it's something I can get at all. I can't figure out how to get a cursor offset request to work properly or even if that would be helpful. I don't need this to be super in-depth, maybe just showing the "summary" section would be enough, with maybe also a link to the full documentation. I think it'd be pretty helpful for people learning Swift!

jpsim commented 5 years ago

The "cursor info" request gives you all that information: https://github.com/jpsim/SourceKitten/blob/0.23.1/Source/SourceKittenFramework/Request.swift#L142-L143

This is used extensively by the sourcekitten doc command and by SwiftLint's Analyzer rules.

joshbirnholz commented 5 years ago

I figured that, but I can't get any cursor info requests to work. Do I need to specify any compiler arguments, and if so, what arguments should I use? Because this code is just giving me an empty dictionary.

let cursorInfoRequest = Request.cursorInfo(file: path, offset: Int64(offset), arguments: [])
let cursorInfo = try cursorInfoRequest.send()
joshbirnholz commented 5 years ago

Nevermind, I got it to work by using the path to the file as an argument.

let cursorInfoRequest = Request.cursorInfo(file: path, offset: Int64(offset), arguments: [path])
let cursorInfo = try cursorInfoRequest.send()
jpsim commented 5 years ago

Those arguments are key. To get complete results, you'll need to pass all arguments needed to get the Swift file that you're inspecting to compile if you passed them to swiftc.