Skip to content

Commit

Permalink
compat layer for scala 2.12
Browse files Browse the repository at this point in the history
  • Loading branch information
yurique committed May 23, 2021
1 parent eaf4051 commit 7ea50a2
Show file tree
Hide file tree
Showing 20 changed files with 166 additions and 73 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.6, 3.0.0]
scala: [2.12.13, 2.13.6, 3.0.0]
java: [[email protected]]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -101,6 +101,16 @@ jobs:
~/Library/Caches/Coursier/v1
key: ${{ runner.os }}-sbt-cache-v2-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }}

- name: Download target directories (2.12.13)
uses: actions/download-artifact@v2
with:
name: target-${{ matrix.os }}-2.12.13-${{ matrix.java }}

- name: Inflate target directories (2.12.13)
run: |
tar xf targets.tar
rm targets.tar
- name: Download target directories (2.13.6)
uses: actions/download-artifact@v2
with:
Expand Down
7 changes: 5 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ inThisBuild(
scalaVersion := ScalaVersions.v213,
description := "String diff for Scala",
crossScalaVersions := Seq(
ScalaVersions.v212,
ScalaVersions.v213,
ScalaVersions.v3
),
Expand All @@ -22,7 +23,8 @@ inThisBuild(
"PGP_SECRET" -> s"$${{ secrets.PGP_SECRET }}",
"SONATYPE_PASSWORD" -> s"$${{ secrets.SONATYPE_PASSWORD }}",
"SONATYPE_USERNAME" -> s"$${{ secrets.SONATYPE_USERNAME }}"
))
)),
versionScheme := Some("early-semver")
)
)

Expand All @@ -39,7 +41,8 @@ lazy val stringdiff =
.settings(
ScalaOptions.fixOptions,
libraryDependencies ++= Seq(
"org.scalatest" %%% "scalatest" % "3.2.9" % Test
"org.scala-lang.modules" %%% "scala-collection-compat" % "2.4.4",
"org.scalatest" %%% "scalatest" % "3.2.9" % Test
)
)

