Skip to content

Commit 5c628d9

Browse files
authored
Backport pc changes from metals (#19617)
metals backport: scalameta/metals#6075 scalameta/metals#6073
2 parents 30cd883 + 0f56dba commit 5c628d9

File tree

4 files changed

+81
-9
lines changed

4 files changed

+81
-9
lines changed

Diff for: presentation-compiler/src/main/dotty/tools/pc/completions/CompletionProvider.scala

+12-4
Original file line numberDiff line numberDiff line change
@@ -243,21 +243,29 @@ class CompletionProvider(
243243
r match
244244
case IndexedContext.Result.InScope =>
245245
mkItem(
246-
ident.backticked(backtickSoftKeyword) + completionTextSuffix
246+
v.insertText.getOrElse(
247+
ident.backticked(
248+
backtickSoftKeyword
249+
) + completionTextSuffix
250+
),
251+
range = v.range,
247252
)
248253
case _ if isInStringInterpolation =>
249254
mkItem(
250-
"{" + sym.fullNameBackticked + completionTextSuffix + "}"
255+
"{" + sym.fullNameBackticked + completionTextSuffix + "}",
256+
range = v.range
251257
)
252258
case _ if v.isExtensionMethod =>
253259
mkItem(
254-
ident.backticked(backtickSoftKeyword) + completionTextSuffix
260+
ident.backticked(backtickSoftKeyword) + completionTextSuffix,
261+
range = v.range
255262
)
256263
case _ =>
257264
mkItem(
258265
sym.fullNameBackticked(
259266
backtickSoftKeyword
260-
) + completionTextSuffix
267+
) + completionTextSuffix,
268+
range = v.range
261269
)
262270
end match
263271
end match

Diff for: presentation-compiler/src/main/dotty/tools/pc/completions/CompletionValue.scala

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import scala.meta.internal.pc.CompletionItemData
66
import dotty.tools.dotc.core.Contexts.Context
77
import dotty.tools.dotc.core.Denotations.Denotation
88
import dotty.tools.dotc.core.Flags.*
9+
import dotty.tools.dotc.core.StdNames.nme
910
import dotty.tools.dotc.core.Symbols.Symbol
1011
import dotty.tools.dotc.core.Types.Type
1112
import dotty.tools.pc.printer.ShortenedTypePrinter
@@ -108,7 +109,7 @@ object CompletionValue:
108109
s"${label}${description(printer)}"
109110
else s"$label: ${description(printer)}"
110111

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

149+
override def labelWithDescription(printer: ShortenedTypePrinter)(using Context): String =
150+
if symbol.is(Method) && symbol.name != nme.apply then
151+
s"${labelWithSuffix(printer)} - ${printer.fullNameString(symbol.effectiveOwner)}"
152+
else super.labelWithDescription(printer)
153+
148154
/**
149155
* CompletionValue for old implicit classes methods via SymbolSearch
150156
*/
@@ -268,6 +274,7 @@ object CompletionValue:
268274
)(using Context): String =
269275
if isExtension then s"${printer.completionSymbol(symbol)} (extension)"
270276
else super.description(printer)
277+
override def isExtensionMethod: Boolean = isExtension
271278
end Interpolator
272279

273280
case class MatchCompletion(

Diff for: presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionInterpolatorSuite.scala

+21-1
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ class CompletionInterpolatorSuite extends BaseCompletionSuite:
635635
|""".stripMargin,
636636
"""|class Paths
637637
|object Main {
638-
| s"this is an interesting {java.nio.file.Paths}"
638+
| s"this is an interesting ${java.nio.file.Paths}"
639639
|}
640640
|""".stripMargin,
641641
assertSingleItem = false,
@@ -710,6 +710,26 @@ class CompletionInterpolatorSuite extends BaseCompletionSuite:
710710
filterText = "aaa.plus"
711711
)
712712

713+
714+
@Test def `extension3` =
715+
checkEdit(
716+
"""|trait Cursor
717+
|
718+
|extension (c: Cursor) def spelling: String = "hello"
719+
|object Main {
720+
| val c = new Cursor {}
721+
| val x = s"$c.spelli@@"
722+
|}
723+
|""".stripMargin,
724+
"""|trait Cursor
725+
|
726+
|extension (c: Cursor) def spelling: String = "hello"
727+
|object Main {
728+
| val c = new Cursor {}
729+
| val x = s"${c.spelling$0}"
730+
|}""".stripMargin
731+
)
732+
713733
@Test def `filter-by-type` =
714734
check(
715735
"""|package example

Diff for: presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionWorkspaceSuite.scala

+40-3
Original file line numberDiff line numberDiff line change
@@ -768,8 +768,29 @@ class CompletionWorkspaceSuite extends BaseCompletionSuite:
768768
| def main: Unit = incre@@
769769
|""".stripMargin,
770770
"""|increment3: Int
771-
|increment: Int
772-
|increment2: Int
771+
|increment - a: Int
772+
|increment2 - a.c: Int
773+
|""".stripMargin
774+
)
775+
776+
@Test def `indent-method` =
777+
check(
778+
"""|package a:
779+
| val y = 123
780+
| given intGiven: Int = 123
781+
| type Alpha = String
782+
| class Foo(x: Int)
783+
| object X:
784+
| val x = 123
785+
| def fooBar(x: Int) = x + 1
786+
| package b:
787+
| def fooBar(x: String) = x.length
788+
|
789+
|package c:
790+
| def main() = foo@@
791+
|""".stripMargin,
792+
"""|fooBar - a(x: Int): Int
793+
|fooBar - a.b(x: String): Int
773794
|""".stripMargin
774795
)
775796

@@ -848,5 +869,21 @@ class CompletionWorkspaceSuite extends BaseCompletionSuite:
848869
| }
849870
|}
850871
|""".stripMargin,
851-
filter = _.contains("mmmm(x: Int)")
872+
filter = _.contains("mmmm - demo.O")
873+
)
874+
875+
@Test def `method-label` =
876+
check(
877+
"""|package demo
878+
|
879+
|object O {
880+
| def method(i: Int): Int = i + 1
881+
|}
882+
|
883+
|object Main {
884+
| val x = meth@@
885+
|}
886+
|""".stripMargin,
887+
"""|method - demo.O(i: Int): Int
888+
|""".stripMargin
852889
)

0 commit comments

Comments
 (0)