-
Notifications
You must be signed in to change notification settings - Fork 440
Add helper function for getting the source edits of two strings #1662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
StevenWong12
wants to merge
2
commits into
swiftlang:main
from
StevenWong12:add_helper_function_for_getting_sourceedit
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
Sources/_SwiftSyntaxTestSupport/SourceEditsTestUtils.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SwiftSyntax | ||
import XCTest | ||
|
||
/// Returns the `ConcurrentEdits`s to transition from `base` to `new`. | ||
public func getConcurrentEdits(from base: String, to new: String) -> ConcurrentEdits { | ||
let diffCollection = new.difference(from: base) | ||
|
||
let diffArray: [(offset: Int, isInsert: Bool)] = | ||
diffCollection | ||
.map { diff in | ||
switch diff { | ||
case .remove(offset: let offset, element: _, associatedWith: _): | ||
return (offset: offset, isInsert: false) | ||
case .insert(offset: let offset, element: _, associatedWith: _): | ||
return (offset: offset, isInsert: true) | ||
} | ||
} | ||
.sorted { | ||
// Change.remove should prior to Change.insert | ||
if $0.offset < $1.offset { | ||
return true | ||
} else if $0.offset == $1.offset { | ||
return !$0.isInsert | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
let sourceEdits = diffArray.map({ | ||
if $0.isInsert { | ||
return SourceEdit(offset: $0.offset, length: 0, replacementLength: 1) | ||
} else { | ||
return SourceEdit(offset: $0.offset, length: 1, replacementLength: 0) | ||
} | ||
}) | ||
|
||
return ConcurrentEdits(fromSequential: sourceEdits) | ||
} | ||
|
||
/// Apply the given edits to `testString` and return the resulting string. | ||
/// `concurrent` specifies whether the edits should be interpreted as being | ||
/// applied sequentially or concurrently. | ||
public func applyEdits( | ||
_ edits: [SourceEdit], | ||
concurrent: Bool, | ||
to testString: String, | ||
replacementChar: Character = "?" | ||
) -> String { | ||
guard let replacementAscii = replacementChar.asciiValue else { | ||
fatalError("replacementChar must be an ASCII character") | ||
} | ||
var edits = edits | ||
if concurrent { | ||
XCTAssert(ConcurrentEdits._isValidConcurrentEditArray(edits)) | ||
|
||
// If the edits are concurrent, sorted and not overlapping (as guaranteed by | ||
// the check above, we can apply them sequentially to the string in reverse | ||
// order because later edits don't affect earlier edits. | ||
edits = edits.reversed() | ||
} | ||
var bytes = Array(testString.utf8) | ||
for edit in edits { | ||
assert(edit.endOffset <= bytes.count) | ||
bytes.removeSubrange(edit.offset..<edit.endOffset) | ||
bytes.insert(contentsOf: [UInt8](repeating: replacementAscii, count: edit.replacementLength), at: edit.offset) | ||
} | ||
return String(bytes: bytes, encoding: .utf8)! | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
Tests/SwiftSyntaxTestSupportTest/SourceEditsTestUtilsTest.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import XCTest | ||
import SwiftSyntax | ||
import _SwiftSyntaxTestSupport | ||
|
||
public class SourceEditsUtilTest: XCTestCase { | ||
public func testStringDifferenceReturnSequentialEdits() { | ||
let base = "0123" | ||
let new = "a0123bc" | ||
|
||
let diffs = getConcurrentEdits(from: base, to: new) | ||
XCTAssertEqual( | ||
diffs.edits, | ||
[ | ||
SourceEdit(offset: 0, length: 0, replacementLength: 1), | ||
SourceEdit(offset: 4, length: 0, replacementLength: 2), | ||
] | ||
) | ||
|
||
let appliedDiffsBase = applyEdits(diffs.edits, concurrent: true, to: base) | ||
|
||
XCTAssertEqual(appliedDiffsBase, "?0123??") | ||
} | ||
|
||
public func testDiffOfTwoStringsSimple() throws { | ||
let base = "struct A { func f() {" | ||
let new = "struct AA { func f() {" | ||
|
||
let diffs = getConcurrentEdits(from: base, to: new) | ||
XCTAssertEqual(diffs.edits.count, 1) | ||
|
||
let firstDiff = try XCTUnwrap(diffs.edits.first) | ||
XCTAssertEqual(firstDiff, SourceEdit(offset: 8, length: 0, replacementLength: 1)) | ||
} | ||
|
||
public func testDiffOfTwoSameStrings() { | ||
let base = "0123456" | ||
|
||
let diffs = getConcurrentEdits(from: base, to: base) | ||
XCTAssert(diffs.edits.isEmpty) | ||
} | ||
|
||
public func testDiffOfTwoStrings() { | ||
let base = "0123456" | ||
let new = "x12456yz" | ||
|
||
let diffs = getConcurrentEdits(from: base, to: new) | ||
|
||
let expectedDiffs: [SourceEdit] = [ | ||
SourceEdit(offset: 0, length: 1, replacementLength: 1), | ||
SourceEdit(offset: 3, length: 1, replacementLength: 0), | ||
SourceEdit(offset: 7, length: 0, replacementLength: 2), | ||
] | ||
|
||
XCTAssertEqual(diffs.edits, expectedDiffs) | ||
|
||
let appliedDiffsBase = applyEdits(expectedDiffs, concurrent: true, to: base) | ||
|
||
XCTAssertEqual(appliedDiffsBase, "?12456??") | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add one more test case that has an insertion at the start and an insertion at the end. That would make sure that
Collection.difference
actually contains sequential edits instead of concurrent edits.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry if I wasn’t clear here. What I was looking for is a test case just like the one you have in
testDiffOfTwoStrings
(which I think has a very good test structure), just with the inputs I gave you. So basically:Which seems to pass, so that’s good ✅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great. Already added this.