Skip to content

Support completionItem/resolve to compute documentation of a code completion item #1938

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

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,33 @@ public struct TextDocumentClientCapabilities: Hashable, Codable, Sendable {

/// Capabilities specific to the `textDocument/...` change notifications.
public struct Completion: Hashable, Codable, Sendable {

/// Capabilities specific to `CompletionItem`.
public struct CompletionItem: Hashable, Codable, Sendable {
public struct TagSupportValueSet: Hashable, Codable, Sendable {
/// The tags supported by the client.
public var valueSet: [CompletionItemTag]

public init(valueSet: [CompletionItemTag]) {
self.valueSet = valueSet
}
}

public struct ResolveSupportProperties: Hashable, Codable, Sendable {
/// The properties that a client can resolve lazily.
public var properties: [String]

public init(properties: [String]) {
self.properties = properties
}
}

public struct InsertTextModeSupportValueSet: Hashable, Codable, Sendable {
public var valueSet: [InsertTextMode]

public init(valueSet: [InsertTextMode]) {
self.valueSet = valueSet
}
}

/// Whether the client supports rich snippets using placeholders, etc.
public var snippetSupport: Bool? = nil
Expand All @@ -273,18 +297,48 @@ public struct TextDocumentClientCapabilities: Hashable, Codable, Sendable {
/// Whether the client supports the `preselect` property on a CompletionItem.
public var preselectSupport: Bool? = nil

/// Client supports the tag property on a completion item. Clients supporting tags have to handle unknown tags
/// gracefully. Clients especially need to preserve unknown tags when sending a completion item back to the server
/// in a resolve call.
public var tagSupport: TagSupportValueSet?

/// Client supports insert replace edit to control different behavior if a completion item is inserted in the text
/// or should replace text.
public var insertReplaceSupport: Bool?

/// Indicates which properties a client can resolve lazily on a completion item. Before version 3.16.0 only the
/// predefined properties `documentation` and `detail` could be resolved lazily.
public var resolveSupport: ResolveSupportProperties?

/// The client supports the `insertTextMode` property on a completion item to override the whitespace handling mode
/// as defined by the client (see `insertTextMode`).
public var insertTextModeSupport: InsertTextModeSupportValueSet?

/// The client has support for completion item label details (see also `CompletionItemLabelDetails`).
public var labelDetailsSupport: Bool?

public init(
snippetSupport: Bool? = nil,
commitCharactersSupport: Bool? = nil,
documentationFormat: [MarkupKind]? = nil,
deprecatedSupport: Bool? = nil,
preselectSupport: Bool? = nil
preselectSupport: Bool? = nil,
tagSupport: TagSupportValueSet? = nil,
insertReplaceSupport: Bool? = nil,
resolveSupport: ResolveSupportProperties? = nil,
insertTextModeSupport: InsertTextModeSupportValueSet? = nil,
labelDetailsSupport: Bool? = nil
) {
self.snippetSupport = snippetSupport
self.commitCharactersSupport = commitCharactersSupport
self.documentationFormat = documentationFormat
self.deprecatedSupport = deprecatedSupport
self.preselectSupport = preselectSupport
self.tagSupport = tagSupport
self.insertReplaceSupport = insertReplaceSupport
self.resolveSupport = resolveSupport
self.insertTextModeSupport = insertTextModeSupport
self.labelDetailsSupport = labelDetailsSupport
}
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/SourceKitLSP/Clang/ClangLanguageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,10 @@ extension ClangLanguageService {
return try await forwardRequestToClangd(req)
}

func completionItemResolve(_ req: CompletionItemResolveRequest) async throws -> CompletionItem {
return try await forwardRequestToClangd(req)
}

func hover(_ req: HoverRequest) async throws -> HoverResponse? {
return try await forwardRequestToClangd(req)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ package actor DocumentationLanguageService: LanguageService, Sendable {
CompletionList(isIncomplete: false, items: [])
}

package func completionItemResolve(_ req: CompletionItemResolveRequest) async throws -> CompletionItem {
return req.item
}

package func hover(_ req: HoverRequest) async throws -> HoverResponse? {
nil
}
Expand Down
1 change: 1 addition & 0 deletions Sources/SourceKitLSP/LanguageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ package protocol LanguageService: AnyObject, Sendable {
// MARK: - Text Document

func completion(_ req: CompletionRequest) async throws -> CompletionList
func completionItemResolve(_ req: CompletionItemResolveRequest) async throws -> CompletionItem
func hover(_ req: HoverRequest) async throws -> HoverResponse?
func symbolInfo(_ request: SymbolInfoRequest) async throws -> [SymbolDetails]

Expand Down
31 changes: 23 additions & 8 deletions Sources/SourceKitLSP/SourceKitLSPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ package actor SourceKitLSPServer {
}.valuePropagatingCancellation
}

private func documentService(for uri: DocumentURI) async throws -> LanguageService {
guard let workspace = await self.workspaceForDocument(uri: uri) else {
throw ResponseError.workspaceNotOpen(uri)
}
guard let languageService = workspace.documentService(for: uri) else {
throw ResponseError.unknown("No language service for '\(uri)' found")
}
return languageService
}

/// This method must be executed on `workspaceQueue` to ensure that the file handling capabilities of the
/// workspaces don't change during the computation. Otherwise, we could run into a race condition like the following:
/// 1. We don't have an entry for file `a.swift` in `workspaceForUri` and start the computation
Expand Down Expand Up @@ -754,6 +764,8 @@ extension SourceKitLSPServer: QueueBasedMessageHandler {
await self.handleRequest(for: request, requestHandler: self.colorPresentation)
case let request as RequestAndReply<CompletionRequest>:
await self.handleRequest(for: request, requestHandler: self.completion)
case let request as RequestAndReply<CompletionItemResolveRequest>:
await request.reply { try await completionItemResolve(request: request.params) }
case let request as RequestAndReply<DeclarationRequest>:
await self.handleRequest(for: request, requestHandler: self.declaration)
case let request as RequestAndReply<DefinitionRequest>:
Expand Down Expand Up @@ -1035,7 +1047,7 @@ extension SourceKitLSPServer {
await registry.clientHasDynamicCompletionRegistration
? nil
: LanguageServerProtocol.CompletionOptions(
resolveProvider: false,
resolveProvider: true,
triggerCharacters: [".", "("]
)

Expand Down Expand Up @@ -1432,6 +1444,15 @@ extension SourceKitLSPServer {
return try await languageService.completion(req)
}

func completionItemResolve(
request: CompletionItemResolveRequest
) async throws -> CompletionItem {
guard let completionItemData = CompletionItemData(fromLSPAny: request.item.data) else {
return request.item
}
return try await documentService(for: completionItemData.uri).completionItemResolve(request)
}

#if canImport(SwiftDocC)
func doccDocumentation(_ req: DoccDocumentationRequest) async throws -> DoccDocumentationResponse {
return try await documentationManager.convertDocumentation(
Expand Down Expand Up @@ -1624,18 +1645,12 @@ extension SourceKitLSPServer {
logger.error("Attempted to perform executeCommand request without an URL")
return nil
}
guard let workspace = await workspaceForDocument(uri: uri) else {
throw ResponseError.workspaceNotOpen(uri)
}
guard let languageService = workspace.documentService(for: uri) else {
return nil
}

let executeCommand = ExecuteCommandRequest(
command: req.command,
arguments: req.argumentsWithoutSourceKitMetadata
)
return try await languageService.executeCommand(executeCommand)
return try await documentService(for: uri).executeCommand(executeCommand)
}

func getReferenceDocument(_ req: GetReferenceDocumentRequest) async throws -> GetReferenceDocumentResponse {
Expand Down
8 changes: 5 additions & 3 deletions Sources/SourceKitLSP/Swift/CodeCompletion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ extension SwiftLanguageService {
let completionPos = await adjustPositionToStartOfIdentifier(req.position, in: snapshot)
let filterText = String(snapshot.text[snapshot.index(of: completionPos)..<snapshot.index(of: req.position)])

let clientSupportsSnippets =
capabilityRegistry.clientCapabilities.textDocument?.completion?.completionItem?.snippetSupport ?? false
let buildSettings = await buildSettings(for: snapshot.uri, fallbackAfterTimeout: false)

let inferredIndentationWidth = BasicFormat.inferIndentation(of: await syntaxTreeManager.syntaxTree(for: snapshot))
Expand All @@ -45,8 +43,12 @@ extension SwiftLanguageService {
completionPosition: completionPos,
cursorPosition: req.position,
compileCommand: buildSettings,
clientSupportsSnippets: clientSupportsSnippets,
clientCapabilities: capabilityRegistry.clientCapabilities,
filterText: filterText
)
}

package func completionItemResolve(_ req: CompletionItemResolveRequest) async throws -> CompletionItem {
return try await CodeCompletionSession.completionItemResolve(item: req.item, sourcekitd: sourcekitd)
}
}
Loading