Skip to content

Commit 6fc36ad

Browse files
committed
Compiler cancellation with new compiler job queue
1 parent 66d15b2 commit 6fc36ad

16 files changed

+661
-542
lines changed

compiler/src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ object Contexts {
180180
val local = incCallback
181181
local != null && local.enabled || forceRun
182182

183-
/** The Zinc compile progress callback implementation if we are run from Zinc, null otherwise */
183+
/** The Zinc compile progress callback implementation if we are run from Zinc or used by presentation compiler, null otherwise */
184184
def progressCallback: ProgressCallback | Null = store(progressCallbackLoc)
185185

186186
/** Run `op` if there exists a Zinc progress callback */

compiler/src/dotty/tools/dotc/interactive/InteractiveDriver.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ import Denotations.staticRef
2323
import classpath.*
2424
import reporting.*
2525
import util.*
26+
import sbt.interfaces.ProgressCallback
2627

2728
/** A Driver subclass designed to be used from IDEs */
2829
class InteractiveDriver(val settings: List[String]) extends Driver {
2930
import tpd.*
3031

3132
override def sourcesRequired: Boolean = false
3233

34+
private var myProgressCallback: ProgressCallback | Null = null
35+
3336
private val myInitCtx: Context = {
3437
val rootCtx = initCtx.fresh.addMode(Mode.ReadPositions).addMode(Mode.Interactive)
3538
rootCtx.setSetting(rootCtx.settings.YretainTrees, true)
@@ -141,6 +144,11 @@ class InteractiveDriver(val settings: List[String]) extends Driver {
141144
(fromSource ++ fromClassPath).distinct
142145
}
143146

147+
def runWithProgressCallback(uri: URI, sourceCode: String, progressCallback: ProgressCallback | Null = null) =
148+
myProgressCallback = progressCallback
149+
run(uri, sourceCode)
150+
myProgressCallback = null
151+
144152
def run(uri: URI, sourceCode: String): List[Diagnostic] = run(uri, SourceFile.virtual(uri, sourceCode))
145153

146154
def run(uri: URI, source: SourceFile): List[Diagnostic] = {
@@ -151,7 +159,7 @@ class InteractiveDriver(val settings: List[String]) extends Driver {
151159
val reporter =
152160
new StoreReporter(null) with UniqueMessagePositions with HideNonSensicalMessages
153161

154-
val run = compiler.newRun(using myInitCtx.fresh.setReporter(reporter))
162+
val run = compiler.newRun(using myInitCtx.fresh.setReporter(reporter).setProgressCallback(myProgressCallback))
155163
myCtx = run.runContext.withRootImports
156164

157165
given Context = myCtx
@@ -169,8 +177,7 @@ class InteractiveDriver(val settings: List[String]) extends Driver {
169177
myCtx = myCtx.fresh.setPhase(myInitCtx.base.typerPhase)
170178

171179
reporter.removeBufferedMessages
172-
}
173-
catch {
180+
} catch {
174181
case ex: FatalError =>
175182
myCtx = previousCtx
176183
close(uri)

presentation-compiler/src/main/dotty/tools/pc/CachingDriver.scala

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,9 @@ class CachingDriver(override val settings: List[String]) extends InteractiveDriv
4242
override def run(uri: URI, source: SourceFile): List[Diagnostic] =
4343
val diags =
4444
if alreadyCompiled(uri, source.content) then Nil
45-
else super.run(uri, source)
46-
lastCompiledURI = uri
47-
diags
48-
49-
override def run(uri: URI, sourceCode: String): List[Diagnostic] =
50-
val diags =
51-
if alreadyCompiled(uri, sourceCode.toCharArray().nn) then Nil
52-
else super.run(uri, sourceCode)
45+
else
46+
val res = super.run(uri, source)
47+
res
5348
lastCompiledURI = uri
5449
diags
5550

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package dotty.tools.pc
2+
3+
4+
import scala.meta.pc.PresentationCompilerConfig
5+
6+
import dotty.tools.dotc.interactive.InteractiveDriver
7+
import scala.meta.pc.CancelToken
8+
import scala.meta.pc.VirtualFileParams
9+
import java.util.concurrent.CompletableFuture
10+
import scala.meta.internal.metals.EmptyCancelToken
11+
import java.net.URI
12+
import scala.meta.pc.OffsetParams
13+
14+
case class CompilationInputs(
15+
uri: URI,
16+
code: String,
17+
cancelToken: CancelToken = EmptyCancelToken,
18+
cleanDriver: Boolean = false
19+
):
20+
def show: String =
21+
s"""|uri: $uri
22+
|code: $code
23+
|cancelToken: $cancelToken
24+
|cleanDriver: $cleanDriver
25+
|""".stripMargin
26+
27+
object CompilationInputs:
28+
def empty: CompilationInputs = CompilationInputs(new URI(""), "", EmptyCancelToken, false)
29+
def fromParams(params: VirtualFileParams | OffsetParams, cleanDriver: Boolean = false): CompilationInputs =
30+
CompilationInputs(params.uri().nn, params.text().nn, params.token().nn, cleanDriver)
31+
32+
class DriverAccess(
33+
config: PresentationCompilerConfig,
34+
driverSettings: List[String],
35+
reportContext: PcReportContext
36+
):
37+
38+
private val taskQueue = TaskQueue(driverSettings, reportContext)
39+
private val worker = Worker(taskQueue, config, reportContext)
40+
41+
def lookup[T](f: InteractiveDriver => T): CompletableFuture[T] =
42+
enqueueTask(taskQueue.LookupTask(f))
43+
44+
def enqueueCancellable[T](inputs: CompilationInputs)
45+
(f: CancelToken ?=> InteractiveDriver => T): CompletableFuture[T] =
46+
47+
given token: CancelToken = inputs.cancelToken
48+
val task = taskQueue.CompilationTask(token, inputs)(f)
49+
50+
enqueueTask(task)
51+
52+
end enqueueCancellable
53+
54+
private def enqueueTask[T](task: taskQueue.Task[T]): CompletableFuture[T] =
55+
taskQueue.enqueue(task)
56+
worker.scheduleProcessing()
57+
task.future
58+
59+
def shutdown(): Unit =
60+
worker.shutdown()
61+
taskQueue.shutdown()
62+
63+
def restart() =
64+
taskQueue.shutdown()
65+

presentation-compiler/src/main/dotty/tools/pc/PcDefinitionProvider.scala

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,21 @@ class PcDefinitionProvider(
4141
val uri = params.uri().nn
4242
val text = params.text().nn
4343
val filePath = Paths.get(uri)
44-
driver.run(
45-
uri,
46-
SourceFile.virtual(filePath.toString, text)
47-
)
44+
45+
val unit = driver.compilationUnits(uri)
46+
val newCtx = driver.currentCtx.fresh.setCompilationUnit(unit)
4847

4948
val pos = driver.sourcePosition(params)
50-
val path =
51-
Interactive.pathTo(driver.openedTrees(uri), pos)(using driver.currentCtx)
49+
val path = Interactive.pathTo(unit.tpdTree, pos.span)(using newCtx)
50+
51+
given localCtx: Context = Interactive.contextOfPath(path)(using newCtx)
52+
val indexedContext = IndexedContext(pos)(using localCtx)
5253

53-
given ctx: Context = driver.localContext(params)
54-
val indexedContext = IndexedContext(pos)(using ctx)
5554
val result =
5655
if findTypeDef then findTypeDefinitions(path, pos, indexedContext, uri)
5756
else findDefinitions(path, pos, indexedContext, uri)
5857

59-
if result.locations().nn.isEmpty() then fallbackToUntyped(pos, uri)(using ctx)
58+
if result.locations().nn.isEmpty() then fallbackToUntyped(pos, uri)
6059
else result
6160
end definitions
6261

presentation-compiler/src/main/dotty/tools/pc/PcInlayHintsProvider.scala

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,18 @@ class PcInlayHintsProvider(
4040
val filePath = Paths.get(uri).nn
4141
val sourceText = params.text().nn
4242
val text = sourceText.toCharArray().nn
43-
val source =
44-
SourceFile.virtual(filePath.toString, sourceText)
45-
driver.run(uri, source)
46-
given InlayHintsParams = params
4743

44+
given InlayHintsParams = params
4845
given InferredType.Text = InferredType.Text(text)
4946
given ctx: Context = driver.currentCtx
50-
val unit = driver.currentCtx.run.nn.units.head
47+
48+
val unit = driver.compilationUnits(uri)
5149
val pos = driver.sourcePosition(params)
5250

5351
def provide(): List[InlayHint] =
5452
val deepFolder = DeepFolder[InlayHints](collectDecorations)
5553
Interactive
56-
.pathTo(driver.openedTrees(uri), pos)(using driver.currentCtx)
54+
.pathTo(unit.tpdTree, pos.span)
5755
.headOption
5856
.getOrElse(unit.tpdTree)
5957
.enclosedChildren(pos.span)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package dotty.tools.pc
2+
3+
import scala.meta.internal.metals.ReportContext
4+
import scala.meta.internal.metals.Reporter
5+
import scala.meta.internal.metals.EmptyReportContext
6+
7+
class PcReportContext(underlying: ReportContext, pcDetails: Map[String, String]) extends ReportContext:
8+
override def incognito: Reporter = underlying.incognito
9+
override def unsanitized: Reporter = underlying.unsanitized
10+
override def bloop: Reporter = underlying.bloop
11+
def additionalData: String = pcDetails.mkString("\n")
12+
13+
object PcReportContext:
14+
def empty: PcReportContext = PcReportContext(EmptyReportContext, Map.empty)
15+

presentation-compiler/src/main/dotty/tools/pc/Scala3CompilerAccess.scala

Lines changed: 0 additions & 40 deletions
This file was deleted.

presentation-compiler/src/main/dotty/tools/pc/Scala3CompilerWrapper.scala

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)