Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Stable Presentation Compiler #17528

Merged
merged 25 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
63d3a35
compiler util: collect comments during Scanner phase and store it in …
rochala May 12, 2023
fde037a
metals initial version: 41e96ee33f82 copied into dotty
rochala May 12, 2023
9f271fe
changes: make mtags compile in dotty, changes metals code and adds ne…
rochala May 12, 2023
38f58dc
additions: copy tests from metals version: 41e96ee33f82 , create diff…
rochala May 12, 2023
1a02dbf
refactor: change package from scala.meta to dotty.tools.pc
rochala May 12, 2023
fffe497
refactor: organize imports, unify formatting with mtags in metals
rochala May 12, 2023
3e857a2
update Build.scala and build.sbt to support nonbootstrapped compiler,…
rochala May 16, 2023
e643f9f
implement FIXME from previous commit
rochala May 16, 2023
3bf65da
add nightly mtags-shared as dependency instead of local snapshot
rochala May 17, 2023
34814ce
remove unnecessary changes in Comments.scala
rochala May 17, 2023
f2d8c64
remove metals wrappers around compiler implementation created to work…
rochala May 22, 2023
32c50e0
update NOTICE.md
rochala May 22, 2023
d823951
use java8 compatible api
rochala May 23, 2023
7074f0d
Mock Symbol Search documentation and definition
rochala May 26, 2023
122f36f
Revert incorrectly removed line
rochala May 26, 2023
421380c
fix build.sbt and windows tests
rochala May 26, 2023
f59457c
Filter tests to contain only completions that should be available on …
rochala May 27, 2023
6ad9dc7
Bump presentatation compiler to c8ef4e0
rochala Jun 15, 2023
5ee339d
use compiler printer instead of ShortenedNames with ShortType
rochala Jul 3, 2023
aa7542f
undo unnecessary changes, add -Wunsued:all, remove unused
rochala Jul 3, 2023
2d6aff2
Remove -Wunused from build.sbt, undo scalafmt
rochala Jul 3, 2023
9856331
apply review comments, inline toTextPrefix
rochala Jul 5, 2023
0869f93
update metals to latest commit - 7d0397b
rochala Jul 5, 2023
ae8ee2f
remove check for JavaStatic, as they are no longer necessary
rochala Jul 6, 2023
2b34077
remove flaky mock
rochala Jul 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) =
rochala marked this conversation as resolved.
Show resolved Hide resolved
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