Skip to content

Commit d482c88

Browse files
committed
remove metals wrappers around compiler implementation created to workaround bugs
1 parent ebbb0f4 commit d482c88

13 files changed

+42
-103
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class InteractiveDriver(val settings: List[String]) extends Driver {
145145
(fromSource ++ fromClassPath).distinct
146146
}
147147

148-
def run(uri: URI, sourceCode: String): List[Diagnostic] = run(uri, toSource(uri, sourceCode))
148+
def run(uri: URI, sourceCode: String): List[Diagnostic] = run(uri, SourceFile.virtual(uri, sourceCode))
149149

150150
def run(uri: URI, source: SourceFile): List[Diagnostic] = {
151151
import typer.ImportInfo._
@@ -297,9 +297,6 @@ class InteractiveDriver(val settings: List[String]) extends Driver {
297297
cleanupTree(tree)
298298
}
299299

300-
private def toSource(uri: URI, sourceCode: String): SourceFile =
301-
SourceFile.virtual(Paths.get(uri).toString, sourceCode)
302-
303300
/**
304301
* Initialize this driver and compiler.
305302
*
@@ -323,7 +320,7 @@ object InteractiveDriver {
323320
else
324321
try
325322
// We don't use file.file here since it'll be null
326-
// for the VirtualFiles created by InteractiveDriver#toSource
323+
// for the VirtualFiles created by SourceFile#virtual
327324
// TODO: To avoid these round trip conversions, we could add an
328325
// AbstractFile#toUri method and implement it by returning a constant
329326
// passed as a parameter to a constructor of VirtualFile

compiler/src/dotty/tools/dotc/util/SourceFile.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ import scala.collection.mutable.ArrayBuffer
1616
import scala.util.chaining.given
1717

1818
import java.io.File.separator
19+
import java.net.URI
1920
import java.nio.charset.StandardCharsets
20-
import java.nio.file.{FileSystemException, NoSuchFileException}
21+
import java.nio.file.{FileSystemException, NoSuchFileException, Paths}
2122
import java.util.Optional
2223
import java.util.concurrent.atomic.AtomicInteger
2324
import java.util.regex.Pattern
@@ -222,6 +223,13 @@ object SourceFile {
222223
SourceFile(new VirtualFile(name.replace(separator, "/"), content.getBytes(StandardCharsets.UTF_8)), content.toCharArray)
223224
.tap(_._maybeInComplete = maybeIncomplete)
224225

226+
/** A helper method to create a virtual source file for given URI.
227+
* It relies on SourceFile#virtual implementation to create the virtual file.
228+
*/
229+
def virtual(uri: URI, content: String): SourceFile =
230+
val path = Paths.get(uri).toString
231+
SourceFile.virtual(path, content)
232+
225233
/** Returns the relative path of `source` within the `reference` path
226234
*
227235
* It returns the absolute path of `source` if it is not contained in `reference`.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ final class AutoImportsProvider(
4545
Interactive.pathTo(newctx.compilationUnit.tpdTree, pos.span)(using newctx)
4646

4747
val indexedContext = IndexedContext(
48-
MetalsInteractive.contextOfPath(path)(using newctx)
48+
Interactive.contextOfPath(path)(using newctx)
4949
)
5050
import indexedContext.ctx
5151

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

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

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import dotty.tools.dotc.interactive.Interactive
2020
import dotty.tools.dotc.interactive.InteractiveDriver
2121
import dotty.tools.dotc.util.SourceFile
2222
import dotty.tools.dotc.util.SourcePosition
23-
import dotty.tools.pc.MetalsInteractive.*
2423
import dotty.tools.pc.printer.MetalsPrinter
2524
import dotty.tools.pc.printer.MetalsPrinter.IncludeDefaultParam
2625
import dotty.tools.pc.utils.MtagsEnrichments.*
@@ -48,7 +47,7 @@ final class ExtractMethodProvider(
4847
Interactive.pathTo(driver.openedTrees(uri), pos)(using driver.currentCtx)
4948
given locatedCtx: Context =
5049
val newctx = driver.currentCtx.fresh.setCompilationUnit(unit)
51-
MetalsInteractive.contextOfPath(path)(using newctx)
50+
Interactive.contextOfPath(path)(using newctx)
5251
val indexedCtx = IndexedContext(locatedCtx)
5352
val printer =
5453
MetalsPrinter.standard(indexedCtx, search, IncludeDefaultParam.Never)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import dotty.tools.dotc.core.Symbols.*
1919
import dotty.tools.dotc.core.Types.*
2020
import dotty.tools.dotc.interactive.Interactive
2121
import dotty.tools.dotc.interactive.InteractiveDriver
22+
import dotty.tools.dotc.util.SourceFile
2223
import dotty.tools.dotc.util.SourcePosition
2324
import dotty.tools.pc.printer.MetalsPrinter
2425
import dotty.tools.pc.utils.MtagsEnrichments.*
@@ -31,7 +32,7 @@ object HoverProvider:
3132
search: SymbolSearch
3233
)(implicit reportContext: ReportContext): ju.Optional[HoverSignature] =
3334
val uri = params.uri
34-
val sourceFile = CompilerInterfaces.toSource(params.uri, params.text)
35+
val sourceFile = SourceFile.virtual(params.uri, params.text)
3536
driver.run(uri, sourceFile)
3637

3738
given ctx: Context = driver.currentCtx
@@ -85,7 +86,7 @@ object HoverProvider:
8586
case Some(unit) =>
8687
val newctx =
8788
ctx.fresh.setCompilationUnit(unit)
88-
MetalsInteractive.contextOfPath(enclosing)(using newctx)
89+
Interactive.contextOfPath(enclosing)(using newctx)
8990
case None => ctx
9091
val printer = MetalsPrinter.standard(
9192
IndexedContext(printerContext),

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

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,66 +19,6 @@ import dotty.tools.dotc.util.SourcePosition
1919

2020
object MetalsInteractive:
2121

22-
// This is a copy of dotty.tools.dotc.Interactive.contextOfPath
23-
// with a minor fix in how it processes `Template`
24-
//
25-
// Might be removed after merging https://github.com/lampepfl/dotty/pull/12783
26-
def contextOfPath(path: List[Tree])(using Context): Context = path match
27-
case Nil | _ :: Nil =>
28-
ctx.fresh
29-
case nested :: encl :: rest =>
30-
val outer = contextOfPath(encl :: rest)
31-
try
32-
encl match
33-
case tree @ PackageDef(pkg, stats) =>
34-
assert(tree.symbol.exists)
35-
if nested `eq` pkg then outer
36-
else
37-
contextOfStat(
38-
stats,
39-
nested,
40-
pkg.symbol.moduleClass,
41-
outer.packageContext(tree, tree.symbol)
42-
)
43-
case tree: DefDef =>
44-
assert(tree.symbol.exists)
45-
val localCtx = outer.localContext(tree, tree.symbol).setNewScope
46-
for params <- tree.paramss; param <- params do
47-
localCtx.enter(param.symbol)
48-
// Note: this overapproximates visibility a bit, since value parameters are only visible
49-
// in subsequent parameter sections
50-
localCtx
51-
case tree: MemberDef =>
52-
assert(tree.symbol.exists)
53-
outer.localContext(tree, tree.symbol)
54-
case tree @ Block(stats, expr) =>
55-
val localCtx = outer.fresh.setNewScope
56-
stats.foreach {
57-
case stat: MemberDef => localCtx.enter(stat.symbol)
58-
case _ =>
59-
}
60-
contextOfStat(stats, nested, ctx.owner, localCtx)
61-
case tree @ CaseDef(pat, guard, rhs) if nested `eq` rhs =>
62-
val localCtx = outer.fresh.setNewScope
63-
pat.foreachSubTree {
64-
case bind: Bind => localCtx.enter(bind.symbol)
65-
case _ =>
66-
}
67-
localCtx
68-
case tree @ Template(constr, _, self, _) =>
69-
if (constr :: self :: tree.parents).contains(nested) then outer
70-
else
71-
contextOfStat(
72-
tree.body,
73-
nested,
74-
tree.symbol,
75-
outer.inClassContext(self.symbol)
76-
)
77-
case _ =>
78-
outer
79-
catch case ex: CyclicReference => outer
80-
end try
81-
8222
def contextOfStat(
8323
stats: List[Tree],
8424
stat: Tree,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ final class PcInlineValueProviderImpl(
162162
val path =
163163
Interactive.pathTo(unit.tpdTree, occurence.pos.span)(using newctx)
164164
val indexedContext = IndexedContext(
165-
MetalsInteractive.contextOfPath(path)(using newctx)
165+
Interactive.contextOfPath(path)(using newctx)
166166
)
167167
import indexedContext.ctx
168168
val conflictingSymbols = symbols

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,26 @@ class SelectionRangeProvider(
5454
selectionRange
5555
}
5656

57-
val comments = driver.compilationUnits.get(uri).map(_.comments).toList.flatten
57+
val comments =
58+
driver.compilationUnits.get(uri).map(_.comments).toList.flatten
5859

59-
val commentRanges = comments.find(_.span.contains(pos.span)).map { comment =>
60-
val startLine = source.offsetToLine(comment.span.start)
61-
val endLine = source.offsetToLine(comment.span.end)
62-
val startChar = source.column(comment.span.start)
63-
val endChar = source.column(comment.span.end)
60+
val commentRanges = comments
61+
.find(_.span.contains(pos.span))
62+
.map { comment =>
63+
val startLine = source.offsetToLine(comment.span.start)
64+
val endLine = source.offsetToLine(comment.span.end)
65+
val startChar = source.column(comment.span.start)
66+
val endChar = source.column(comment.span.end)
6467

65-
new SelectionRange():
66-
setRange(new lsp4j.Range(lsp4j.Position(startLine, startChar), lsp4j.Position(endLine, endChar)))
67-
}.toList
68+
new SelectionRange():
69+
setRange(
70+
new lsp4j.Range(
71+
lsp4j.Position(startLine, startChar),
72+
lsp4j.Position(endLine, endChar)
73+
)
74+
)
75+
}
76+
.toList
6877

6978
(commentRanges ++ bareRanges)
7079
.reduceRightOption(setParent)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import dotty.tools.dotc.interactive.Interactive
1616
import dotty.tools.dotc.interactive.InteractiveDriver
1717
import dotty.tools.dotc.util.Signatures
1818
import dotty.tools.dotc.util.Signatures.Signature
19+
import dotty.tools.dotc.util.SourceFile
1920
import dotty.tools.dotc.util.SourcePosition
2021
import dotty.tools.pc.utils.MtagsEnrichments.*
2122

@@ -29,7 +30,7 @@ object SignatureHelpProvider:
2930
search: SymbolSearch
3031
) =
3132
val uri = params.uri
32-
val sourceFile = CompilerInterfaces.toSource(params.uri, params.text)
33+
val sourceFile = SourceFile.virtual(params.uri, params.text)
3334
driver.run(uri, sourceFile)
3435

3536
given ctx: Context = driver.currentCtx

presentation-compiler/src/main/dotty/tools/pc/completions/CompletionProvider.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import dotty.tools.dotc.core.Contexts.Context
1616
import dotty.tools.dotc.core.StdNames
1717
import dotty.tools.dotc.interactive.Interactive
1818
import dotty.tools.dotc.interactive.InteractiveDriver
19+
import dotty.tools.dotc.util.SourceFile
1920
import dotty.tools.pc.AutoImports.AutoImportEdits
2021
import dotty.tools.pc.AutoImports.AutoImportsGenerator
2122
import dotty.tools.pc.printer.MetalsPrinter
@@ -42,7 +43,7 @@ class CompletionProvider(
4243
val uri = params.uri
4344

4445
val code = applyCompletionCursor(params)
45-
val sourceFile = CompilerInterfaces.toSource(params.uri, code)
46+
val sourceFile = SourceFile.virtual(params.uri, code)
4647
driver.run(uri, sourceFile)
4748

4849
val ctx = driver.currentCtx
@@ -58,7 +59,7 @@ class CompletionProvider(
5859
newctx
5960
)
6061
val locatedCtx =
61-
MetalsInteractive.contextOfPath(tpdPath)(using newctx)
62+
Interactive.contextOfPath(tpdPath)(using newctx)
6263
val indexedCtx = IndexedContext(locatedCtx)
6364
val completionPos =
6465
CompletionPos.infer(pos, params, path)(using newctx)

presentation-compiler/src/main/dotty/tools/pc/completions/OverrideCompletions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ object OverrideCompletions:
188188
case path => path
189189

190190
val indexedContext = IndexedContext(
191-
MetalsInteractive.contextOfPath(path)(using newctx)
191+
Interactive.contextOfPath(path)(using newctx)
192192
)
193193
import indexedContext.ctx
194194

presentation-compiler/src/main/dotty/tools/pc/utils/MtagsEnrichments.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import dotty.tools.dotc.semanticdb.SymbolInformation.Kind
2929
import dotty.tools.dotc.util.SourcePosition
3030
import dotty.tools.dotc.util.Spans
3131
import dotty.tools.dotc.util.Spans.Span
32-
import dotty.tools.pc.MetalsInteractive
3332
import dotty.tools.pc.SemanticdbSymbols
3433

3534
import org.eclipse.lsp4j as l
@@ -70,7 +69,7 @@ object MtagsEnrichments extends CommonMtagsEnrichments:
7069
Interactive.pathTo(newctx.compilationUnit.tpdTree, pos.span)(using
7170
newctx
7271
)
73-
MetalsInteractive.contextOfPath(tpdPath)(using newctx)
72+
Interactive.contextOfPath(tpdPath)(using newctx)
7473
end localContext
7574

7675
end extension

0 commit comments

Comments
 (0)