Skip to content

Commit 5a5573b

Browse files
committed
changes: make mtags compile in dotty, changes metals code and adds new based on comment collection commit
1 parent 7345d9c commit 5a5573b

25 files changed

+367
-197
lines changed

build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ val `scala3-bench-run` = Build.`scala3-bench-run`
2828
val dist = Build.dist
2929
val `community-build` = Build.`community-build`
3030
val `sbt-community-build` = Build.`sbt-community-build`
31+
val `scala3-presentation-compiler` = Build.`scala3-presentation-compiler`
3132

3233
val sjsSandbox = Build.sjsSandbox
3334
val sjsJUnitTests = Build.sjsJUnitTests

presentation-compiler/src/main/scala/meta/internal/mtags/MtagsEnrichments.scala

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ import dotty.tools.dotc.util.SourcePosition
2828
import dotty.tools.dotc.util.Spans
2929
import dotty.tools.dotc.util.Spans.Span
3030
import org.eclipse.{lsp4j as l}
31+
import dotty.tools.dotc.semanticdb.SymbolInformation
32+
import scala.meta.internal.metals.WorkspaceSymbolQuery
33+
import dotty.tools.dotc.semanticdb.SymbolInformation.Kind
3134

32-
object MtagsEnrichments extends ScalametaCommonEnrichments:
35+
object MtagsEnrichments extends CommonMtagsEnrichments:
3336

3437
extension (driver: InteractiveDriver)
3538

@@ -155,7 +158,7 @@ object MtagsEnrichments extends ScalametaCommonEnrichments:
155158
def isStale: Boolean =
156159
sym.sourcePos.span.exists && {
157160
val source = ctx.source
158-
if source ne sym.source then
161+
if (source ne sym.source) && source.path == sym.source.path then
159162
!source.content.startsWith(
160163
sym.decodedName.toString(),
161164
sym.sourcePos.span.point,
@@ -270,4 +273,16 @@ object MtagsEnrichments extends ScalametaCommonEnrichments:
270273
AppliedType(tycon, params.map(_.metalsDealias))
271274
case dealised => dealised
272275

276+
extension (query: WorkspaceSymbolQuery)
277+
def matches(info: SymbolInformation) =
278+
info.kind.isRelevantKind && query.matches(info.symbol)
279+
280+
extension (kind: Kind)
281+
def isRelevantKind: Boolean =
282+
kind match
283+
case Kind.OBJECT | Kind.PACKAGE_OBJECT | Kind.CLASS | Kind.TRAIT |
284+
Kind.INTERFACE | Kind.METHOD | Kind.TYPE =>
285+
true
286+
case _ => false
287+
273288
end MtagsEnrichments

presentation-compiler/src/main/scala/meta/internal/pc/AutoImports.scala

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import dotty.tools.dotc.core.Symbols.*
1616
import dotty.tools.dotc.util.SourcePosition
1717
import dotty.tools.dotc.util.Spans
1818
import org.eclipse.{lsp4j as l}
19+
import dotty.tools.dotc.core.Comments.Comment
1920

2021
object AutoImports extends AutoImportsBackticks:
2122

@@ -90,13 +91,14 @@ object AutoImports extends AutoImportsBackticks:
9091
pos: SourcePosition,
9192
text: String,
9293
tree: Tree,
94+
comments: List[Comment],
9395
indexedContext: IndexedContext,
9496
config: PresentationCompilerConfig,
9597
): AutoImportsGenerator =
9698

9799
import indexedContext.ctx
98100

99-
val importPos = autoImportPosition(pos, text, tree)
101+
val importPos = autoImportPosition(pos, text, tree, comments)
100102
val renameConfig: Map[Symbol, String] = AutoImport.renameConfigMap(config)
101103

102104
val renames =
@@ -288,6 +290,7 @@ object AutoImports extends AutoImportsBackticks:
288290
pos: SourcePosition,
289291
text: String,
290292
tree: Tree,
293+
comments: List[Comment],
291294
)(using Context): AutoImportPosition =
292295

293296
@tailrec
@@ -314,16 +317,23 @@ object AutoImports extends AutoImportsBackticks:
314317
}.headOption
315318
case _ => None
316319

