Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit 37ef320

Browse files
committed
working
1 parent 4a460d5 commit 37ef320

File tree

8 files changed

+911
-78
lines changed

8 files changed

+911
-78
lines changed

build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ lazy val core = (projectMatrix in file("core"))
5252
libraryDependencies ++= Seq(
5353
"com.propensive" %%% "magnolia" % "0.17.0",
5454
"org.scala-lang" % "scala-reflect" % scalaVersion.value,
55+
"io.github.java-diff-utils" % "java-diff-utils" % "4.10",
5556
"org.scalatest" %%% "scalatest-flatspec" % scalatestVersion % Test,
5657
"org.scalatest" %%% "scalatest-freespec" % scalatestVersion % Test,
5758
"org.scalatest" %%% "scalatest-shouldmatchers" % scalatestVersion % Test,

core/src/main/scala/com/github/difflib/text/DiffRowGenerator2.java

Lines changed: 666 additions & 0 deletions
Large diffs are not rendered by default.

core/src/main/scala/com/softwaremill/diffx/DiffResult.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ case class DiffResultString(diffs: List[DiffResult]) extends DiffResult {
9696
override private[diffx] def showIndented(indent: Int, renderIdentical: Boolean)(implicit
9797
c: ConsoleColorConfig
9898
): String = {
99-
s"${diffs.map(_.showIndented(indent, renderIdentical)).mkString("\n")}"
99+
s"${diffs.map(_.showIndented(indent, renderIdentical)).mkString}"
100100
}
101101

102102
override def isIdentical: Boolean = diffs.forall(_.isIdentical)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.softwaremill.diffx.instances.string
2+
3+
trait ComparisonFailExceptionHandler {
4+
def handle(
5+
message: String,
6+
obtained: String,
7+
expected: String
8+
): Nothing
9+
}

core/src/main/scala/com/softwaremill/diffx/instances/string/ExDiff.scala renamed to core/src/main/scala/com/softwaremill/diffx/instances/string/Diff.scala

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package com.softwaremill.diffx.instances.string
22

3-
import com.softwaremill.diffx.DiffResultString
4-
53
import scala.collection.JavaConverters._
64

7-
class ExDiff(val obtained: String, val expected: String) extends Serializable {
5+
class AADiff(val obtained: String, val expected: String) extends Serializable {
86
val obtainedClean: String = filterAnsi(obtained)
97
val expectedClean: String = filterAnsi(expected)
108
val obtainedLines: Seq[String] = splitIntoLines(obtainedClean)
119
val expectedLines: Seq[String] = splitIntoLines(expectedClean)
12-
val unifiedDiff: DiffResultString = createUnifiedDiff(obtainedLines, expectedLines)
10+
val unifiedDiff: String = createUnifiedDiff(obtainedLines, expectedLines)
11+
def isEmpty: Boolean = unifiedDiff.isEmpty()
1312

1413
def createReport(
1514
title: String,
@@ -65,19 +64,37 @@ class ExDiff(val obtained: String, val expected: String) extends Serializable {
6564
private def createUnifiedDiff(
6665
original: Seq[String],
6766
revised: Seq[String]
68-
): DiffResultString = {
67+
): String = {
6968
val diff = DiffUtils.diff(original.asJava, revised.asJava)
70-
val result = DiffUtils
71-
.generateUnifiedDiff(
72-
"obtained",
73-
"expected",
74-
original.asJava,
75-
diff,
76-
1
77-
)
78-
.asScala
79-
.iterator
80-
DiffResultString(result.toList)
69+
val result =
70+
if (diff.getDeltas.isEmpty) ""
71+
else {
72+
DiffUtils
73+
.generateUnifiedDiff(
74+
"obtained",
75+
"expected",
76+
original.asJava,
77+
diff,
78+
1
79+
)
80+
.asScala
81+
.iterator
82+
.drop(2)
83+
.filterNot(_.startsWith("@@"))
84+
.map { line =>
85+
if (line.isEmpty()) line
86+
else if (line.last == ' ') line + ""
87+
else line
88+
}
89+
.map { line =>
90+
if (line.startsWith("-")) line
91+
else if (line.startsWith("+"))
92+
line
93+
else line
94+
}
95+
.mkString("\n")
96+
}
97+
result
8198
}
8299

83100
private def splitIntoLines(string: String): Seq[String] = {

core/src/main/scala/com/softwaremill/diffx/instances/string/DiffUtils.scala

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.softwaremill.diffx.instances.string
22

3-
import com.softwaremill.diffx.{DiffResult, DiffResultValue, IdenticalValue}
4-
53
import java.util
64

75
object DiffUtils {
@@ -11,11 +9,11 @@ object DiffUtils {
119
originalLines: util.List[String],
1210
patch: Patch[String],
1311
contextSize: Int
14-
): util.List[DiffResult] = {
12+
): util.List[String] = {
1513
if (!patch.getDeltas.isEmpty) {
16-
val ret: util.List[DiffResult] = new util.ArrayList()
17-
// ret.add("--- " + original)
18-
// ret.add("+++ " + revised)
14+
val ret: util.List[String] = new util.ArrayList()
15+
ret.add("--- " + original)
16+
ret.add("+++ " + revised)
1917
val patchDeltas: util.List[Delta[String]] =
2018
new util.ArrayList(patch.getDeltas)
2119
val deltas: util.List[Delta[String]] = new util.ArrayList()
@@ -55,7 +53,7 @@ object DiffUtils {
5553
ret.addAll(curBlock)
5654
ret
5755
} else {
58-
new util.ArrayList[DiffResult]()
56+
new util.ArrayList[String]()
5957
}
6058
}
6159
def diff(
@@ -69,7 +67,7 @@ object DiffUtils {
6967
deltas: util.List[Delta[String]],
7068
contextSize: Int
7169
) = {
72-
val buffer = new util.ArrayList[DiffResult]
70+
val buffer = new util.ArrayList[String]
7371
var origTotal = 0 // counter for total lines output from Original
7472
var revTotal = 0
7573
var line = 0
@@ -87,14 +85,14 @@ object DiffUtils {
8785
while ({
8886
line < curDelta.getOriginal.getPosition
8987
}) { //
90-
buffer.add(IdenticalValue(" " + origLines.get(line)))
88+
buffer.add(" " + origLines.get(line))
9189
origTotal += 1
9290
revTotal += 1
9391

9492
line += 1
9593
}
9694
// output the first Delta
97-
buffer.add(getDeltaText(curDelta))
95+
buffer.addAll(getDeltaText(curDelta))
9896
origTotal += curDelta.getOriginal.getLines.size
9997
revTotal += curDelta.getRevised.getLines.size
10098
var deltaIndex = 1
@@ -108,13 +106,13 @@ object DiffUtils {
108106
while ({
109107
line < nextDelta.getOriginal.getPosition
110108
}) { // output the code between the last Delta and this one
111-
buffer.add(IdenticalValue(" " + origLines.get(line)))
109+
buffer.add(" " + origLines.get(line))
112110
origTotal += 1
113111
revTotal += 1
114112

115113
line += 1
116114
}
117-
buffer.add(getDeltaText(nextDelta)) // output the Delta
115+
buffer.addAll(getDeltaText(nextDelta)) // output the Delta
118116

119117
origTotal += nextDelta.getOriginal.getLines.size
120118
revTotal += nextDelta.getRevised.getLines.size
@@ -127,39 +125,38 @@ object DiffUtils {
127125
while ({
128126
(line < (contextStart + contextSize)) && (line < origLines.size)
129127
}) {
130-
buffer.add(IdenticalValue(" " + origLines.get(line)))
128+
buffer.add(" " + origLines.get(line))
131129
origTotal += 1
132130
revTotal += 1
133131

134132
line += 1
135133
}
136134
// Create and insert the block header, conforming to the Unified Diff
137-
// // standard
138-
// val header = new StringBuffer
139-
// header.append("@@ -")
140-
// header.append(origStart)
141-
// header.append(",")
142-
// header.append(origTotal)
143-
// header.append(" +")
144-
// header.append(revStart)
145-
// header.append(",")
146-
// header.append(revTotal)
147-
// header.append(" @@")
148-
// buffer.add(0, header.toString)
135+
// standard
136+
val header = new StringBuffer
137+
header.append("@@ -")
138+
header.append(origStart)
139+
header.append(",")
140+
header.append(origTotal)
141+
header.append(" +")
142+
header.append(revStart)
143+
header.append(",")
144+
header.append(revTotal)
145+
header.append(" @@")
146+
buffer.add(0, header.toString)
149147
buffer
150148
}
151149

152-
private def getDeltaText(delta: Delta[String]) = { //TOOD tutaj trzeba zmienic na missing/additional
150+
private def getDeltaText(delta: Delta[String]) = {
153151
import scala.collection.JavaConverters._
154-
// val buffer = new util.ArrayList[String]
155-
// for (line <- delta.getOriginal.getLines.asScala) {
156-
// buffer.add("-" + line)
157-
// }
158-
// for (line <- delta.getRevised.getLines.asScala) {
159-
// buffer.add("+" + line)
160-
// }
161-
// buffer
162-
DiffResultValue(delta.getOriginal.getLines.asScala.mkString("\n"), delta.getRevised.getLines.asScala.mkString("\n"))
152+
val buffer = new util.ArrayList[String]
153+
for (line <- delta.getOriginal.getLines.asScala) {
154+
buffer.add("-" + line)
155+
}
156+
for (line <- delta.getRevised.getLines.asScala) {
157+
buffer.add("+" + line)
158+
}
159+
buffer
163160
}
164161

165162
}

core/src/main/scala/com/softwaremill/diffx/instances/string/MyersDiff.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package com.softwaremill.diffx.instances.string
22

33
import java.util
44

5-
class MyersDiff[T](equalizer: Equalizer[T]) extends DiffAlgorithm[T] {
5+
class MyersDiff[T](equalizer: Equalizer[T]) {
66
def this() = this(Equalizer.default[T])
7-
override def diff(
7+
def diff(
88
original: util.List[T],
99
revised: util.List[T]
10-
): Patch[T] = {
10+
) = {
1111
try {
1212
buildRevision(buildPath(original, revised), original, revised)
1313
} catch {
@@ -20,7 +20,7 @@ class MyersDiff[T](equalizer: Equalizer[T]) extends DiffAlgorithm[T] {
2020
_path: PathNode,
2121
orig: util.List[T],
2222
rev: util.List[T]
23-
): Patch[T] = {
23+
) = {
2424
var path = _path
2525
val patch = new Patch[T]
2626
if (path.isSnake) path = path.prev

0 commit comments

Comments
 (0)