Skip to content

Commit 6ad9dc7

Browse files
committed
Bump presentatation compiler to c8ef4e0
1 parent f59457c commit 6ad9dc7

30 files changed

+485
-75
lines changed

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

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import dotty.tools.pc.utils.MtagsEnrichments.*
1919

2020
import org.eclipse.lsp4j as l
2121

22-
object AutoImports extends AutoImportsBackticks:
22+
object AutoImports:
2323

2424
object AutoImport:
2525
def renameConfigMap(config: PresentationCompilerConfig)(using
@@ -199,7 +199,7 @@ object AutoImports extends AutoImportsBackticks:
199199
(
200200
SymbolIdent.Select(
201201
ownerImport.ident,
202-
symbol.nameBacktickedImport
202+
symbol.nameBackticked(false)
203203
),
204204
ownerImport.importSel,
205205
)
@@ -233,7 +233,7 @@ object AutoImports extends AutoImportsBackticks:
233233
symbol,
234234
SymbolIdent.Select(
235235
SymbolIdent.direct(rename),
236-
symbol.nameBacktickedImport
236+
symbol.nameBackticked(false)
237237
),
238238
importSel
239239
)
@@ -272,7 +272,7 @@ object AutoImports extends AutoImportsBackticks:
272272
.map {
273273
case ImportSel.Direct(sym) => importName(sym)
274274
case ImportSel.Rename(sym, rename) =>
275-
s"${importName(sym.owner)}.{${sym.nameBacktickedImport} => $rename}"
275+
s"${importName(sym.owner)}.{${sym.nameBackticked(false)} => $rename}"
276276
}
277277
.map(sel => s"${indent}import $sel")
278278
.mkString(topPadding, "\n", "\n")
@@ -283,8 +283,8 @@ object AutoImports extends AutoImportsBackticks:
283283

284284
private def importName(sym: Symbol): String =
285285
if indexedContext.importContext.toplevelClashes(sym) then
286-
s"_root_.${sym.fullNameBacktickedImport}"
287-
else sym.fullNameBacktickedImport
286+
s"_root_.${sym.fullNameBackticked(false)}"
287+
else sym.fullNameBackticked(false)
288288
end AutoImportsGenerator
289289

290290
private def autoImportPosition(
@@ -361,9 +361,12 @@ object AutoImports extends AutoImportsBackticks:
361361
else
362362
ScriptFirstImportPosition.scalaCliScStartOffset(text, comments)
363363

364-
scriptOffset.getOrElse(
365-
pos.source.lineToOffset(tmpl.self.srcPos.line)
366-
)
364+
scriptOffset.getOrElse {
365+
val tmplPoint = tmpl.self.srcPos.span.point
366+
if tmplPoint >= 0 && tmplPoint < pos.source.length
367+
then pos.source.lineToOffset(tmpl.self.srcPos.line)
368+
else 0
369+
}
367370
new AutoImportPosition(offset, text, false)
368371
}
369372
end forScript
@@ -388,11 +391,3 @@ object AutoImports extends AutoImportsBackticks:
388391
end autoImportPosition
389392

390393
end AutoImports
391-
392-
trait AutoImportsBackticks:
393-
// Avoids backticketing import parts that match soft keywords
394-
extension (sym: Symbol)(using Context)
395-
def fullNameBacktickedImport: String =
396-
sym.fullNameBackticked(KeywordWrapper.Scala3SoftKeywords)
397-
def nameBacktickedImport: String =
398-
sym.nameBackticked(KeywordWrapper.Scala3SoftKeywords)

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dotty.tools.pc
22

33
import java.nio.file.Paths
44

5+
import scala.meta.internal.metals.ReportContext
56
import scala.meta.internal.pc.ExtractMethodUtils
67
import scala.meta.pc.OffsetParams
78
import scala.meta.pc.RangeParams
@@ -33,7 +34,8 @@ final class ExtractMethodProvider(
3334
driver: InteractiveDriver,
3435
search: SymbolSearch,
3536
noIndent: Boolean
36-
) extends ExtractMethodUtils:
37+
)(using ReportContext)
38+
extends ExtractMethodUtils:
3739

3840
def extractMethod(): List[TextEdit] =
3941
val text = range.text()

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,21 @@ object IndexedContext:
136136
)
137137

