Skip to content

Fetch full documentation in code completion #2207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

a7medev
Copy link

@a7medev a7medev commented Jul 15, 2025

Depends on swiftlang/swift#82464


When resolving documentation for code completion items, we fetch full documentation through the newly added swiftide_completion_item_get_doc_full_copy SourceKitD function, if not found we fallback to brief documentation as before using swiftide_completion_item_get_doc_brief.

Note

Unlike brief documentation, SourceKitD doesn't cache full documentation for completion results to avoid bloating memory with a lot of large strings.

As of now, SourceKit-LSP doesn't cache completion item documentation either, should we introduce a new full documentation cache (e.g. using LRUCache)?

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.

Very cool stuff. Excited to see this. I left a couple comments inline.

session.sourcekitd.ideApi.completion_item_get_doc_full_copy(session.response, rawItem) {
if let cstr = $0 {
result = String(cString: cstr)
free(cstr)
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to free the pointer here? I would assume that the memory is still owned by sourcedkitd since it only yields the pointer to us in a closure and is thus still responsible for its lifetime.

Copy link
Author

Choose a reason for hiding this comment

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

sourcekitd doesn't keep full documentation comments in-memory, it prints them in a temporary buffer and passes a copy through strdup that clients should free (hence the name is suffixed with _copy).

Copy link
Member

Choose a reason for hiding this comment

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

If we pass ownership to the client, I think completion_item_get_doc_full_copy should return the pointer. E.g. like swiftide_completion_result_description_copy.

Copy link
Author

Choose a reason for hiding this comment

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

I just modified completion_item_get_doc_full to not strdup the string and pass it as is to the handler, and SourceKit-LSP will copy it into a Swift String anyway and it doesn't have to manually call free.

Can you please recheck?

if let fullDocumentation = info.fullDocumentation {
response.set(request.sourcekitd.keys.docFullAsXML, to: fullDocumentation)
} else {
response.set(request.sourcekitd.keys.docBrief, to: info.briefDocumentation)
Copy link
Member

Choose a reason for hiding this comment

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

Open question: Does it make sense to also return the brief documentation in addition to the full documentation? Eg. an editor might want to display a brief documentation of the symbol by default and then expand that to full documentation once the user performs an action.

Copy link
Author

Choose a reason for hiding this comment

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

Do you have a specific flow in mind? AFAIK this doesn't happen in code completion since documentation is only shown when the completion item is selected in the completions list.

Copy link
Member

Choose a reason for hiding this comment

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

For example, you could think about a UI where the brief documentation is displayed in the completion list (similar to how Xcode shows documentation) and only showing the full documentation when the user requests it.

Copy link
Author

Choose a reason for hiding this comment

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

I assume the plugin is used in SourceKit-LSP only, no? If so, then we don't have to return the brief documentation now as we don't need it. We can always add it later if we found a use case for it.

If not, it'd probably make sense to return both yes, especially if it's used by Xcode which already has this use case.

Copy link
Contributor

Choose a reason for hiding this comment

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

The plugin can potentially be used by any SourceKit client yes, so I think we should return both by default. We can always add options to the request for cases where the client only wants the brief or full doc

Copy link
Author

Choose a reason for hiding this comment

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

Now it makes sense, thanks for the clarification @ahoppen and @hamishknight 🙏🏼

I've changed the request to return both now.

@a7medev a7medev requested a review from ahoppen July 16, 2025 22:30
item.documentation = .markupContent(MarkupContent(kind: .markdown, value: docString))
}
}
return item
}

