Skip to content

Commit

Permalink
improvement: Better labels for workspace methods completions
Browse files Browse the repository at this point in the history
[Cherry-picked 0f56dba]
  • Loading branch information
kasiaMarek authored and WojciechMazur committed Jun 30, 2024
1 parent 4dc0e93 commit 0ab789c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import scala.meta.internal.pc.CompletionItemData
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.core.Denotations.Denotation
import dotty.tools.dotc.core.Flags.*
import dotty.tools.dotc.core.StdNames.nme
import dotty.tools.dotc.core.Symbols.Symbol
import dotty.tools.dotc.core.Types.Type
import dotty.tools.pc.printer.ShortenedTypePrinter
Expand Down Expand Up @@ -108,7 +109,7 @@ object CompletionValue:
s"${label}${description(printer)}"
else s"$label: ${description(printer)}"

private def labelWithSuffix(printer: ShortenedTypePrinter)(using Context): String =
protected def labelWithSuffix(printer: ShortenedTypePrinter)(using Context): String =
if snippetSuffix.addLabelSnippet
then
val printedParams = symbol.info.typeParams.map(p =>
Expand Down Expand Up @@ -145,6 +146,11 @@ object CompletionValue:
override def isFromWorkspace: Boolean = true
override def completionItemDataKind: Integer = CompletionSource.WorkspaceKind.ordinal

override def labelWithDescription(printer: ShortenedTypePrinter)(using Context): String =
if symbol.is(Method) && symbol.name != nme.apply then
s"${labelWithSuffix(printer)} - ${printer.fullNameString(symbol.effectiveOwner)}"
else super.labelWithDescription(printer)

/**
* CompletionValue for old implicit classes methods via SymbolSearch
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -768,8 +768,29 @@ class CompletionWorkspaceSuite extends BaseCompletionSuite:
| def main: Unit = incre@@
|""".stripMargin,
"""|increment3: Int
|increment: Int
|increment2: Int
|increment - a: Int
|increment2 - a.c: Int
|""".stripMargin
)

@Test def `indent-method` =
check(
"""|package a:
| val y = 123
| given intGiven: Int = 123
| type Alpha = String
| class Foo(x: Int)
| object X:
| val x = 123
| def fooBar(x: Int) = x + 1
| package b:
| def fooBar(x: String) = x.length
|
|package c:
| def main() = foo@@
|""".stripMargin,
"""|fooBar - a(x: Int): Int
|fooBar - a.b(x: String): Int
|""".stripMargin
)

Expand Down Expand Up @@ -848,5 +869,21 @@ class CompletionWorkspaceSuite extends BaseCompletionSuite:
| }
|}
|""".stripMargin,
filter = _.contains("mmmm(x: Int)")
filter = _.contains("mmmm - demo.O")
)

@Test def `method-label` =
check(
"""|package demo
|
|object O {
| def method(i: Int): Int = i + 1
|}
|
|object Main {
| val x = meth@@
|}
|""".stripMargin,
"""|method - demo.O(i: Int): Int
|""".stripMargin
)

0 comments on commit 0ab789c

Please sign in to comment.