-
Notifications
You must be signed in to change notification settings - Fork 314
Implement completionItem/resolve
#432
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,23 @@ | |
//===----------------------------------------------------------------------===// | ||
|
||
/// A single completion result. | ||
public struct CompletionItem: Codable, Hashable { | ||
public struct CompletionItem: TextDocumentRequest, RequestType, ResponseType, Codable, Hashable { | ||
public typealias Response = CompletionItem | ||
|
||
public var textDocument: TextDocumentIdentifier { | ||
if | ||
let data = self.data, | ||
case let .dictionary(dict) = data, | ||
case let .string(textDocURI) = dict["textDocURI"] | ||
{ | ||
return TextDocumentIdentifier(DocumentURI(string: textDocURI)) | ||
} else { | ||
fatalError("Missing textDocURI in CompletionItem.data: \(data ?? "(CompletionItem.data is nil)")") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don’t think we should make any assumptions about the requests sent to us by the client and, in particular, not crash if it is malformed. Could we return a failure value (e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you are not using this computed property at all, so let’s just remove it. |
||
} | ||
} | ||
|
||
public static var method: String = "completionItem/resolve" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the |
||
|
||
|
||
/// The display name of the completion. | ||
public var label: String | ||
|
@@ -48,6 +64,9 @@ public struct CompletionItem: Codable, Hashable { | |
|
||
/// Whether the completion is for a deprecated symbol. | ||
public var deprecated: Bool? | ||
|
||
/// A data entry field that is preserved on a `CompletionItem` between a completion and a completion resolve request. | ||
public var data: LSPAny? | ||
|
||
public init( | ||
label: String, | ||
|
@@ -59,7 +78,8 @@ public struct CompletionItem: Codable, Hashable { | |
textEdit: TextEdit? = nil, | ||
insertText: String? = nil, | ||
insertTextFormat: InsertTextFormat? = nil, | ||
deprecated: Bool? = nil) | ||
deprecated: Bool? = nil, | ||
data: LSPAny? = nil) | ||
{ | ||
self.label = label | ||
self.detail = detail | ||
|
@@ -71,6 +91,21 @@ public struct CompletionItem: Codable, Hashable { | |
self.insertTextFormat = insertTextFormat | ||
self.kind = kind | ||
self.deprecated = deprecated | ||
self.data = data | ||
} | ||
|
||
enum CodingKeys: String, CodingKey { | ||
ahoppen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case label, | ||
detail, | ||
documentation, | ||
sortText, | ||
filterText, | ||
textEdit, | ||
insertText, | ||
insertTextFormat, | ||
kind, | ||
deprecated, | ||
data | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
public struct Lib { | ||
/// Documentation for `foo`. | ||
public func /*Lib.foo:def*/foo() {} | ||
|
||
public init() {} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,18 +169,33 @@ extension SwiftLanguageServer { | |
// Map SourceKit's not_recommended field to LSP's deprecated | ||
let notRecommended = (value[self.keys.not_recommended] as Int?).map({ $0 != 0 }) | ||
|
||
let kind: sourcekitd_uid_t? = value[self.keys.kind] | ||
let kind = (value[self.keys.kind] as sourcekitd_uid_t?)?.asCompletionItemKind(self.values) ?? .value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason why you need to refactor |
||
var usr: String? = nil | ||
|
||
switch kind { | ||
// Don't fill in `usr` for things other than Swift symbols. | ||
case .class, .struct, .constant, .variable, .enum, .enumMember, .constructor, | ||
.field, .function, .interface, .method, .module, .operator, .property, .typeParameter: | ||
ahoppen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
usr = value[self.keys.associated_usrs] | ||
default: break | ||
} | ||
|
||
var data: [String: LSPAny] = ["textDocURI": .string(snapshot.document.uri.stringValue)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to define the magic |
||
|
||
if usr != nil { data["usr"] = .string(usr!) } | ||
|
||
result.items.append(CompletionItem( | ||
label: name, | ||
kind: kind?.asCompletionItemKind(self.values) ?? .value, | ||
kind: kind, | ||
detail: typeName, | ||
documentation: docBrief != nil ? .markupContent(MarkupContent(kind: .markdown, value: docBrief!)) : nil, | ||
sortText: nil, | ||
filterText: filterName, | ||
textEdit: textEdit, | ||
insertText: text, | ||
insertTextFormat: isInsertTextSnippet ? .snippet : .plain, | ||
deprecated: notRecommended ?? false | ||
deprecated: notRecommended ?? false, | ||
data: .dictionary(data) | ||
)) | ||
|
||
return true | ||
|
@@ -283,4 +298,56 @@ extension SwiftLanguageServer { | |
|
||
return TextEdit(range: textEditRangeStart..<requestPosition, newText: newText) | ||
} | ||
|
||
/// Must be called on `queue`. | ||
ahoppen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func _completionResolve(_ req: Request<CompletionItem>) { | ||
if | ||
let data = req.params.data, | ||
case let .dictionary(dict) = data, | ||
case let .string(usr) = dict["usr"], | ||
case let .string(textDocURI) = dict["textDocURI"] | ||
{ | ||
let uri = DocumentURI(string: textDocURI) | ||
let skreq = SKDRequestDictionary(sourcekitd: sourcekitd) | ||
|
||
skreq[keys.request] = requests.cursorinfo | ||
skreq[keys.usr] = usr | ||
skreq[keys.sourcefile] = uri.pseudoPath | ||
|
||
// FIXME: SourceKit should probably cache this for us. | ||
if let compileCommand = commandsByFile[uri] { | ||
skreq[keys.compilerargs] = compileCommand.compilerArgs | ||
} | ||
|
||
let handle = sourcekitd.send(skreq, queue) { [weak self] result in | ||
guard let dict = result.success else { | ||
req.reply(.failure(ResponseError(result.failure!))) | ||
return | ||
} | ||
|
||
if let documentationXML: String = dict[self?.keys.doc_full_as_xml] { | ||
|
||
do { | ||
let documentationMarkdown = try xmlDocumentationToMarkdown(documentationXML) | ||
var completionItem = req.params | ||
|
||
completionItem.documentation = .markupContent(MarkupContent(kind: .markdown, value: documentationMarkdown)) | ||
req.reply(completionItem) | ||
} catch { | ||
log("Completion Resolve: Unable to convert XML documentation to Markdown", level: .debug) | ||
req.reply(req.params) | ||
} | ||
|
||
} else { | ||
log("Completion Resolve: No doc_full_as_xml in SourceKit response", level: .debug) | ||
req.reply(req.params) | ||
} | ||
} | ||
|
||
// FIXME: cancellation | ||
_ = handle | ||
} else { | ||
req.reply(req.params) | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.