@@ -2260,6 +2260,36 @@ extension SourceKitLSPServer {
22602260 )
22612261 }
22622262
2263+ private func callHierarchyItemAdjustedForCopiedFiles(
2264+ _ item: CallHierarchyItem ,
2265+ workspace: Workspace
2266+ ) async -> CallHierarchyItem {
2267+ let adjustedLocation = await workspace. buildServerManager. locationAdjustedForCopiedFiles (
2268+ Location ( uri: item. uri, range: item. range)
2269+ )
2270+ let adjustedSelectionLocation = await workspace. buildServerManager. locationAdjustedForCopiedFiles (
2271+ Location ( uri: item. uri, range: item. selectionRange)
2272+ )
2273+ return CallHierarchyItem (
2274+ name: item. name,
2275+ kind: item. kind,
2276+ tags: item. tags,
2277+ detail: item. detail,
2278+ uri: adjustedLocation. uri,
2279+ range: adjustedLocation. range,
2280+ selectionRange: adjustedSelectionLocation. range,
2281+ data: . dictionary( [
2282+ " usr " : item. data. flatMap { data in
2283+ if case let . dictionary( dict) = data {
2284+ return dict [ " usr " ]
2285+ }
2286+ return nil
2287+ } ?? . null,
2288+ " uri " : . string( adjustedLocation. uri. stringValue) ,
2289+ ] )
2290+ )
2291+ }
2292+
22632293 func prepareCallHierarchy(
22642294 _ req: CallHierarchyPrepareRequest ,
22652295 workspace: Workspace ,
@@ -2284,31 +2314,11 @@ extension SourceKitLSPServer {
22842314 guard let definition = index. primaryDefinitionOrDeclarationOccurrence ( ofUSR: usr) else {
22852315 continue
22862316 }
2287- let location = indexToLSPLocation ( definition. location)
2288- guard let originalLocation = location else {
2317+ guard let item = indexToLSPCallHierarchyItem ( definition: definition, index: index) else {
22892318 continue
22902319 }
2291- let remappedLocation = await workspace. buildServerManager. locationAdjustedForCopiedFiles ( originalLocation)
2292-
2293- // Create a new CallHierarchyItem with the remapped location (similar to how we handle TypeHierarchyItem)
2294- let name = index. fullyQualifiedName ( of: definition)
2295- let symbol = definition. symbol
2296-
2297- let item = CallHierarchyItem (
2298- name: name,
2299- kind: symbol. kind. asLspSymbolKind ( ) ,
2300- tags: nil ,
2301- detail: nil ,
2302- uri: remappedLocation. uri,
2303- range: remappedLocation. range,
2304- selectionRange: remappedLocation. range,
2305- // We encode usr and uri for incoming/outgoing call lookups in the implementation-specific data field
2306- data: . dictionary( [
2307- " usr " : . string( symbol. usr) ,
2308- " uri " : . string( remappedLocation. uri. stringValue) ,
2309- ] )
2310- )
2311- callHierarchyItems. append ( item)
2320+ let adjustedItem = await callHierarchyItemAdjustedForCopiedFiles ( item, workspace: workspace)
2321+ callHierarchyItems. append ( adjustedItem)
23122322 }
23132323 callHierarchyItems. sort ( by: { Location ( uri: $0. uri, range: $0. range) < Location ( uri: $1. uri, range: $1. range) } )
23142324
@@ -2358,7 +2368,7 @@ extension SourceKitLSPServer {
23582368 var callersToCalls : [ Symbol : [ SymbolOccurrence ] ] = [ : ]
23592369
23602370 for call in callOccurrences {
2361- // Callers are all `calledBy ` relations of a call to a USR in `callableUSrs`, ie. all the functions that contain a
2371+ // Callers are all `containedBy ` relations of a call to a USR in `callableUSrs`, ie. all the functions that contain a
23622372 // call to a USR in callableUSRs. In practice, this should always be a single item.
23632373 let callers = call. relations. filter { $0. roles. contains ( . containedBy) } . map ( \. symbol)
23642374 for caller in callers {
@@ -2506,32 +2516,22 @@ extension SourceKitLSPServer {
25062516 }
25072517
25082518 let symbol = definition. symbol
2509- switch symbol. kind {
2510- case . extension:
2511- // Query the conformance added by this extension
2512- let conformances = index. occurrences ( relatedToUSR: symbol. usr, roles: . baseOf)
2513- if conformances. isEmpty {
2514- name = symbol. name
2515- } else {
2516- name = " \( symbol. name) : \( conformances. map ( \. symbol. name) . sorted ( ) . joined ( separator: " , " ) ) "
2517- }
2518- // Add the file name and line to the detail string
2519- if let url = remappedLocation. uri. fileURL,
2520- let basename = ( try ? AbsolutePath ( validating: url. filePath) ) ? . basename
2521- {
2522- detail = " Extension at \( basename) : \( remappedLocation. range. lowerBound. line + 1 ) "
2523- } else if !definition. location. moduleName. isEmpty {
2524- detail = " Extension in \( definition. location. moduleName) "
2525- } else {
2526- detail = " Extension "
2527- }
2519+ switch symbol. kind {
2520+ case . extension:
2521+ // Query the conformance added by this extension
2522+ let conformances = index. occurrences ( relatedToUSR: symbol. usr, roles: . baseOf)
2523+ if conformances. isEmpty {
2524+ name = symbol. name
2525+ } else {
2526+ name = " \( symbol. name) : \( conformances. map ( \. symbol. name) . sorted ( ) . joined ( separator: " , " ) ) "
2527+ }
25282528 // Add the file name and line to the detail string
25292529 if let url = location. uri. fileURL,
25302530 let basename = ( try ? AbsolutePath ( validating: url. filePath) ) ? . basename
25312531 {
25322532 detail = " Extension at \( basename) : \( location. range. lowerBound. line + 1 ) "
2533- } else if let moduleName = moduleName {
2534- detail = " Extension in \( moduleName) "
2533+ } else if !definition . location . moduleName. isEmpty {
2534+ detail = " Extension in \( definition . location . moduleName) "
25352535 } else {
25362536 detail = " Extension "
25372537 }
@@ -2602,55 +2602,16 @@ extension SourceKitLSPServer {
26022602 }
26032603
26042604 let location = indexToLSPLocation ( info. location)
2605- guard let originalLocation = location else {
2605+ guard location != nil else {
26062606 continue
26072607 }
2608- let remappedLocation = await workspace. buildServerManager. locationAdjustedForCopiedFiles ( originalLocation)
2609-
2610- // Create a new TypeHierarchyItem with the original location, then adjust for copied files
2611- let name : String
2612- let detail : String ?
2613- let symbol = info. symbol
2614- switch symbol. kind {
2615- case . extension:
2616- // Query the conformance added by this extension
2617- let conformances = index. occurrences ( relatedToUSR: symbol. usr, roles: . baseOf)
2618- if conformances. isEmpty {
2619- name = symbol. name
2620- } else {
2621- name = " \( symbol. name) : \( conformances. map ( \. symbol. name) . sorted ( ) . joined ( separator: " , " ) ) "
2622- }
2623- // Add the file name and line to the detail string
2624- if let url = remappedLocation. uri. fileURL,
2625- let basename = ( try ? AbsolutePath ( validating: url. filePath) ) ? . basename
2626- {
2627- detail = " Extension at \( basename) : \( remappedLocation. range. lowerBound. line + 1 ) "
2628- } else if !info. location. moduleName. isEmpty {
2629- detail = " Extension in \( info. location. moduleName) "
2630- } else {
2631- detail = " Extension "
2632- }
2633- default :
2634- name = index. fullyQualifiedName ( of: info)
2635- detail = info. location. moduleName
2636- }
2637-
2638- let item = TypeHierarchyItem (
2639- name: name,
2640- kind: symbol. kind. asLspSymbolKind ( ) ,
2641- tags: nil ,
2642- detail: detail,
2643- uri: originalLocation. uri,
2644- range: originalLocation. range,
2645- selectionRange: originalLocation. range,
2646- // We encode usr and uri for incoming/outgoing type lookups in the implementation-specific data field
2647- data: . dictionary( [
2648- " usr " : . string( symbol. usr) ,
2649- " uri " : . string( originalLocation. uri. stringValue) ,
2650- ] )
2651- )
2652- let remappedItem = await workspace. buildServerManager. typeHierarchyItemAdjustedForCopiedFiles ( item)
2653- typeHierarchyItems. append ( remappedItem)
2608+
2609+ let moduleName = info. location. moduleName. isEmpty ? nil : info. location. moduleName
2610+ guard let item = indexToLSPTypeHierarchyItem ( definition: info, moduleName: moduleName, index: index) else {
2611+ continue
2612+ }
2613+ let remappedItem = await workspace. buildServerManager. typeHierarchyItemAdjustedForCopiedFiles ( item)
2614+ typeHierarchyItems. append ( remappedItem)
26542615 }
26552616 typeHierarchyItems. sort ( by: { $0. name < $1. name } )
26562617
@@ -2754,10 +2715,10 @@ extension SourceKitLSPServer {
27542715 name = " \( symbol. name) : \( conformances. map ( \. symbol. name) . sorted ( ) . joined ( separator: " , " ) ) "
27552716 }
27562717 // Add the file name and line to the detail string
2757- if let url = adjustedLocation . uri. fileURL,
2718+ if let url = remappedLocation . uri. fileURL,
27582719 let basename = ( try ? AbsolutePath ( validating: url. filePath) ) ? . basename
27592720 {
2760- detail = " Extension at \( basename) : \( adjustedLocation . range. lowerBound. line + 1 ) "
2721+ detail = " Extension at \( basename) : \( remappedLocation . range. lowerBound. line + 1 ) "
27612722 } else if !definition. location. moduleName. isEmpty {
27622723 detail = " Extension in \( definition. location. moduleName) "
27632724 } else {
0 commit comments