Sourcekit-lsp and the typeHierarchy family of requests

Hi folks!

I was recently looking for a way to surface protocol conformance on the terminal the way Xcode makes available through the search menu, and had sourcekit-lsp suggested to me as a possible avenue to take. So I went about my experiment, finding some helpful articles folks have written along the way.

It seemed to me that the textDocument/typeHierarchy family of requests was the way to go, but after a decent amount of debugging, I was left wondering whether I was doing something wrong. I came up with a very rough macOS executable prototype and a barebones Swift Package as sample input to try to tease out the behavior I was looking for, but I kept coming up short.

I’ve been able to successfully execute textDocument/didOpen as well as textDocument/hover requests, but attempting to execute textDocument/prepareTypeHierarchy and/or typeHierarchy/supertypes / typeHierarchy/subtypes comes back with nil and empty array responses.

And so my question is: is this user error? Is there something I’m overlooking about how I’m making these requests?

For reference, I’m using macOS 14.7.1 and Xcode 15.2.

As an aside, was the --log-level option removed from recent versions of sourcekit-lsp?

Thanks!

2 Likes

What kind of project are you using (SwiftPM, compile_commands.json, build server)? Type hierarchy requires an up-to-date index and you might need to build your project for that (sourcekit-lsp/Contributor Documentation/Background Indexing.md at main · swiftlang/sourcekit-lsp · GitHub has some information about that).

SourceKit-LSP now logs to the system log. sourcekit-lsp/CONTRIBUTING.md at main · swiftlang/sourcekit-lsp · GitHub has some information.

1 Like

Hi Alex,

If I understand your question correctly, I'm using a SwiftPM package project as input for the sourcekit-lsp tool.

Assuming I need to build the project before making the type hierarchy requests, which method should I use to do that? swift build?

Thanks for the tip about the logs, I'll check those out.

Alternatively to running a build, you could enable background indexing, as described in sourcekit-lsp/Documentation/Enable Experimental Background Indexing.md at main · swiftlang/sourcekit-lsp · GitHub. If your tool needs to wait for background indexing to finish, you can use the workspace/_pollIndex request.

1 Like

Thanks so much for all this information @ahoppen !

On this topic, is there any place where custom requests like this are documented? This is absolutely not accusatory in the slightest, I'm just very curious to see what other features the language server is hiding!

Oh, interesting! Unfortunately I'm confined to the Xcode 15.2/Swift 5.9.2 toolchain for the time being, but I'll keep the background indexing feature in mind for future projects. Thanks for the insights, @ahoppen! I was able to see results from those type hierarchy requests like I was expecting after running a build on the command line.

1 Like

They are documented in the very document I linked from workspace/_pollIndex.

That’s great to hear. Good luck implementing the rest of your tool.

1 Like

Well that's embarrassing.

Thank you!

1 Like