Skip to content

Commit

Permalink
Print diff out to console for multiline autocorrections
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed Jun 9, 2020
1 parent d761a4f commit 532b727
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ If needed, pluralize to `Tasks`, `PRs` or `Authors` and list multiple entries se
### Changed
- Made internal extension methos public for usage in `customCheck`.
PR: [#35](https://github.com/Flinesoft/AnyLint/pull/35) | Author: [Cihat Gündüz](https://github.com/Jeehut)
- Print diff out to console for multiline autocorrections that were applied.
Issue: [#27](https://github.com/Flinesoft/AnyLint/issues/27) | PR: [#35](https://github.com/Flinesoft/AnyLint/pull/35) | Author: [Cihat Gündüz](https://github.com/Jeehut)
### Deprecated
- None.
### Removed
Expand Down
62 changes: 56 additions & 6 deletions Sources/AnyLint/AutoCorrection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,35 @@ public struct AutoCorrection {
public let after: String

var appliedMessageLines: [String] {
[
"Autocorrection applied (before >>> after):",
"> ✗ \(before.showWhitespacesAndNewlines())",
">>>",
"> ✓ \(after.showWhitespacesAndNewlines())",
]
if useDiffOutput, #available(OSX 10.15, *) {
var lines: [String] = ["Autocorrection applied, the diff is: (+ added, - removed)"]

let beforeLines = before.components(separatedBy: .newlines)
let afterLines = after.components(separatedBy: .newlines)

for difference in afterLines.difference(from: beforeLines).sorted() {
switch difference {
case let .insert(offset, element, _):
lines.append("+ [L\(offset + 1)] \(element)".green)

case let .remove(offset, element, _):
lines.append("- [L\(offset + 1)] \(element)".red)
}
}

return lines
} else {
return [
"Autocorrection applied, the diff is: (+ added, - removed)",
"- \(before.showWhitespacesAndNewlines())".red,
"+ \(after.showWhitespacesAndNewlines())".green,
]
}
}

var useDiffOutput: Bool {
before.components(separatedBy: .newlines).count >= Constants.newlinesRequiredForDiffing ||
after.components(separatedBy: .newlines).count >= Constants.newlinesRequiredForDiffing
}

/// Initializes an autocorrection.
Expand All @@ -39,3 +62,30 @@ extension AutoCorrection: ExpressibleByDictionaryLiteral {
self = AutoCorrection(before: before, after: after)
}
}

// TODO: make the autocorrection diff sorted by line number
@available(OSX 10.15, *)
extension CollectionDifference.Change: Comparable where ChangeElement == String {
public static func < (lhs: Self, rhs: Self) -> Bool {
switch (lhs, rhs) {
case let (.remove(leftOffset, _, _), .remove(rightOffset, _, _)), let (.insert(leftOffset, _, _), .insert(rightOffset, _, _)):
return leftOffset < rightOffset

case let (.remove(leftOffset, _, _), .insert(rightOffset, _, _)):
return leftOffset < rightOffset || true

case let (.insert(leftOffset, _, _), .remove(rightOffset, _, _)):
return leftOffset < rightOffset || false
}
}

public static func == (lhs: Self, rhs: Self) -> Bool {
switch (lhs, rhs) {
case let (.remove(leftOffset, _, _), .remove(rightOffset, _, _)), let (.insert(leftOffset, _, _), .insert(rightOffset, _, _)):
return leftOffset == rightOffset

case (.remove, .insert), (.insert, .remove):
return false
}
}
}
1 change: 1 addition & 0 deletions Sources/AnyLint/Extensions/StringExt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extension String {
/// Info about the exact location of a character in a given file.
public typealias LocationInfo = (line: Int, charInLine: Int)

/// Returns the location info for a given line index.
public func locationInfo(of index: String.Index) -> LocationInfo {
let prefix = self[startIndex ..< index]
let prefixLines = prefix.components(separatedBy: .newlines)
Expand Down
3 changes: 3 additions & 0 deletions Sources/Utility/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ public enum Constants {

/// Hint that the case dot matches newline option should be active on a Regex.
public static let dotMatchesNewlinesRegexOption: String = "m"

/// The number of newlines required in both before and after of AutoCorrections required to use diff for outputs.
public static let newlinesRequiredForDiffing: Int = 3
}
27 changes: 27 additions & 0 deletions Tests/AnyLintTests/AutoCorrectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,31 @@ final class AutoCorrectionTests: XCTestCase {
XCTAssertEqual(autoCorrection.before, "Lisence")
XCTAssertEqual(autoCorrection.after, "License")
}

func testAppliedMessageLines() {
let singleLineAutoCorrection: AutoCorrection = ["before": "Lisence", "after": "License"]
XCTAssertEqual(
singleLineAutoCorrection.appliedMessageLines,
[
"Autocorrection applied, the diff is: (+ added, - removed)",
"- Lisence",
"+ License"
]
)

let multiLineAutoCorrection: AutoCorrection = [
"before": "A\nB\nC\nD\nE\nF\nG\nH\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nU\nV\nW\nX\nY\nZ\n",
"after": "A\nB\nD\nE\nF1\nF2\nG\nH\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nU\nV\nW\nX\nY\nZ\n"
]
XCTAssertEqual(
multiLineAutoCorrection.appliedMessageLines,
[
"Autocorrection applied, the diff is: (+ added, - removed)",
"- [L3] C",
"+ [L5] F1",
"- [L6] F",
"+ [L6] F2"
]
)
}
}

0 comments on commit 532b727

Please sign in to comment.