Skip to content

Forward definition requests when index lookup fails #294

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 2 commits into from
Jul 9, 2020
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
28 changes: 19 additions & 9 deletions Sources/SourceKitLSP/SourceKitServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -801,22 +801,32 @@ extension SourceKitServer {
) {
let symbolInfo = SymbolInfoRequest(textDocument: req.params.textDocument, position: req.params.position)
let index = self.workspace?.index
// If we're unable to handle the definition request using our index, see if the
// language service can handle it (e.g. clangd can provide AST based definitions).
let resultHandler: ([Location], ResponseError?) -> Void = { (locs, error) in
guard locs.isEmpty else {
req.reply(.locations(locs))
return
}
let handled = languageService.definition(req)
guard !handled else { return }
if let error = error {
req.reply(.failure(error))
} else {
req.reply(.locations([]))
}
}
let callback = callbackOnQueue(self.queue) { (result: Result<SymbolInfoRequest.Response, ResponseError>) in
guard let symbols: [SymbolDetails] = result.success ?? nil, let symbol = symbols.first else {
let handled = languageService.definition(req)
guard !handled else { return }
if let error = result.failure {
req.reply(.failure(error))
} else {
req.reply(.locations([]))
}
resultHandler([], result.failure)
return
}

let fallbackLocation = [symbol.bestLocalDeclaration].compactMap { $0 }

guard let usr = symbol.usr, let index = index else {
return req.reply(.locations(fallbackLocation))
resultHandler(fallbackLocation, nil)
return
}

log("performing indexed jump-to-def with usr \(usr)")
Expand All @@ -842,7 +852,7 @@ extension SourceKitServer {
)
}

req.reply(.locations(locations.isEmpty ? fallbackLocation : locations))
resultHandler(locations.isEmpty ? fallbackLocation : locations, nil)
}
let request = Request(symbolInfo, id: req.id, clientID: ObjectIdentifier(self),
cancellation: req.cancellationToken, reply: callback)
Expand Down
2 changes: 1 addition & 1 deletion Tests/INPUTS/BasicCXX/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include /*Object:include:main*/"Object.h"

int main(int argc, const char *argv[]) {
struct Object *obj = newObject();
struct /*Object:ref:main*/Object *obj = newObject();
return obj->field;
}
32 changes: 32 additions & 0 deletions Tests/SourceKitLSPTests/SourceKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,36 @@ final class SKTests: XCTestCase {
}
}
}

func testClangdGoToDefinitionWithoutIndex() throws {
guard let ws = try staticSourceKitTibsWorkspace(name: "BasicCXX") else { return }
guard ToolchainRegistry.shared.default?.clangd != nil else { return }

let refLoc = ws.testLoc("Object:ref:main")
let expectedDoc = ws.testLoc("Object").docIdentifier.uri
let refPos = Position(line: refLoc.position.line, utf16index: refLoc.utf16Column + 2)

try ws.openDocument(refLoc.url, language: .c)

let goToDefinition = DefinitionRequest(
textDocument: refLoc.docIdentifier, position: refPos)
let resp = try! ws.sk.sendSync(goToDefinition)

guard let locationsOrLinks = resp else {
XCTFail("No response for go-to-definition")
return
}
switch locationsOrLinks {
case .locations(let locations):
XCTAssert(!locations.isEmpty, "Found no locations for go-to-definition")
if let loc = locations.first {
XCTAssertEqual(loc.uri, expectedDoc)
}
case .locationLinks(let locationLinks):
XCTAssert(!locationLinks.isEmpty, "Found no location links for go-to-definition")
if let link = locationLinks.first {
XCTAssertEqual(link.targetUri, expectedDoc)
}
}
}
}
1 change: 1 addition & 0 deletions Tests/SourceKitLSPTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ extension SKTests {
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__SKTests = [
("testClangdGoToDefinitionWithoutIndex", testClangdGoToDefinitionWithoutIndex),
("testClangdGoToInclude", testClangdGoToInclude),
("testCodeCompleteSwiftTibs", testCodeCompleteSwiftTibs),
("testDependenciesUpdatedCXXTibs", testDependenciesUpdatedCXXTibs),
Expand Down