Skip to content

Commit 5ee339d

Browse files
committed
use compiler printer instead of ShortenedNames with ShortType
1 parent 6ad9dc7 commit 5ee339d

17 files changed

+222
-513
lines changed

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
179179
if (printWithoutPrefix.contains(tp.symbol))
180180
toText(tp.name)
181181
else
182-
toTextPrefix(tp.prefix) ~ selectionString(tp)
182+
trimPrefixToScope(tp) ~ selectionString(tp)
183183
case tp: TermParamRef =>
184184
ParamRefNameString(tp) ~ lambdaHash(tp.binder) ~ ".type"
185185
case tp: TypeParamRef =>
@@ -353,7 +353,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
353353
def toTextRef(tp: SingletonType): Text = controlled {
354354
tp match {
355355
case tp: TermRef =>
356-
toTextPrefix(tp.prefix) ~ selectionString(tp)
356+
trimPrefixToScope(tp) ~ selectionString(tp)
357357
case tp: ThisType =>
358358
nameString(tp.cls) + ".this"
359359
case SuperType(thistpe: SingletonType, _) =>
@@ -393,6 +393,9 @@ class PlainPrinter(_ctx: Context) extends Printer {
393393
protected def isOmittablePrefix(sym: Symbol): Boolean =
394394
defn.unqualifiedOwnerTypes.exists(_.symbol == sym) || isEmptyPrefix(sym)
395395

396+
protected def trimPrefixToScope(tp: NamedType) =
397+
toTextPrefix(tp.prefix)
398+
396399
protected def isEmptyPrefix(sym: Symbol): Boolean =
397400
sym.isEffectiveRoot || sym.isAnonymousClass || sym.name.isReplWrapperName
398401

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import dotty.tools.dotc.core.Names.*
1414
import dotty.tools.dotc.core.Symbols.*
1515
import dotty.tools.dotc.util.SourcePosition
1616
import dotty.tools.dotc.util.Spans
17-
import dotty.tools.pc.printer.ShortenedNames.ShortName
1817
import dotty.tools.pc.utils.MtagsEnrichments.*
1918

2019
import org.eclipse.lsp4j as l
@@ -153,19 +152,6 @@ object AutoImports:
153152
def forSymbol(symbol: Symbol): Option[List[l.TextEdit]] =
154153
editsForSymbol(symbol).map(_.edits)
155154

156-
/**
157-
* Construct auto imports for the given ShortName,
158-
* if the shortName has different name with it's symbol name,
159-
* generate renamed import. For example,
160-
* `ShortName("ju", <java.util>)` => `import java.{util => ju}`.
161-
*/
162-
def forShortName(shortName: ShortName): Option[List[l.TextEdit]] =
163-
if shortName.isRename then
164-
renderImports(
165-
List(ImportSel.Rename(shortName.symbol, shortName.name.show))
166-
).map(List(_))
167-
else forSymbol(shortName.symbol)
168-
169155
/**
170156
* @param symbol A missing symbol to auto-import
171157
*/

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import dotty.tools.dotc.interactive.Interactive
2121
import dotty.tools.dotc.interactive.InteractiveDriver
2222
import dotty.tools.dotc.util.SourceFile
2323
import dotty.tools.dotc.util.SourcePosition
24-
import dotty.tools.pc.printer.MetalsPrinter
25-
import dotty.tools.pc.printer.MetalsPrinter.IncludeDefaultParam
24+
import dotty.tools.pc.printer.ShortenedTypePrinter
25+
import dotty.tools.pc.printer.ShortenedTypePrinter.IncludeDefaultParam
2626
import dotty.tools.pc.utils.MtagsEnrichments.*
2727

2828
import org.eclipse.lsp4j.TextEdit
@@ -51,8 +51,7 @@ final class ExtractMethodProvider(
5151
val newctx = driver.currentCtx.fresh.setCompilationUnit(unit)
5252
Interactive.contextOfPath(path)(using newctx)
5353
val indexedCtx = IndexedContext(locatedCtx)
54-
val printer =
55-
MetalsPrinter.standard(indexedCtx, search, IncludeDefaultParam.Never)
54+
val printer = ShortenedTypePrinter(search, IncludeDefaultParam.Never)(using indexedCtx)
5655
def prettyPrint(tpe: Type) =
5756
def prettyPrintReturnType(tpe: Type): String =
5857
tpe match

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import dotty.tools.dotc.interactive.Interactive
2121
import dotty.tools.dotc.interactive.InteractiveDriver
2222
import dotty.tools.dotc.util.SourceFile
2323
import dotty.tools.dotc.util.SourcePosition
24-
import dotty.tools.pc.printer.MetalsPrinter
24+
import dotty.tools.pc.printer.ShortenedTypePrinter
25+
import dotty.tools.pc.printer.ShortenedTypePrinter.IncludeDefaultParam
2526
import dotty.tools.pc.utils.MtagsEnrichments.*
2627

2728
object HoverProvider:
@@ -88,11 +89,7 @@ object HoverProvider:
8889
ctx.fresh.setCompilationUnit(unit)
8990
Interactive.contextOfPath(enclosing)(using newctx)
9091
case None => ctx
91-
val printer = MetalsPrinter.standard(
92-
IndexedContext(printerContext),
93-
search,
94-
includeDefaultParam = MetalsPrinter.IncludeDefaultParam.Include
95-
)
92+
val printer = ShortenedTypePrinter(search, IncludeDefaultParam.Include)(using IndexedContext(printerContext))
9693
MetalsInteractive.enclosingSymbolsWithExpressionType(
9794
enclosing,
9895
pos,
@@ -157,7 +154,7 @@ object HoverProvider:
157154

158155
private def fallbackToDynamics(
159156
path: List[Tree],
160-
printer: MetalsPrinter
157+
printer: ShortenedTypePrinter
161158
)(using Context): ju.Optional[HoverSignature] = path match
162159
case SelectDynamicExtractor(sel, n, name) =>
163160
def findRefinement(tp: Type): ju.Optional[HoverSignature] =

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import dotty.tools.dotc.util.SourceFile
2222
import dotty.tools.dotc.util.SourcePosition
2323
import dotty.tools.dotc.util.Spans
2424
import dotty.tools.dotc.util.Spans.Span
25-
import dotty.tools.pc.printer.MetalsPrinter
26-
import dotty.tools.pc.printer.ShortenedNames
25+
import dotty.tools.pc.printer.ShortenedTypePrinter
26+
import dotty.tools.pc.printer.ShortenedTypePrinter.IncludeDefaultParam
2727
import dotty.tools.pc.utils.MtagsEnrichments.*
2828

2929
import org.eclipse.lsp4j.TextEdit
@@ -84,15 +84,11 @@ final class InferredTypeProvider(
8484
indexedCtx,
8585
config
8686
)
87-
val shortenedNames = new ShortenedNames(indexedCtx)
8887

8988
def removeType(nameEnd: Int, tptEnd: Int) =
9089
sourceText.substring(0, nameEnd) +
9190
sourceText.substring(tptEnd + 1, sourceText.length())
9291

93-
def imports: List[TextEdit] =
94-
shortenedNames.imports(autoImportsGen)
95-
9692
def optDealias(tpe: Type): Type =
9793
def isInScope(tpe: Type): Boolean =
9894
tpe match
@@ -107,13 +103,16 @@ final class InferredTypeProvider(
107103
then tpe
108104
else tpe.metalsDealias
109105

106+
val printer = ShortenedTypePrinter(
107+
symbolSearch,
108+
includeDefaultParam = IncludeDefaultParam.ResolveLater,
109+
isTextEdit = true
110+
)(using indexedCtx)
111+
112+
def imports: List[TextEdit] =
113+
printer.imports(autoImportsGen)
114+
110115
def printType(tpe: Type): String =
111-
val printer = MetalsPrinter.forInferredType(
112-
shortenedNames,
113-
indexedCtx,
114-
symbolSearch,
115-
includeDefaultParam = MetalsPrinter.IncludeDefaultParam.ResolveLater
116-
)
117116
printer.tpe(tpe)
118117

119118
path.headOption match

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import dotty.tools.dotc.interactive.InteractiveDriver
1919
import dotty.tools.dotc.util.SourceFile
2020
import dotty.tools.pc.AutoImports.AutoImportEdits
2121
import dotty.tools.pc.AutoImports.AutoImportsGenerator
22-
import dotty.tools.pc.printer.MetalsPrinter
22+
import dotty.tools.pc.printer.ShortenedTypePrinter
23+
import dotty.tools.pc.printer.ShortenedTypePrinter.IncludeDefaultParam
2324
import dotty.tools.pc.utils.MtagsEnrichments.*
2425

2526
import org.eclipse.lsp4j.Command
@@ -149,11 +150,7 @@ class CompletionProvider(
149150
path: List[Tree],
150151
indexedContext: IndexedContext
151152
)(using ctx: Context): CompletionItem =
152-
val printer = MetalsPrinter.standard(
153-
indexedContext,
154-
search,
155-
includeDefaultParam = MetalsPrinter.IncludeDefaultParam.ResolveLater
156-
)
153+
val printer = ShortenedTypePrinter(search, IncludeDefaultParam.ResolveLater)(using indexedContext)
157154
val editRange = completionPos.toEditRange
158155

159156
// For overloaded signatures we get multiple symbols, so we need

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import dotty.tools.dotc.core.Flags.*
88
import dotty.tools.dotc.core.Symbols.Symbol
99
import dotty.tools.dotc.core.Types.Type
1010
import dotty.tools.dotc.transform.SymUtils.*
11-
import dotty.tools.pc.printer.MetalsPrinter
11+
import dotty.tools.pc.printer.ShortenedTypePrinter
1212

1313
import org.eclipse.lsp4j.CompletionItemKind
1414
import org.eclipse.lsp4j.CompletionItemTag
@@ -24,7 +24,7 @@ sealed trait CompletionValue:
2424
def range: Option[Range] = None
2525
def filterText: Option[String] = None
2626
def completionItemKind(using Context): CompletionItemKind
27-
def description(printer: MetalsPrinter)(using Context): String = ""
27+
def description(printer: ShortenedTypePrinter)(using Context): String = ""
2828
def insertMode: Option[InsertTextMode] = None
2929
def completionData(buildTargetIdentifier: String)(using
3030
Context
@@ -34,7 +34,7 @@ sealed trait CompletionValue:
3434
/**
3535
* Label with potentially attached description.
3636
*/
37-
def labelWithDescription(printer: MetalsPrinter)(using Context): String =
37+
def labelWithDescription(printer: ShortenedTypePrinter)(using Context): String =
3838
label
3939
def lspTags(using Context): List[CompletionItemTag] = Nil
4040
end CompletionValue
@@ -73,7 +73,7 @@ object CompletionValue:
7373
if symbol.isDeprecated then List(CompletionItemTag.Deprecated) else Nil
7474

7575
override def labelWithDescription(
76-
printer: MetalsPrinter
76+
printer: ShortenedTypePrinter
7777
)(using Context): String =
7878
if symbol.is(Method) then s"${label}${description(printer)}"
7979
else if symbol.isConstructor then label
@@ -83,7 +83,7 @@ object CompletionValue:
8383
else s"${label}${description(printer)}"
8484
else s"${label}: ${description(printer)}"
8585

86-
override def description(printer: MetalsPrinter)(using Context): String =
86+
override def description(printer: ShortenedTypePrinter)(using Context): String =
8787
printer.completionSymbol(symbol)
8888
end Symbolic
8989

@@ -111,7 +111,7 @@ object CompletionValue:
111111
) extends Symbolic:
112112
override def completionItemKind(using Context): CompletionItemKind =
113113
CompletionItemKind.Method
114-
override def description(printer: MetalsPrinter)(using Context): String =
114+
override def description(printer: ShortenedTypePrinter)(using Context): String =
115115
s"${printer.completionSymbol(symbol)} (extension)"
116116

117117
/**
@@ -135,7 +135,7 @@ object CompletionValue:
135135
CompletionItemData.OverrideKind
136136
override def completionItemKind(using Context): CompletionItemKind =
137137
CompletionItemKind.Method
138-
override def labelWithDescription(printer: MetalsPrinter)(using
138+
override def labelWithDescription(printer: ShortenedTypePrinter)(using
139139
Context
140140
): String = label
141141
end Override
@@ -148,10 +148,10 @@ object CompletionValue:
148148
override def insertText: Option[String] = Some(label.replace("$", "$$"))
149149
override def completionItemKind(using Context): CompletionItemKind =
150150
CompletionItemKind.Field
151-
override def description(printer: MetalsPrinter)(using Context): String =
151+
override def description(printer: ShortenedTypePrinter)(using Context): String =
152152
": " + printer.tpe(tpe)
153153

154-
override def labelWithDescription(printer: MetalsPrinter)(using
154+
override def labelWithDescription(printer: ShortenedTypePrinter)(using
155155
Context
156156
): String = label
157157
end NamedArg
@@ -199,7 +199,7 @@ object CompletionValue:
199199
isWorkspace: Boolean = false,
200200
isExtension: Boolean = false
201201
) extends Symbolic:
202-
override def description(printer: MetalsPrinter)(using Context): String =
202+
override def description(printer: ShortenedTypePrinter)(using Context): String =
203203
if isExtension then s"${printer.completionSymbol(symbol)} (extension)"
204204
else super.description(printer)
205205
end Interpolator
@@ -212,7 +212,7 @@ object CompletionValue:
212212
) extends CompletionValue:
213213
override def completionItemKind(using Context): CompletionItemKind =
214214
CompletionItemKind.Enum
215-
override def description(printer: MetalsPrinter)(using Context): String =
215+
override def description(printer: ShortenedTypePrinter)(using Context): String =
216216
desc
217217

218218
case class CaseKeyword(
@@ -226,7 +226,7 @@ object CompletionValue:
226226
override def completionItemKind(using Context): CompletionItemKind =
227227
CompletionItemKind.Method
228228

229-
override def labelWithDescription(printer: MetalsPrinter)(using
229+
override def labelWithDescription(printer: ShortenedTypePrinter)(using
230230
Context
231231
): String = label
232232
end CaseKeyword
@@ -239,7 +239,7 @@ object CompletionValue:
239239
override def completionItemKind(using Context): CompletionItemKind =
240240
CompletionItemKind.Snippet
241241

242-
override def description(printer: MetalsPrinter)(using Context): String =
242+
override def description(printer: ShortenedTypePrinter)(using Context): String =
243243
description
244244
override def insertMode: Option[InsertTextMode] = Some(InsertTextMode.AsIs)
245245

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import dotty.tools.dotc.util.SourceFile
2727
import dotty.tools.dotc.util.SourcePosition
2828
import dotty.tools.pc.AutoImports.AutoImport
2929
import dotty.tools.pc.AutoImports.AutoImportsGenerator
30-
import dotty.tools.pc.printer.MetalsPrinter
30+
import dotty.tools.pc.printer.ShortenedTypePrinter
31+
import dotty.tools.pc.printer.ShortenedTypePrinter.IncludeDefaultParam
3132
import dotty.tools.pc.utils.MtagsEnrichments.*
3233

3334
import org.eclipse.lsp4j as l
@@ -394,12 +395,11 @@ object OverrideCompletions:
394395
shouldAddOverrideKwd: Boolean
395396
)(using Context, ReportContext): CompletionValue.Override =
396397
val renames = AutoImport.renameConfigMap(config)
397-
val printer = MetalsPrinter.standard(
398-
indexedContext,
398+
val printer = ShortenedTypePrinter(
399399
search,
400-
includeDefaultParam = MetalsPrinter.IncludeDefaultParam.Never,
401-
renames
402-
)
400+
includeDefaultParam = IncludeDefaultParam.Never,
401+
renameConfigMap = renames
402+
)(using indexedContext)
403403
val overrideKeyword: String =
404404
// if the overriding method is not an abstract member, add `override` keyword
405405
if !sym.isOneOf(Deferred) || shouldAddOverrideKwd
@@ -443,16 +443,12 @@ object OverrideCompletions:
443443
if config.isCompletionSnippetsEnabled && shouldMoveCursor then "${0:???}"
444444
else "???"
445445
val value = s"$signature = $stub"
446-
val additionalEdits =
447-
printer.shortenedNames
448-
.sortBy(nme => nme.name)
449-
.flatMap(name => autoImportsGen.forShortName(name))
450-
.flatten
446+
451447
CompletionValue.Override(
452448
label,
453449
value,
454450
sym.symbol,
455-
additionalEdits,
451+
printer.imports(autoImportsGen),
456452
Some(signature),
457453
Some(autoImportsGen.pos.withStart(start).toLsp)
458454
)

0 commit comments

Comments
 (0)