-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert sbt style deps on paste in for scala-cli
- Loading branch information
Showing
5 changed files
with
175 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...c/main/scala/scala/meta/internal/metals/formatting/ScalaCliDependencyRangeFormatter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package scala.meta.internal.metals.formatting | ||
|
||
import scala.meta.internal.metals.formatting.RangeFormatter | ||
import scala.meta.internal.metals.formatting.RangeFormatterParams | ||
import scala.meta.internal.metals.scalacli.DependencyConverter | ||
|
||
import org.eclipse.{lsp4j => l} | ||
|
||
object ScalaCliDependencyRangeFormatter extends RangeFormatter { | ||
|
||
override def contribute( | ||
range: RangeFormatterParams | ||
): Option[List[l.TextEdit]] = { | ||
|
||
val line = range.sourceText.substring( | ||
range.startPos.start - range.startPos.startColumn, | ||
range.endPos.end, | ||
) | ||
DependencyConverter | ||
.convertSbtToMillStyleIfPossible( | ||
line | ||
) | ||
.map(converted => | ||
new l.TextEdit(range.range, converted.millStyleDependency) | ||
) | ||
.map(List(_)) | ||
|
||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
metals/src/main/scala/scala/meta/internal/metals/scalacli/DependencyConverter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package scala.meta.internal.metals.scalacli | ||
|
||
object DependencyConverter { | ||
def convertSbtToMillStyleIfPossible( | ||
sbtStyleDirective: String | ||
): Option[ReplacementSuggestion] = | ||
sbtStyleDirective.split(" ").filterNot(_.isEmpty) match { | ||
case Array( | ||
"//>", | ||
"using", | ||
dependencyIdentifierLike, | ||
groupId, | ||
groupDelimiter, | ||
artifactId, | ||
"%", | ||
version, | ||
) | ||
if dependencyIdentifiers(dependencyIdentifierLike) && | ||
sbtDependencyDelimiters(groupDelimiter) => | ||
val groupArtifactJoin = groupDelimiter.replace('%', ':') | ||
val millStyleDependency = | ||
s"$groupId$groupArtifactJoin$artifactId:$version".replace("\"", "") | ||
Some( | ||
ReplacementSuggestion(dependencyIdentifierLike, millStyleDependency) | ||
) | ||
case _ => None | ||
} | ||
|
||
private val dependencyIdentifiers = Set("dep", "lib", "plugin") | ||
private val sbtDependencyDelimiters = Set("%", "%%", "%%%") | ||
|
||
case class ReplacementSuggestion( | ||
dependencyIdentifier: String, | ||
millStyleDependency: String, | ||
) { | ||
val replacementDirective: String = | ||
s"//> using $dependencyIdentifier \"$millStyleDependency\"" | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
tests/unit/src/test/scala/tests/rangeFormatting/ScalaCliDependencyRangeFormatter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package tests.rangeFormatting | ||
|
||
import munit.Location | ||
import munit.TestOptions | ||
import org.eclipse.lsp4j.FormattingOptions | ||
import tests.BaseLspSuite | ||
|
||
class ScalaCliDependencyRangeFormatterPastingSuite | ||
extends BaseLspSuite("MillifyRangeFormatting") { | ||
|
||
check( | ||
"change-dep-format-on-paste", | ||
s""" | ||
|//> using dep @@ | ||
|object Main { | ||
| println("hello") | ||
|}""".stripMargin, | ||
s"""|"org.scalameta" %% "munit" % "0.7.26"""".stripMargin, | ||
s""" | ||
|//> using dep org.scalameta::munit:0.7.26 | ||
|object Main { | ||
| println("hello") | ||
|}""".stripMargin, | ||
) | ||
|
||
check( | ||
"change-dep-format-within-existing-deps", | ||
s""" | ||
|//> using dep com.lihaoyi::utest::0.7.10 | ||
|//> using dep @@ | ||
|//> using dep com.lihaoyi::pprint::0.6.6 | ||
|object Main { | ||
| println("hello") | ||
|}""".stripMargin, | ||
s"""|"org.scalameta" %% "munit" % "0.7.26"""".stripMargin, | ||
s""" | ||
|//> using dep com.lihaoyi::utest::0.7.10 | ||
|//> using dep org.scalameta::munit:0.7.26 | ||
|//> using dep com.lihaoyi::pprint::0.6.6 | ||
|object Main { | ||
| println("hello") | ||
|}""".stripMargin, | ||
) | ||
|
||
check( | ||
"not-change-format-outside-using-directive", | ||
s""" | ||
|//> using dep com.lihaoyi::utest::0.7.10 | ||
|//> using dep com.lihaoyi::pprint::0.6.6 | ||
|// TODO: @@ | ||
|object Main { | ||
| println("hello") | ||
|}""".stripMargin, | ||
s"""|"org.scalameta" %% "munit" % "0.7.26"""".stripMargin, | ||
s""" | ||
|//> using dep com.lihaoyi::utest::0.7.10 | ||
|//> using dep com.lihaoyi::pprint::0.6.6 | ||
|// TODO: "org.scalameta" %% "munit" % "0.7.26" | ||
|object Main { | ||
| println("hello") | ||
|}""".stripMargin, | ||
) | ||
|
||
val formattingOptions = new FormattingOptions(2, true) | ||
|
||
// TODO this function should be deduplicated across suites | ||
def check( | ||
name: TestOptions, | ||
testCase: String, | ||
paste: String, | ||
expectedCase: String, | ||
)(implicit loc: Location): Unit = { | ||
val tripleQuote = "\"\"\"" | ||
def unmangle(string: String): String = | ||
string.replaceAll("'''", tripleQuote) | ||
|
||
val testCode = unmangle(testCase) | ||
val base = testCode.replaceAll("(@@)", "") | ||
val expected = unmangle(expectedCase) | ||
test(name) { | ||
for { | ||
_ <- initialize( | ||
s"""/metals.json | ||
|{"a":{}} | ||
|/a/src/main/scala/a/Main.scala | ||
|""".stripMargin + base | ||
) | ||
_ <- server.didOpen("a/src/main/scala/a/Main.scala") | ||
_ <- server.rangeFormatting( | ||
"a/src/main/scala/a/Main.scala", | ||
testCode, // bez @@ | ||
expected, | ||
unmangle(paste), | ||
workspace, | ||
Some(formattingOptions), | ||
) | ||
} yield () | ||
} | ||
} | ||
} |