From b848984dd7b0ed96aa0addbdac33bc813a8f6521 Mon Sep 17 00:00:00 2001 From: "Frank S. Thomas" Date: Sat, 11 Nov 2023 13:13:15 +0100 Subject: [PATCH] Use correct start indices in ModulePositionScanner `ModulePositionScanner` currently reports wrong substring positions if an an artifactId is a proper substring of a groupId. With this change, regex capturing groups are used to get the correct start indices of substring positions of `ModulePositions`. --- .../edit/update/ModulePositionScanner.scala | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/modules/core/src/main/scala/org/scalasteward/core/edit/update/ModulePositionScanner.scala b/modules/core/src/main/scala/org/scalasteward/core/edit/update/ModulePositionScanner.scala index 3cb6ae2184..b40185724a 100644 --- a/modules/core/src/main/scala/org/scalasteward/core/edit/update/ModulePositionScanner.scala +++ b/modules/core/src/main/scala/org/scalasteward/core/edit/update/ModulePositionScanner.scala @@ -36,16 +36,16 @@ object ModulePositionScanner { fileData: FileData ): Iterator[ModulePosition] = sbtModuleIdRegex(dependency).findAllIn(fileData.content).matchData.map { m => - val groupId = Substring.Position.fromMatch(fileData.path, m, dependency.groupId.value) - val artifactId = Substring.Position.fromMatch(fileData.path, m, dependency.artifactId.name) - val version = Substring.Position.fromMatch(fileData.path, m, m.group(1)) + val groupId = Substring.Position(fileData.path, m.start(1), dependency.groupId.value) + val artifactId = Substring.Position(fileData.path, m.start(2), dependency.artifactId.name) + val version = Substring.Position(fileData.path, m.start(3), m.group(3)) ModulePosition(groupId, artifactId, version) } private def sbtModuleIdRegex(dependency: Dependency): Regex = { val g = Regex.quote(dependency.groupId.value) val a = Regex.quote(dependency.artifactId.name) - raw""""$g"\s*%+\s*"$a"\s*%+\s*(.*)""".r + raw""""($g)"\s*%+\s*"($a)"\s*%+\s*(.*)""".r } private def findMillDependency( @@ -53,16 +53,16 @@ object ModulePositionScanner { fileData: FileData ): Iterator[ModulePosition] = millDependencyRegex(dependency).findAllIn(fileData.content).matchData.map { m => - val groupId = Substring.Position.fromMatch(fileData.path, m, dependency.groupId.value) - val artifactId = Substring.Position.fromMatch(fileData.path, m, dependency.artifactId.name) - val version = Substring.Position.fromMatch(fileData.path, m, m.group(1)) + val groupId = Substring.Position(fileData.path, m.start(1), dependency.groupId.value) + val artifactId = Substring.Position(fileData.path, m.start(2), dependency.artifactId.name) + val version = Substring.Position(fileData.path, m.start(3), m.group(3)) ModulePosition(groupId, artifactId, version) } private def millDependencyRegex(dependency: Dependency): Regex = { val g = Regex.quote(dependency.groupId.value) val a = Regex.quote(dependency.artifactId.name) - raw"""["`]$g:+$a:+(.*)["`;]""".r + raw"""["`]($g):+($a):+(.*)["`;]""".r } private def findMavenDependency( @@ -70,15 +70,15 @@ object ModulePositionScanner { fileData: FileData ): Iterator[ModulePosition] = mavenDependencyRegex(dependency).findAllIn(fileData.content).matchData.map { m => - val groupId = Substring.Position.fromMatch(fileData.path, m, dependency.groupId.value) - val artifactId = Substring.Position.fromMatch(fileData.path, m, dependency.artifactId.name) - val version = Substring.Position.fromMatch(fileData.path, m, m.group(2)) + val groupId = Substring.Position(fileData.path, m.start(1), dependency.groupId.value) + val artifactId = Substring.Position(fileData.path, m.start(2), dependency.artifactId.name) + val version = Substring.Position(fileData.path, m.start(4), m.group(4)) ModulePosition(groupId, artifactId, version) } private def mavenDependencyRegex(dependency: Dependency): Regex = { val g = Regex.quote(dependency.groupId.value) val a = Regex.quote(dependency.artifactId.name) - raw"""$g\s*$a(|_[^<]+)\s*(.*)""".r + raw"""($g)\s*($a)(|_[^<]+)\s*(.*)""".r } }