-
Notifications
You must be signed in to change notification settings - Fork 315
/
ScalaPresentationCompiler.scala
605 lines (547 loc) Β· 18.1 KB
/
ScalaPresentationCompiler.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
package scala.meta.internal.pc
import java.io.File
import java.net.URI
import java.nio.file.Files
import java.nio.file.Path
import java.util
import java.util.Optional
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ExecutorService
import java.util.concurrent.ScheduledExecutorService
import java.util.logging.Logger
import java.{util => ju}
import scala.collection.Seq
import scala.collection.concurrent.TrieMap
import scala.concurrent.ExecutionContext
import scala.concurrent.ExecutionContextExecutor
import scala.reflect.io.VirtualDirectory
import scala.tools.nsc.Settings
import scala.tools.nsc.reporters.StoreReporter
import scala.util.control.NonFatal
import scala.meta.internal.jdk.CollectionConverters._
import scala.meta.internal.metals.CompilerOffsetParams
import scala.meta.internal.metals.CompilerVirtualFileParams
import scala.meta.internal.metals.EmptyCancelToken
import scala.meta.internal.metals.EmptyReportContext
import scala.meta.internal.metals.Report
import scala.meta.internal.metals.ReportContext
import scala.meta.internal.metals.ReportLevel
import scala.meta.internal.metals.StdReportContext
import scala.meta.internal.mtags.BuildInfo
import scala.meta.internal.mtags.MtagsEnrichments._
import scala.meta.pc.AutoImportsResult
import scala.meta.pc.CompilerFiles
import scala.meta.pc.DefinitionResult
import scala.meta.pc.DisplayableException
import scala.meta.pc.HoverSignature
import scala.meta.pc.Node
import scala.meta.pc.OffsetParams
import scala.meta.pc.PresentationCompiler
import scala.meta.pc.PresentationCompilerConfig
import scala.meta.pc.RangeParams
import scala.meta.pc.SymbolSearch
import scala.meta.pc.SyntheticDecoration
import scala.meta.pc.SyntheticDecorationsParams
import scala.meta.pc.VirtualFileParams
import org.eclipse.lsp4j.CompletionItem
import org.eclipse.lsp4j.CompletionList
import org.eclipse.lsp4j.Diagnostic
import org.eclipse.lsp4j.DocumentHighlight
import org.eclipse.lsp4j.Range
import org.eclipse.lsp4j.SelectionRange
import org.eclipse.lsp4j.SignatureHelp
import org.eclipse.lsp4j.TextEdit
case class ScalaPresentationCompiler(
buildTargetIdentifier: String = "",
buildTargetName: Option[String] = None,
classpath: Seq[Path] = Nil,
options: List[String] = Nil,
search: SymbolSearch = EmptySymbolSearch,
ec: ExecutionContextExecutor = ExecutionContext.global,
sh: Option[ScheduledExecutorService] = None,
config: PresentationCompilerConfig = PresentationCompilerConfigImpl(),
folderPath: Option[Path] = None,
reportsLevel: ReportLevel = ReportLevel.Info,
compilerFiles: Option[CompilerFiles] = None,
wasSuccessfullyCompiledInitial: Option[Boolean] = None
) extends PresentationCompiler {
private val wasSuccessfullyCompiled =
new util.concurrent.atomic.AtomicReference[Option[Boolean]](
wasSuccessfullyCompiledInitial
)
implicit val executionContext: ExecutionContextExecutor = ec
val scalaVersion = BuildInfo.scalaCompilerVersion
private val changedDocuments = TrieMap.empty[URI, VirtualFileParams]
val logger: Logger =
Logger.getLogger(classOf[ScalaPresentationCompiler].getName)
implicit val reportContex: ReportContext =
folderPath
.map(new StdReportContext(_, _ => buildTargetName, reportsLevel))
.getOrElse(EmptyReportContext)
override def withBuildTargetName(
buildTargetName: String
): ScalaPresentationCompiler =
copy(buildTargetName = Some(buildTargetName))
override def withReportsLoggerLevel(level: String): PresentationCompiler =
copy(reportsLevel = ReportLevel.fromString(level))
override def withSearch(search: SymbolSearch): PresentationCompiler =
copy(search = search)
override def withWorkspace(workspace: Path): PresentationCompiler =
copy(folderPath = Some(workspace))
override def withExecutorService(
executorService: ExecutorService
): PresentationCompiler =
copy(ec = ExecutionContext.fromExecutorService(executorService))
override def withScheduledExecutorService(
sh: ScheduledExecutorService
): PresentationCompiler =
copy(sh = Some(sh))
override def withCompilerFiles(
compilerFiles: CompilerFiles
): PresentationCompiler =
copy(compilerFiles = Some(compilerFiles))
override def withConfiguration(
config: PresentationCompilerConfig
): PresentationCompiler =
copy(config = config)
def this() = this(buildTargetIdentifier = "")
val compilerAccess =
new ScalaCompilerAccess(
config,
sh,
() => new ScalaCompilerWrapper(newCompiler()),
{ () =>
s"""|Scala version: $scalaVersion
|Classpath:
|${classpath
.map(path => s"$path [${if (path.exists) "exists" else "missing"} ]")
.mkString(", ")}
|Options:
|${options.mkString(" ")}
|""".stripMargin
}
)(
ec,
reportContex
)
override def shutdown(): Unit = {
compilerAccess.shutdown()
}
def restart(): Unit = restoreOutlineAndRestart()
override def restart(wasSuccessful: Boolean): Unit = {
wasSuccessfullyCompiled.updateAndGet {
case None => Some(wasSuccessful)
case _ if wasSuccessful => Some(true)
case value => value
}
if (wasSuccessful) {
changedDocuments.clear()
compilerAccess.shutdownCurrentCompiler()
} else {
restoreOutlineAndRestart()
}
}
private def restoreOutlineAndRestart() = {
compilerAccess
.withNonInterruptableCompiler(None)(
(),
EmptyCancelToken
) { pc =>
/* we will still want outline recompiled if the compilation was not succesful */
pc.compiler().richCompilationCache.foreach { case (uriString, unit) =>
try {
val text = unit.source.content.mkString
val uri = uriString.toAbsolutePath.toURI
val params =
CompilerOffsetParams(uri, text, 0, EmptyCancelToken)
changedDocuments += uri -> params
} catch {
case NonFatal(error) =>
reportContex.incognito.create(
Report(
"restoring_cache",
"Error while restoring outline compiler cache",
error
)
)
logger
.log(util.logging.Level.SEVERE, error.getMessage(), error)
}
}
}
.get()
compilerAccess.shutdownCurrentCompiler()
}
def isLoaded(): Boolean = compilerAccess.isLoaded()
override def newInstance(
buildTargetIdentifier: String,
classpath: util.List[Path],
options: util.List[String]
): PresentationCompiler = {
copy(
buildTargetIdentifier = buildTargetIdentifier,
classpath = classpath.asScala,
options = options.asScala.toList
)
}
private def outlineFiles(
current: VirtualFileParams
): OutlineFiles = {
val shouldCompileAll = wasSuccessfullyCompiled
.getAndUpdate {
case Some(false) => Some(true)
case value => value
}
.exists(!_)
val result =
if (shouldCompileAll) {
// if first compilation was unsuccessful we want to outline all files
compilerFiles.iterator.flatMap(_.allPaths().asScala).foreach { path =>
val uri = path.toUri()
if (!changedDocuments.contains(uri)) {
val text = Files.readString(path)
changedDocuments += uri -> CompilerOffsetParams(uri, text, 0)
}
}
OutlineFiles(
changedDocuments.values.toList,
firstCompileSubstitute = true
)
} else {
val files =
changedDocuments.values.filterNot(_.uri() == current.uri()).toList
OutlineFiles(files)
}
changedDocuments.clear()
changedDocuments += current.uri() -> current
result
}
override def didChange(
params: VirtualFileParams
): CompletableFuture[ju.List[Diagnostic]] = {
changedDocuments += params.uri() -> params
CompletableFuture.completedFuture(Nil.asJava)
}
def didClose(uri: URI): Unit = {
compilerAccess.withNonInterruptableCompiler(None)(
(),
EmptyCancelToken
) { pc =>
pc.compiler().richCompilationCache.remove(uri.toString())
}
}
override def semanticTokens(
params: VirtualFileParams
): CompletableFuture[ju.List[Node]] = {
val empty: ju.List[Node] = new ju.ArrayList[Node]()
compilerAccess.withInterruptableCompiler(Some(params))(
empty,
params.token
) { pc =>
new PcSemanticTokensProvider(
pc.compiler(outlineFiles(params)),
params
).provide().asJava
}
}
override def syntheticDecorations(
params: SyntheticDecorationsParams
): CompletableFuture[ju.List[SyntheticDecoration]] = {
val empty: ju.List[SyntheticDecoration] =
new ju.ArrayList[SyntheticDecoration]()
compilerAccess.withInterruptableCompiler(Some(params))(
empty,
params.token
) { pc =>
new PcSyntheticDecorationsProvider(
pc.compiler(),
params
).provide().asJava
}
}
override def complete(
params: OffsetParams
): CompletableFuture[CompletionList] = {
compilerAccess.withInterruptableCompiler(Some(params))(
EmptyCompletionList(),
params.token
) { pc =>
new CompletionProvider(pc.compiler(outlineFiles(params)), params)
.completions()
}
}
override def implementAbstractMembers(
params: OffsetParams
): CompletableFuture[ju.List[TextEdit]] = {
val empty: ju.List[TextEdit] = new ju.ArrayList[TextEdit]()
compilerAccess.withInterruptableCompiler(Some(params))(
empty,
params.token
) { pc =>
new CompletionProvider(pc.compiler(outlineFiles(params)), params)
.implementAll()
}
}
override def insertInferredType(
params: OffsetParams
): CompletableFuture[ju.List[TextEdit]] = {
val empty: ju.List[TextEdit] = new ju.ArrayList[TextEdit]()
compilerAccess.withInterruptableCompiler(Some(params))(
empty,
params.token
) { pc =>
new InferredTypeProvider(pc.compiler(outlineFiles(params)), params)
.inferredTypeEdits()
.asJava
}
}
override def inlineValue(
params: OffsetParams
): CompletableFuture[ju.List[TextEdit]] = {
val empty: Either[String, List[TextEdit]] = Right(List())
(compilerAccess
.withInterruptableCompiler(Some(params))(empty, params.token) { pc =>
new PcInlineValueProviderImpl(
pc.compiler(outlineFiles(params)),
params
).getInlineTextEdits
})
.thenApply {
case Right(edits: List[TextEdit]) => edits.asJava
case Left(error: String) => throw new DisplayableException(error)
}
}
override def extractMethod(
range: RangeParams,
extractionPos: OffsetParams
): CompletableFuture[ju.List[TextEdit]] = {
val empty: ju.List[TextEdit] = new ju.ArrayList[TextEdit]()
compilerAccess.withInterruptableCompiler(Some(range))(empty, range.token) {
pc =>
new ExtractMethodProvider(
pc.compiler(outlineFiles(range)),
range,
extractionPos
).extractMethod.asJava
}
}
override def convertToNamedArguments(
params: OffsetParams,
argIndices: ju.List[Integer]
): CompletableFuture[ju.List[TextEdit]] = {
val empty: Either[String, List[TextEdit]] = Right(List())
(compilerAccess
.withInterruptableCompiler(Some(params))(empty, params.token) { pc =>
new ConvertToNamedArgumentsProvider(
pc.compiler(outlineFiles(params)),
params,
argIndices.asScala.map(_.toInt).toSet
).convertToNamedArguments
})
.thenApply {
case Left(error: String) => throw new DisplayableException(error)
case Right(edits: List[TextEdit]) => edits.asJava
}
}
override def autoImports(
name: String,
params: OffsetParams,
isExtension: java.lang.Boolean // ignore, because Scala2 doesn't support extension method
): CompletableFuture[ju.List[AutoImportsResult]] =
compilerAccess.withInterruptableCompiler(Some(params))(
List.empty[AutoImportsResult].asJava,
params.token
) { pc =>
new AutoImportsProvider(pc.compiler(outlineFiles(params)), name, params)
.autoImports()
.asJava
}
override def getTasty(
targetUri: URI,
isHttpEnabled: Boolean
): CompletableFuture[String] =
CompletableFuture.completedFuture("")
// NOTE(olafur): hover and signature help use a "shared" compiler instance because
// we don't typecheck any sources, we only poke into the symbol table.
// If we used a shared compiler then we risk hitting `Thread.interrupt`,
// which can close open `*-sources.jar` files containing Scaladoc/Javadoc strings.
override def completionItemResolve(
item: CompletionItem,
symbol: String
): CompletableFuture[CompletionItem] =
CompletableFuture.completedFuture {
compilerAccess.withSharedCompiler(None)(item) { pc =>
new CompletionItemResolver(pc.compiler()).resolve(item, symbol)
}
}
override def signatureHelp(
params: OffsetParams
): CompletableFuture[SignatureHelp] =
compilerAccess.withNonInterruptableCompiler(Some(params))(
new SignatureHelp(),
params.token
) { pc =>
new SignatureHelpProvider(pc.compiler(outlineFiles(params)))
.signatureHelp(params)
}
override def prepareRename(
params: OffsetParams
): CompletableFuture[ju.Optional[Range]] =
compilerAccess.withNonInterruptableCompiler(Some(params))(
Optional.empty[Range](),
params.token
) { pc =>
new PcRenameProvider(pc.compiler(outlineFiles(params)), params, None)
.prepareRename()
.asJava
}
override def rename(
params: OffsetParams,
name: String
): CompletableFuture[ju.List[TextEdit]] =
compilerAccess.withNonInterruptableCompiler(Some(params))(
List[TextEdit]().asJava,
params.token
) { pc =>
new PcRenameProvider(
pc.compiler(outlineFiles(params)),
params,
Some(name)
).rename().asJava
}
override def hover(
params: OffsetParams
): CompletableFuture[Optional[HoverSignature]] =
compilerAccess.withNonInterruptableCompiler(Some(params))(
Optional.empty[HoverSignature](),
params.token
) { pc =>
Optional.ofNullable(
new HoverProvider(pc.compiler(outlineFiles(params)), params)
.hover()
.orNull
)
}
def definition(params: OffsetParams): CompletableFuture[DefinitionResult] = {
compilerAccess.withNonInterruptableCompiler(Some(params))(
DefinitionResultImpl.empty,
params.token
) { pc =>
new PcDefinitionProvider(pc.compiler(outlineFiles(params)), params)
.definition()
}
}
override def typeDefinition(
params: OffsetParams
): CompletableFuture[DefinitionResult] = {
compilerAccess.withNonInterruptableCompiler(Some(params))(
DefinitionResultImpl.empty,
params.token
) { pc =>
new PcDefinitionProvider(pc.compiler(outlineFiles(params)), params)
.typeDefinition()
}
}
override def documentHighlight(
params: OffsetParams
): CompletableFuture[util.List[DocumentHighlight]] =
compilerAccess.withInterruptableCompiler(Some(params))(
List.empty[DocumentHighlight].asJava,
params.token()
) { pc =>
new PcDocumentHighlightProvider(pc.compiler(outlineFiles(params)), params)
.highlights()
.asJava
}
override def semanticdbTextDocument(
fileUri: URI,
code: String
): CompletableFuture[Array[Byte]] = {
val virtualFile = CompilerVirtualFileParams(fileUri, code)
compilerAccess.withInterruptableCompiler(Some(virtualFile))(
Array.emptyByteArray,
EmptyCancelToken
) { pc =>
new SemanticdbTextDocumentProvider(
pc.compiler(outlineFiles(CompilerOffsetParams(fileUri, code, 0))),
config.semanticdbCompilerOptions().asScala.toList
)
.textDocument(fileUri, code)
.toByteArray
}
}
override def selectionRange(
params: ju.List[OffsetParams]
): CompletableFuture[ju.List[SelectionRange]] = {
CompletableFuture.completedFuture {
compilerAccess.withSharedCompiler(params.asScala.headOption)(
List.empty[SelectionRange].asJava
) { pc =>
new SelectionRangeProvider(pc.compiler(), params)
.selectionRange()
.asJava
}
}
}
def newCompiler(): MetalsGlobal = {
val classpath = this.classpath.mkString(File.pathSeparator)
val vd = new VirtualDirectory("(memory)", None)
val settings = new Settings
settings.Ymacroexpand.value = "discard"
settings.outputDirs.setSingleOutput(vd)
settings.classpath.value = classpath
settings.YpresentationAnyThread.value = true
if (
!BuildInfo.scalaCompilerVersion.startsWith("2.11") &&
BuildInfo.scalaCompilerVersion != "2.12.4"
) {
settings.processArguments(
List("-Ycache-plugin-class-loader:last-modified"),
processAll = true
)
}
if (classpath.isEmpty) {
settings.usejavacp.value = true
}
val (isSuccess, unprocessed) =
settings.processArguments(options, processAll = true)
if (unprocessed.nonEmpty || !isSuccess) {
logger.warning(s"Unknown compiler options: ${unprocessed.mkString(", ")}")
}
new MetalsGlobal(
settings,
new StoreReporter,
search,
buildTargetIdentifier,
config,
folderPath
)
}
// ================
// Internal methods
// ================
override def diagnosticsForDebuggingPurposes(): util.List[String] = {
compilerAccess.reporter.infos.iterator
.map { info =>
new StringBuilder()
.append(info.pos.source.file.path)
.append(":")
.append(info.pos.column)
.append(" ")
.append(info.msg)
.append("\n")
.append(info.pos.lineContent)
.append("\n")
.append(info.pos.lineCaret)
.toString
}
.filterNot(_.contains("_CURSOR_"))
.toList
.asJava
}
override def withWasSuccessfullyCompiled(
wasSuccessful: Boolean
): PresentationCompiler = {
this.copy(wasSuccessfullyCompiledInitial = Some(wasSuccessful))
}
}