Skip to content

Commit

Permalink
Rewrite longestCommonPrefixGreater withtout refined
Browse files Browse the repository at this point in the history
I had problems replacing `Witness.Aux` with a Scala 3 equivalent that
make the code compile, so I rewrote this function without refined.
  • Loading branch information
fthomas committed Jan 6, 2025
1 parent dc2a5a9 commit a84aba9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import io.circe.{Decoder, Encoder, HCursor, Json}
import org.scalasteward.core.repoconfig.PullRequestGroup
import org.scalasteward.core.util
import org.scalasteward.core.util.Nel
import org.scalasteward.core.util.string.MinLengthString

sealed trait Update {

Expand Down Expand Up @@ -137,7 +136,7 @@ object Update {
val possibleMainArtifactIds = for {
prefix <- artifactIdsPrefix.toList
suffix <- commonSuffixes
} yield prefix.value + suffix
} yield prefix + suffix

artifactIds
.map(_.name)
Expand All @@ -151,8 +150,8 @@ object Update {
override def newerVersions: Nel[Version] =
forArtifactIds.head.newerVersions

def artifactIdsPrefix: Option[MinLengthString[3]] =
util.string.longestCommonPrefixGreater[3](artifactIds.map(_.name))
def artifactIdsPrefix: Option[String] =
util.string.longestCommonPrefixGteq(artifactIds.map(_.name), 3)
}

val commonSuffixes: List[String] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ object Selector {
private def heuristic1SearchTerms(update: Update.Single): List[String] = {
val terms = update match {
case s: Update.ForArtifactId => List(s.artifactId.name)
case g: Update.ForGroupId =>
g.artifactIds.map(_.name).toList ++ g.artifactIdsPrefix.map(_.value).toList
case g: Update.ForGroupId => g.artifactIds.map(_.name).toList ++ g.artifactIdsPrefix.toList
}
terms.map(Update.nameOf(update.groupId, _))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@ package org.scalasteward.core.util

import cats.Foldable
import cats.syntax.all._
import eu.timepit.refined.api.Refined
import eu.timepit.refined.collection.MinSize
import eu.timepit.refined.refineV
import scala.util.matching.Regex
import shapeless.Witness

object string {
type MinLengthString[N] = String Refined MinSize[N]

/** Extracts words from a string.
*
Expand Down Expand Up @@ -53,10 +48,10 @@ object string {
s1.substring(0, i)
}

def longestCommonPrefixGreater[N <: Int: Witness.Aux](
xs: Nel[String]
): Option[MinLengthString[N]] =
refineV[MinSize[N]](xs.reduceLeft(longestCommonPrefix)).toOption
def longestCommonPrefixGteq(xs: Nel[String], n: Int): Option[String] = {
val prefix = xs.reduceLeft(longestCommonPrefix)
Option.when(prefix.length >= n)(prefix)
}

def removeSuffix(target: String, suffixes: List[String]): String =
suffixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class stringTest extends ScalaCheckSuite {
assertEquals(string.longestCommonPrefix("abc", "def"), "")
}

test("longestCommonPrefixGreater") {
assertEquals(string.longestCommonPrefixGreater[1](Nel.of("abcde", "abchk")).get.value, "abc")
assertEquals(string.longestCommonPrefixGreater[3](Nel.of("abcde", "abchk")).get.value, "abc")
assertEquals(string.longestCommonPrefixGreater[3](Nel.of("abcde", "abhk")), None)
test("longestCommonPrefixGteq") {
assertEquals(string.longestCommonPrefixGteq(Nel.of("abcde", "abchk"), 1), Some("abc"))
assertEquals(string.longestCommonPrefixGteq(Nel.of("abcde", "abchk"), 3), Some("abc"))
assertEquals(string.longestCommonPrefixGteq(Nel.of("abcde", "abhk"), 3), None)
}

test("rightmostLabel") {
Expand Down

0 comments on commit a84aba9

Please sign in to comment.