From c04d2db43a23d18d895878eedb02784c2eabf830 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Rochala?= <48657087+rochala@users.noreply.github.com> Date: Thu, 27 Jul 2023 12:36:43 +0200 Subject: [PATCH] Update scala3-presentation-compiler to 39e349e (#18296) Update presentation compiler to mtags: 39e349e --- .../pc/completions/OverrideCompletions.scala | 24 +++++-- .../pc/completions/ScalaCliCompletions.scala | 3 + .../tests/completion/CompletionArgSuite.scala | 17 +++++ .../completion/CompletionScalaCliSuite.scala | 11 +++ .../AutoImplementAbstractMembersSuite.scala | 70 +++++++++++++++++++ 5 files changed, 118 insertions(+), 7 deletions(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/completions/OverrideCompletions.scala b/presentation-compiler/src/main/dotty/tools/pc/completions/OverrideCompletions.scala index 9d710750b22e..c4c9b47ff4a4 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/completions/OverrideCompletions.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/completions/OverrideCompletions.scala @@ -317,7 +317,7 @@ object OverrideCompletions: .sortBy(_.sourcePos.start) val source = indexedContext.ctx.source - val shouldCompleteBraces = decls.isEmpty && hasBraces(text, defn).isEmpty + val shouldCompleteBraces = decls.isEmpty && hasBracesOrColon(text, defn).isEmpty val (startIndent, indent, lastIndent) = calcIndent(defn, decls, source, text, shouldCompleteBraces) @@ -470,7 +470,7 @@ object OverrideCompletions: private def inferEditPosition(text: String, defn: TargetDef)(using Context ): SourcePosition = - val span = hasBraces(text, defn) + val span = hasBracesOrColon(text, defn) .map { offset => defn.sourcePos.span.withStart(offset + 1).withEnd(offset + 1) } @@ -480,7 +480,9 @@ object OverrideCompletions: defn.sourcePos.withSpan(span) end inferEditPosition - private def hasBraces(text: String, defn: TargetDef): Option[Int] = + private def hasBracesOrColon(text: String, defn: TargetDef)(using + Context + ): Option[Int] = def hasSelfTypeAnnot = defn match case td: TypeDef => td.rhs match @@ -489,12 +491,20 @@ object OverrideCompletions: case _ => false case _ => false val start = defn.span.start - val offset = + val braceOffset = if hasSelfTypeAnnot then text.indexOf("=>", start) + 1 else text.indexOf("{", start) - if offset > 0 && offset < defn.span.end then Some(offset) - else None - end hasBraces + if braceOffset > 0 && braceOffset < defn.span.end then Some(braceOffset) + else hasColon(text, defn) + end hasBracesOrColon + + private def hasColon(text: String, defn: TargetDef)(using + Context + ): Option[Int] = + defn match + case td: TypeDef if text.charAt(td.rhs.span.end) == ':' => + Some(td.rhs.span.end) + case _ => None private def fallbackFromParent(parent: Tree, name: String)(using Context) = val stats = parent match diff --git a/presentation-compiler/src/main/dotty/tools/pc/completions/ScalaCliCompletions.scala b/presentation-compiler/src/main/dotty/tools/pc/completions/ScalaCliCompletions.scala index 010de4873e91..551322c8ac43 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/completions/ScalaCliCompletions.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/completions/ScalaCliCompletions.scala @@ -20,6 +20,9 @@ class ScalaCliCompletions( // generated script file will end with .sc.scala case (_: TypeDef) :: Nil if pos.source.file.path.endsWith(".sc.scala") => scalaCliDep + case (_: Template) :: (_: TypeDef) :: Nil + if pos.source.file.path.endsWith(".sc.scala") => + scalaCliDep case head :: next => None def contribute(dependency: String) = diff --git a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionArgSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionArgSuite.scala index 5cdddeb8c826..119a320fde2b 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionArgSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionArgSuite.scala @@ -659,3 +659,20 @@ class CompletionArgSuite extends BaseCompletionSuite: |""".stripMargin, topLines = Some(4) ) + + @Test def `recursive` = + check( + """| + |object Main { + | def foo(value: Int): Int = { + | foo(valu@@) + | } + |} + |""".stripMargin, + """|value = : Int + |value = value : Int + |value: Int + |""".stripMargin, + topLines = Some(4), + ) + diff --git a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionScalaCliSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionScalaCliSuite.scala index c55b5277122b..4018b2fabee4 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionScalaCliSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionScalaCliSuite.scala @@ -61,6 +61,17 @@ class CompletionScalaCliSuite extends BaseCompletionSuite: "0.14.1" ) + // We don't to add `::` before version if `sjs1` is specified + @Test def `version-edit` = + checkEdit( + """|//> using lib "io.circe::circe-core_sjs1:0.14.1@@" + |package A + |""".stripMargin, + """|//> using lib "io.circe::circe-core_sjs1:0.14.1" + |package A + |""".stripMargin, + ) + @Test def `multiple-libs` = check( """|//> using lib "io.circe::circe-core:0.14.0", "io.circe::circe-core_na@@" diff --git a/presentation-compiler/test/dotty/tools/pc/tests/edit/AutoImplementAbstractMembersSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/edit/AutoImplementAbstractMembersSuite.scala index f781e226944b..72a7c01597a7 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/edit/AutoImplementAbstractMembersSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/edit/AutoImplementAbstractMembersSuite.scala @@ -1201,6 +1201,76 @@ class AutoImplementAbstractMembersSuite extends BaseCodeActionSuite: |""".stripMargin ) + @Test def `end-marker` = + checkEdit( + """|package a + | + |object A { + | trait Base: + | def foo(x: Int): Int + | def bar(x: String): String + | + | class <> extends Base: + | + | end Concrete + | + |} + |""".stripMargin, + """|package a + | + |object A { + | trait Base: + | def foo(x: Int): Int + | def bar(x: String): String + | + | class Concrete extends Base: + | + | override def foo(x: Int): Int = ??? + | + | override def bar(x: String): String = ??? + | + | + | end Concrete + | + |} + |""".stripMargin, + ) + + @Test def `end-marker2` = + checkEdit( + """|package a + | + |object A { + | trait Base: + | def foo(x: Int): Int + | def bar(x: String): String + | + | class <>(x: Int, y: String) extends Base: + | + | end Concrete + | + |} + |""".stripMargin, + """|package a + | + |object A { + | trait Base: + | def foo(x: Int): Int + | def bar(x: String): String + | + | class Concrete(x: Int, y: String) extends Base: + | + | override def foo(x: Int): Int = ??? + | + | override def bar(x: String): String = ??? + | + | + | end Concrete + | + |} + |""".stripMargin, + ) + def checkEdit( original: String, expected: String