From b474209fdf6c56fa6d0e8b0d672541a6463f2f2e Mon Sep 17 00:00:00 2001 From: Jakub Ciesluk <323892@uwr.edu.pl> Date: Tue, 28 Nov 2023 11:04:39 +0100 Subject: [PATCH 1/2] presentation compiler: Show semantic tokens for vals from predef as classes --- .../tools/pc/PcSemanticTokensProvider.scala | 5 +-- .../pc/tests/tokens/SemanticTokensSuite.scala | 38 +++++++++++++++---- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/PcSemanticTokensProvider.scala b/presentation-compiler/src/main/dotty/tools/pc/PcSemanticTokensProvider.scala index c9af3a5f671a..f1a0b6a65aa7 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/PcSemanticTokensProvider.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/PcSemanticTokensProvider.scala @@ -129,7 +129,7 @@ final class PcSemanticTokensProvider( if sym.isGetter | sym.isSetter then getTypeId(SemanticTokenTypes.Variable) else getTypeId(SemanticTokenTypes.Method) // "def" - else if isPredefClass(sym) then + else if sym.isTerm && sym.info.typeSymbol.is(Flags.Module) then getTypeId(SemanticTokenTypes.Class) // "class" else if sym.isTerm && (!sym.is(Flags.Param) || sym.is(Flags.ParamAccessor)) @@ -151,7 +151,4 @@ final class PcSemanticTokensProvider( TokenNode(pos.start, pos.`end`, typ, mod) end makeNode - def isPredefClass(sym: Symbol)(using Context) = - sym.is(Flags.Method) && sym.info.resultType.typeSymbol.is(Flags.Module) - end PcSemanticTokensProvider diff --git a/presentation-compiler/test/dotty/tools/pc/tests/tokens/SemanticTokensSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/tokens/SemanticTokensSuite.scala index 5d1fe1083b52..4773e3a5e8d3 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/tokens/SemanticTokensSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/tokens/SemanticTokensSuite.scala @@ -194,7 +194,7 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite: s"""|package <>/*namespace*/ | |object <>/*class*/ { - | val <>/*variable,definition,readonly*/ = <>/*variable,readonly*/(1,2,3) + | val <>/*variable,definition,readonly*/ = <>/*class*/(1,2,3) | val <>/*variable,definition,readonly*/ = <>/*class*/(1) | val <>/*class*/(<>/*variable,definition,readonly*/) = <>/*variable,readonly*/ | val <>/*class*/(<>/*variable,definition,readonly*/) = <>/*variable,readonly*/ @@ -269,7 +269,7 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite: |object <>/*class*/ { | val <>/*variable,definition,readonly*/ = 1 | var <>/*variable,definition*/ = 2 - | val <>/*variable,definition,readonly*/ = <>/*variable,readonly*/(1,<>/*variable,readonly*/,<>/*variable*/) + | val <>/*variable,definition,readonly*/ = <>/*class*/(1,<>/*variable,readonly*/,<>/*variable*/) | <>/*variable*/ = <>/*variable,readonly*/ |""".stripMargin ) @@ -278,13 +278,37 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite: check( """ |object <
>/*class*/ { - |val <>/*variable,definition,readonly*/ = <>/*variable,readonly*/(1,2,3) - |val <>/*variable,definition,readonly*/ = <>/*variable,readonly*/(1,2) - |val <>/*variable,definition,readonly*/ = <>/*variable,readonly*/(1,2,3) - |val <>/*variable,definition,readonly*/ = <>/*variable,readonly*/(1) + |val <>/*variable,definition,readonly*/ = <>/*class*/(1,2,3) + |val <>/*variable,definition,readonly*/ = <>/*class*/(1,2) + |val <>/*variable,definition,readonly*/ = <>/*class*/(1,2,3) + |val <>/*variable,definition,readonly*/ = <>/*class*/(1) |}""".stripMargin ) + @Test def `predef1` = + check( + """ + |object <
>/*class*/ { + | val <>/*variable,definition,readonly*/ = <>/*class*/(1,2,3) + | val <>/*class,definition*/ = <>/*class*/ + | val <>/*class,definition*/ = <>/*namespace*/.<>/*namespace*/.<>/*namespace*/.<>/*class*/ + |} + |""".stripMargin + ) + + @Test def `val-object` = + check( + """ + |case class <>/*class*/(<>/*variable,declaration,readonly*/: <>/*class,abstract*/) + |object <>/*class*/ + | + |object <
>/*class*/ { + | val <>/*class,definition*/ = <>/*class*/ + | val <>/*variable,definition,readonly*/ = <>/*class*/(1) + |} + |""".stripMargin + ) + @Test def `case-class` = check( """|case class <>/*class*/(<>/*variable,declaration,readonly*/: <>/*class,abstract*/, <>/*variable,declaration,readonly*/: <>/*class,abstract*/) @@ -326,7 +350,7 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite: | |object <>/*class*/ { | val <>/*variable,definition,readonly*/ = for { - | <>/*variable,definition,readonly*/ <- <>/*variable,readonly*/("a", "b", "c") + | <>/*variable,definition,readonly*/ <- <>/*class*/("a", "b", "c") | <<_>>/*class,abstract*/ = <>/*method*/("print!") | } yield <>/*variable,readonly*/ |} From 9e0eb8ef3a4f60bbf5da9d31838cb122fcce6083 Mon Sep 17 00:00:00 2001 From: Jakub Ciesluk <323892@uwr.edu.pl> Date: Tue, 28 Nov 2023 11:30:07 +0100 Subject: [PATCH 2/2] presentation compiler: remove synthetic decorations for script wrapper --- .../pc/PcSyntheticDecorationProvider.scala | 18 +++++++++++++++--- .../SyntheticDecorationsSuite.scala | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/PcSyntheticDecorationProvider.scala b/presentation-compiler/src/main/dotty/tools/pc/PcSyntheticDecorationProvider.scala index 62c24dcc7990..1ff410ad7f10 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/PcSyntheticDecorationProvider.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/PcSyntheticDecorationProvider.scala @@ -46,7 +46,7 @@ final class PcSyntheticDecorationsProvider( def provide(): List[SyntheticDecoration] = val deepFolder = DeepFolder[Synthetics](collectDecorations) - deepFolder(Synthetics.empty, tpdTree).decorations + deepFolder(Synthetics.empty, tpdTree).result() def collectDecorations( decorations: Synthetics, @@ -256,11 +256,23 @@ case class Synthetics( def containsDef(offset: Int) = definitions(offset) def add(decoration: Decoration, offset: Int) = copy( - decorations = decoration :: decorations, + decorations = addDecoration(decoration), definitions = definitions + offset, ) def add(decoration: Decoration) = - copy(decorations = decoration :: decorations) + copy ( + decorations = addDecoration(decoration) + ) + + // If method has both type parameter and implicit parameter, we want the type parameter decoration to be displayed first, + // but it's added second. This method adds the decoration to the right position in the list. + private def addDecoration(decoration: Decoration): List[Decoration] = + val atSamePos = + decorations.takeWhile(_.range.getStart() == decoration.range.getStart()) + (atSamePos :+ decoration) ++ decorations.drop(atSamePos.size) + + def result(): List[Decoration] = decorations.reverse +end Synthetics object Synthetics: def empty: Synthetics = Synthetics(Nil, Set.empty) \ No newline at end of file diff --git a/presentation-compiler/test/dotty/tools/pc/tests/decorations/SyntheticDecorationsSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/decorations/SyntheticDecorationsSuite.scala index 49ac1478cd35..7d4cc3cf41a4 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/decorations/SyntheticDecorationsSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/decorations/SyntheticDecorationsSuite.scala @@ -483,4 +483,18 @@ class SyntheticDecorationsSuite extends BaseSyntheticDecorationsSuite: |} |""".stripMargin ) + + @Test def `ord` = + check( + """ + |object Main { + | val ordered = "acb".sorted + |} + |""".stripMargin, + """ + |object Main { + | val ordered: String = augmentString("acb").sorted[Char](Char) + |} + |""".stripMargin + )