138138
private def extractNames(ctx: Context): Names =
139+
def isAccessibleFromSafe(sym: Symbol, site: Type): Boolean =
140+
try sym.isAccessibleFrom(site, superAccess = false)
141+
catch
142+
case NonFatal(e) =>
143+
false
139144

140145
def accessibleSymbols(site: Type, tpe: Type)(using
141146
Context
142147
): List[Symbol] =
143-
tpe.decls.toList.filter(sym =>
144-
sym.isAccessibleFrom(site, superAccess = false)
145-
)
148+
tpe.decls.toList.filter(sym => isAccessibleFromSafe(sym, site))
146149

147150
def accesibleMembers(site: Type)(using Context): List[Symbol] =
148151
site.allMembers
149152
.filter(denot =>
150-
try denot.symbol.isAccessibleFrom(site, superAccess = false)
153+
try isAccessibleFromSafe(denot.symbol, site)
151154
catch
152155
case NonFatal(e) =>
153156
false

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dotty.tools.pc
33
import java.nio.file.Paths
44

55
import scala.annotation.tailrec
6+
import scala.meta.internal.metals.ReportContext
67
import scala.meta.pc.OffsetParams
78
import scala.meta.pc.PresentationCompilerConfig
89
import scala.meta.pc.SymbolSearch
@@ -50,7 +51,7 @@ final class InferredTypeProvider(
5051
driver: InteractiveDriver,
5152
config: PresentationCompilerConfig,
5253
symbolSearch: SymbolSearch
53-
):
54+
)(using ReportContext):
5455

