Skip to content

Show container name in call hierarchy item name instead of detail field #1396

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 1 commit into from
Jun 3, 2024
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
34 changes: 29 additions & 5 deletions Sources/SourceKitLSP/SourceKitLSPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1851,11 +1851,28 @@ extension SourceKitLSPServer {
containerName: String?,
location: Location
) -> CallHierarchyItem {
CallHierarchyItem(
name: symbol.name,
let name: String
if let containerName {
switch symbol.language {
case .objc where symbol.kind == .instanceMethod || symbol.kind == .instanceProperty:
name = "-[\(containerName) \(symbol.name)]"
case .objc where symbol.kind == .classMethod || symbol.kind == .classProperty:
name = "+[\(containerName) \(symbol.name)]"
case .cxx, .c, .objc:
// C shouldn't have container names for call hierarchy and Objective-C should be covered above.
// Fall back to using the C++ notation using `::`.
name = "\(containerName)::\(symbol.name)"
case .swift:
name = "\(containerName).\(symbol.name)"
}
} else {
name = symbol.name
}
return CallHierarchyItem(
name: name,
kind: symbol.kind.asLspSymbolKind(),
tags: nil,
detail: containerName,
detail: nil,
uri: location.uri,
range: location.range,
selectionRange: location.range,
Expand Down Expand Up @@ -2289,8 +2306,15 @@ extension IndexSymbolKind {
return .struct
case .parameter:
return .typeParameter

default:
case .module, .namespace:
return .namespace
case .field:
return .property
case .constructor:
return .constructor
case .destructor:
return .null
case .commentTag, .concept, .extension, .macro, .namespaceAlias, .typealias, .union, .unknown, .using:
return .null
}
}
Expand Down
94 changes: 90 additions & 4 deletions Tests/SourceKitLSPTests/CallHierarchyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,9 @@ final class CallHierarchyTests: XCTestCase {
result,
[
CallHierarchyItem(
name: "foo",
name: "FilePathIndex::foo",
kind: .method,
tags: nil,
detail: "FilePathIndex",
uri: try project.uri(for: "lib.cpp"),
range: try Range(project.position(of: "2️⃣", in: "lib.cpp")),
selectionRange: try Range(project.position(of: "2️⃣", in: "lib.cpp")),
Expand Down Expand Up @@ -583,10 +582,9 @@ final class CallHierarchyTests: XCTestCase {
[
CallHierarchyIncomingCall(
from: CallHierarchyItem(
name: "foo()",
name: "MyClass.foo()",
kind: .method,
tags: nil,
detail: "MyClass",
uri: project.fileURI,
range: Range(project.positions["1️⃣"]),
selectionRange: Range(project.positions["1️⃣"]),
Expand Down Expand Up @@ -764,4 +762,92 @@ final class CallHierarchyTests: XCTestCase {
]
)
}

func testInitializerInCallHierarchy() async throws {
try await SkipUnless.indexOnlyHasContainedByRelationsToIndexedDecls()
let project = try await IndexedSingleSwiftFileTestProject(
"""
func 1️⃣foo() {}

struct Bar {
2️⃣init() {
3️⃣foo()
}
}
"""
)
let prepare = try await project.testClient.send(
CallHierarchyPrepareRequest(
textDocument: TextDocumentIdentifier(project.fileURI),
position: project.positions["1️⃣"]
)
)
let initialItem = try XCTUnwrap(prepare?.only)
let calls = try await project.testClient.send(CallHierarchyIncomingCallsRequest(item: initialItem))
XCTAssertEqual(
calls,
[
CallHierarchyIncomingCall(
from: CallHierarchyItem(
name: "Bar.init()",
kind: .constructor,
tags: nil,
uri: project.fileURI,
range: Range(project.positions["2️⃣"]),
selectionRange: Range(project.positions["2️⃣"]),
data: .dictionary([
"usr": .string("s:4test3BarVACycfc"),
"uri": .string(project.fileURI.stringValue),
])
),
fromRanges: [Range(project.positions["3️⃣"])]
)
]
)
}

func testCallHierarchyOfNestedClass() async throws {
try await SkipUnless.indexOnlyHasContainedByRelationsToIndexedDecls()
let project = try await IndexedSingleSwiftFileTestProject(
"""
func 1️⃣foo() {}

struct Outer {
struct Bar {
2️⃣init() {
3️⃣foo()
}
}
}
"""
)
let prepare = try await project.testClient.send(
CallHierarchyPrepareRequest(
textDocument: TextDocumentIdentifier(project.fileURI),
position: project.positions["1️⃣"]
)
)
let initialItem = try XCTUnwrap(prepare?.only)
let calls = try await project.testClient.send(CallHierarchyIncomingCallsRequest(item: initialItem))
XCTAssertEqual(
calls,
[
CallHierarchyIncomingCall(
from: CallHierarchyItem(
name: "Bar.init()",
kind: .constructor,
tags: nil,
uri: project.fileURI,
range: Range(project.positions["2️⃣"]),
selectionRange: Range(project.positions["2️⃣"]),
data: .dictionary([
"usr": .string("s:4test5OuterV3BarVAEycfc"),
"uri": .string(project.fileURI.stringValue),
])
),
fromRanges: [Range(project.positions["3️⃣"])]
)
]
)
}
}