File tree Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -1818,7 +1818,7 @@ extension SourceKitServer {
1818
1818
position: req. position
1819
1819
)
1820
1820
)
1821
- return try await symbols. asyncMap { ( symbol) -> [ Location ] in
1821
+ let locations = try await symbols. asyncMap { ( symbol) -> [ Location ] in
1822
1822
if symbol. bestLocalDeclaration != nil && !( symbol. isDynamic ?? true )
1823
1823
&& symbol. usr? . hasPrefix ( " s: " ) ?? false /* Swift symbols have USRs starting with s: */
1824
1824
{
@@ -1830,6 +1830,11 @@ extension SourceKitServer {
1830
1830
}
1831
1831
return try await self . definitionLocations ( for: symbol, in: req. textDocument. uri, languageService: languageService)
1832
1832
} . flatMap { $0 }
1833
+ // Remove any duplicate locations. We might end up with duplicate locations when performing a definition request
1834
+ // on eg. `MyStruct()` when no explicit initializer is declared. In this case we get two symbol infos, one for the
1835
+ // declaration of the `MyStruct` type and one for the initializer, which is implicit and thus has the location of
1836
+ // the `MyStruct` declaration itself.
1837
+ return locations. unique
1833
1838
}
1834
1839
1835
1840
func definition(
@@ -2374,3 +2379,11 @@ fileprivate extension RequestID {
2374
2379
}
2375
2380
}
2376
2381
}
2382
+
2383
+ fileprivate extension Sequence where Element: Hashable {
2384
+ /// Removes all duplicate elements from the sequence, maintaining order.
2385
+ var unique : [ Element ] {
2386
+ var set = Set < Element > ( )
2387
+ return self . filter { set. insert ( $0) . inserted }
2388
+ }
2389
+ }
Original file line number Diff line number Diff line change @@ -362,4 +362,32 @@ class DefinitionTests: XCTestCase {
362
362
)
363
363
)
364
364
}
365
+
366
+ func testDefinitionOfImplicitInitializer( ) async throws {
367
+ let testClient = try await TestSourceKitLSPClient ( )
368
+ let uri = DocumentURI . for ( . swift)
369
+
370
+ let positions = testClient. openDocument (
371
+ """
372
+ class 1️⃣Foo {}
373
+
374
+ func test() {
375
+ 2️⃣Foo()
376
+ }
377
+ """ ,
378
+ uri: uri
379
+ )
380
+
381
+ let response = try await testClient. send (
382
+ DefinitionRequest ( textDocument: TextDocumentIdentifier ( uri) , position: positions [ " 2️⃣ " ] )
383
+ )
384
+ guard case . locations( let locations) = response else {
385
+ XCTFail ( " Expected locations response " )
386
+ return
387
+ }
388
+ XCTAssertEqual (
389
+ locations,
390
+ [ Location ( uri: uri, range: Range ( positions [ " 1️⃣ " ] ) ) ]
391
+ )
392
+ }
365
393
}
You can’t perform that action at this time.
0 commit comments