Skip to content

Commit

Permalink
improvement: Remove semantic tokens fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
jkciesluk committed Dec 29, 2023
1 parent 510386d commit a438ca1
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ abstract class BaseWorksheetLspSuite(
test("semantic-highlighting2") {
val expected =
s"""|
|<<val>>/*keyword*/ <<hellos>>/*variable,definition,readonly*/ = <<List>>/*class*/(<<hi1>>/*variable,readonly*/, <<hi2>>/*variable,readonly*/)
|<<val>>/*keyword*/ <<hellos>>/*variable,definition,readonly*/ = <<List>>/*class*/(hi1, hi2)
|""".stripMargin

val fileContent =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

<<// vararg>>/*comment*/
<<List>>/*class*/(
<<elems>>/*variable,readonly*/ = <<2>>/*number*/
elems = <<2>>/*number*/
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
} = <<new>>/*keyword*/:
<<def>>/*keyword*/ <<scalameta>>/*method,definition*/ = <<"4.0">>/*string*/
<<V>>/*variable,readonly*/.<<scalameta>>/*method*/
<<end>>/*keyword*/ <<StructuralTypes>>/*class*/
<<end>>/*keyword*/ StructuralTypes
6 changes: 3 additions & 3 deletions tests/unit/src/test/scala/tests/SemanticTokensLspSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class SemanticTokensLspSuite extends BaseLspSuite("SemanticTokens") {
"""|<<object>>/*keyword*/ <<Main>>/*class*/ {
| <<val>>/*keyword*/ <<x>>/*variable,definition,readonly*/ = <<List>>/*class*/(<<1>>/*number*/,<<2>>/*number*/,<<3>>/*number*/)
| <<val>>/*keyword*/ <<y>>/*variable,definition,readonly*/ = <<a>>/*variable,readonly*/ <<match>>/*keyword*/ {
| <<case>>/*keyword*/ <<List>>/*class*/(<<a>>/*variable,readonly*/,<<b>>/*variable,readonly*/,<<c>>/*variable,readonly*/) <<=>>>/*operator*/ <<a>>/*variable,readonly*/
| <<case>>/*keyword*/ <<List>>/*class*/(a,b,c) <<=>>>/*operator*/ <<a>>/*variable,readonly*/
| <<case>>/*keyword*/ <<_>>/*variable,readonly*/ <<=>>>/*operator*/ <<0>>/*number*/
| }
| <<val>>/*keyword*/ <<z>>/*variable,definition,readonly*/ = <<Set>>/*class*/(<<1>>/*number*/,<<2>>/*number*/,<<3>>/*number*/)
Expand Down Expand Up @@ -171,7 +171,7 @@ class SemanticTokensLspSuite extends BaseLspSuite("SemanticTokens") {
"self-type",
"""|<<package>>/*keyword*/ <<a>>/*namespace*/
|
|<<object>>/*keyword*/ <<Abc>>/*class*/ { <<self>>/*variable,readonly*/: <<Any>>/*class*/ <<=>>>/*operator*/
|<<object>>/*keyword*/ <<Abc>>/*class*/ { self: Any <<=>>>/*operator*/
| <<val>>/*keyword*/ <<xyz>>/*variable,definition,readonly*/ = <<1>>/*number*/
|}
|""".stripMargin,
Expand All @@ -189,7 +189,7 @@ class SemanticTokensLspSuite extends BaseLspSuite("SemanticTokens") {
|}
|
|<<trait>>/*keyword*/ <<Tweeter>>/*interface,abstract*/ {
| <<self>>/*variable,readonly*/: <<User>>/*interface,abstract*/ <<=>>>/*operator*/ <<// reassign this>>/*comment*/
| self: <<User>>/*interface,abstract*/ <<=>>>/*operator*/ <<// reassign this>>/*comment*/
| <<def>>/*keyword*/ <<tweet>>/*method,definition*/(<<tweetText>>/*parameter,declaration,readonly*/: <<String>>/*type*/) = <<println>>/*method*/(<<s>>/*keyword*/<<">>/*string*/<<$>>/*keyword*/<<username>>/*method,abstract*/<<: >>/*string*/<<$>>/*keyword*/<<tweetText>>/*parameter,readonly*/<<">>/*string*/)
|}
|""".stripMargin,
Expand Down

0 comments on commit a438ca1

Please sign in to comment.