320+
def skipUsingDirectivesOffset =
321+
comments
322+
.takeWhile(comment =>
323+
!comment.isDocComment && comment.span.end < firstObjectBody(tree)
324+
.fold(0)(_.span.start)
325+
)
326+
.lastOption
327+
.fold(0)(_.span.end + 1)
328+
317329
def forScalaSource: Option[AutoImportPosition] =
318330
lastPackageDef(None, tree).map { pkg =>
319331
val lastImportStatement =
320332
pkg.stats.takeWhile(_.isInstanceOf[Import]).lastOption
321333
val (lineNumber, padTop) = lastImportStatement match
322334
case Some(stm) => (stm.endPos.line + 1, false)
323335
case None if pkg.pid.symbol.isEmptyPackage =>
324-
val offset =
325-
ScriptFirstImportPosition.skipUsingDirectivesOffset(text)
326-
(pos.source.offsetToLine(offset), false)
336+
(pos.source.offsetToLine(skipUsingDirectivesOffset), false)
327337
case None =>
328338
val pos = pkg.pid.endPos
329339
val line =
@@ -346,8 +356,9 @@ object AutoImports extends AutoImportsBackticks:
346356
case None =>
347357
val scriptOffset =
348358
if isAmmonite then
349-
ScriptFirstImportPosition.ammoniteScStartOffset(text)
350-
else ScriptFirstImportPosition.scalaCliScStartOffset(text)
359+
ScriptFirstImportPosition.ammoniteScStartOffset(text, comments)
360+
else
361+
ScriptFirstImportPosition.scalaCliScStartOffset(text, comments)
351362

352363
scriptOffset.getOrElse(
353364
pos.source.lineToOffset(tmpl.self.srcPos.line)
@@ -360,7 +371,7 @@ object AutoImports extends AutoImportsBackticks:
360371

361372
def fileStart =
362373
AutoImportPosition(
363-
ScriptFirstImportPosition.skipUsingDirectivesOffset(text),
374+
skipUsingDirectivesOffset,
364375
0,
365376
padTop = false,
366377
)

presentation-compiler/src/main/scala/meta/internal/pc/AutoImportsProvider.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ final class AutoImportsProvider(
8282
correctedPos,
8383
params.text,
8484
tree,
85+
unit.comments,
8586
indexedContext.importContext,
8687
config,
8788
)

presentation-compiler/src/main/scala/meta/internal/pc/HoverProvider.scala

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -183,30 +183,16 @@ end HoverProvider
183183
object SelectDynamicExtractor:
184184
def unapply(path: List[Tree])(using Context) =
185185
path match
186-
// the same tests as below, since 3.3.1-RC1 path starts with Select
187-
case Select(_, _) :: Apply(
188-
Select(Apply(reflSel, List(sel)), n),
189-
List(Literal(Constant(name: String))),
190-
) :: _
191-
if (n == nme.selectDynamic || n == nme.applyDynamic) &&
192-
nme.reflectiveSelectable == reflSel.symbol.name =>
193-
Some(sel, n, name)
194186
// tests `structural-types` and `structural-types1` in HoverScala3TypeSuite
195-
case Apply(
187+
case Select(_, _) :: Apply(
196188
Select(Apply(reflSel, List(sel)), n),
197189
List(Literal(Constant(name: String))),
198190
) :: _
199191
if (n == nme.selectDynamic || n == nme.applyDynamic) &&
200192
nme.reflectiveSelectable == reflSel.symbol.name =>
201193
Some(sel, n, name)
202-
// the same tests as below, since 3.3.1-RC1 path starts with Select
203-
case Select(_, _) :: Apply(
204-
Select(sel, n),
205-
List(Literal(Constant(name: String))),
206-
) :: _ if n == nme.selectDynamic || n == nme.applyDynamic =>
207-
Some(sel, n, name)
208194
// tests `selectable`, `selectable2` and `selectable-full` in HoverScala3TypeSuite
209-
case Apply(
195+
case Select(_, _) :: Apply(
210196
Select(sel, n),
211197
List(Literal(Constant(name: String))),
212198
) :: _ if n == nme.selectDynamic || n == nme.applyDynamic =>

presentation-compiler/src/main/scala/meta/internal/pc/InferredTypeProvider.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ final class InferredTypeProvider(
7979
pos,
8080
params.text,
8181
unit.tpdTree,
82+
unit.comments,
8283
indexedCtx,
8384
config,
8485
)

presentation-compiler/src/main/scala/meta/internal/pc/PcCollector.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,10 @@ abstract class PcCollector[T](
247247
def collectParams(
248248
extMethods: ExtMethods
249249
): Option[ExtensionParamOccurence] =
250-
MetalsNavigateAST
251-
.pathToExtensionParam(pos.span, extMethods)(using compilatonUnitContext)
250+
NavigateAST
251+
.pathTo(pos.span, extMethods.paramss.flatten)(using
252+
compilatonUnitContext
253+
)
252254
.collectFirst {
253255
case v: untpd.ValOrTypeDef =>
254256
ExtensionParamOccurence(

presentation-compiler/src/main/scala/meta/internal/pc/PcInlineValueProviderImpl.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ final class PcInlineValueProviderImpl(
9292
case _ => false
9393

9494
end referenceRequiresBrackets
95-
// format: on
9695

9796
private def adjustRhs(pos: SourcePosition) =
9897
def extend(point: Int, acceptedChar: Char, step: Int): Int =

presentation-compiler/src/main/scala/meta/internal/pc/ScalaPresentationCompiler.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import java.util.concurrent.ExecutorService
99
import java.util.concurrent.ScheduledExecutorService
1010
import java.{util as ju}
1111

12-
import scala.collection.JavaConverters.*
12+
import scala.jdk.CollectionConverters._
1313
import scala.concurrent.ExecutionContext
1414
import scala.concurrent.ExecutionContextExecutor
1515

@@ -18,12 +18,12 @@ import scala.meta.internal.metals.EmptyReportContext
1818
import scala.meta.internal.metals.ReportContext
1919
import scala.meta.internal.metals.ReportLevel
2020
import scala.meta.internal.metals.StdReportContext
21-
import scala.meta.internal.mtags.BuildInfo
2221
import scala.meta.internal.pc.completions.CompletionProvider
2322
import scala.meta.internal.pc.completions.OverrideCompletions
2423
import scala.meta.pc.*
2524

2625
import dotty.tools.dotc.reporting.StoreReporter
26+
import dotty.tools.pc.util.BuildInfo
2727
import org.eclipse.lsp4j.DocumentHighlight
2828
import org.eclipse.lsp4j.TextEdit
2929
import org.eclipse.{lsp4j as l}
@@ -42,7 +42,7 @@ case class ScalaPresentationCompiler(
4242

4343
def this() = this("", Nil, Nil)
4444

45-
val scalaVersion = BuildInfo.scalaCompilerVersion
45+
val scalaVersion = BuildInfo.scalaVersion
4646

4747
private val forbiddenOptions = Set("-print-lines", "-print-tasty")
4848
private val forbiddenDoubleOptions = Set("-release")
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package scala.meta.internal.pc
2+
3+
import dotty.tools.dotc.core.Comments.Comment
4+
import dotty.tools.dotc.ast.tpd.*
5+
6+
object ScriptFirstImportPosition:
7+
8+
val usingDirectives: List[String] = List("// using", "//> using")
9+
val ammHeaders: List[String] = List("// scala", "// ammonite")
10+
11+
def ammoniteScStartOffset(
12+
text: String,
13+
comments: List[Comment],
14+
): Option[Int] =
15+
findStartOffset(text, comments, commentQuery = "/*<start>*/", ammHeaders)
16+
17+
def scalaCliScStartOffset(
18+
text: String,
19+
comments: List[Comment],
20+
): Option[Int] =
21+
findStartOffset(
22+
text,
23+
comments,
24+
commentQuery = "/*<script>*/",
25+
usingDirectives,
26+
)
27+
28+
def findStartOffset(
29+
text: String,
30+
comments: List[Comment],
31+
commentQuery: String,
32+
excludedComments: List[String],
33+
): Option[Int] =
34+
val startComment =
35+
Option(comments.indexWhere(_.raw == commentQuery)).filter(_ >= 0)
36+
startComment.flatMap { startIndex =>
37+
val commentsInsideScript = comments.drop(startIndex + 1)
38+
val (headers, rest) =
39+
commentsInsideScript.span(comment =>
40+
excludedComments.exists(comment.raw.startsWith)
41+
)
42+
if headers.isEmpty then Some(comments(startIndex).span.end + 1)
43+
else headers.lastOption.map(_.span.end + 1)
44+
}

0 commit comments

Comments
 (0)