Skip to content

Commit

Permalink
improvement: recompile focused file if depends on last compiled bt
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Oct 25, 2023
1 parent 45ac965 commit e004fe3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,20 @@ final class Compilations(
} yield result
}

def compileFiles(paths: Seq[AbsolutePath]): Future[Unit] = {
def compileFiles(
paths: Seq[AbsolutePath],
focusedDocumentBuildTarget: Option[BuildTargetIdentifier],
): Future[Unit] = {
for {
targets <- expand(paths)
_ <- compileBatch(targets)
_ <- focusedDocumentBuildTarget match {
case Some(bt)
if !targets.contains(bt) &&
buildTargets.isInverseDependency(bt, targets.toList) =>
compileBatch(bt)
case _ => Future.successful(())
}
_ <- compileWorksheets(paths)
} yield ()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1330,11 +1330,13 @@ class MetalsLspService(
paths.foreach { path =>
fingerprints.add(path, FileIO.slurp(path, charset))
}

Future
.sequence(
List(
Future(indexer.reindexWorkspaceSources(paths)),
compilations.compileFiles(paths),
compilations
.compileFiles(paths, Option(focusedDocumentBuildTarget.get())),
onBuildChanged(paths).ignoreValue,
Future.sequence(paths.map(onBuildToolAdded)),
) ++ paths.map(f => Future(interactiveSemanticdbs.textDocument(f)))
Expand All @@ -1346,7 +1348,8 @@ class MetalsLspService(
Future
.sequence(
List(
compilations.compileFiles(List(path)),
compilations
.compileFiles(List(path), Option(focusedDocumentBuildTarget.get())),
Future {
diagnostics.didDelete(path)
testProvider.onFileDelete(path)
Expand Down
59 changes: 58 additions & 1 deletion tests/unit/src/test/scala/tests/DidFocusLspSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class DidFocusLspSuite extends BaseLspSuite("did-focus") {
| "a": {},
| "b": {
| "dependsOn": ["a"]
| }
| },
| "c": {}
|}
|/a/src/main/scala/a/A.scala
|package a
Expand All @@ -38,6 +39,11 @@ class DidFocusLspSuite extends BaseLspSuite("did-focus") {
|object C {
| val z: Int = a.A.x
|}
|/c/src/main/scala/c/C.scala
|package c
|object E {
| val i: Int = "aaa"
|}
""".stripMargin
)
_ <- server.didOpen("a/src/main/scala/a/A.scala")
Expand All @@ -48,6 +54,7 @@ class DidFocusLspSuite extends BaseLspSuite("did-focus") {
_ = assert(didCompile == AlreadyCompiled)
didCompile <- server.didFocus("b/src/main/scala/b/B.scala")
_ = assert(didCompile == AlreadyCompiled)
didCompile <- server.didFocus("c/src/main/scala/c/C.scala")
_ <- server.didSave("a/src/main/scala/a/A.scala")(
_.replace("val x = 1", "val x = \"string\"")
)
Expand All @@ -68,6 +75,56 @@ class DidFocusLspSuite extends BaseLspSuite("did-focus") {
)
} yield ()
}

test("compiled-focused") {
cleanWorkspace()
for {
_ <- initialize(
"""
|/metals.json
|{
| "a": {},
| "b": {
| "dependsOn": ["a"]
| }
|}
|/a/src/main/scala/a/A.scala
|package a
|object A {
| val x = 1
|}
|/a/src/main/scala/a/A2.scala
|package a
|object A2 {
| val y = 1
|}
|/b/src/main/scala/b/B.scala
|package b
|object C {
| val z: Int = a.A.x
|}
""".stripMargin
)
_ <- server.didOpen("a/src/main/scala/a/A.scala")
_ <- server.didOpen("b/src/main/scala/b/B.scala")
_ = assertNoDiagnostics()
_ = fakeTime.elapseSeconds(10)
didCompile <- server.didFocus("b/src/main/scala/b/B.scala")
_ <- server.didSave("a/src/main/scala/a/A.scala")(
_.replace("val x = 1", "val x = \"string\"")
)
_ = fakeTime.elapseSeconds(10)
_ = assertNoDiff(
client.workspaceDiagnostics,
"""|b/src/main/scala/b/B.scala:3:16: error: type mismatch;
| found : String
| required: Int
| val z: Int = a.A.x
| ^^^^^
|""".stripMargin,
)
} yield ()
}
}

// https://github.com/scalameta/metals/issues/497
Expand Down

0 comments on commit e004fe3

Please sign in to comment.