Skip to content

Commit

Permalink
Merge pull request #35 from Flinesoft/wip/cg_diff
Browse files Browse the repository at this point in the history
Make extension APIs public & More intelligent diffing
  • Loading branch information
Jeehut committed Jun 9, 2020
2 parents 586f5a4 + 2e9a2ac commit 5ae163f
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 13 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ on:
branches: [main]

jobs:
cancel-previous-runs:
runs-on: ubuntu-latest

steps:
- name: Cancel previous runs of this workflow on same branch
uses: rokroskar/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

anylint:
runs-on: ubuntu-latest

Expand All @@ -20,14 +29,14 @@ jobs:
}
echo "::set-env name=ANYLINT_LATEST_VERSION::$( latest_version Flinesoft/AnyLint )"
echo "::set-env name=SWIFT_SH_LATEST_VERSION::$( latest_version mxcl/swift-sh )"
- name: AnyLint Cache
uses: actions/cache@v1
id: anylint-cache
with:
path: anylint-cache
key: ${{ runner.os }}-v1-anylint-${{ env.ANYLINT_LATEST_VERSION }}-swift-sh-${{ env.SWIFT_SH_LATEST_VERSION }}

- name: Copy from cache
if: steps.anylint-cache.outputs.cache-hit
run: |
Expand All @@ -49,7 +58,7 @@ jobs:
cd swift-sh
swift build -c release
sudo cp -f .build/release/swift-sh /usr/local/bin/swift-sh
- name: Copy to cache
if: steps.anylint-cache.outputs.cache-hit != 'true'
run: |
Expand Down
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ If needed, pluralize to `Tasks`, `PRs` or `Authors` and list multiple entries se
### Added
- None.
### Changed
- None.
- 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
}
}
}
2 changes: 1 addition & 1 deletion Sources/AnyLint/Extensions/FileManagerExt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Utility

extension FileManager {
/// Moves a file from one path to another, making sure that all directories are created and no files are overwritten.
func moveFileSafely(from sourcePath: String, to targetPath: String) throws {
public func moveFileSafely(from sourcePath: String, to targetPath: String) throws {
guard fileExists(atPath: sourcePath) else {
log.message("No file found at \(sourcePath) to move.", level: .error)
log.exit(status: .failure)
Expand Down
3 changes: 2 additions & 1 deletion Sources/AnyLint/Extensions/StringExt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ extension String {
/// Info about the exact location of a character in a given file.
public typealias LocationInfo = (line: Int, charInLine: Int)

func locationInfo(of index: String.Index) -> LocationInfo {
/// 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)
guard let lastPrefixLine = prefixLines.last else { return (line: 1, charInLine: 1) }
Expand Down
3 changes: 2 additions & 1 deletion Sources/AnyLint/Extensions/URLExt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import Foundation
import Utility

extension URL {
var relativePathFromCurrent: String {
/// Returns the relative path of from the current path.
public var relativePathFromCurrent: String {
String(path.replacingOccurrences(of: fileManager.currentDirectoryPath, with: "").dropFirst())
}
}
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 5ae163f

Please sign in to comment.