Skip to content

Commit

Permalink
use compiler printer instead of ShortenedNames with ShortType
Browse files Browse the repository at this point in the history
  • Loading branch information
rochala committed Jul 3, 2023
1 parent 6ad9dc7 commit 5ee339d
Show file tree
Hide file tree
Showing 17 changed files with 222 additions and 513 deletions.
7 changes: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
if (printWithoutPrefix.contains(tp.symbol))
toText(tp.name)
else
toTextPrefix(tp.prefix) ~ selectionString(tp)
trimPrefixToScope(tp) ~ selectionString(tp)
case tp: TermParamRef =>
ParamRefNameString(tp) ~ lambdaHash(tp.binder) ~ ".type"
case tp: TypeParamRef =>
Expand Down Expand Up @@ -353,7 +353,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
def toTextRef(tp: SingletonType): Text = controlled {
tp match {
case tp: TermRef =>
toTextPrefix(tp.prefix) ~ selectionString(tp)
trimPrefixToScope(tp) ~ selectionString(tp)
case tp: ThisType =>
nameString(tp.cls) + ".this"
case SuperType(thistpe: SingletonType, _) =>
Expand Down Expand Up @@ -393,6 +393,9 @@ class PlainPrinter(_ctx: Context) extends Printer {
protected def isOmittablePrefix(sym: Symbol): Boolean =
defn.unqualifiedOwnerTypes.exists(_.symbol == sym) || isEmptyPrefix(sym)

protected def trimPrefixToScope(tp: NamedType) =
toTextPrefix(tp.prefix)

protected def isEmptyPrefix(sym: Symbol): Boolean =
sym.isEffectiveRoot || sym.isAnonymousClass || sym.name.isReplWrapperName

Expand Down
14 changes: 0 additions & 14 deletions presentation-compiler/src/main/dotty/tools/pc/AutoImports.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import dotty.tools.dotc.core.Names.*
import dotty.tools.dotc.core.Symbols.*
import dotty.tools.dotc.util.SourcePosition
import dotty.tools.dotc.util.Spans
import dotty.tools.pc.printer.ShortenedNames.ShortName
import dotty.tools.pc.utils.MtagsEnrichments.*

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

/**
* Construct auto imports for the given ShortName,
* if the shortName has different name with it's symbol name,
* generate renamed import. For example,
* `ShortName("ju", <java.util>)` => `import java.{util => ju}`.
*/
def forShortName(shortName: ShortName): Option[List[l.TextEdit]] =
if shortName.isRename then
renderImports(
List(ImportSel.Rename(shortName.symbol, shortName.name.show))
).map(List(_))
else forSymbol(shortName.symbol)

/**
* @param symbol A missing symbol to auto-import
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import dotty.tools.dotc.interactive.Interactive
import dotty.tools.dotc.interactive.InteractiveDriver
import dotty.tools.dotc.util.SourceFile
import dotty.tools.dotc.util.SourcePosition
import dotty.tools.pc.printer.MetalsPrinter
import dotty.tools.pc.printer.MetalsPrinter.IncludeDefaultParam
import dotty.tools.pc.printer.ShortenedTypePrinter
import dotty.tools.pc.printer.ShortenedTypePrinter.IncludeDefaultParam
import dotty.tools.pc.utils.MtagsEnrichments.*

import org.eclipse.lsp4j.TextEdit
Expand Down Expand Up @@ -51,8 +51,7 @@ final class ExtractMethodProvider(
val newctx = driver.currentCtx.fresh.setCompilationUnit(unit)
Interactive.contextOfPath(path)(using newctx)
val indexedCtx = IndexedContext(locatedCtx)
val printer =
MetalsPrinter.standard(indexedCtx, search, IncludeDefaultParam.Never)
val printer = ShortenedTypePrinter(search, IncludeDefaultParam.Never)(using indexedCtx)
def prettyPrint(tpe: Type) =
def prettyPrintReturnType(tpe: Type): String =
tpe match
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import dotty.tools.dotc.interactive.Interactive
import dotty.tools.dotc.interactive.InteractiveDriver
import dotty.tools.dotc.util.SourceFile
import dotty.tools.dotc.util.SourcePosition
import dotty.tools.pc.printer.MetalsPrinter
import dotty.tools.pc.printer.ShortenedTypePrinter
import dotty.tools.pc.printer.ShortenedTypePrinter.IncludeDefaultParam
import dotty.tools.pc.utils.MtagsEnrichments.*

object HoverProvider:
Expand Down Expand Up @@ -88,11 +89,7 @@ object HoverProvider:
ctx.fresh.setCompilationUnit(unit)
Interactive.contextOfPath(enclosing)(using newctx)
case None => ctx
val printer = MetalsPrinter.standard(
IndexedContext(printerContext),
search,
includeDefaultParam = MetalsPrinter.IncludeDefaultParam.Include
)
val printer = ShortenedTypePrinter(search, IncludeDefaultParam.Include)(using IndexedContext(printerContext))
MetalsInteractive.enclosingSymbolsWithExpressionType(
enclosing,
pos,
Expand Down Expand Up @@ -157,7 +154,7 @@ object HoverProvider:

private def fallbackToDynamics(
path: List[Tree],
printer: MetalsPrinter
printer: ShortenedTypePrinter
)(using Context): ju.Optional[HoverSignature] = path match
case SelectDynamicExtractor(sel, n, name) =>
def findRefinement(tp: Type): ju.Optional[HoverSignature] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import dotty.tools.dotc.util.SourceFile
import dotty.tools.dotc.util.SourcePosition
import dotty.tools.dotc.util.Spans
import dotty.tools.dotc.util.Spans.Span
import dotty.tools.pc.printer.MetalsPrinter
import dotty.tools.pc.printer.ShortenedNames
import dotty.tools.pc.printer.ShortenedTypePrinter
import dotty.tools.pc.printer.ShortenedTypePrinter.IncludeDefaultParam
import dotty.tools.pc.utils.MtagsEnrichments.*

import org.eclipse.lsp4j.TextEdit
Expand Down Expand Up @@ -84,15 +84,11 @@ final class InferredTypeProvider(
indexedCtx,
config
)
val shortenedNames = new ShortenedNames(indexedCtx)

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

def imports: List[TextEdit] =
shortenedNames.imports(autoImportsGen)

def optDealias(tpe: Type): Type =
def isInScope(tpe: Type): Boolean =
tpe match
Expand All @@ -107,13 +103,16 @@ final class InferredTypeProvider(
then tpe
else tpe.metalsDealias

val printer = ShortenedTypePrinter(
symbolSearch,
includeDefaultParam = IncludeDefaultParam.ResolveLater,
isTextEdit = true
)(using indexedCtx)

def imports: List[TextEdit] =
printer.imports(autoImportsGen)

def printType(tpe: Type): String =
val printer = MetalsPrinter.forInferredType(
shortenedNames,
indexedCtx,
symbolSearch,
includeDefaultParam = MetalsPrinter.IncludeDefaultParam.ResolveLater
)
printer.tpe(tpe)

path.headOption match
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import dotty.tools.dotc.interactive.InteractiveDriver
import dotty.tools.dotc.util.SourceFile
import dotty.tools.pc.AutoImports.AutoImportEdits
import dotty.tools.pc.AutoImports.AutoImportsGenerator
import dotty.tools.pc.printer.MetalsPrinter
import dotty.tools.pc.printer.ShortenedTypePrinter
import dotty.tools.pc.printer.ShortenedTypePrinter.IncludeDefaultParam
import dotty.tools.pc.utils.MtagsEnrichments.*

import org.eclipse.lsp4j.Command
Expand Down Expand Up @@ -149,11 +150,7 @@ class CompletionProvider(
path: List[Tree],
indexedContext: IndexedContext
)(using ctx: Context): CompletionItem =
val printer = MetalsPrinter.standard(
indexedContext,
search,
includeDefaultParam = MetalsPrinter.IncludeDefaultParam.ResolveLater
)
val printer = ShortenedTypePrinter(search, IncludeDefaultParam.ResolveLater)(using indexedContext)
val editRange = completionPos.toEditRange

// For overloaded signatures we get multiple symbols, so we need
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import dotty.tools.dotc.core.Flags.*
import dotty.tools.dotc.core.Symbols.Symbol
import dotty.tools.dotc.core.Types.Type
import dotty.tools.dotc.transform.SymUtils.*
import dotty.tools.pc.printer.MetalsPrinter
import dotty.tools.pc.printer.ShortenedTypePrinter

import org.eclipse.lsp4j.CompletionItemKind
import org.eclipse.lsp4j.CompletionItemTag
Expand All @@ -24,7 +24,7 @@ sealed trait CompletionValue:
def range: Option[Range] = None
def filterText: Option[String] = None
def completionItemKind(using Context): CompletionItemKind
def description(printer: MetalsPrinter)(using Context): String = ""
def description(printer: ShortenedTypePrinter)(using Context): String = ""
def insertMode: Option[InsertTextMode] = None
def completionData(buildTargetIdentifier: String)(using
Context
Expand All @@ -34,7 +34,7 @@ sealed trait CompletionValue:
/**
* Label with potentially attached description.
*/
def labelWithDescription(printer: MetalsPrinter)(using Context): String =
def labelWithDescription(printer: ShortenedTypePrinter)(using Context): String =
label
def lspTags(using Context): List[CompletionItemTag] = Nil
end CompletionValue
Expand Down Expand Up @@ -73,7 +73,7 @@ object CompletionValue:
if symbol.isDeprecated then List(CompletionItemTag.Deprecated) else Nil

override def labelWithDescription(
printer: MetalsPrinter
printer: ShortenedTypePrinter
)(using Context): String =
if symbol.is(Method) then s"${label}${description(printer)}"
else if symbol.isConstructor then label
Expand All @@ -83,7 +83,7 @@ object CompletionValue:
else s"${label}${description(printer)}"
else s"${label}: ${description(printer)}"

override def description(printer: MetalsPrinter)(using Context): String =
override def description(printer: ShortenedTypePrinter)(using Context): String =
printer.completionSymbol(symbol)
end Symbolic

Expand Down Expand Up @@ -111,7 +111,7 @@ object CompletionValue:
) extends Symbolic:
override def completionItemKind(using Context): CompletionItemKind =
CompletionItemKind.Method
override def description(printer: MetalsPrinter)(using Context): String =
override def description(printer: ShortenedTypePrinter)(using Context): String =
s"${printer.completionSymbol(symbol)} (extension)"

/**
Expand All @@ -135,7 +135,7 @@ object CompletionValue:
CompletionItemData.OverrideKind
override def completionItemKind(using Context): CompletionItemKind =
CompletionItemKind.Method
override def labelWithDescription(printer: MetalsPrinter)(using
override def labelWithDescription(printer: ShortenedTypePrinter)(using
Context
): String = label
end Override
Expand All @@ -148,10 +148,10 @@ object CompletionValue:
override def insertText: Option[String] = Some(label.replace("$", "$$"))
override def completionItemKind(using Context): CompletionItemKind =
CompletionItemKind.Field
override def description(printer: MetalsPrinter)(using Context): String =
override def description(printer: ShortenedTypePrinter)(using Context): String =
": " + printer.tpe(tpe)

override def labelWithDescription(printer: MetalsPrinter)(using
override def labelWithDescription(printer: ShortenedTypePrinter)(using
Context
): String = label
end NamedArg
Expand Down Expand Up @@ -199,7 +199,7 @@ object CompletionValue:
isWorkspace: Boolean = false,
isExtension: Boolean = false
) extends Symbolic:
override def description(printer: MetalsPrinter)(using Context): String =
override def description(printer: ShortenedTypePrinter)(using Context): String =
if isExtension then s"${printer.completionSymbol(symbol)} (extension)"
else super.description(printer)
end Interpolator
Expand All @@ -212,7 +212,7 @@ object CompletionValue:
) extends CompletionValue:
override def completionItemKind(using Context): CompletionItemKind =
CompletionItemKind.Enum
override def description(printer: MetalsPrinter)(using Context): String =
override def description(printer: ShortenedTypePrinter)(using Context): String =
desc

case class CaseKeyword(
Expand All @@ -226,7 +226,7 @@ object CompletionValue:
override def completionItemKind(using Context): CompletionItemKind =
CompletionItemKind.Method

override def labelWithDescription(printer: MetalsPrinter)(using
override def labelWithDescription(printer: ShortenedTypePrinter)(using
Context
): String = label
end CaseKeyword
Expand All @@ -239,7 +239,7 @@ object CompletionValue:
override def completionItemKind(using Context): CompletionItemKind =
CompletionItemKind.Snippet

override def description(printer: MetalsPrinter)(using Context): String =
override def description(printer: ShortenedTypePrinter)(using Context): String =
description
override def insertMode: Option[InsertTextMode] = Some(InsertTextMode.AsIs)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import dotty.tools.dotc.util.SourceFile
import dotty.tools.dotc.util.SourcePosition
import dotty.tools.pc.AutoImports.AutoImport
import dotty.tools.pc.AutoImports.AutoImportsGenerator
import dotty.tools.pc.printer.MetalsPrinter
import dotty.tools.pc.printer.ShortenedTypePrinter
import dotty.tools.pc.printer.ShortenedTypePrinter.IncludeDefaultParam
import dotty.tools.pc.utils.MtagsEnrichments.*

import org.eclipse.lsp4j as l
Expand Down Expand Up @@ -394,12 +395,11 @@ object OverrideCompletions:
shouldAddOverrideKwd: Boolean
)(using Context, ReportContext): CompletionValue.Override =
val renames = AutoImport.renameConfigMap(config)
val printer = MetalsPrinter.standard(
indexedContext,
val printer = ShortenedTypePrinter(
search,
includeDefaultParam = MetalsPrinter.IncludeDefaultParam.Never,
renames
)
includeDefaultParam = IncludeDefaultParam.Never,
renameConfigMap = renames
)(using indexedContext)
val overrideKeyword: String =
// if the overriding method is not an abstract member, add `override` keyword
if !sym.isOneOf(Deferred) || shouldAddOverrideKwd
Expand Down Expand Up @@ -443,16 +443,12 @@ object OverrideCompletions:
if config.isCompletionSnippetsEnabled && shouldMoveCursor then "${0:???}"
else "???"
val value = s"$signature = $stub"
val additionalEdits =
printer.shortenedNames
.sortBy(nme => nme.name)
.flatMap(name => autoImportsGen.forShortName(name))
.flatten

CompletionValue.Override(
label,
value,
sym.symbol,
additionalEdits,
printer.imports(autoImportsGen),
Some(signature),
Some(autoImportsGen.pos.withStart(start).toLsp)
)
Expand Down
Loading

0 comments on commit 5ee339d

Please sign in to comment.