Skip to content

Commit 9388caf

Browse files
authored
Merge pull request #1396 from ahoppen/container-in-call-hierarchy-name
Show container name in call hierarchy item name instead of detail field
2 parents 14775e8 + 532a37b commit 9388caf

File tree

2 files changed

+119
-9
lines changed

2 files changed

+119
-9
lines changed

Sources/SourceKitLSP/SourceKitLSPServer.swift

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,11 +1852,28 @@ extension SourceKitLSPServer {
18521852
containerName: String?,
18531853
location: Location
18541854
) -> CallHierarchyItem {
1855-
CallHierarchyItem(
1856-
name: symbol.name,
1855+
let name: String
1856+
if let containerName {
1857+
switch symbol.language {
1858+
case .objc where symbol.kind == .instanceMethod || symbol.kind == .instanceProperty:
1859+
name = "-[\(containerName) \(symbol.name)]"
1860+
case .objc where symbol.kind == .classMethod || symbol.kind == .classProperty:
1861+
name = "+[\(containerName) \(symbol.name)]"
1862+
case .cxx, .c, .objc:
1863+
// C shouldn't have container names for call hierarchy and Objective-C should be covered above.
1864+
// Fall back to using the C++ notation using `::`.
1865+
name = "\(containerName)::\(symbol.name)"
1866+
case .swift:
1867+
name = "\(containerName).\(symbol.name)"
1868+
}
1869+
} else {
1870+
name = symbol.name
1871+
}
1872+
return CallHierarchyItem(
1873+
name: name,
18571874
kind: symbol.kind.asLspSymbolKind(),
18581875
tags: nil,
1859-
detail: containerName,
1876+
detail: nil,
18601877
uri: location.uri,
18611878
range: location.range,
18621879
selectionRange: location.range,
@@ -2290,8 +2307,15 @@ extension IndexSymbolKind {
22902307
return .struct
22912308
case .parameter:
22922309
return .typeParameter
2293-
2294-
default:
2310+
case .module, .namespace:
2311+
return .namespace
2312+
case .field:
2313+
return .property
2314+
case .constructor:
2315+
return .constructor
2316+
case .destructor:
2317+
return .null
2318+
case .commentTag, .concept, .extension, .macro, .namespaceAlias, .typealias, .union, .unknown, .using:
22952319
return .null
22962320
}
22972321
}

Tests/SourceKitLSPTests/CallHierarchyTests.swift

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,9 @@ final class CallHierarchyTests: XCTestCase {
212212
result,
213213
[
214214
CallHierarchyItem(
215-
name: "foo",
215+
name: "FilePathIndex::foo",
216216
kind: .method,
217217
tags: nil,
218-
detail: "FilePathIndex",
219218
uri: try project.uri(for: "lib.cpp"),
220219
range: try Range(project.position(of: "2️⃣", in: "lib.cpp")),
221220
selectionRange: try Range(project.position(of: "2️⃣", in: "lib.cpp")),
@@ -583,10 +582,9 @@ final class CallHierarchyTests: XCTestCase {
583582
[
584583
CallHierarchyIncomingCall(
585584
from: CallHierarchyItem(
586-
name: "foo()",
585+
name: "MyClass.foo()",
587586
kind: .method,
588587
tags: nil,
589-
detail: "MyClass",
590588
uri: project.fileURI,
591589
range: Range(project.positions["1️⃣"]),
592590
selectionRange: Range(project.positions["1️⃣"]),
@@ -764,4 +762,92 @@ final class CallHierarchyTests: XCTestCase {
764762
]
765763
)
766764
}
765+
766+
func testInitializerInCallHierarchy() async throws {
767+
try await SkipUnless.indexOnlyHasContainedByRelationsToIndexedDecls()
768+
let project = try await IndexedSingleSwiftFileTestProject(
769+
"""
770+
func 1️⃣foo() {}
771+
772+
struct Bar {
773+
2️⃣init() {
774+
3️⃣foo()
775+
}
776+
}
777+
"""
778+
)
779+
let prepare = try await project.testClient.send(
780+
CallHierarchyPrepareRequest(
781+
textDocument: TextDocumentIdentifier(project.fileURI),
782+
position: project.positions["1️⃣"]
783+
)
784+
)
785+
let initialItem = try XCTUnwrap(prepare?.only)
786+
let calls = try await project.testClient.send(CallHierarchyIncomingCallsRequest(item: initialItem))
787+
XCTAssertEqual(
788+
calls,
789+
[
790+
CallHierarchyIncomingCall(
791+
from: CallHierarchyItem(
792+
name: "Bar.init()",
793+
kind: .constructor,
794+
tags: nil,
795+
uri: project.fileURI,
796+
range: Range(project.positions["2️⃣"]),
797+
selectionRange: Range(project.positions["2️⃣"]),
798+
data: .dictionary([
799+
"usr": .string("s:4test3BarVACycfc"),
800+
"uri": .string(project.fileURI.stringValue),
801+
])
802+
),
803+
fromRanges: [Range(project.positions["3️⃣"])]
804+
)
805+
]
806+
)
807+
}
808+
809+
func testCallHierarchyOfNestedClass() async throws {
810+
try await SkipUnless.indexOnlyHasContainedByRelationsToIndexedDecls()
811+
let project = try await IndexedSingleSwiftFileTestProject(
812+
"""
813+
func 1️⃣foo() {}
814+
815+
struct Outer {
816+
struct Bar {
817+
2️⃣init() {
818+
3️⃣foo()
819+
}
820+
}
821+
}
822+
"""
823+
)
824+
let prepare = try await project.testClient.send(
825+
CallHierarchyPrepareRequest(
826+
textDocument: TextDocumentIdentifier(project.fileURI),
827+
position: project.positions["1️⃣"]
828+
)
829+
)
830+
let initialItem = try XCTUnwrap(prepare?.only)
831+
let calls = try await project.testClient.send(CallHierarchyIncomingCallsRequest(item: initialItem))
832+
XCTAssertEqual(
833+
calls,
834+
[
835+
CallHierarchyIncomingCall(
836+
from: CallHierarchyItem(
837+
name: "Bar.init()",
838+
kind: .constructor,
839+
tags: nil,
840+
uri: project.fileURI,
841+
range: Range(project.positions["2️⃣"]),
842+
selectionRange: Range(project.positions["2️⃣"]),
843+
data: .dictionary([
844+
"usr": .string("s:4test5OuterV3BarVAEycfc"),
845+
"uri": .string(project.fileURI.stringValue),
846+
])
847+
),
848+
fromRanges: [Range(project.positions["3️⃣"])]
849+
)
850+
]
851+
)
852+
}
767853
}

0 commit comments

Comments
 (0)