Skip to content

Commit cca2177

Browse files
committed
De-duplicate results from find references
We might have duplicate results for multiple references inside a macro. In this case we only want to show the macro attribute or macro expansion expr/decl once. Fixes #1047 rdar://122237704
1 parent 331f16d commit cca2177

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

Sources/SourceKitLSP/SourceKitServer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2000,7 +2000,7 @@ extension SourceKitServer {
20002000
}
20012001
return index.occurrences(ofUSR: usr, roles: roles).compactMap { indexToLSPLocation($0.location) }
20022002
}
2003-
return locations.sorted()
2003+
return locations.unique.sorted()
20042004
}
20052005

20062006
private func indexToLSPCallHierarchyItem(
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import LanguageServerProtocol
14+
import SKTestSupport
15+
import XCTest
16+
17+
/// Tests that test the overall state of the SourceKit-LSP server, that's not really specific to any language
18+
final class ReferencesTests: XCTestCase {
19+
func testReferencesInMacro() async throws {
20+
let ws = try await IndexedSingleSwiftFileWorkspace(
21+
"""
22+
import Observation
23+
24+
@available(macOS 14.0, *)
25+
1️⃣@Observable
26+
class 2️⃣Foo {
27+
var x: Int = 2
28+
}
29+
"""
30+
)
31+
32+
let response = try await ws.testClient.send(
33+
ReferencesRequest(
34+
textDocument: TextDocumentIdentifier(ws.fileURI),
35+
position: ws.positions["2️⃣"],
36+
context: ReferencesContext(includeDeclaration: true)
37+
)
38+
)
39+
XCTAssertEqual(
40+
response,
41+
[
42+
Location(uri: ws.fileURI, range: Range(ws.positions["1️⃣"])),
43+
Location(uri: ws.fileURI, range: Range(ws.positions["2️⃣"])),
44+
]
45+
)
46+
}
47+
}

0 commit comments

Comments
 (0)