Skip to content

Commit

Permalink
presentation compiler: Bugfix for semantic tokens and synthetic decor…
Browse files Browse the repository at this point in the history
  • Loading branch information
tgodzik authored Nov 28, 2023
2 parents f1e440e + 9e0eb8e commit fab3f21
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite:
s"""|package <<example>>/*namespace*/
|
|object <<A>>/*class*/ {
| val <<x>>/*variable,definition,readonly*/ = <<List>>/*variable,readonly*/(1,2,3)
| val <<x>>/*variable,definition,readonly*/ = <<List>>/*class*/(1,2,3)
| val <<s>>/*variable,definition,readonly*/ = <<Some>>/*class*/(1)
| val <<Some>>/*class*/(<<s1>>/*variable,definition,readonly*/) = <<s>>/*variable,readonly*/
| val <<Some>>/*class*/(<<s2>>/*variable,definition,readonly*/) = <<s>>/*variable,readonly*/
Expand Down Expand Up @@ -269,7 +269,7 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite:
|object <<A>>/*class*/ {
| val <<a>>/*variable,definition,readonly*/ = 1
| var <<b>>/*variable,definition*/ = 2
| val <<c>>/*variable,definition,readonly*/ = <<List>>/*variable,readonly*/(1,<<a>>/*variable,readonly*/,<<b>>/*variable*/)
| val <<c>>/*variable,definition,readonly*/ = <<List>>/*class*/(1,<<a>>/*variable,readonly*/,<<b>>/*variable*/)
| <<b>>/*variable*/ = <<a>>/*variable,readonly*/
|""".stripMargin
)
Expand All @@ -278,13 +278,37 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite:
check(
"""
|object <<Main>>/*class*/ {
|val <<a>>/*variable,definition,readonly*/ = <<List>>/*variable,readonly*/(1,2,3)
|val <<y>>/*variable,definition,readonly*/ = <<Vector>>/*variable,readonly*/(1,2)
|val <<z>>/*variable,definition,readonly*/ = <<Set>>/*variable,readonly*/(1,2,3)
|val <<w>>/*variable,definition,readonly*/ = <<Right>>/*variable,readonly*/(1)
|val <<a>>/*variable,definition,readonly*/ = <<List>>/*class*/(1,2,3)
|val <<y>>/*variable,definition,readonly*/ = <<Vector>>/*class*/(1,2)
|val <<z>>/*variable,definition,readonly*/ = <<Set>>/*class*/(1,2,3)
|val <<w>>/*variable,definition,readonly*/ = <<Right>>/*class*/(1)
|}""".stripMargin
)

@Test def `predef1` =
check(
"""
|object <<Main>>/*class*/ {
| val <<a>>/*variable,definition,readonly*/ = <<List>>/*class*/(1,2,3)
| val <<y>>/*class,definition*/ = <<List>>/*class*/
| val <<z>>/*class,definition*/ = <<scala>>/*namespace*/.<<collection>>/*namespace*/.<<immutable>>/*namespace*/.<<List>>/*class*/
|}
|""".stripMargin
)

@Test def `val-object` =
check(
"""
|case class <<X>>/*class*/(<<a>>/*variable,declaration,readonly*/: <<Int>>/*class,abstract*/)
|object <<X>>/*class*/
|
|object <<Main>>/*class*/ {
| val <<x>>/*class,definition*/ = <<X>>/*class*/
| val <<y>>/*variable,definition,readonly*/ = <<X>>/*class*/(1)
|}
|""".stripMargin
)

@Test def `case-class` =
check(
"""|case class <<Foo>>/*class*/(<<i>>/*variable,declaration,readonly*/: <<Int>>/*class,abstract*/, <<j>>/*variable,declaration,readonly*/: <<Int>>/*class,abstract*/)
Expand Down Expand Up @@ -326,7 +350,7 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite:
|
|object <<B>>/*class*/ {
| val <<a>>/*variable,definition,readonly*/ = for {
| <<foo>>/*variable,definition,readonly*/ <- <<List>>/*variable,readonly*/("a", "b", "c")
| <<foo>>/*variable,definition,readonly*/ <- <<List>>/*class*/("a", "b", "c")
| <<_>>/*class,abstract*/ = <<println>>/*method*/("print!")
| } yield <<foo>>/*variable,readonly*/
|}
Expand Down

0 comments on commit fab3f21

Please sign in to comment.