Skip to content

Include parameters in initializer document symbols #1852

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
Dec 5, 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
14 changes: 13 additions & 1 deletion Sources/SourceKitLSP/Swift/DocumentSymbols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ fileprivate final class DocumentSymbolsFinder: SyntaxAnyVisitor {
override func visit(_ node: InitializerDeclSyntax) -> SyntaxVisitorContinueKind {
return record(
node: node,
name: node.initKeyword.text,
name: node.declName,
symbolKind: .constructor,
range: node.rangeWithoutTrivia,
selection: node.initKeyword
Expand Down Expand Up @@ -304,6 +304,18 @@ fileprivate extension FunctionDeclSyntax {
}
}

fileprivate extension InitializerDeclSyntax {
var declName: String {
var result = self.initKeyword.text
result += "("
for parameter in self.signature.parameterClause.parameters {
result += "\(parameter.firstName.text):"
}
result += ")"
return result
}
}

fileprivate extension SyntaxProtocol {
/// The position range of this node without its leading and trailing trivia.
var rangeWithoutTrivia: Range<AbsolutePosition> {
Expand Down
34 changes: 33 additions & 1 deletion Tests/SourceKitLSPTests/DocumentSymbolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,39 @@ final class DocumentSymbolTests: XCTestCase {
selectionRange: positions["2️⃣"]..<positions["3️⃣"],
children: [
DocumentSymbol(
name: "init",
name: "init()",
detail: nil,
kind: .constructor,
deprecated: nil,
range: positions["4️⃣"]..<positions["6️⃣"],
selectionRange: positions["4️⃣"]..<positions["5️⃣"],
children: []
)
]
)
]
}
}

func testInitializerWithParameters() async throws {
try await assertDocumentSymbols(
"""
1️⃣class 2️⃣Foo3️⃣ {
4️⃣init(_ first: Int, second: Int)5️⃣ { }6️⃣
}7️⃣
"""
) { positions in
[
DocumentSymbol(
name: "Foo",
detail: nil,
kind: .class,
deprecated: nil,
range: positions["1️⃣"]..<positions["7️⃣"],
selectionRange: positions["2️⃣"]..<positions["3️⃣"],
children: [
DocumentSymbol(
name: "init(_:second:)",
detail: nil,
kind: .constructor,
deprecated: nil,
Expand Down