Expand Down
11 changes: 3 additions & 8 deletions project/ScalaOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@ object ScalaOptions {
"-Wunused:params"
)
)),
scalacOptions ++= (CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, _)) =>
Seq(
"-Ymacro-annotations"
)
case Some((3, _)) => Seq()
case _ => Seq()
}),
scalacOptions ++= CrossVersion.partialVersion(scalaVersion.value).collect { case (2, 13) =>
"-Ymacro-annotations"
},
(Compile / doc / scalacOptions) ~= (_.filterNot(
Set(
"-scalajs",
Expand Down
1 change: 1 addition & 0 deletions project/ScalaVersions.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
object ScalaVersions {
val v212 = "2.12.13"
val v213 = "2.13.6"
val v3 = "3.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package app.tulz.diff
package compat

import scala.collection.SeqView
import scala.collection.immutable.Range

class IndexedSeqView[A](
private[compat] val underlying: SeqView[A, _]
) {
def apply(index: Int): A = underlying(index)

def size: Int = underlying.length

def isEmpty: Boolean = underlying.isEmpty

def concat(other: IndexedSeqView[A]): IndexedSeqView[A] =
new IndexedSeqView((underlying ++ other.underlying).view)

def take(n: Int): IndexedSeqView[A] = new IndexedSeqView(underlying.take(n))

def slice(from: Int, until: Int): IndexedSeqView[A] = new IndexedSeqView(underlying.slice(from, until))

def toIndexedSeq: IndexedSeq[A] = underlying.toIndexedSeq

def indices: Range = underlying.indices

def forall(predicate: A => Boolean): Boolean = underlying.forall(predicate)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package app.tulz.diff.compat

class IndexedSeqViewOfCharOps(
underlying: IndexedSeqView[Char]
) {

def mkString: String = underlying.underlying.mkString

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package app.tulz.diff.compat

class IndexedSeqViewOfStringOps(
underlying: IndexedSeqView[String]
) {

def mkString: String = underlying.underlying.mkString

}
24 changes: 24 additions & 0 deletions stringdiff/src/main/scala-2.12/app/tulz/diff/compat/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package app.tulz.diff

import scala.collection.SeqView
import scala.language.implicitConversions

package object compat {

implicit def indexedSeq_ViewToIndexedSeqView[A](
underlying: SeqView[A, IndexedSeq[A]]
): IndexedSeqView[A] = new IndexedSeqView[A](underlying)

implicit def string_ViewToIndexedSeqView(
underlying: SeqView[Char, String]
): IndexedSeqView[Char] = new IndexedSeqView[Char](underlying)

implicit def indexedSeqViewOfCharOps(
underlying: IndexedSeqView[Char]
): IndexedSeqViewOfCharOps = new IndexedSeqViewOfCharOps(underlying)

implicit def indexedSeqViewOfStringOps(
underlying: IndexedSeqView[String]
): IndexedSeqViewOfStringOps = new IndexedSeqViewOfStringOps(underlying)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package app.tulz.diff

package object compat {

type IndexedSeqView[+A] = scala.collection.IndexedSeqView[A]

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package app.tulz.diff

package object compat {

type IndexedSeqView[+A] = scala.collection.IndexedSeqView[A]

}
4 changes: 2 additions & 2 deletions stringdiff/src/main/scala/app/tulz/diff/MyersDiff.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.tulz.diff

import scala.collection.IndexedSeqView
import compat._

object MyersDiff {

Expand Down Expand Up @@ -79,7 +79,7 @@ object MyersDiff {
}
}
}
rec(ss1.toIndexedSeq.view, ss2.toIndexedSeq.view, 0, 0)
rec(ss1, ss2, 0, 0)
}

sealed trait Operation extends Product with Serializable
Expand Down
30 changes: 15 additions & 15 deletions stringdiff/src/main/scala/app/tulz/diff/MyersInterpret.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package app.tulz.diff
import app.tulz.diff.MyersDiff.Operation

import scala.collection.mutable.ListBuffer
import scala.collection.IndexedSeqView
import compat._

object MyersInterpret {

Expand All @@ -23,91 +23,91 @@ object MyersInterpret {
(Start +: ops).zip(ops :+ End).foreach {
case (Start, Delete(deleteFrom, deleteCount)) =>
if (deleteFrom > 0) {
buffer.addOne(
buffer.append(
InBoth(
s1.take(deleteFrom)
)
)
}
buffer.addOne(
buffer.append(
InFirst(
s1.slice(deleteFrom, deleteFrom + deleteCount)
)
)

case (Start, Insert(insertAt, insertFrom, insertCount)) =>
if (insertAt > 0) {
buffer.addOne(
buffer.append(
InBoth(
s1.take(insertFrom)
)
)
}
buffer.addOne(
buffer.append(
InSecond(
s2.slice(insertFrom, insertFrom + insertCount)
)
)

case (Delete(deleteFrom, deleteCount), Insert(insertAt, insertFrom, insertCount)) =>
if (insertAt > deleteFrom + deleteCount) {
buffer.addOne(
buffer.append(
InBoth(
s1.slice(deleteFrom + deleteCount, insertAt)
)
)
}
buffer.addOne(
buffer.append(
InSecond(
s2.slice(insertFrom, insertFrom + insertCount)
)
)

case (Insert(insertAt, _, _), Delete(deleteFrom, deleteCount)) =>
if (deleteFrom > insertAt) {
buffer.addOne(
buffer.append(
InBoth(
s1.slice(insertAt, deleteFrom)
)
)
}
buffer.addOne(
buffer.append(
InFirst(
s1.slice(deleteFrom, deleteFrom + deleteCount)
)
)

case (Insert(insertAt1, _, _), Insert(insertAt2, insertFrom2, insertCount2)) =>
if (insertAt2 > insertAt1) {
buffer.addOne(
buffer.append(
InBoth(
s1.slice(insertAt1, insertAt2)
)
)
}
buffer.addOne(
buffer.append(
InSecond(
s2.slice(insertFrom2, insertFrom2 + insertCount2)
)
)

case (Delete(deleteFrom1, deleteCount1), Delete(deleteFrom2, deleteCount2)) =>
if (deleteFrom2 > deleteFrom1 + deleteCount1) {
buffer.addOne(
buffer.append(
InBoth(
s1.slice(deleteFrom1 + deleteCount1, deleteFrom2)
)
)
}
buffer.addOne(
buffer.append(
InFirst(
s1.slice(deleteFrom2, deleteFrom2 + deleteCount2)
)
)

case (Insert(insertAt, _, _), End) =>
if (s1.size > insertAt) {
buffer.addOne(
buffer.append(
InBoth(
s1.slice(insertAt, s1.size)
)
Expand All @@ -116,7 +116,7 @@ object MyersInterpret {

case (Delete(deleteFrom, deleteCount), End) =>
if (s1.size > deleteFrom + deleteCount) {
buffer.addOne(
buffer.append(
InBoth(
s1.slice(deleteFrom + deleteCount, s1.size)
)
Expand Down
3 changes: 1 addition & 2 deletions stringdiff/src/main/scala/app/tulz/diff/SeqDiff.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package app.tulz.diff

import app.tulz.diff.util.DiffCollapse

import scala.collection.IndexedSeqView
import compat._

object SeqDiff {

Expand Down
3 changes: 2 additions & 1 deletion stringdiff/src/main/scala/app/tulz/diff/StringDiff.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package app.tulz.diff

import app.tulz.diff.format.DiffFormat
import app.tulz.diff.util.DiffCollapse
import compat._

object StringDiff {

def apply(
s1: String,
s2: String,
collapse: Boolean = true
): String = ansi(s1, s2)
): String = ansi(s1, s2, collapse)

def ansi(
s1: String,
Expand Down
8 changes: 4 additions & 4 deletions stringdiff/src/main/scala/app/tulz/diff/TokenDiff.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import app.tulz.diff.format.DiffFormat
import app.tulz.diff.util.DiffCollapse
import app.tulz.diff.util.DiffPrettier
import app.tulz.diff.util.DiffTokenize

import compat._
import scala.collection.mutable.ListBuffer

object TokenDiff {
Expand All @@ -15,13 +15,13 @@ object TokenDiff {
var pos = 0
whitespace.findAllMatchIn(s).foreach { m =>
if (m.start > pos) {
buffer.addOne(s.substring(pos, m.start))
buffer.append(s.substring(pos, m.start))
}
buffer.addOne(s.substring(m.start, m.end))
buffer.append(s.substring(m.start, m.end))
pos = m.end
}
if (pos < s.length) {
buffer.addOne(s.substring(pos))
buffer.append(s.substring(pos))
}
buffer.toIndexedSeq
}
Expand Down
17 changes: 8 additions & 9 deletions stringdiff/src/main/scala/app/tulz/diff/util/DiffCollapse.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package app.tulz.diff.util

import app.tulz.diff.DiffElement
import app.tulz.diff.DiffElement.Diff
import app.tulz.diff.DiffElement.InBoth
import app.tulz.diff.DiffElement.InFirst
import app.tulz.diff.DiffElement.InSecond

import scala.collection.IndexedSeqView
package app.tulz.diff
package util

import DiffElement.Diff
import DiffElement.InBoth
import DiffElement.InFirst
import DiffElement.InSecond
import compat._

private[diff] object DiffCollapse {

Expand Down
9 changes: 4 additions & 5 deletions stringdiff/src/main/scala/app/tulz/diff/util/DiffLog.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package app.tulz.diff.util
package app.tulz.diff
package util

import app.tulz.diff.DiffElement
import app.tulz.diff.format.DiffFormat

import scala.collection.IndexedSeqView
import format.DiffFormat
import compat._

private[diff] object DiffLog {

Expand Down
Loading

0 comments on commit 7ea50a2

Please sign in to comment.