Skip to content

Commit debb5dc

Browse files
Merge pull request #1852 from matthewbastien/document-symbols
Include parameters in initializer document symbols
2 parents 3131ca3 + e938dfa commit debb5dc

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

Sources/SourceKitLSP/Swift/DocumentSymbols.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ fileprivate final class DocumentSymbolsFinder: SyntaxAnyVisitor {
247247
override func visit(_ node: InitializerDeclSyntax) -> SyntaxVisitorContinueKind {
248248
return record(
249249
node: node,
250-
name: node.initKeyword.text,
250+
name: node.declName,
251251
symbolKind: .constructor,
252252
range: node.rangeWithoutTrivia,
253253
selection: node.initKeyword
@@ -304,6 +304,18 @@ fileprivate extension FunctionDeclSyntax {
304304
}
305305
}
306306

307+
fileprivate extension InitializerDeclSyntax {
308+
var declName: String {
309+
var result = self.initKeyword.text
310+
result += "("
311+
for parameter in self.signature.parameterClause.parameters {
312+
result += "\(parameter.firstName.text):"
313+
}
314+
result += ")"
315+
return result
316+
}
317+
}
318+
307319
fileprivate extension SyntaxProtocol {
308320
/// The position range of this node without its leading and trailing trivia.
309321
var rangeWithoutTrivia: Range<AbsolutePosition> {

Tests/SourceKitLSPTests/DocumentSymbolTests.swift

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,39 @@ final class DocumentSymbolTests: XCTestCase {
552552
selectionRange: positions["2️⃣"]..<positions["3️⃣"],
553553
children: [
554554
DocumentSymbol(
555-
name: "init",
555+
name: "init()",
556+
detail: nil,
557+
kind: .constructor,
558+
deprecated: nil,
559+
range: positions["4️⃣"]..<positions["6️⃣"],
560+
selectionRange: positions["4️⃣"]..<positions["5️⃣"],
561+
children: []
562+
)
563+
]
564+
)
565+
]
566+
}
567+
}
568+
569+
func testInitializerWithParameters() async throws {
570+
try await assertDocumentSymbols(
571+
"""
572+
1️⃣class 2️⃣Foo3️⃣ {
573+
4️⃣init(_ first: Int, second: Int)5️⃣ { }6️⃣
574+
}7️⃣
575+
"""
576+
) { positions in
577+
[
578+
DocumentSymbol(
579+
name: "Foo",
580+
detail: nil,
581+
kind: .class,
582+
deprecated: nil,
583+
range: positions["1️⃣"]..<positions["7️⃣"],
584+
selectionRange: positions["2️⃣"]..<positions["3️⃣"],
585+
children: [
586+
DocumentSymbol(
587+
name: "init(_:second:)",
556588
detail: nil,
557589
kind: .constructor,
558590
deprecated: nil,

0 commit comments

Comments
 (0)