Skip to content

Commit

Permalink
Reduce spurious change tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Aug 24, 2020
1 parent 2010510 commit c0accac
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Sources/Formatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@ public extension Formatter {

/// Replaces the token at the specified index with a new token
func replaceToken(at index: Int, with token: Token) {
if token != tokens[index] {
if trackChanges, token.string != tokens[index].string {
trackChange(at: index)
tokens[index] = token
}
tokens[index] = token
}

/// Replaces the tokens in the specified range with new tokens
Expand Down
6 changes: 4 additions & 2 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,8 @@ public struct _FormatRules {

/// Remove blank lines immediately after an opening brace, bracket, paren or chevron
public let blankLinesAtStartOfScope = FormatRule(
help: "Remove leading blank line at the start of a scope."
help: "Remove leading blank line at the start of a scope.",
orderAfter: ["organizeDeclarations"]
) { formatter in
formatter.forEach(.startOfScope) { i, token in
guard ["{", "(", "[", "<"].contains(token.string),
Expand Down Expand Up @@ -784,7 +785,8 @@ public struct _FormatRules {
/// Remove blank lines immediately before a closing brace, bracket, paren or chevron
/// unless it's followed by more code on the same line (e.g. } else { )
public let blankLinesAtEndOfScope = FormatRule(
help: "Remove trailing blank line at the end of a scope."
help: "Remove trailing blank line at the end of a scope.",
orderAfter: ["organizeDeclarations"]
) { formatter in
formatter.forEach(.endOfScope) { i, token in
guard ["}", ")", "]", ">"].contains(token.string),
Expand Down
11 changes: 11 additions & 0 deletions Tests/FormatterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -441,4 +441,15 @@ class FormatterTests: XCTestCase {
"""))
XCTAssertEqual(formatter.endOfScope(at: 4), 13)
}

// MARK: change tracking

func testTrackChangesIgnoresLinebreakIndex() {
let formatter = Formatter(tokenize("\n\n"), trackChanges: true)
var tokens = formatter.tokens
tokens.insert(tokens.removeLast(), at: 0)
XCTAssertNotEqual(formatter.tokens, tokens)
formatter.replaceTokens(in: 0 ..< 2, with: tokens)
XCTAssert(formatter.changes.isEmpty)
}
}
1 change: 1 addition & 0 deletions Tests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ extension FormatterTests {
("testRemoveNextTokenWhileEnumerating", testRemoveNextTokenWhileEnumerating),
("testRemovePreviousTokenWhileEnumerating", testRemovePreviousTokenWhileEnumerating),
("testSwiftVersionNext", testSwiftVersionNext),
("testTrackChangesIgnoresLinebreakIndex", testTrackChangesIgnoresLinebreakIndex),
]
}

Expand Down

0 comments on commit c0accac

Please sign in to comment.