@@ -1752,10 +1752,7 @@ extension SourceKitServer {
1752
1752
}
1753
1753
guard let usr = symbol. usr else { return [ ] }
1754
1754
logger. info ( " performing indexed jump-to-def with usr \( usr) " )
1755
- var occurrences = index. occurrences ( ofUSR: usr, roles: [ . definition] )
1756
- if occurrences. isEmpty {
1757
- occurrences = index. occurrences ( ofUSR: usr, roles: [ . declaration] )
1758
- }
1755
+ var occurrences = index. definitionOrDeclarationOccurances ( ofUSR: usr)
1759
1756
if symbol. isDynamic ?? true {
1760
1757
lazy var transitiveReceiverUsrs : [ String ] ? = {
1761
1758
if let receiverUsrs = symbol. receiverUsrs {
@@ -1963,17 +1960,23 @@ extension SourceKitServer {
1963
1960
}
1964
1961
// For call hierarchy preparation we only locate the definition
1965
1962
guard let usr = symbol. usr else { return nil }
1966
- return index. occurrences ( ofUSR: usr, roles: [ . definition, . declaration] )
1967
- . compactMap { info -> CallHierarchyItem ? in
1968
- guard let location = indexToLSPLocation ( info. location) else {
1969
- return nil
1970
- }
1971
- return self . indexToLSPCallHierarchyItem (
1972
- symbol: info. symbol,
1973
- moduleName: info. location. moduleName,
1974
- location: location
1975
- )
1976
- }
1963
+
1964
+ // Only return a single call hierarchy item. Returning multiple doesn't make sense because they will all have the
1965
+ // same USR (because we query them by USR) and will thus expand to the exact same call hierarchy.
1966
+ // Also, VS Code doesn't seem to like multiple call hiearchy items being returned and fails to display any results
1967
+ // if they are, failing with `Cannot read properties of undefined (reading 'map')`.
1968
+ guard let definition = index. definitionOrDeclarationOccurrences ( ofUSR: usr) . first else {
1969
+ return nil
1970
+ }
1971
+ guard let location = indexToLSPLocation ( definition. location) else {
1972
+ return nil
1973
+ }
1974
+ let callHierachyItem = self . indexToLSPCallHierarchyItem (
1975
+ symbol: definition. symbol,
1976
+ moduleName: definition. location. moduleName,
1977
+ location: location
1978
+ )
1979
+ return [ callHierachyItem]
1977
1980
}
1978
1981
1979
1982
/// Extracts our implementation-specific data about a call hierarchy
@@ -2255,6 +2258,18 @@ private let maxWorkspaceSymbolResults = 4096
2255
2258
2256
2259
public typealias Diagnostic = LanguageServerProtocol . Diagnostic
2257
2260
2261
+ fileprivate extension IndexStoreDB {
2262
+ /// If there are any definition occurrences of the given USR, return these.
2263
+ /// Otherwise return declaration occurrences.
2264
+ func definitionOrDeclarationOccurrences( ofUSR usr: String ) -> [ SymbolOccurrence ] {
2265
+ let definitions = occurrences ( ofUSR: usr, roles: [ . definition] )
2266
+ if !definitions. isEmpty {
2267
+ return definitions
2268
+ }
2269
+ return occurrences ( ofUSR: usr, roles: [ . declaration] )
2270
+ }
2271
+ }
2272
+
2258
2273
extension IndexSymbolKind {
2259
2274
func asLspSymbolKind( ) -> SymbolKind {
2260
2275
switch self {
0 commit comments