Skip to content

Commit 281576d

Browse files
committed
remove duplicated code
1 parent aa04b2f commit 281576d

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ class Completions(
6363
case Literal(Constant(_: String)) :: _ => Mode.Term // literal completions
6464
case _ => mode
6565

66-
6766
private lazy val shouldAddSnippet =
6867
path match
6968
/* In case of `method@@()` we should not add snippets and the path
@@ -114,7 +113,6 @@ class Completions(
114113
end if
115114
end includeSymbol
116115

117-
118116
def completions(): (List[CompletionValue], SymbolSearch.Result) =
119117
val (advanced, exclusive) = advancedCompletions(path, pos, completionPos)
120118
val (all, result) =
@@ -242,9 +240,8 @@ class Completions(
242240
def companionSynthetic = sym.companion.exists && sym.companion.is(Synthetic)
243241
// find the apply completion that would need a snippet
244242
val methodSymbols =
245-
if shouldAddSnippet &&
246-
(sym.is(Flags.Module) || sym.isClass && !sym.is(Flags.Trait)) &&
247-
!sym.is(Flags.JavaDefined) && completionMode.is(Mode.Term)
243+
if shouldAddSnippet && completionMode.is(Mode.Term) &&
244+
(sym.is(Flags.Module) || sym.isClass && !sym.is(Flags.Trait)) && !sym.is(Flags.JavaDefined)
248245
then
249246
val info =
250247
/* Companion will be added even for normal classes now,

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,25 @@ class ShortenedTypePrinter(
234234
end match
235235
end hoverSymbol
236236

237+
def isImportedByDefault(sym: Symbol): Boolean =
238+
import dotty.tools.dotc.core.Symbols.defn
239+
lazy val effectiveOwner = sym.effectiveOwner
240+
sym.isType && (effectiveOwner == defn.ScalaPackageClass || effectiveOwner == defn.ScalaPredefModuleClass)
241+
237242
def completionSymbol(sym: Symbol): String =
238243
val info = sym.info.widenTermRefExpr
239244
val typeSymbol = info.typeSymbol
240245

241-
if sym.is(Flags.Package) || sym.isClass then " " + fullNameString(sym.effectiveOwner)
242-
else if sym.is(Flags.Module) || typeSymbol.is(Flags.Module) then
246+
lazy val typeEffectiveOwner =
243247
if typeSymbol != NoSymbol then " " + fullNameString(typeSymbol.effectiveOwner)
244248
else " " + fullNameString(sym.effectiveOwner)
249+
250+
if isImportedByDefault(sym) then typeEffectiveOwner
251+
else if sym.is(Flags.Package) || sym.isClass then " " + fullNameString(sym.effectiveOwner)
252+
else if sym.is(Flags.Module) || typeSymbol.is(Flags.Module) then typeEffectiveOwner
245253
else if sym.is(Flags.Method) then
246254
defaultMethodSignature(sym, info, onlyMethodParams = true)
247-
else if sym.isType
248-
then
255+
else if sym.isType then
249256
info match
250257
case TypeAlias(t) => " = " + tpe(t.resultType)
251258
case t => tpe(t.resultType)

presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,8 @@ class CompletionSuite extends BaseCompletionSuite:
673673
|}
674674
|""".stripMargin,
675675
"""|Some[?] scala
676-
|SafeVarargs java.lang
677676
|ScalaReflectionException scala
677+
|Seq[A] scala.collection.immutable
678678
|""".stripMargin,
679679
topLines = Some(3)
680680
)
@@ -709,8 +709,8 @@ class CompletionSuite extends BaseCompletionSuite:
709709
|}
710710
|""".stripMargin,
711711
"""|Number: Regex
712-
|NegativeArraySizeException java.lang
713-
|NoClassDefFoundError java.lang
712+
|NoSuchElementException java.util
713+
|NoSuchFieldError java.lang
714714
|""".stripMargin,
715715
topLines = Option(3)
716716
)
@@ -724,12 +724,24 @@ class CompletionSuite extends BaseCompletionSuite:
724724
|}
725725
|""".stripMargin,
726726
"""|Number: Regex
727-
|NegativeArraySizeException java.lang
728-
|NoClassDefFoundError java.lang
727+
|NoSuchElementException java.util
728+
|NoSuchFieldError java.lang
729729
|""".stripMargin,
730730
topLines = Option(3)
731731
)
732732

733+
@Test def `no-methods-on-case-type` =
734+
check(
735+
s"""|object Main {
736+
| val Number = "".r
737+
| "" match {
738+
| case _: NotImpl@@
739+
|}
740+
|""".stripMargin,
741+
"""|NotImplementedError scala
742+
|""".stripMargin,
743+
)
744+
733745
@Test def underscore =
734746
check(
735747
s"""|object Main {
@@ -1344,7 +1356,7 @@ class CompletionSuite extends BaseCompletionSuite:
13441356
"""|object T:
13451357
| extension (x: ListBuffe@@)
13461358
|""".stripMargin,
1347-
"""|ListBuffer[T] - scala.collection.mutable
1359+
"""|ListBuffer[A] - scala.collection.mutable
13481360
|ListBuffer - scala.collection.mutable
13491361
|""".stripMargin,
13501362
)
@@ -1364,7 +1376,7 @@ class CompletionSuite extends BaseCompletionSuite:
13641376
"""|object T:
13651377
| extension [A <: ListBuffe@@]
13661378
|""".stripMargin,
1367-
"""|ListBuffer[T] - scala.collection.mutable
1379+
"""|ListBuffer[A] - scala.collection.mutable
13681380
|ListBuffer - scala.collection.mutable
13691381
|""".stripMargin
13701382
)

0 commit comments

Comments
 (0)