Skip to content

Commit

Permalink
[clangd] Added highlighting to enum constants.
Browse files Browse the repository at this point in the history
Summary: VSCode does not have a scope for enum constants. So they were placed under "constant.other.enum" as that seems to be the most correct scope for enum constants. However, this makes theia color them blue (the same color it uses for keywords).

Reviewers: hokein, sammccall, ilya-biryukov

Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D64624

llvm-svn: 366045
  • Loading branch information
jvikstrom committed Jul 15, 2019
1 parent 6bd02a4 commit d02f17d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
6 changes: 6 additions & 0 deletions clang-tools-extra/clangd/SemanticHighlighting.cpp
Expand Up @@ -119,6 +119,10 @@ class HighlightingTokenCollector
addToken(Loc, HighlightingKind::Enum);
return;
}
if (isa<EnumConstantDecl>(D)) {
addToken(Loc, HighlightingKind::EnumConstant);
return;
}
if (isa<VarDecl>(D)) {
addToken(Loc, HighlightingKind::Variable);
return;
Expand Down Expand Up @@ -249,6 +253,8 @@ llvm::StringRef toTextMateScope(HighlightingKind Kind) {
return "entity.name.type.class.cpp";
case HighlightingKind::Enum:
return "entity.name.type.enum.cpp";
case HighlightingKind::EnumConstant:
return "variable.other.enummember.cpp";
case HighlightingKind::Namespace:
return "entity.name.namespace.cpp";
case HighlightingKind::NumKinds:
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/SemanticHighlighting.h
Expand Up @@ -28,6 +28,7 @@ enum class HighlightingKind {
Function,
Class,
Enum,
EnumConstant,
Namespace,

NumKinds,
Expand Down
3 changes: 3 additions & 0 deletions clang-tools-extra/clangd/test/semantic-highlighting.test
Expand Up @@ -17,6 +17,9 @@
# CHECK-NEXT: "entity.name.type.enum.cpp"
# CHECK-NEXT: ],
# CHECK-NEXT: [
# CHECK-NEXT: "variable.other.enummember.cpp"
# CHECK-NEXT: ],
# CHECK-NEXT: [
# CHECK-NEXT: "entity.name.namespace.cpp"
# CHECK-NEXT: ]
# CHECK-NEXT: ]
Expand Down
18 changes: 13 additions & 5 deletions clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
Expand Up @@ -37,7 +37,8 @@ void checkHighlightings(llvm::StringRef Code) {
{HighlightingKind::Function, "Function"},
{HighlightingKind::Class, "Class"},
{HighlightingKind::Enum, "Enum"},
{HighlightingKind::Namespace, "Namespace"}};
{HighlightingKind::Namespace, "Namespace"},
{HighlightingKind::EnumConstant, "EnumConstant"}};
std::vector<HighlightingToken> ExpectedTokens;
for (const auto &KindString : KindToString) {
std::vector<HighlightingToken> Toks = makeHighlightingTokens(
Expand Down Expand Up @@ -103,12 +104,19 @@ TEST(SemanticHighlighting, GetsCorrectTokens) {
}
)cpp",
R"cpp(
enum class $Enum[[E]] {};
enum $Enum[[EE]] {};
enum class $Enum[[E]] {
$EnumConstant[[A]],
$EnumConstant[[B]],
};
enum $Enum[[EE]] {
$EnumConstant[[Hi]],
};
struct $Class[[A]] {
$Enum[[E]] EEE;
$Enum[[EE]] EEEE;
};
int $Variable[[I]] = $EnumConstant[[Hi]];
$Enum[[E]] $Variable[[L]] = $Enum[[E]]::$EnumConstant[[B]];
)cpp",
R"cpp(
namespace $Namespace[[abc]] {
Expand All @@ -118,7 +126,7 @@ TEST(SemanticHighlighting, GetsCorrectTokens) {
namespace $Namespace[[cde]] {
struct $Class[[A]] {
enum class $Enum[[B]] {
Hi,
$EnumConstant[[Hi]],
};
};
}
Expand All @@ -129,7 +137,7 @@ TEST(SemanticHighlighting, GetsCorrectTokens) {
$Namespace[[abc]]::$Namespace[[bcd]]::$Namespace[[cde]];
$Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable[[AA]];
$Namespace[[vwz]]::$Class[[A]]::$Enum[[B]] $Variable[[AAA]] =
$Namespace[[vwz]]::$Class[[A]]::$Enum[[B]]::Hi;
$Namespace[[vwz]]::$Class[[A]]::$Enum[[B]]::$EnumConstant[[Hi]];
::$Namespace[[vwz]]::$Class[[A]] $Variable[[B]];
::$Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable[[BB]];
)cpp"};
Expand Down

0 comments on commit d02f17d

Please sign in to comment.