Skip to content

Commit 83aeca0

Browse files
committed
Update the code per comments
Simplify the test Move the SourceEdit.swift from module SwiftDiagnostics to module SwiftSyntax Add copyright to new files
1 parent 96c7ddc commit 83aeca0

File tree

4 files changed

+45
-38
lines changed

4 files changed

+45
-38
lines changed

Sources/SwiftDiagnostics/FixIt.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,21 @@ public extension FixIt {
5151
var edits: [SourceEdit] {
5252
var existingEdits = [SourceEdit]()
5353
for change in changes {
54-
let edit = edit(from: change)
55-
var isOverlapping = false
56-
for existingEdit in existingEdits {
54+
let edit = SourceEdit.edit(from: change)
55+
let isOverlapping = existingEdits.contains { edit.range.overlaps($0.range) }
56+
if !isOverlapping {
5757
// The edit overlaps with the previous edit. We can't apply both
5858
// without conflicts. Apply the one that's listed first and drop the
5959
// later edit.
60-
if edit.range.overlaps(existingEdit.range) {
61-
isOverlapping = true
62-
break
63-
}
64-
}
65-
if !isOverlapping {
6660
existingEdits.append(edit)
6761
}
6862
}
6963
return existingEdits
7064
}
65+
}
7166

72-
private func edit(from change: Change) -> SourceEdit {
67+
private extension SourceEdit {
68+
static func edit(from change: FixIt.Change) -> SourceEdit {
7369
switch change {
7470
case .replace(let oldNode, let newNode):
7571
return SourceEdit(

Sources/SwiftRefactor/RefactoringProvider.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import SwiftSyntax
14-
import SwiftDiagnostics
15-
16-
@available(*, deprecated, message: "SourceEdit has been moved to the SwiftDiagnostics module")
17-
public typealias SourceEdit = SwiftDiagnostics.SourceEdit
1814

1915
/// A refactoring expressed as textual edits on the original syntax tree. In
2016
/// general clients should prefer `SyntaxRefactoringProvider` where possible.

Sources/SwiftDiagnostics/SourceEdit.swift renamed to Sources/SwiftSyntax/SourceEdit.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
import SwiftSyntax
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 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+
//===----------------------------------------------------------------------===//
212

313
/// A textual edit to the original source represented by a range and a
414
/// replacement.
Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 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+
113
import SwiftParser
214
import SwiftParserDiagnostics
315
import SwiftSyntax
416
import XCTest
517
import _SwiftSyntaxTestSupport
618

719
final class FixItTests: XCTestCase {
8-
func testEditsForFixIt() {
9-
let testCases: [[(range: Range<Int>, replacement: String)]] = [
10-
[(9..<15, "Multiident "), (15..<21, "")],
11-
[(9..<15, "MultiIdent "), (15..<21, "")]
12-
]
20+
func testEditsForFixIt() throws {
21+
let markedSource = "protocol 0️⃣Multi 1️⃣ident 2️⃣{}"
22+
let (markers, source) = extractMarkers(markedSource)
23+
let positions = ["0️⃣", "1️⃣", "2️⃣"].compactMap { markers[$0] }
24+
XCTAssertEqual(positions.count, 3)
1325

14-
let markedSource = "protocol Multi 1️⃣ident {}"
15-
let (_, source) = extractMarkers(markedSource)
26+
let expectedEdits = [
27+
SourceEdit(range: AbsolutePosition(utf8Offset: positions[0]) ..< AbsolutePosition(utf8Offset: positions[1]), replacement: "Multiident "),
28+
SourceEdit(range: AbsolutePosition(utf8Offset: positions[1]) ..< AbsolutePosition(utf8Offset: positions[2]), replacement: "")
29+
]
1630
let tree = Parser.parse(source: source)
1731
let diags = ParseDiagnosticsGenerator.diagnostics(for: tree)
1832
XCTAssertEqual(diags.count, 1)
19-
let diag = diags.first!
33+
let diag = try XCTUnwrap(diags.first)
2034
XCTAssertEqual(diag.fixIts.count, 2)
21-
22-
for i in 0..<diag.fixIts.count {
23-
let fixIt = diag.fixIts[i]
24-
let changes = fixIt.changes
25-
let edits = fixIt.edits
26-
XCTAssertNotEqual(changes.count, edits.count)
27-
let expctedEdits = testCases[i]
28-
XCTAssertEqual(expctedEdits.count, edits.count)
29-
for j in 0..<edits.count {
30-
let edit = edits[j]
31-
let expectedEdit = expctedEdits[j]
32-
XCTAssertEqual(edit.range, AbsolutePosition(utf8Offset: expectedEdit.range.lowerBound)..<AbsolutePosition(utf8Offset: expectedEdit.range.upperBound))
33-
XCTAssertEqual(edit.replacement, expectedEdit.replacement)
34-
}
35-
}
35+
36+
let fixIt = try XCTUnwrap(diag.fixIts.first)
37+
let changes = fixIt.changes
38+
let edits = fixIt.edits
39+
XCTAssertNotEqual(changes.count, edits.count)
40+
XCTAssertEqual(expectedEdits, edits)
3641
}
3742
}

0 commit comments

Comments
 (0)