private static func documentationString(from response: SKDResponseDictionary, sourcekitd: SourceKitD) -> String? {
if let docFullAsXML: String = response[sourcekitd.keys.docFullAsXML] {
return try? xmlDocumentationToMarkdown(docFullAsXML)
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I should have noticed this before: We generally prefer to use orLog over try? because it means that the error will get logged instead of silently swallowing it, which helps debugging.

Copy link
Author

Choose a reason for hiding this comment

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

Done ✅

}

private func sourcekitdSupportsFullDocumentation() async throws -> Bool {
let sourcekitdPath = await ToolchainRegistry.forTesting.default!.sourcekitd!
Copy link
Member

Choose a reason for hiding this comment

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

Could you use XCTUnwrap instead of ! to avoid crashing the test execution if for some reason one of these values is nil.

Copy link
Author

Choose a reason for hiding this comment

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

Removed now that we're using SkipUnless

line: UInt = #line
) async throws {
let supportsFullDocumentation = try await sourcekitdSupportsFullDocumentation()
let expected = supportsFullDocumentation ? expectedFull : expectedBrief
Copy link
Member

Choose a reason for hiding this comment

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

I’m a little concerned with executing a different test path depending on whether sourcekitd supports full documentation because in Swift CI I imagine the sourcekitd will always support full documentation which means that the expectedBrief case will not get tested in Swift CI.

Instead, I would prefer to add a function to SkipUnless and skip the tests if sourcekitd doesn’t support full documentation.

Same comment for assertDocumentation for the sourcekitd plugin.

Copy link
Author

Choose a reason for hiding this comment

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

My only concern is that tests like SwiftCompletionTests.testCompletionBasic will get skipped in environments without full documentation support.
This might be accepted since it's still exercised in Swift CI anyway.

What do you think? Should we just assume full documentation exists with a SkipUnless or do you prefer duplicating the tests each skipped depending on full documentation support to ensure brief documentation is still tested if full documentation isn't supported?

Copy link
Author

Choose a reason for hiding this comment

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

I assumed we'd just skip them if full documentation is not supported with no tests for brief documentation alone (avoiding duplication & relying on the fact that Swift CI is going to have full documentation support anyway) to speedup review.

If you'd prefer another approach please let me know.

Copy link
Member

Choose a reason for hiding this comment

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

CI should be able to execute all tests. Otherwise tests can break and you don’t notice until you run them locally. So, since CI will only able to test full documentation and the brief documentation part is being phased out, we should just test full documentation support.

@a7medev a7medev requested a review from ahoppen July 17, 2025 08:19
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, looks good to me, the only open question is whether we want to return both the brief and the full documentation from the plugin.

Also: Could you format your changes using swift-format as described in https://github.com/swiftlang/sourcekit-lsp/blob/main/CONTRIBUTING.md#formatting?

@a7medev a7medev requested a review from ahoppen July 17, 2025 19:11
@a7medev
Copy link
Author

a7medev commented Jul 17, 2025

I have added one more functionality change.
In the initial implementation, I converted XML documentation to markdown using the same existing flow which includes the declaration as part of the documentation.

I found that existing LSP implementations (mainly Clang and TypeScript) don't show the declaration itself as part of the full documentation in completion (contrary to the hover request!) and I think the reason for this is that the completion item already describes the declaration and typically the popup showing the documentation is small so it makes sense to show the documentation comment right away.

@ahoppen @hamishknight Can you please recheck? 🙏🏼

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.

Looks great to me. Thank you 🚀

@ahoppen
Copy link
Member

ahoppen commented Jul 18, 2025

@swift-ci Please test

@ahoppen
Copy link
Member

ahoppen commented Jul 18, 2025

CI failed because Swift CI unconditionally enables all tests that are guarded by skipUnlessSupportedByToolchain (we don’t want to accidentally leave tests disabled in CI due to configuration issues). I suggest that we wait until swiftlang/swift#82464 is merged before merging this. Is that alright with you?

@a7medev
Copy link
Author

a7medev commented Jul 18, 2025

@ahoppen Yes, I think that makes sense.

I wonder where in the console output did you find this though 😅

Edit: I found it by downloading the plain text output and using grep to filter it out. Let me know if there's a more efficient way to do that though 😅

@ahoppen
Copy link
Member

ahoppen commented Jul 18, 2025

No, I always download the log files as well.

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.

3 participants