From 281576dca7cbdf5cf41b0d96673e986c4c1514b7 Mon Sep 17 00:00:00 2001 From: rochala Date: Thu, 24 Aug 2023 18:08:53 +0200 Subject: [PATCH] remove duplicated code --- .../tools/pc/completions/Completions.scala | 7 ++--- .../pc/printer/ShortenedTypePrinter.scala | 15 ++++++++--- .../pc/tests/completion/CompletionSuite.scala | 26 ++++++++++++++----- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala b/presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala index 1212b98a2289..a55f19e2ab68 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala @@ -63,7 +63,6 @@ class Completions( case Literal(Constant(_: String)) :: _ => Mode.Term // literal completions case _ => mode - private lazy val shouldAddSnippet = path match /* In case of `method@@()` we should not add snippets and the path @@ -114,7 +113,6 @@ class Completions( end if end includeSymbol - def completions(): (List[CompletionValue], SymbolSearch.Result) = val (advanced, exclusive) = advancedCompletions(path, pos, completionPos) val (all, result) = @@ -242,9 +240,8 @@ class Completions( def companionSynthetic = sym.companion.exists && sym.companion.is(Synthetic) // find the apply completion that would need a snippet val methodSymbols = - if shouldAddSnippet && - (sym.is(Flags.Module) || sym.isClass && !sym.is(Flags.Trait)) && - !sym.is(Flags.JavaDefined) && completionMode.is(Mode.Term) + if shouldAddSnippet && completionMode.is(Mode.Term) && + (sym.is(Flags.Module) || sym.isClass && !sym.is(Flags.Trait)) && !sym.is(Flags.JavaDefined) then val info = /* Companion will be added even for normal classes now, diff --git a/presentation-compiler/src/main/dotty/tools/pc/printer/ShortenedTypePrinter.scala b/presentation-compiler/src/main/dotty/tools/pc/printer/ShortenedTypePrinter.scala index 088ecd6c3a0c..5652fd0d9bcc 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/printer/ShortenedTypePrinter.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/printer/ShortenedTypePrinter.scala @@ -234,18 +234,25 @@ class ShortenedTypePrinter( end match end hoverSymbol + def isImportedByDefault(sym: Symbol): Boolean = + import dotty.tools.dotc.core.Symbols.defn + lazy val effectiveOwner = sym.effectiveOwner + sym.isType && (effectiveOwner == defn.ScalaPackageClass || effectiveOwner == defn.ScalaPredefModuleClass) + def completionSymbol(sym: Symbol): String = val info = sym.info.widenTermRefExpr val typeSymbol = info.typeSymbol - if sym.is(Flags.Package) || sym.isClass then " " + fullNameString(sym.effectiveOwner) - else if sym.is(Flags.Module) || typeSymbol.is(Flags.Module) then + lazy val typeEffectiveOwner = if typeSymbol != NoSymbol then " " + fullNameString(typeSymbol.effectiveOwner) else " " + fullNameString(sym.effectiveOwner) + + if isImportedByDefault(sym) then typeEffectiveOwner + else if sym.is(Flags.Package) || sym.isClass then " " + fullNameString(sym.effectiveOwner) + else if sym.is(Flags.Module) || typeSymbol.is(Flags.Module) then typeEffectiveOwner else if sym.is(Flags.Method) then defaultMethodSignature(sym, info, onlyMethodParams = true) - else if sym.isType - then + else if sym.isType then info match case TypeAlias(t) => " = " + tpe(t.resultType) case t => tpe(t.resultType) diff --git a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala index 543aead14817..b55fabd55abf 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala @@ -673,8 +673,8 @@ class CompletionSuite extends BaseCompletionSuite: |} |""".stripMargin, """|Some[?] scala - |SafeVarargs java.lang |ScalaReflectionException scala + |Seq[A] scala.collection.immutable |""".stripMargin, topLines = Some(3) ) @@ -709,8 +709,8 @@ class CompletionSuite extends BaseCompletionSuite: |} |""".stripMargin, """|Number: Regex - |NegativeArraySizeException java.lang - |NoClassDefFoundError java.lang + |NoSuchElementException java.util + |NoSuchFieldError java.lang |""".stripMargin, topLines = Option(3) ) @@ -724,12 +724,24 @@ class CompletionSuite extends BaseCompletionSuite: |} |""".stripMargin, """|Number: Regex - |NegativeArraySizeException java.lang - |NoClassDefFoundError java.lang + |NoSuchElementException java.util + |NoSuchFieldError java.lang |""".stripMargin, topLines = Option(3) ) + @Test def `no-methods-on-case-type` = + check( + s"""|object Main { + | val Number = "".r + | "" match { + | case _: NotImpl@@ + |} + |""".stripMargin, + """|NotImplementedError scala + |""".stripMargin, + ) + @Test def underscore = check( s"""|object Main { @@ -1344,7 +1356,7 @@ class CompletionSuite extends BaseCompletionSuite: """|object T: | extension (x: ListBuffe@@) |""".stripMargin, - """|ListBuffer[T] - scala.collection.mutable + """|ListBuffer[A] - scala.collection.mutable |ListBuffer - scala.collection.mutable |""".stripMargin, ) @@ -1364,7 +1376,7 @@ class CompletionSuite extends BaseCompletionSuite: """|object T: | extension [A <: ListBuffe@@] |""".stripMargin, - """|ListBuffer[T] - scala.collection.mutable + """|ListBuffer[A] - scala.collection.mutable |ListBuffer - scala.collection.mutable |""".stripMargin )