Skip to content

presentation compiler: Bugfix for semantic tokens and synthetic decorations #18955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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