Skip to content

Commit

Permalink
SuperMethodProvider as private field
Browse files Browse the repository at this point in the history
  • Loading branch information
kpbochenek committed Mar 16, 2020
1 parent 2974613 commit 94b3686
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class MetalsLanguageServer(
private var bloopServers: BloopServers = _
private var bspServers: BspServers = _
private var codeLensProvider: CodeLensProvider = _
var goToSuperMethod: GoToSuperMethod = _
private var goToSuperMethod: GoToSuperMethod = _
private var codeActionProvider: CodeActionProvider = _
private var definitionProvider: DefinitionProvider = _
private var semanticDBIndexer: SemanticdbIndexer = _
Expand Down
86 changes: 63 additions & 23 deletions tests/unit/src/main/scala/tests/TestingServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import org.eclipse.lsp4j.CodeActionParams
import org.eclipse.lsp4j.CodeActionContext
import scala.meta.internal.implementation.GoToSuperMethod.GoToSuperMethodParams
import scala.meta.internal.implementation.GoToSuperMethod.formatMethodSymbolForQuickPick
import scala.meta.internal.metals.ClientCommands

/**
* Wrapper around `MetalsLanguageServer` with helpers methods for testing purposes.
Expand Down Expand Up @@ -198,34 +199,73 @@ final class TestingServer(
}

def assertGotoSuperMethod(
pos: Int,
maybeSuperPos: Option[Int],
asserts: Map[Int, Option[Int]],
context: Map[Int, (l.Position, String)]
)(implicit loc: munit.Location): Unit = {
val (position, document) = context(pos)
val command = GoToSuperMethodParams(document, position)
val maybeFoundLocation =
// executeCommand(ServerCommands.GotoSuperMethod, params)
server.goToSuperMethod.getGoToSuperMethodLocation(command)

// client.clientCommands
val maybeFoundPosition =
maybeFoundLocation.map(l => (l.getRange.getStart, l.getUri))
val maybeExpectedPosition = maybeSuperPos.flatMap(context.get)
Assertions.assertEquals(maybeFoundPosition, maybeExpectedPosition)
)(implicit loc: munit.Location): Future[Unit] = {
def exec(
toCheck: List[(Int, Option[Int])]
): Future[List[Option[(l.Position, String)]]] = {
toCheck match {
case (pos, expectedPos) :: tl =>
val (position, document) = context(pos)
val command = GoToSuperMethodParams(document, position)
executeCommand(ServerCommands.GotoSuperMethod.id, command)
.flatMap(_ =>
exec(tl).map(rest => expectedPos.flatMap(context.get) +: rest)
)
case _ =>
Future.successful(List.empty)
}
}

val resultsF = exec(asserts.toList)

resultsF.map { expectedGotoPositionsOpts =>
val expectedGotoPositions = expectedGotoPositionsOpts.collect {
case Some(pos) => pos
}
val gotoExecutedCommandPositions = client.clientCommands.asScala
.filter(_.getCommand == ClientCommands.GotoLocation.id)
.map(_.getArguments.asScala.head.asInstanceOf[l.Location])
.map(l => (l.getRange.getStart, l.getUri))
.toList

Assertions.assertEquals(
gotoExecutedCommandPositions,
expectedGotoPositions
)
}
}

def assertSuperMethodHierarchy(
uri: String,
pos: l.Position,
expected: List[String]
)(implicit loc: munit.Location): Unit = {
val params = GoToSuperMethodParams(uri, pos)
val result = server.goToSuperMethod
.getSuperMethodHierarchySymbols(params)
.map(_.map(formatMethodSymbolForQuickPick))
.get
Assertions.assertNoDiff(result.toString, expected.toString)
expectations: List[(Int, List[String])],
context: Map[Int, l.Position]
)(implicit loc: munit.Location): Future[Unit] = {
val obtained = scala.collection.mutable.Buffer[Set[String]]()
client.showMessageRequestHandler = { req =>
val titles = req.getActions.asScala
.map(action => formatMethodSymbolForQuickPick(action.getTitle))
.toSet
obtained.append(titles)
Some(req.getActions.get(0))
}

def exec(toCheck: List[(Int, List[String])]): Future[List[Set[String]]] = {
toCheck match {
case (pos, expected) :: tl =>
val command = GoToSuperMethodParams(uri, context(pos))
executeCommand(ServerCommands.SuperMethodHierarchy.id, command)
.flatMap(_ => exec(tl).map(rest => expected.toSet +: rest))

case _ =>
Future.successful(List.empty)
}
}

exec(expectations).map(expected => {
Assertions.assertEquals(obtained.toList, expected)
})
}

def assertReferenceDefinitionBijection()(
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/src/test/scala/tests/SuperHierarchyLspSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class SuperHierarchyLspSuite extends BaseLspSuite("super-method-hierarchy") {
checkHierarchy(
code,
Map(
1 -> List(),
3 -> List("a.A#xxx"),
4 -> List("a.C#xxx", "a.A#xxx")
)
Expand Down Expand Up @@ -55,7 +54,6 @@ class SuperHierarchyLspSuite extends BaseLspSuite("super-method-hierarchy") {
checkHierarchy(
code,
Map(
1 -> List(),
2 -> List("a.A#xxx"),
3 -> List("a.A#xxx"),
4 -> List("a.C2#xxx", "a.A#xxx"),
Expand Down Expand Up @@ -120,13 +118,15 @@ class SuperHierarchyLspSuite extends BaseLspSuite("super-method-hierarchy") {
_ <- server.initialize(strip(header + code))
_ <- server.didOpen("a/src/main/scala/a/A.scala")
_ = assertNoDiagnostics()
} yield {
val path = server.toPath("a/src/main/scala/a/A.scala").toURI.toString
val context = parse(code)
for (e <- expectations) {
server.assertSuperMethodHierarchy(path, context(e._1), e._2)
}
}

path = server.toPath("a/src/main/scala/a/A.scala").toURI.toString
context = parse(code)
result <- server.assertSuperMethodHierarchy(
path,
expectations.toList,
context
)
} yield result
}

private def parse(
Expand Down
33 changes: 16 additions & 17 deletions tests/unit/src/test/scala/tests/SuperMethodLspSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,16 @@ class SuperMethodLspSuite extends BaseLspSuite("gotosupermethod") {
_ <- server.didOpen("a/src/main/scala/a/A.scala")
_ <- server.didOpen("b/src/main/scala/b/B.scala")
_ = assertNoDiagnostics()
} yield {
val pathA = server.toPath("a/src/main/scala/a/A.scala").toURI.toString
val pathB = server.toPath("b/src/main/scala/b/B.scala").toURI.toString
val (contextA, assertsA) = parseWithUri(codeA, pathA)
val (contextB, assertsB) = parseWithUri(codeB, pathB)
for (check <- assertsA ++ assertsB) {
server.assertGotoSuperMethod(check._1, check._2, contextA ++ contextB)
}
}
pathA = server.toPath("a/src/main/scala/a/A.scala").toURI.toString
pathB = server.toPath("b/src/main/scala/b/B.scala").toURI.toString
(contextA, assertsA) = parseWithUri(codeA, pathA)
(contextB, assertsB) = parseWithUri(codeB, pathB)
result <- server.assertGotoSuperMethod(
assertsA ++ assertsB,
contextA ++ contextB
)

} yield result
}

def checkSuperMethod(
Expand All @@ -262,19 +263,17 @@ class SuperMethodLspSuite extends BaseLspSuite("gotosupermethod") {
_ <- server.initialize(strip(header + code))
_ <- server.didOpen("a/src/main/scala/a/A.scala")
_ = assertNoDiagnostics()
} yield {
val path = server.toPath("a/src/main/scala/a/A.scala").toURI.toString

path = server.toPath("a/src/main/scala/a/A.scala").toURI.toString

// Checked manually it is actually there and operated under artificial ID link "50"
val externalDep = Map(
externalDep = Map(
50 -> (new Position(60, 6), workspace.toURI.toString + ".metals/readonly/io/circe/Decoder.scala")
)

val (context, assertions) = parseWithUri(code, path)
for (check <- assertions) {
server.assertGotoSuperMethod(check._1, check._2, context ++ externalDep)
}
}
(context, assertions) = parseWithUri(code, path)
result <- server.assertGotoSuperMethod(assertions, context ++ externalDep)
} yield result
}

private def strip(code: String): String = {
Expand Down

0 comments on commit 94b3686

Please sign in to comment.