Skip to content

Commit 63171ab

Browse files
authored
Merge pull request #1178 from ahoppen/nil-for-empty-prepare-type-hierarchy
Instead of returning an empty array in `prepareTypeHierarchy`, sourcekit-lsp should return `nil`
2 parents e13ce13 + f482982 commit 63171ab

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Sources/SourceKitLSP/SourceKitLSPServer.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,6 +2291,13 @@ extension SourceKitLSPServer {
22912291
}
22922292
.sorted(by: { $0.name < $1.name })
22932293

2294+
if typeHierarchyItems.isEmpty {
2295+
// When returning an empty array, VS Code fails with the following two errors. Returning `nil` works around those
2296+
// VS Code-internal errors showing up
2297+
// - MISSING provider
2298+
// - Cannot read properties of null (reading 'kind')
2299+
return nil
2300+
}
22942301
// Ideally, we should show multiple symbols. But VS Code fails to display type hierarchies with multiple root items,
22952302
// failing with `Cannot read properties of undefined (reading 'map')`. Pick the first one.
22962303
return Array(typeHierarchyItems.prefix(1))

Tests/SourceKitLSPTests/TypeHierarchyTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import ISDBTestSupport
14+
import LSPTestSupport
1415
import LanguageServerProtocol
1516
import SKTestSupport
1617
import TSCBasic
@@ -185,6 +186,28 @@ final class TypeHierarchyTests: XCTestCase {
185186
]
186187
)
187188
}
189+
190+
func testTypeHierarchyForFileWithoutIndex() async throws {
191+
let project = try await SwiftPMTestProject(files: [
192+
"Test.swift": """
193+
class MyClass {}
194+
class 1️⃣MySubclass: MyClass {}
195+
196+
let x = MySubclass()
197+
"""
198+
])
199+
200+
let (uri, positions) = try project.openDocument("Test.swift")
201+
202+
// We don't have the type hierarchy because the file hasn't been indexed yet. Returning an empty array makes VS Code
203+
// fail with the following two errors, so we should return `nil` instead.
204+
// - MISSING provider
205+
// - Cannot read properties of null (reading 'kind')
206+
let response = try await project.testClient.send(
207+
TypeHierarchyPrepareRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
208+
)
209+
XCTAssertNil(response)
210+
}
188211
}
189212

190213
// MARK: - Utilities

0 commit comments

Comments
 (0)