File tree Expand file tree Collapse file tree 2 files changed +20
-15
lines changed
Sources/SourceKitLSP/Swift Expand file tree Collapse file tree 2 files changed +20
-15
lines changed Original file line number Diff line number Diff line change @@ -180,7 +180,9 @@ fileprivate final class DocumentSymbolsFinder: SyntaxAnyVisitor {
180
180
// If there is only one pattern binding within the variable decl, consider the entire variable decl as the
181
181
// referenced range. If there are multiple, consider each pattern binding separately since the `var` keyword doesn't
182
182
// belong to any pattern binding in particular.
183
- guard let variableDecl = node. parent? . parent? . as ( VariableDeclSyntax . self) else {
183
+ guard let variableDecl = node. parent? . parent? . as ( VariableDeclSyntax . self) ,
184
+ variableDecl. isMemberOrTopLevelDeclaration
185
+ else {
184
186
return . visitChildren
185
187
}
186
188
let rangeNode : Syntax = variableDecl. bindings. count == 1 ? Syntax ( variableDecl) : Syntax ( node)
@@ -228,6 +230,19 @@ fileprivate extension SyntaxProtocol {
228
230
var rangeWithoutTrivia : Range < AbsolutePosition > {
229
231
return positionAfterSkippingLeadingTrivia..< endPositionBeforeTrailingTrivia
230
232
}
233
+
234
+ /// Whether this is a top-level constant or a member of a type, ie. if this is not a local variable.
235
+ var isMemberOrTopLevelDeclaration : Bool {
236
+ if self . parent? . is ( MemberBlockItemSyntax . self) ?? false {
237
+ return true
238
+ } else if let codeBlockItem = self . parent? . as ( CodeBlockItemSyntax . self) ,
239
+ codeBlockItem. parent? . parent? . is ( SourceFileSyntax . self) ?? false
240
+ {
241
+ return true
242
+ } else {
243
+ return false
244
+ }
245
+ }
231
246
}
232
247
233
248
fileprivate extension TokenKind {
Original file line number Diff line number Diff line change @@ -571,8 +571,8 @@ final class DocumentSymbolTests: XCTestCase {
571
571
try await assertDocumentSymbols (
572
572
"""
573
573
1️⃣func 2️⃣f()3️⃣ {
574
- 4️⃣let 5️⃣localConstant6️⃣ = 07️⃣
575
- }8️⃣
574
+ let localConstant = 0
575
+ }4️⃣
576
576
"""
577
577
) { positions in
578
578
[
@@ -581,19 +581,9 @@ final class DocumentSymbolTests: XCTestCase {
581
581
detail: nil ,
582
582
kind: . function,
583
583
deprecated: nil ,
584
- range: positions [ " 1️⃣ " ] ..< positions [ " 8️⃣ " ] ,
584
+ range: positions [ " 1️⃣ " ] ..< positions [ " 4️⃣ " ] ,
585
585
selectionRange: positions [ " 2️⃣ " ] ..< positions [ " 3️⃣ " ] ,
586
- children: [
587
- DocumentSymbol (
588
- name: " localConstant " ,
589
- detail: nil ,
590
- kind: . variable,
591
- deprecated: nil ,
592
- range: positions [ " 4️⃣ " ] ..< positions [ " 7️⃣ " ] ,
593
- selectionRange: positions [ " 5️⃣ " ] ..< positions [ " 6️⃣ " ] ,
594
- children: [ ]
595
- )
596
- ]
586
+ children: [ ]
597
587
)
598
588
]
599
589
}
You can’t perform that action at this time.
0 commit comments