From 4cab332139122bd416da10d9876e5a3312106314 Mon Sep 17 00:00:00 2001 From: Katarzyna Marek Date: Mon, 5 Feb 2024 13:08:43 +0100 Subject: [PATCH 1/2] bugfix: Extension completions in interpolated strings --- .../pc/completions/CompletionProvider.scala | 16 ++++++++++---- .../pc/completions/CompletionValue.scala | 1 + .../CompletionInterpolatorSuite.scala | 22 ++++++++++++++++++- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/completions/CompletionProvider.scala b/presentation-compiler/src/main/dotty/tools/pc/completions/CompletionProvider.scala index 302219e12e10..9d46a460850a 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/completions/CompletionProvider.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/completions/CompletionProvider.scala @@ -243,21 +243,29 @@ class CompletionProvider( r match case IndexedContext.Result.InScope => mkItem( - ident.backticked(backtickSoftKeyword) + completionTextSuffix + v.insertText.getOrElse( + ident.backticked( + backtickSoftKeyword + ) + completionTextSuffix + ), + range = v.range, ) case _ if isInStringInterpolation => mkItem( - "{" + sym.fullNameBackticked + completionTextSuffix + "}" + "{" + sym.fullNameBackticked + completionTextSuffix + "}", + range = v.range ) case _ if v.isExtensionMethod => mkItem( - ident.backticked(backtickSoftKeyword) + completionTextSuffix + ident.backticked(backtickSoftKeyword) + completionTextSuffix, + range = v.range ) case _ => mkItem( sym.fullNameBackticked( backtickSoftKeyword - ) + completionTextSuffix + ) + completionTextSuffix, + range = v.range ) end match end match diff --git a/presentation-compiler/src/main/dotty/tools/pc/completions/CompletionValue.scala b/presentation-compiler/src/main/dotty/tools/pc/completions/CompletionValue.scala index f6217ba21ebf..18e43f5141e7 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/completions/CompletionValue.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/completions/CompletionValue.scala @@ -268,6 +268,7 @@ object CompletionValue: )(using Context): String = if isExtension then s"${printer.completionSymbol(symbol)} (extension)" else super.description(printer) + override def isExtensionMethod: Boolean = isExtension end Interpolator case class MatchCompletion( diff --git a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionInterpolatorSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionInterpolatorSuite.scala index b5024e78f2e9..d70f28d08486 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionInterpolatorSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionInterpolatorSuite.scala @@ -635,7 +635,7 @@ class CompletionInterpolatorSuite extends BaseCompletionSuite: |""".stripMargin, """|class Paths |object Main { - | s"this is an interesting {java.nio.file.Paths}" + | s"this is an interesting ${java.nio.file.Paths}" |} |""".stripMargin, assertSingleItem = false, @@ -710,6 +710,26 @@ class CompletionInterpolatorSuite extends BaseCompletionSuite: filterText = "aaa.plus" ) + + @Test def `extension3` = + checkEdit( + """|trait Cursor + | + |extension (c: Cursor) def spelling: String = "hello" + |object Main { + | val c = new Cursor {} + | val x = s"$c.spelli@@" + |} + |""".stripMargin, + """|trait Cursor + | + |extension (c: Cursor) def spelling: String = "hello" + |object Main { + | val c = new Cursor {} + | val x = s"${c.spelling$0}" + |}""".stripMargin + ) + @Test def `filter-by-type` = check( """|package example From 0f56dba4d4b126a310683bb40428d1dc52fe1315 Mon Sep 17 00:00:00 2001 From: Katarzyna Marek Date: Mon, 5 Feb 2024 13:21:53 +0100 Subject: [PATCH 2/2] improvement: Better labels for workspace methods completions --- .../pc/completions/CompletionValue.scala | 8 +++- .../completion/CompletionWorkspaceSuite.scala | 43 +++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/completions/CompletionValue.scala b/presentation-compiler/src/main/dotty/tools/pc/completions/CompletionValue.scala index 18e43f5141e7..2810fe728b9a 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/completions/CompletionValue.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/completions/CompletionValue.scala @@ -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 @@ -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 => @@ -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 */ diff --git a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionWorkspaceSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionWorkspaceSuite.scala index 706ae5062308..504df812b37f 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionWorkspaceSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionWorkspaceSuite.scala @@ -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 ) @@ -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 )