Skip to content

Commit 6913a22

Browse files
committed
additions: copy tests from metals version: 41e96ee33f82 , create diff utils, implement test framework
1 parent 5a5573b commit 6913a22

File tree

77 files changed

+20317
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+20317
-1
lines changed

compiler/src/dotty/tools/dotc/util/DiffUtil.scala

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ object DiffUtil {
7070
* differences are highlighted.
7171
*/
7272
def mkColoredLineDiff(expected: Seq[String], actual: Seq[String]): String = {
73-
val expectedSize = EOF.length max expected.maxBy(_.length).length
73+
val longestExpected = expected.map(_.length).maxOption.getOrElse(0)
74+
val longestActual = actual.map(_.length).maxOption.getOrElse(0)
75+
val expectedSize = EOF.length max longestActual max longestExpected
7476
actual.padTo(expected.length, "").zip(expected.padTo(actual.length, "")).map { case (act, exp) =>
7577
mkColoredLineDiff(exp, act, expectedSize)
7678
}.mkString(System.lineSeparator)
@@ -101,11 +103,75 @@ object DiffUtil {
101103
case Deleted(str) => deleted(str)
102104
}.mkString
103105

106+
(expectedDiff, actualDiff)
104107
val pad = " " * 0.max(expectedSize - expected.length)
105108

106109
expectedDiff + pad + " | " + actualDiff
107110
}
108111

112+
private def ensureLineSeparator(str: String): String =
113+
if str.endsWith(System.lineSeparator) then
114+
str
115+
else
116+
str + System.lineSeparator
117+
118+
/**
119+
* Returns a colored diffs by comparison of lines instead of tokens.
120+
* It will automatically group subsequential pairs of `Insert` and `Delete`
121+
* in order to improve the readability
122+
*
123+
* @param expected The expected lines
124+
* @param actual The actual lines
125+
* @return A string with colored diffs between `expected` and `actual` grouped whenever possible
126+
*/
127+
def mkColoredHorizontalLineDiff(expected: String, actual: String): String = {
128+
val indent = 2
129+
val tab = " " * indent
130+
val insertIndent = "+" ++ (" " * (indent - 1))
131+
val deleteIndent = "-" ++ (" " * (indent - 1))
132+
133+
if actual.isEmpty then
134+
(expected.linesIterator.map(line => added(insertIndent + line)).toList :+ deleted("--- EMPTY OUTPUT ---"))
135+
.map(ensureLineSeparator).mkString
136+
else if expected.isEmpty then
137+
(added("--- NO VALUE EXPECTED ---") +: actual.linesIterator.map(line => deleted(deleteIndent + line)).toList)
138+
.map(ensureLineSeparator).mkString
139+
else
140+
lazy val diff = {
141+
val expectedTokens = expected.linesWithSeparators.toArray
142+
val actualTokens = actual.linesWithSeparators.toArray
143+
hirschberg(actualTokens, expectedTokens)
144+
}.toList
145+
146+
val transformedDiff = diff.flatMap {
147+
case Modified(original, str) => Seq(
148+
Inserted(ensureLineSeparator(original)), Deleted(ensureLineSeparator(str))
149+
)
150+
case other => Seq(other)
151+
}
152+
153+
val zipped = transformedDiff zip transformedDiff.drop(1)
154+
155+
val (acc, inserts, deletions) = zipped.foldLeft((Seq[Patch](), Seq[Inserted](), Seq[Deleted]())): (acc, patches) =>
156+
val (currAcc, inserts, deletions) = acc
157+
patches match
158+
case (currentPatch: Inserted, nextPatch: Deleted) =>
159+
(currAcc, inserts :+ currentPatch, deletions)
160+
case (currentPatch: Deleted, nextPatch: Inserted) =>
161+
(currAcc, inserts, deletions :+ currentPatch)
162+
case (currentPatch, nextPatch) =>
163+
(currAcc :++ inserts :++ deletions :+ currentPatch, Seq.empty, Seq.empty)
164+
165+
val stackedDiff = acc :++ inserts :++ deletions :+ diff.last
166+
167+
stackedDiff.collect {
168+
case Unmodified(str) => tab + str
169+
case Inserted(str) => added(insertIndent + str)
170+
case Deleted(str) => deleted(deleteIndent + str)
171+
}.map(ensureLineSeparator).mkString
172+
173+
}
174+
109175
def mkColoredCodeDiff(code: String, lastCode: String, printDiffDel: Boolean): String = {
110176
val tokens = splitTokens(code, Nil).toArray
111177
val lastTokens = splitTokens(lastCode, Nil).toArray
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package dotty.tools.pc.base
2+
3+
import dotty.tools.pc.utils.TextEdits
4+
5+
import java.nio.file.Paths
6+
import scala.meta.internal.jdk.CollectionConverters.*
7+
import scala.meta.internal.metals.CompilerOffsetParams
8+
import scala.meta.pc.AutoImportsResult
9+
10+
trait BaseAutoImportsSuite extends BaseCodeActionSuite:
11+
12+
val isExtensionMethods: Boolean = false
13+
14+
def check(
15+
original: String,
16+
expected: String
17+
): Unit =
18+
val imports = getAutoImports(original, "A.scala")
19+
val obtained = imports.map(_.packageName()).mkString("\n")
20+
assertNoDiff(expected, obtained)
21+
22+
def checkEdit(
23+
original: String,
24+
expected: String,
25+
selection: Int = 0,
26+
filename: String = "A.scala"
27+
): Unit =
28+
checkEditSelection(filename, original, expected, selection)
29+
30+
def checkAmmoniteEdit(
31+
original: String,
32+
expected: String,
33+
selection: Int = 0
34+
): Unit =
35+
checkEditSelection(
36+
"script.amm.sc.scala",
37+
original,
38+
expected,
39+
selection
40+
)
41+
42+
def checkEditSelection(
43+
filename: String,
44+
original: String,
45+
expected: String,
46+
selection: Int
47+
): Unit =
48+
val imports = getAutoImports(original, filename)
49+
if (imports.size <= selection) fail("Obtained no expected imports")
50+
val edits = imports(selection).edits().asScala.toList
51+
val (code, _, _) = params(original)
52+
val obtained = TextEdits.applyEdits(code, edits)
53+
54+
assertNoDiff(expected, obtained)
55+
56+
def getAutoImports(
57+
original: String,
58+
filename: String
59+
): List[AutoImportsResult] =
60+
val (code, symbol, offset) = params(original)
61+
val result = presentationCompiler
62+
.autoImports(
63+
symbol,
64+
CompilerOffsetParams(
65+
Paths.get(filename).toUri(),
66+
code,
67+
offset,
68+
cancelToken
69+
),
70+
isExtensionMethods
71+
)
72+
.get()
73+
result.asScala.toList
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package dotty.tools.pc.base
2+
3+
import java.nio.charset.StandardCharsets
4+
import java.nio.file.Files
5+
import scala.collection.immutable
6+
import scala.meta.internal.metals.EmptyCancelToken
7+
import scala.meta.pc.CancelToken
8+
9+
abstract class BaseCodeActionSuite extends BasePCSuite:
10+
11+
def cancelToken: CancelToken = EmptyCancelToken
12+
13+
def params(
14+
code: String
15+
): (String, String, Int) =
16+
val filename = "test.scala"
17+
val targetRegex = "<<(.+)>>".r
18+
val target = targetRegex.findAllMatchIn(code).toList match
19+
case immutable.Nil => fail("Missing <<target>>")
20+
case t :: immutable.Nil => t.group(1)
21+
case _ => fail("Multiple <<targets>> found")
22+
val code2 = code.replace("<<", "").replace(">>", "")
23+
val offset = code.indexOf("<<") + target.length()
24+
val file = tmp.resolve(filename)
25+
Files.write(file, code2.getBytes(StandardCharsets.UTF_8))
26+
27+
testingWorkspaceSearch.inputs.update(filename, code)
28+
(code2, target, offset)

0 commit comments

Comments
 (0)