5556
case class AdjustTypeOpts(
5657
text: String,

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,22 @@ abstract class PcCollector[T](
9191
else pos1
9292

9393
val pos =
94-
if sourceText(pos0.`end` - 1) == ',' then pos0.withEnd(pos0.`end` - 1)
94+
if pos0.end > 0 && sourceText(pos0.end - 1) == ',' then
95+
pos0.withEnd(pos0.end - 1)
9596
else pos0
9697
val isBackticked =
97-
sourceText(pos.start) == '`' && sourceText(pos.end - 1) == '`'
98+
sourceText(pos.start) == '`' &&
99+
pos.end > 0 &&
100+
sourceText(pos.end - 1) == '`'
98101
// when the old name contains backticks, the position is incorrect
99102
val isOldNameBackticked = sourceText(pos.start) != '`' &&
103+
pos.start > 0 &&
100104
sourceText(pos.start - 1) == '`' &&
101105
sourceText(pos.end) == '`'
102106
if isBackticked && forRename then
103-
(pos.withStart(pos.start + 1).withEnd(pos.`end` - 1), true)
107+
(pos.withStart(pos.start + 1).withEnd(pos.end - 1), true)
104108
else if isOldNameBackticked then
105-
(pos.withStart(pos.start - 1).withEnd(pos.`end` + 1), false)
109+
(pos.withStart(pos.start - 1).withEnd(pos.end + 1), false)
106110
else (pos, false)
107111
end adjust
108112

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import dotty.tools.dotc.core.Contexts.*
66
import dotty.tools.dotc.core.Flags.*
77
import dotty.tools.dotc.core.Names.*
88
import dotty.tools.dotc.core.Symbols.*
9-
import dotty.tools.dotc.semanticdb._
9+
import dotty.tools.dotc.semanticdb.*
1010

1111
object SemanticdbSymbols:
1212

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,18 @@ class CompletionProvider(
218218
case _ =>
219219
false
220220

221+
lazy val backtickSoftKeyword = path match
222+
case (_: Select) :: _ => false
223+
case _ => true
224+
221225
def mkItemWithImports(
222226
v: CompletionValue.Workspace | CompletionValue.Extension |
223227
CompletionValue.Interpolator
224228
) =
225229
val sym = v.symbol
226230
path match
227231
case (_: Ident) :: (_: Import) :: _ =>
228-
mkItem(sym.fullNameBackticked)
232+
mkItem(sym.fullNameBackticked(backtickSoftKeyword = false))
229233
case _ =>
230234
autoImports.editsForSymbol(v.importSymbol) match
231235
case Some(edits) =>
@@ -239,7 +243,9 @@ class CompletionProvider(
239243
case _ =>
240244
mkItem(
241245
v.insertText.getOrElse(
242-
ident.backticked + completionTextSuffix
246+
ident.backticked(
247+
backtickSoftKeyword
248+
) + completionTextSuffix
243249
),
244250
edits.edits,
245251
range = v.range
@@ -249,14 +255,18 @@ class CompletionProvider(
249255
r match
250256
case IndexedContext.Result.InScope =>
251257
mkItem(
252-
ident.backticked + completionTextSuffix
258+
ident.backticked(backtickSoftKeyword) + completionTextSuffix
253259
)
254260
case _ if isInStringInterpolation =>
255261
mkItem(
256262
"{" + sym.fullNameBackticked + completionTextSuffix + "}"
257263
)
258264
case _ =>
259-
mkItem(sym.fullNameBackticked + completionTextSuffix)
265+
mkItem(
266+
sym.fullNameBackticked(
267+
backtickSoftKeyword
268+
) + completionTextSuffix
269+
)
260270
end match
261271
end match
262272
end match
@@ -268,7 +278,8 @@ class CompletionProvider(
268278
case v: CompletionValue.Interpolator if v.isWorkspace || v.isExtension =>
269279
mkItemWithImports(v)
270280
case _ =>
271-
val insert = completion.insertText.getOrElse(ident.backticked)
281+
val insert =
282+
completion.insertText.getOrElse(ident.backticked(backtickSoftKeyword))
272283
mkItem(
273284
insert + completionTextSuffix,
274285
range = completion.range

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ class Completions(
579579
).map(visit).forall(_ == true),
580580
)
581581
Some(search.search(query, buildTargetIdentifier, visitor))
582-
case CompletionKind.Members if query.nonEmpty =>
582+
case CompletionKind.Members =>
583583
val visitor = new CompilerSearchVisitor(sym =>
584584
if sym.is(ExtensionMethod) &&
585585
qualType.widenDealias <:< sym.extensionParam.info.widenDealias
@@ -592,8 +592,6 @@ class Completions(
592592
else false,
593593
)
594594
Some(search.searchMethods(query, buildTargetIdentifier, visitor))
595-
case CompletionKind.Members => // query.isEmpry
596-
Some(SymbolSearch.Result.INCOMPLETE)
597595
end match
598596
end enrichWithSymbolSearch
599597

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package completions
44
import java.util as ju
55

66
import scala.jdk.CollectionConverters._
7+
import scala.meta.internal.metals.ReportContext
78
import scala.meta.pc.OffsetParams
89
import scala.meta.pc.PresentationCompilerConfig
910
import scala.meta.pc.PresentationCompilerConfig.OverrideDefFormat
@@ -52,7 +53,7 @@ object OverrideCompletions:
5253
config: PresentationCompilerConfig,
5354
autoImportsGen: AutoImportsGenerator,
5455
fallbackName: Option[String]
55-
): List[CompletionValue] =
56+
)(using ReportContext): List[CompletionValue] =
5657
import indexedContext.ctx
5758
val clazz = td.symbol.asClass
5859
val syntheticCoreMethods: Set[Name] =
@@ -133,7 +134,7 @@ object OverrideCompletions:
133134
driver: InteractiveDriver,
134135
search: SymbolSearch,
135136
config: PresentationCompilerConfig
136-
): ju.List[l.TextEdit] =
137+
)(using ReportContext): ju.List[l.TextEdit] =
137138
object FindTypeDef:
138139
def unapply(path: List[Tree])(using Context): Option[TypeDef] = path match
139140
// class <<Foo>> extends ... {}
@@ -225,7 +226,7 @@ object OverrideCompletions:
225226
config: PresentationCompilerConfig
226227
)(
227228
defn: TargetDef
228-
)(using Context): List[l.TextEdit] =
229+
)(using Context, ReportContext): List[l.TextEdit] =
229230
def calcIndent(
230231
defn: TargetDef,
231232
decls: List[Symbol],
@@ -391,7 +392,7 @@ object OverrideCompletions:
391392
config: PresentationCompilerConfig,
392393
autoImportsGen: AutoImportsGenerator,
393394
shouldAddOverrideKwd: Boolean
394-
)(using Context): CompletionValue.Override =
395+
)(using Context, ReportContext): CompletionValue.Override =
395396
val renames = AutoImport.renameConfigMap(config)
396397
val printer = MetalsPrinter.standard(
397398
indexedContext,

presentation-compiler/src/main/dotty/tools/pc/printer/DotcPrinter.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ object DotcPrinter:
6565
def fullName(sym: Symbol): String =
6666
fullNameString(sym)
6767

68+
override def toTextRef(tp: SingletonType): Text = controlled {
69+
tp match
70+
case tp: TermRef if !printDebug && tp.symbol.is(Package) =>
71+
toTextPrefix(tp.prefix) ~
72+
tp.symbol.name.stripModuleClassSuffix.toString()
73+
case _ =>
74+
super.toTextRef(tp)
75+
}
76+
6877
override def toText(tp: Type): Text =
6978
// Override the behavior for `AppliedType` because
7079
// `toText` in RefinedPrinter doesn't pretty print AppliedType

presentation-compiler/src/main/dotty/tools/pc/printer/MetalsPrinter.scala

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ package dotty.tools.pc.printer
22

33
import scala.collection.mutable
44
import scala.meta.internal.jdk.CollectionConverters.*
5+
import scala.meta.internal.metals.Report
6+
import scala.meta.internal.metals.ReportContext
57
import scala.meta.pc.SymbolDocumentation
68
import scala.meta.pc.SymbolSearch
9+
import scala.util.Failure
10+
import scala.util.Success
11+
import scala.util.Try
712

813
import dotty.tools.dotc.core.Contexts.Context
914
import dotty.tools.dotc.core.Flags
@@ -28,7 +33,8 @@ class MetalsPrinter(
2833
includeDefaultParam: MetalsPrinter.IncludeDefaultParam =
2934
IncludeDefaultParam.ResolveLater,
3035
)(using
31-
Context
36+
Context,
37+
ReportContext
3238
):
3339

3440
private val methodFlags =
@@ -63,8 +69,26 @@ class MetalsPrinter(
6369
case _ => None
6470

6571
def tpe(tpe: Type): String =
66-
val short = names.shortType(tpe)
72+
val short = Try(names.shortType(tpe)) match
73+
case Success(short) => short
74+
case Failure(e) =>
75+
val reportContext = summon[ReportContext]
76+
val report = Report(
77+
"short-name-error",
78+
s"""|Error while printing type, could not create short name for type:
79+
|
80+
|$tpe
81+
|
82+
|Exception:
83+
|${e.getMessage}
84+
|${e.getStackTrace.mkString("\n")}
85+
|""".stripMargin,
86+
tpe.typeSymbol.name.show
87+
)
88+
reportContext.unsanitized.create(report, ifVerbose = false)
89+
tpe
6790
dotcPrinter.tpe(short)
91+
end tpe
6892

6993
def hoverSymbol(sym: Symbol, info: Type)(using Context): String =
7094
val typeSymbol = info.typeSymbol
@@ -322,7 +346,7 @@ class MetalsPrinter(
322346
index: Int,
323347
defaultValues: => Seq[SymbolDocumentation],
324348
nameToInfo: Map[Name, Type]
325-
): String =
349+
)(using ReportContext): String =
326350
val docInfo = defaultValues.lift(index)
327351
val rawKeywordName = dotcPrinter.name(param)
328352
val keywordName = docInfo match
@@ -409,7 +433,7 @@ object MetalsPrinter:
409433
symbolSearch: SymbolSearch,
410434
includeDefaultParam: IncludeDefaultParam,
411435
renames: Map[Symbol, String] = Map.empty
412-
): MetalsPrinter =
436+
)(using ReportContext): MetalsPrinter =
413437
import indexed.ctx
414438
MetalsPrinter(
415439
new ShortenedNames(indexed, renames),
@@ -423,7 +447,7 @@ object MetalsPrinter:
423447
indexed: IndexedContext,
424448
symbolSearch: SymbolSearch,
425449
includeDefaultParam: IncludeDefaultParam
426-
): MetalsPrinter =
450+
)(using ReportContext): MetalsPrinter =
427451
import shortenedNames.indexedContext.ctx
428452
MetalsPrinter(
429453
shortenedNames,

0 commit comments

Comments
 (0)