-
Notifications
You must be signed in to change notification settings - Fork 254
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
Implement completionItem/resolve
#432
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -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,23 @@ 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 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 let usr: String = value[self.keys.associated_usrs] { 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 +288,58 @@ 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>) { | ||
dispatchPrecondition(condition: .onQueue(queue)) | ||
|
||
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) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
nil
) here instead offatalError
ing?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
TextDocumentIdentifier(DocumentURI(string: ""))
ok?There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.