-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ScalaPresentationCompiler.scala
476 lines (425 loc) · 14.6 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
package dotty.tools.pc
import java.io.File
import java.net.URI
import java.nio.file.Path
import java.util.Optional
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ExecutorService
import java.util.concurrent.ScheduledExecutorService
import java.util as ju
import scala.concurrent.ExecutionContext
import scala.concurrent.ExecutionContextExecutor
import scala.jdk.CollectionConverters._
import scala.language.unsafeNulls
import scala.meta.internal.metals.CompilerVirtualFileParams
import scala.meta.internal.metals.EmptyCancelToken
import scala.meta.internal.metals.EmptyReportContext
import scala.meta.internal.metals.ReportContext
import scala.meta.internal.metals.ReportLevel
import scala.meta.internal.metals.StdReportContext
import scala.meta.internal.mtags.CommonMtagsEnrichments.*
import scala.meta.internal.pc.CompilerAccess
import scala.meta.internal.pc.DefinitionResultImpl
import scala.meta.internal.pc.EmptyCompletionList
import scala.meta.internal.pc.EmptySymbolSearch
import scala.meta.internal.pc.PresentationCompilerConfigImpl
import scala.meta.pc.*
import scala.meta.pc.{PcSymbolInformation as IPcSymbolInformation}
import dotty.tools.dotc.reporting.StoreReporter
import dotty.tools.pc.completions.CompletionProvider
import dotty.tools.pc.InferExpectedType
import dotty.tools.pc.completions.OverrideCompletions
import dotty.tools.pc.buildinfo.BuildInfo
import org.eclipse.lsp4j.DocumentHighlight
import org.eclipse.lsp4j.TextEdit
import org.eclipse.lsp4j as l
import dotty.tools.pc.SymbolInformationProvider
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,
completionItemPriority: CompletionItemPriority = (_: String) => 0,
) extends PresentationCompiler:
def this() = this("", None, Nil, Nil)
val scalaVersion = BuildInfo.scalaVersion
private val forbiddenOptions = Set("-print-lines", "-print-tasty")
private val forbiddenDoubleOptions = Set("-release")
given ReportContext =
folderPath
.map(StdReportContext(_, _ => buildTargetName, reportsLevel))
.getOrElse(EmptyReportContext)
override def withCompletionItemPriority(
priority: CompletionItemPriority
): PresentationCompiler =
copy(completionItemPriority = priority)
override def withBuildTargetName(buildTargetName: String) =
copy(buildTargetName = Some(buildTargetName))
override def withReportsLoggerLevel(level: String): PresentationCompiler =
copy(reportsLevel = ReportLevel.fromString(level))
val compilerAccess: CompilerAccess[StoreReporter, MetalsDriver] =
Scala3CompilerAccess(
config,
sh,
() => new Scala3CompilerWrapper(newDriver)
)(using
ec
)
private def removeDoubleOptions(options: List[String]): List[String] =
options match
case head :: _ :: tail if forbiddenDoubleOptions(head) =>
removeDoubleOptions(tail)
case head :: tail => head :: removeDoubleOptions(tail)
case Nil => options
def newDriver: MetalsDriver =
val implicitSuggestionTimeout = List("-Ximport-suggestion-timeout", "0")
val defaultFlags = List("-color:never")
val filteredOptions = removeDoubleOptions(
options.filterNot(forbiddenOptions)
)
val settings =
filteredOptions ::: defaultFlags ::: implicitSuggestionTimeout ::: "-classpath" :: classpath
.mkString(
File.pathSeparator
) :: Nil
new MetalsDriver(settings)
override def semanticTokens(
params: VirtualFileParams
): CompletableFuture[ju.List[Node]] =
compilerAccess.withInterruptableCompiler(Some(params))(
new ju.ArrayList[Node](),
params.token()
) { access =>
val driver = access.compiler()
new PcSemanticTokensProvider(driver, params).provide().asJava
}
override def inlayHints(
params: InlayHintsParams
): ju.concurrent.CompletableFuture[ju.List[l.InlayHint]] =
compilerAccess.withInterruptableCompiler(Some(params))(
new ju.ArrayList[l.InlayHint](),
params.token(),
) { access =>
val driver = access.compiler()
new PcInlayHintsProvider(driver, params, search)
.provide()
.asJava
}
override def getTasty(
targetUri: URI,
isHttpEnabled: Boolean
): CompletableFuture[String] =
CompletableFuture.completedFuture {
TastyUtils.getTasty(targetUri, isHttpEnabled)
}
def complete(params: OffsetParams): CompletableFuture[l.CompletionList] =
compilerAccess.withInterruptableCompiler(Some(params))(
EmptyCompletionList(),
params.token()
) { access =>
val driver = access.compiler()
new CompletionProvider(
search,
driver,
params,
config,
buildTargetIdentifier,
folderPath,
completionItemPriority
).completions()
}
def definition(params: OffsetParams): CompletableFuture[DefinitionResult] =
compilerAccess.withInterruptableCompiler(Some(params))(
DefinitionResultImpl.empty,
params.token()
) { access =>
val driver = access.compiler()
PcDefinitionProvider(driver, params, search).definitions()
}
override def typeDefinition(
params: OffsetParams
): CompletableFuture[DefinitionResult] =
compilerAccess.withInterruptableCompiler(Some(params))(
DefinitionResultImpl.empty,
params.token()
) { access =>
val driver = access.compiler()
PcDefinitionProvider(driver, params, search).typeDefinitions()
}
def documentHighlight(
params: OffsetParams
): CompletableFuture[ju.List[DocumentHighlight]] =
compilerAccess.withInterruptableCompiler(Some(params))(
List.empty[DocumentHighlight].asJava,
params.token()
) { access =>
val driver = access.compiler()
PcDocumentHighlightProvider(driver, params).highlights.asJava
}
override def references(
params: ReferencesRequest
): CompletableFuture[ju.List[ReferencesResult]] =
compilerAccess.withNonInterruptableCompiler(Some(params.file()))(
List.empty[ReferencesResult].asJava,
params.file().token,
) { access =>
val driver = access.compiler()
PcReferencesProvider(driver, params)
.references()
.asJava
}
def inferExpectedType(params: OffsetParams): CompletableFuture[ju.Optional[String]] =
compilerAccess.withInterruptableCompiler(Some(params))(
Optional.empty(),
params.token,
) { access =>
val driver = access.compiler()
new InferExpectedType(search, driver, params).infer().asJava
}
def shutdown(): Unit =
compilerAccess.shutdown()
def restart(): Unit =
compilerAccess.shutdownCurrentCompiler()
def diagnosticsForDebuggingPurposes(): ju.List[String] =
List[String]().asJava
override def info(
symbol: String
): CompletableFuture[Optional[IPcSymbolInformation]] =
compilerAccess.withNonInterruptableCompiler[Optional[IPcSymbolInformation]](
None
)(
Optional.empty(),
EmptyCancelToken,
) { access =>
SymbolInformationProvider(using access.compiler().currentCtx)
.info(symbol)
.map(_.asJava)
.asJava
}
def semanticdbTextDocument(
filename: URI,
code: String
): CompletableFuture[Array[Byte]] =
val virtualFile = CompilerVirtualFileParams(filename, code)
compilerAccess.withNonInterruptableCompiler(Some(virtualFile))(
Array.empty[Byte],
EmptyCancelToken
) { access =>
val driver = access.compiler()
val provider = SemanticdbTextDocumentProvider(driver, folderPath)
provider.textDocument(filename, code)
}
def completionItemResolve(
item: l.CompletionItem,
symbol: String
): CompletableFuture[l.CompletionItem] =
compilerAccess.withNonInterruptableCompiler(None)(
item,
EmptyCancelToken
) { access =>
val driver = access.compiler()
CompletionItemResolver.resolve(item, symbol, search, config)(using
driver.currentCtx
)
}
def autoImports(
name: String,
params: scala.meta.pc.OffsetParams,
isExtension: java.lang.Boolean
): CompletableFuture[
ju.List[scala.meta.pc.AutoImportsResult]
] =
compilerAccess.withNonInterruptableCompiler(Some(params))(
List.empty[scala.meta.pc.AutoImportsResult].asJava,
params.token()
) { access =>
val driver = access.compiler()
new AutoImportsProvider(
search,
driver,
name,
params,
config,
buildTargetIdentifier
)
.autoImports(isExtension)
.asJava
}
def implementAbstractMembers(
params: OffsetParams
): CompletableFuture[ju.List[l.TextEdit]] =
val empty: ju.List[l.TextEdit] = new ju.ArrayList[l.TextEdit]()
compilerAccess.withNonInterruptableCompiler(Some(params))(
empty,
params.token()
) { pc =>
val driver = pc.compiler()
OverrideCompletions.implementAllAt(
params,
driver,
search,
config
)
}
end implementAbstractMembers
override def insertInferredType(
params: OffsetParams
): CompletableFuture[ju.List[l.TextEdit]] =
val empty: ju.List[l.TextEdit] = new ju.ArrayList[l.TextEdit]()
compilerAccess.withNonInterruptableCompiler(Some(params))(
empty,
params.token()
) { pc =>
new InferredTypeProvider(params, pc.compiler(), config, search)
.inferredTypeEdits()
.asJava
}
override def inlineValue(
params: OffsetParams
): CompletableFuture[ju.List[l.TextEdit]] =
val empty: Either[String, List[l.TextEdit]] = Right(List())
(compilerAccess
.withInterruptableCompiler(Some(params))(empty, params.token()) { pc =>
new PcInlineValueProviderImpl(pc.compiler(), params)
.getInlineTextEdits()
})
.thenApply {
case Right(edits: List[TextEdit]) => edits.asJava
case Left(error: String) => throw new DisplayableException(error)
}
end inlineValue
override def extractMethod(
range: RangeParams,
extractionPos: OffsetParams
): CompletableFuture[ju.List[l.TextEdit]] =
val empty: ju.List[l.TextEdit] = new ju.ArrayList[l.TextEdit]()
compilerAccess.withInterruptableCompiler(Some(range))(empty, range.token()) {
pc =>
new ExtractMethodProvider(
range,
extractionPos,
pc.compiler(),
search,
options.contains("-no-indent"),
)
.extractMethod()
.asJava
}
end extractMethod
override def convertToNamedArguments(
params: OffsetParams,
argIndices: ju.List[Integer]
): CompletableFuture[ju.List[l.TextEdit]] =
val empty: Either[String, List[l.TextEdit]] = Right(List())
(compilerAccess
.withNonInterruptableCompiler(Some(params))(empty, params.token()) { pc =>
new ConvertToNamedArgumentsProvider(
pc.compiler(),
params,
argIndices.asScala.map(_.toInt).toSet
).convertToNamedArguments
})
.thenApplyAsync {
case Left(error: String) => throw new DisplayableException(error)
case Right(edits: List[l.TextEdit]) => edits.asJava
}
end convertToNamedArguments
override def selectionRange(
params: ju.List[OffsetParams]
): CompletableFuture[ju.List[l.SelectionRange]] =
CompletableFuture.completedFuture {
compilerAccess.withSharedCompiler(params.asScala.headOption)(
List.empty[l.SelectionRange].asJava
) { pc =>
new SelectionRangeProvider(
pc.compiler(),
params,
).selectionRange().asJava
}
}
end selectionRange
def hover(
params: OffsetParams
): CompletableFuture[ju.Optional[HoverSignature]] =
compilerAccess.withNonInterruptableCompiler(Some(params))(
ju.Optional.empty[HoverSignature](),
params.token()
) { access =>
val driver = access.compiler()
HoverProvider.hover(params, driver, search, config.hoverContentType())
}
end hover
def prepareRename(
params: OffsetParams
): CompletableFuture[ju.Optional[l.Range]] =
compilerAccess.withNonInterruptableCompiler(Some(params))(
Optional.empty[l.Range](),
params.token()
) { access =>
val driver = access.compiler()
Optional.ofNullable(
PcRenameProvider(driver, params, None).prepareRename().orNull
)
}
def rename(
params: OffsetParams,
name: String
): CompletableFuture[ju.List[l.TextEdit]] =
compilerAccess.withNonInterruptableCompiler(Some(params))(
List[l.TextEdit]().asJava,
params.token()
) { access =>
val driver = access.compiler()
PcRenameProvider(driver, params, Some(name)).rename().asJava
}
def newInstance(
buildTargetIdentifier: String,
classpath: ju.List[Path],
options: ju.List[String]
): PresentationCompiler =
copy(
buildTargetIdentifier = buildTargetIdentifier,
classpath = classpath.asScala.toSeq,
options = options.asScala.toList
)
def signatureHelp(params: OffsetParams): CompletableFuture[l.SignatureHelp] =
compilerAccess.withNonInterruptableCompiler(Some(params))(
new l.SignatureHelp(),
params.token()
) { access =>
val driver = access.compiler()
SignatureHelpProvider.signatureHelp(driver, params, search)
}
override def didChange(
params: VirtualFileParams
): CompletableFuture[ju.List[l.Diagnostic]] =
CompletableFuture.completedFuture(Nil.asJava)
override def didClose(uri: URI): Unit =
compilerAccess.withNonInterruptableCompiler(None)(
(),
EmptyCancelToken
) { access => access.compiler().close(uri) }
override def withExecutorService(
executorService: ExecutorService
): PresentationCompiler =
copy(ec = ExecutionContext.fromExecutorService(executorService))
override def withConfiguration(
config: PresentationCompilerConfig
): PresentationCompiler =
copy(config = config)
override def withScheduledExecutorService(
sh: ScheduledExecutorService
): PresentationCompiler =
copy(sh = Some(sh))
def withSearch(search: SymbolSearch): PresentationCompiler =
copy(search = search)
def withWorkspace(workspace: Path): PresentationCompiler =
copy(folderPath = Some(workspace))
override def isLoaded() = compilerAccess.isLoaded()
end ScalaPresentationCompiler