Skip to content

Commit a8d4ac7

Browse files
Backport "presentation compiler: Bugfix for semantic tokens and synthetic decorations" to LTS (#20780)
Backports #18955 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents ff6bebe + 46de62f commit a8d4ac7

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

presentation-compiler/src/main/dotty/tools/pc/PcSemanticTokensProvider.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ final class PcSemanticTokensProvider(
129129
if sym.isGetter | sym.isSetter then
130130
getTypeId(SemanticTokenTypes.Variable)
131131
else getTypeId(SemanticTokenTypes.Method) // "def"
132-
else if isPredefClass(sym) then
132+
else if sym.isTerm && sym.info.typeSymbol.is(Flags.Module) then
133133
getTypeId(SemanticTokenTypes.Class) // "class"
134134
else if sym.isTerm &&
135135
(!sym.is(Flags.Param) || sym.is(Flags.ParamAccessor))
@@ -151,7 +151,4 @@ final class PcSemanticTokensProvider(
151151
TokenNode(pos.start, pos.`end`, typ, mod)
152152
end makeNode
153153

154-
def isPredefClass(sym: Symbol)(using Context) =
155-
sym.is(Flags.Method) && sym.info.resultType.typeSymbol.is(Flags.Module)
156-
157154
end PcSemanticTokensProvider

presentation-compiler/src/main/dotty/tools/pc/PcSyntheticDecorationProvider.scala

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class PcSyntheticDecorationsProvider(
4646

4747
def provide(): List[SyntheticDecoration] =
4848
val deepFolder = DeepFolder[Synthetics](collectDecorations)
49-
deepFolder(Synthetics.empty, tpdTree).decorations
49+
deepFolder(Synthetics.empty, tpdTree).result()
5050

5151
def collectDecorations(
5252
decorations: Synthetics,
@@ -256,11 +256,23 @@ case class Synthetics(
256256
def containsDef(offset: Int) = definitions(offset)
257257
def add(decoration: Decoration, offset: Int) =
258258
copy(
259-
decorations = decoration :: decorations,
259+
decorations = addDecoration(decoration),
260260
definitions = definitions + offset,
261261
)
262262
def add(decoration: Decoration) =
263-
copy(decorations = decoration :: decorations)
263+
copy (
264+
decorations = addDecoration(decoration)
265+
)
266+
267+
// If method has both type parameter and implicit parameter, we want the type parameter decoration to be displayed first,
268+
// but it's added second. This method adds the decoration to the right position in the list.
269+
private def addDecoration(decoration: Decoration): List[Decoration] =
270+
val atSamePos =
271+
decorations.takeWhile(_.range.getStart() == decoration.range.getStart())
272+
(atSamePos :+ decoration) ++ decorations.drop(atSamePos.size)
273+
274+
def result(): List[Decoration] = decorations.reverse
275+
end Synthetics
264276

265277
object Synthetics:
266278
def empty: Synthetics = Synthetics(Nil, Set.empty)

presentation-compiler/test/dotty/tools/pc/tests/decorations/SyntheticDecorationsSuite.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,18 @@ class SyntheticDecorationsSuite extends BaseSyntheticDecorationsSuite:
483483
|}
484484
|""".stripMargin
485485
)
486+
487+
@Test def `ord` =
488+
check(
489+
"""
490+
|object Main {
491+
| val ordered = "acb".sorted
492+
|}
493+
|""".stripMargin,
494+
"""
495+
|object Main {
496+
| val ordered: String = augmentString("acb").sorted[Char](Char)
497+
|}
498+
|""".stripMargin
499+
)
486500

presentation-compiler/test/dotty/tools/pc/tests/tokens/SemanticTokensSuite.scala

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,37 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite:
278278
check(
279279
"""
280280
|object <<Main>>/*class*/ {
281-
| val <<a>>/*variable,definition,readonly*/ = <<List>>/*class*/(1,2,3)
282-
| val <<y>>/*variable,definition,readonly*/ = <<Vector>>/*class*/(1,2)
283-
| val <<z>>/*variable,definition,readonly*/ = <<Set>>/*class*/(1,2,3)
284-
| val <<w>>/*variable,definition,readonly*/ = <<Right>>/*class*/(1)
281+
|val <<a>>/*variable,definition,readonly*/ = <<List>>/*class*/(1,2,3)
282+
|val <<y>>/*variable,definition,readonly*/ = <<Vector>>/*class*/(1,2)
283+
|val <<z>>/*variable,definition,readonly*/ = <<Set>>/*class*/(1,2,3)
284+
|val <<w>>/*variable,definition,readonly*/ = <<Right>>/*class*/(1)
285285
|}""".stripMargin
286286
)
287287

288+
@Test def `predef1` =
289+
check(
290+
"""
291+
|object <<Main>>/*class*/ {
292+
| val <<a>>/*variable,definition,readonly*/ = <<List>>/*class*/(1,2,3)
293+
| val <<y>>/*class,definition*/ = <<List>>/*class*/
294+
| val <<z>>/*class,definition*/ = <<scala>>/*namespace*/.<<collection>>/*namespace*/.<<immutable>>/*namespace*/.<<List>>/*class*/
295+
|}
296+
|""".stripMargin
297+
)
298+
299+
@Test def `val-object` =
300+
check(
301+
"""
302+
|case class <<X>>/*class*/(<<a>>/*variable,declaration,readonly*/: <<Int>>/*class,abstract*/)
303+
|object <<X>>/*class*/
304+
|
305+
|object <<Main>>/*class*/ {
306+
| val <<x>>/*class,definition*/ = <<X>>/*class*/
307+
| val <<y>>/*variable,definition,readonly*/ = <<X>>/*class*/(1)
308+
|}
309+
|""".stripMargin
310+
)
311+
288312
@Test def `case-class` =
289313
check(
290314
"""|case class <<Foo>>/*class*/(<<i>>/*variable,declaration,readonly*/: <<Int>>/*class,abstract*/, <<j>>/*variable,declaration,readonly*/: <<Int>>/*class,abstract*/)

0 commit comments

Comments
 (0)