Skip to content

Commit ec8512c

Browse files
committed
Address review comments
1 parent 06d3d5d commit ec8512c

File tree

3 files changed

+27
-54
lines changed

3 files changed

+27
-54
lines changed

Sources/LanguageServerProtocol/SupportTypes/PlaygroundItem.swift

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public struct PlaygroundItem: ResponseType, Equatable {
2020
/// Display name describing the playground.
2121
public var label: String?
2222

23-
/// The range of the playground item in the source code.
23+
/// The location of the #Playground macro expansion in the source code.
2424
public var location: Location
2525

2626
public init(
@@ -32,17 +32,4 @@ public struct PlaygroundItem: ResponseType, Equatable {
3232
self.label = label
3333
self.location = location
3434
}
35-
36-
public func encodeToLSPAny() -> LSPAny {
37-
var dict: [String: LSPAny] = [
38-
"id": .string(id),
39-
"location": location.encodeToLSPAny()
40-
]
41-
42-
if let label {
43-
dict["label"] = .string(label)
44-
}
45-
46-
return .dictionary(dict)
47-
}
4835
}

Sources/SwiftLanguageService/PlaygroundsMacro.swift

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ extension SwiftLanguageService {
2828
try Task.checkCancellation()
2929
return
3030
await PlaygroundMacroFinder.find(
31-
in: [Syntax(syntaxTree)],
31+
in: Syntax(syntaxTree),
3232
workspace: workspace,
3333
snapshot: snapshot,
34-
range: syntaxTree.position..<syntaxTree.endPosition
3534
)
3635
}
3736
}
@@ -45,39 +44,32 @@ final class PlaygroundMacroFinder: SyntaxAnyVisitor {
4544
/// The snapshot of the document for which we are getting playgrounds.
4645
private let snapshot: DocumentSnapshot
4746

48-
/// Only playgrounds that intersect with this range get reported.
49-
private let range: Range<AbsolutePosition>
50-
5147
/// Accumulating the result in here.
5248
private var result: [PlaygroundItem] = []
5349

5450
/// Keep track of if "Playgrounds" has been imported
5551
private var isPlaygroundImported: Bool = false
5652

57-
private init(baseID: String, snapshot: DocumentSnapshot, range: Range<AbsolutePosition>) {
53+
private init(baseID: String, snapshot: DocumentSnapshot) {
5854
self.baseID = baseID
5955
self.snapshot = snapshot
60-
self.range = range
6156
super.init(viewMode: .sourceAccurate)
6257
}
6358

6459
/// Designated entry point for `PlaygroundMacroFinder`.
6560
static func find(
66-
in nodes: some Sequence<Syntax>,
61+
in node: some SyntaxProtocol,
6762
workspace: Workspace,
68-
snapshot: DocumentSnapshot,
69-
range: Range<AbsolutePosition>
63+
snapshot: DocumentSnapshot
7064
) async -> [PlaygroundItem] {
7165
guard let canonicalTarget = await workspace.buildServerManager.canonicalTarget(for: snapshot.uri),
7266
let moduleName = await workspace.buildServerManager.moduleName(for: snapshot.uri, in: canonicalTarget),
7367
let baseName = snapshot.uri.fileURL?.lastPathComponent
7468
else {
7569
return []
7670
}
77-
let visitor = PlaygroundMacroFinder(baseID: "\(moduleName)/\(baseName)", snapshot: snapshot, range: range)
78-
for node in nodes {
79-
visitor.walk(node)
80-
}
71+
let visitor = PlaygroundMacroFinder(baseID: "\(moduleName)/\(baseName)", snapshot: snapshot)
72+
visitor.walk(node)
8173
return visitor.result
8274
}
8375

@@ -87,9 +79,6 @@ final class PlaygroundMacroFinder: SyntaxAnyVisitor {
8779
label: String?,
8880
range: Range<AbsolutePosition>
8981
) {
90-
if !self.range.overlaps(range) {
91-
return
92-
}
9382
let positionRange = snapshot.absolutePositionRange(of: range)
9483
let location = Location(uri: snapshot.uri, range: positionRange)
9584

Tests/SourceKitLSPTests/DocumentPlaygroundDiscoveryTests.swift

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,32 @@ import SourceKitLSP
1616
import XCTest
1717

1818
final class DocumentPlaygroundDiscoveryTests: XCTestCase {
19-
func testParsePlaygroundsTests() async throws {
19+
func testParsePlaygrounds() async throws {
2020
let project = try await SwiftPMTestProject(
2121
files: [
2222
"MyLib.swift": """
2323
import Playgrounds
2424
2525
public func foo() -> String {
26-
"bar"
26+
"bar"
2727
}
2828
2929
1️⃣#Playground("foo") {
30-
print(foo())
30+
print(foo())
3131
}2️⃣
3232
3333
3️⃣#Playground {
34-
print(foo())
34+
print(foo())
3535
}4️⃣
3636
3737
public func bar(_ i: Int, _ j: Int) -> Int {
38-
i + j
38+
i + j
3939
}
4040
4141
5️⃣#Playground("bar") {
42-
var i = bar(1, 2)
43-
i = i + 1
44-
print(i)
42+
var i = bar(1, 2)
43+
i = i + 1
44+
print(i)
4545
}6️⃣
4646
"""
4747
],
@@ -52,7 +52,6 @@ final class DocumentPlaygroundDiscoveryTests: XCTestCase {
5252
targets: [.target(name: "MyLibrary")]
5353
)
5454
""",
55-
enableBackgroundIndexing: false
5655
)
5756

5857
let (uri, positions) = try project.openDocument("MyLib.swift")
@@ -76,35 +75,35 @@ final class DocumentPlaygroundDiscoveryTests: XCTestCase {
7675
id: "MyLibrary/MyLib.swift:19",
7776
label: "bar",
7877
location: Location(uri: uri, range: positions["5️⃣"]..<positions["6️⃣"]),
79-
)
78+
),
8079
]
8180
)
8281
}
8382

84-
func testNoImportPlaygroundsTests() async throws {
83+
func testNoImportPlaygrounds() async throws {
8584
let project = try await SwiftPMTestProject(
8685
files: [
8786
"Sources/MyLibrary/MyLib.swift": """
8887
public func foo() -> String {
89-
"bar"
88+
"bar"
9089
}
9190
9291
#Playground("foo") {
93-
print(foo())
92+
print(foo())
9493
}
9594
9695
#Playground {
97-
print(foo())
96+
print(foo())
9897
}
9998
10099
public func bar(_ i: Int, _ j: Int) -> Int {
101-
i + j
100+
i + j
102101
}
103102
104103
#Playground("bar") {
105-
var i = bar(1, 2)
106-
i = i + 1
107-
print(i)
104+
var i = bar(1, 2)
105+
i = i + 1
106+
print(i)
108107
}
109108
"""
110109
],
@@ -115,7 +114,6 @@ final class DocumentPlaygroundDiscoveryTests: XCTestCase {
115114
targets: [.target(name: "MyLibrary")]
116115
)
117116
""",
118-
enableBackgroundIndexing: false
119117
)
120118

121119
let (uri, _) = try project.openDocument("MyLib.swift")
@@ -125,16 +123,16 @@ final class DocumentPlaygroundDiscoveryTests: XCTestCase {
125123
XCTAssertEqual(playgrounds, [])
126124
}
127125

128-
func testParseNoPlaygroundsTests() async throws {
126+
func testParseNoPlaygrounds() async throws {
129127
let project = try await SwiftPMTestProject(
130128
files: [
131129
"Sources/MyLibrary/MyLib.swift": """
132130
import Playgrounds
133131
134132
public func Playground(_ i: Int, _ j: Int) -> Int {
135-
i + j
133+
i + j
136134
}
137-
135+
138136
@Playground
139137
struct MyPlayground {
140138
public var playground: String = ""
@@ -148,7 +146,6 @@ final class DocumentPlaygroundDiscoveryTests: XCTestCase {
148146
targets: [.target(name: "MyLibrary")]
149147
)
150148
""",
151-
enableBackgroundIndexing: false
152149
)
153150

154151
let (uri, _) = try project.openDocument("MyLib.swift")

0 commit comments

Comments
 (0)