From a438ca16e6a2839fd0078d288ebdd8d6a6065a10 Mon Sep 17 00:00:00 2001 From: Jakub Ciesluk <323892@uwr.edu.pl> Date: Fri, 29 Dec 2023 10:35:58 +0100 Subject: [PATCH] improvement: Remove semantic tokens fallback --- .../internal/metals/SemanticTokensProvider.scala | 16 +--------------- .../internal/pc/PcSemanticTokensProvider.scala | 3 ++- .../internal/pc/PcSemanticTokensProvider.scala | 2 +- .../main/scala/tests/BaseWorksheetLspSuite.scala | 2 +- .../semanticTokens3/example/NamedArguments.scala | 2 +- .../example/StructuralTypes.scala | 2 +- .../scala/tests/SemanticTokensLspSuite.scala | 6 +++--- 7 files changed, 10 insertions(+), 23 deletions(-) diff --git a/metals/src/main/scala/scala/meta/internal/metals/SemanticTokensProvider.scala b/metals/src/main/scala/scala/meta/internal/metals/SemanticTokensProvider.scala index f8d7d62fdff..12cef05c00d 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/SemanticTokensProvider.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/SemanticTokensProvider.scala @@ -263,21 +263,7 @@ object SemanticTokensProvider { case _ => 0 } - if (tokenType != -1) (tokenType, tokenModifier) - else if (!tk.isWhiteSpaceOrComment) { - tokenFallback(tk) - } else (-1, 0) - - } - def tokenFallback(tk: scala.meta.tokens.Token): (Integer, Integer) = { - tk.text.headOption match { - case Some(upper) if upper.isUpper => - (getTypeId(SemanticTokenTypes.Class), 0) - case Some(lower) if lower.isLower => - val readOnlyMod = 1 << getModifierId(SemanticTokenModifiers.Readonly) - (getTypeId(SemanticTokenTypes.Variable), readOnlyMod) - case _ => (-1, 0) - } + (tokenType, tokenModifier) } private val SoftKeywordsUnapply = new SoftKeywords(scala.meta.dialects.Scala3) diff --git a/mtags/src/main/scala-2/scala/meta/internal/pc/PcSemanticTokensProvider.scala b/mtags/src/main/scala-2/scala/meta/internal/pc/PcSemanticTokensProvider.scala index 75179a44975..b7cd7c1ef7d 100644 --- a/mtags/src/main/scala-2/scala/meta/internal/pc/PcSemanticTokensProvider.scala +++ b/mtags/src/main/scala-2/scala/meta/internal/pc/PcSemanticTokensProvider.scala @@ -53,7 +53,8 @@ final class PcSemanticTokensProvider( ): Option[Node] = { val sym = symbol.fold(tree.symbol)(identity) if ( - !pos.isDefined || sym == null || sym == compiler.NoSymbol || sym.isConstructor + !pos.isDefined || sym == null || + sym == compiler.NoSymbol || sym.isError || sym.isConstructor ) { None } else { diff --git a/mtags/src/main/scala-3/scala/meta/internal/pc/PcSemanticTokensProvider.scala b/mtags/src/main/scala-3/scala/meta/internal/pc/PcSemanticTokensProvider.scala index baeae4508a3..f9762a8a729 100644 --- a/mtags/src/main/scala-3/scala/meta/internal/pc/PcSemanticTokensProvider.scala +++ b/mtags/src/main/scala-3/scala/meta/internal/pc/PcSemanticTokensProvider.scala @@ -71,7 +71,7 @@ final class PcSemanticTokensProvider( case tree: Tree => symbol.fold(tree.symbol)(identity) case EndMarker(sym) => sym - if !pos.exists || sym == null || sym == NoSymbol then None + if !pos.exists || sym == null || sym == NoSymbol || sym.isError then None else Some( makeNode( diff --git a/tests/unit/src/main/scala/tests/BaseWorksheetLspSuite.scala b/tests/unit/src/main/scala/tests/BaseWorksheetLspSuite.scala index fc9083a471a..2a0f676a6a0 100644 --- a/tests/unit/src/main/scala/tests/BaseWorksheetLspSuite.scala +++ b/tests/unit/src/main/scala/tests/BaseWorksheetLspSuite.scala @@ -964,7 +964,7 @@ abstract class BaseWorksheetLspSuite( test("semantic-highlighting2") { val expected = s"""| - |<>/*keyword*/ <>/*variable,definition,readonly*/ = <>/*class*/(<>/*variable,readonly*/, <>/*variable,readonly*/) + |<>/*keyword*/ <>/*variable,definition,readonly*/ = <>/*class*/(hi1, hi2) |""".stripMargin val fileContent = diff --git a/tests/unit/src/test/resources/semanticTokens3/example/NamedArguments.scala b/tests/unit/src/test/resources/semanticTokens3/example/NamedArguments.scala index 7bd3bd278bf..28103967b44 100644 --- a/tests/unit/src/test/resources/semanticTokens3/example/NamedArguments.scala +++ b/tests/unit/src/test/resources/semanticTokens3/example/NamedArguments.scala @@ -28,7 +28,7 @@ <>/*comment*/ <>/*class*/( - <>/*variable,readonly*/ = <<2>>/*number*/ + elems = <<2>>/*number*/ ) } \ No newline at end of file diff --git a/tests/unit/src/test/resources/semanticTokens3/example/StructuralTypes.scala b/tests/unit/src/test/resources/semanticTokens3/example/StructuralTypes.scala index 42055e4f567..c3895840dfc 100644 --- a/tests/unit/src/test/resources/semanticTokens3/example/StructuralTypes.scala +++ b/tests/unit/src/test/resources/semanticTokens3/example/StructuralTypes.scala @@ -17,4 +17,4 @@ } = <>/*keyword*/: <>/*keyword*/ <>/*method,definition*/ = <<"4.0">>/*string*/ <>/*variable,readonly*/.<>/*method*/ -<>/*keyword*/ <>/*class*/ \ No newline at end of file +<>/*keyword*/ StructuralTypes \ No newline at end of file diff --git a/tests/unit/src/test/scala/tests/SemanticTokensLspSuite.scala b/tests/unit/src/test/scala/tests/SemanticTokensLspSuite.scala index e014e2bc9e6..79d12faae85 100644 --- a/tests/unit/src/test/scala/tests/SemanticTokensLspSuite.scala +++ b/tests/unit/src/test/scala/tests/SemanticTokensLspSuite.scala @@ -128,7 +128,7 @@ class SemanticTokensLspSuite extends BaseLspSuite("SemanticTokens") { """|<>/*keyword*/ <
>/*class*/ { | <>/*keyword*/ <>/*variable,definition,readonly*/ = <>/*class*/(<<1>>/*number*/,<<2>>/*number*/,<<3>>/*number*/) | <>/*keyword*/ <>/*variable,definition,readonly*/ = <>/*variable,readonly*/ <>/*keyword*/ { - | <>/*keyword*/ <>/*class*/(<>/*variable,readonly*/,<>/*variable,readonly*/,<>/*variable,readonly*/) <<=>>>/*operator*/ <>/*variable,readonly*/ + | <>/*keyword*/ <>/*class*/(a,b,c) <<=>>>/*operator*/ <>/*variable,readonly*/ | <>/*keyword*/ <<_>>/*variable,readonly*/ <<=>>>/*operator*/ <<0>>/*number*/ | } | <>/*keyword*/ <>/*variable,definition,readonly*/ = <>/*class*/(<<1>>/*number*/,<<2>>/*number*/,<<3>>/*number*/) @@ -171,7 +171,7 @@ class SemanticTokensLspSuite extends BaseLspSuite("SemanticTokens") { "self-type", """|<>/*keyword*/ <>/*namespace*/ | - |<>/*keyword*/ <>/*class*/ { <>/*variable,readonly*/: <>/*class*/ <<=>>>/*operator*/ + |<>/*keyword*/ <>/*class*/ { self: Any <<=>>>/*operator*/ | <>/*keyword*/ <>/*variable,definition,readonly*/ = <<1>>/*number*/ |} |""".stripMargin, @@ -189,7 +189,7 @@ class SemanticTokensLspSuite extends BaseLspSuite("SemanticTokens") { |} | |<>/*keyword*/ <>/*interface,abstract*/ { - | <>/*variable,readonly*/: <>/*interface,abstract*/ <<=>>>/*operator*/ <>/*comment*/ + | self: <>/*interface,abstract*/ <<=>>>/*operator*/ <>/*comment*/ | <>/*keyword*/ <>/*method,definition*/(<>/*parameter,declaration,readonly*/: <>/*type*/) = <>/*method*/(<>/*keyword*/<<">>/*string*/<<$>>/*keyword*/<>/*method,abstract*/<<: >>/*string*/<<$>>/*keyword*/<>/*parameter,readonly*/<<">>/*string*/) |} |""".stripMargin,