Skip to content

Commit 17b4a93

Browse files
committedJul 15, 2019
[clangd] Added highlighting for members and methods.
Summary: Added highlighting for members and methods. Reviewers: hokein, sammccall, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64617 llvm-svn: 366047
1 parent ea36cdc commit 17b4a93

File tree

4 files changed

+67
-11
lines changed

4 files changed

+67
-11
lines changed
 

‎clang-tools-extra/clangd/SemanticHighlighting.cpp

+23-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ class HighlightingTokenCollector
4040
return true;
4141
}
4242

43+
bool VisitMemberExpr(MemberExpr *ME) {
44+
const auto *MD = ME->getMemberDecl();
45+
if (isa<CXXDestructorDecl>(MD))
46+
// When calling the destructor manually like: AAA::~A(); The ~ is a
47+
// MemberExpr. Other methods should still be highlighted though.
48+
return true;
49+
addToken(ME->getMemberLoc(), MD);
50+
return true;
51+
}
52+
4353
bool VisitNamedDecl(NamedDecl *ND) {
4454
// UsingDirectiveDecl's namespaces do not show up anywhere else in the
4555
// Visit/Traverse mehods. But they should also be highlighted as a
@@ -115,6 +125,14 @@ class HighlightingTokenCollector
115125
addToken(Loc, HighlightingKind::Class);
116126
return;
117127
}
128+
if (isa<CXXMethodDecl>(D)) {
129+
addToken(Loc, HighlightingKind::Method);
130+
return;
131+
}
132+
if (isa<FieldDecl>(D)) {
133+
addToken(Loc, HighlightingKind::Field);
134+
return;
135+
}
118136
if (isa<EnumDecl>(D)) {
119137
addToken(Loc, HighlightingKind::Enum);
120138
return;
@@ -247,8 +265,12 @@ llvm::StringRef toTextMateScope(HighlightingKind Kind) {
247265
switch (Kind) {
248266
case HighlightingKind::Function:
249267
return "entity.name.function.cpp";
268+
case HighlightingKind::Method:
269+
return "entity.name.function.method.cpp";
250270
case HighlightingKind::Variable:
251-
return "variable.cpp";
271+
return "variable.other.cpp";
272+
case HighlightingKind::Field:
273+
return "variable.other.field.cpp";
252274
case HighlightingKind::Class:
253275
return "entity.name.type.class.cpp";
254276
case HighlightingKind::Enum:

‎clang-tools-extra/clangd/SemanticHighlighting.h

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ namespace clangd {
2626
enum class HighlightingKind {
2727
Variable = 0,
2828
Function,
29+
Method,
30+
Field,
2931
Class,
3032
Enum,
3133
EnumConstant,

‎clang-tools-extra/clangd/test/semantic-highlighting.test

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@
55
# CHECK: "semanticHighlighting": {
66
# CHECK-NEXT: "scopes": [
77
# CHECK-NEXT: [
8-
# CHECK-NEXT: "variable.cpp"
8+
# CHECK-NEXT: "variable.other.cpp"
99
# CHECK-NEXT: ],
1010
# CHECK-NEXT: [
1111
# CHECK-NEXT: "entity.name.function.cpp"
1212
# CHECK-NEXT: ],
1313
# CHECK-NEXT: [
14+
# CHECK-NEXT: "entity.name.function.method.cpp"
15+
# CHECK-NEXT: ],
16+
# CHECK-NEXT: [
17+
# CHECK-NEXT: "variable.other.field.cpp"
18+
# CHECK-NEXT: ],
19+
# CHECK-NEXT: [
1420
# CHECK-NEXT: "entity.name.type.class.cpp"
1521
# CHECK-NEXT: ],
1622
# CHECK-NEXT: [

‎clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

+35-9
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ void checkHighlightings(llvm::StringRef Code) {
3838
{HighlightingKind::Class, "Class"},
3939
{HighlightingKind::Enum, "Enum"},
4040
{HighlightingKind::Namespace, "Namespace"},
41-
{HighlightingKind::EnumConstant, "EnumConstant"}};
41+
{HighlightingKind::EnumConstant, "EnumConstant"},
42+
{HighlightingKind::Field, "Field"},
43+
{HighlightingKind::Method, "Method"}};
4244
std::vector<HighlightingToken> ExpectedTokens;
4345
for (const auto &KindString : KindToString) {
4446
std::vector<HighlightingToken> Toks = makeHighlightingTokens(
@@ -54,14 +56,14 @@ TEST(SemanticHighlighting, GetsCorrectTokens) {
5456
const char *TestCases[] = {
5557
R"cpp(
5658
struct $Class[[AS]] {
57-
double SomeMember;
59+
double $Field[[SomeMember]];
5860
};
5961
struct {
6062
} $Variable[[S]];
6163
void $Function[[foo]](int $Variable[[A]], $Class[[AS]] $Variable[[As]]) {
6264
auto $Variable[[VeryLongVariableName]] = 12312;
6365
$Class[[AS]] $Variable[[AA]];
64-
auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
66+
auto $Variable[[L]] = $Variable[[AA]].$Field[[SomeMember]] + $Variable[[A]];
6567
auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
6668
$Variable[[FN]](12312);
6769
}
@@ -73,27 +75,27 @@ TEST(SemanticHighlighting, GetsCorrectTokens) {
7375
auto $Variable[[Bou]] = $Function[[Gah]];
7476
}
7577
struct $Class[[A]] {
76-
void $Function[[abc]]();
78+
void $Method[[abc]]();
7779
};
7880
)cpp",
7981
R"cpp(
8082
namespace $Namespace[[abc]] {
8183
template<typename T>
8284
struct $Class[[A]] {
83-
T t;
85+
T $Field[[t]];
8486
};
8587
}
8688
template<typename T>
8789
struct $Class[[C]] : $Namespace[[abc]]::A<T> {
88-
typename T::A* D;
90+
typename T::A* $Field[[D]];
8991
};
9092
$Namespace[[abc]]::$Class[[A]]<int> $Variable[[AA]];
9193
typedef $Namespace[[abc]]::$Class[[A]]<int> AAA;
9294
struct $Class[[B]] {
9395
$Class[[B]]();
9496
~$Class[[B]]();
9597
void operator<<($Class[[B]]);
96-
$Class[[AAA]] AA;
98+
$Class[[AAA]] $Field[[AA]];
9799
};
98100
$Class[[B]]::$Class[[B]]() {}
99101
$Class[[B]]::~$Class[[B]]() {}
@@ -112,8 +114,8 @@ TEST(SemanticHighlighting, GetsCorrectTokens) {
112114
$EnumConstant[[Hi]],
113115
};
114116
struct $Class[[A]] {
115-
$Enum[[E]] EEE;
116-
$Enum[[EE]] EEEE;
117+
$Enum[[E]] $Field[[EEE]];
118+
$Enum[[EE]] $Field[[EEEE]];
117119
};
118120
int $Variable[[I]] = $EnumConstant[[Hi]];
119121
$Enum[[E]] $Variable[[L]] = $Enum[[E]]::$EnumConstant[[B]];
@@ -140,6 +142,30 @@ TEST(SemanticHighlighting, GetsCorrectTokens) {
140142
$Namespace[[vwz]]::$Class[[A]]::$Enum[[B]]::$EnumConstant[[Hi]];
141143
::$Namespace[[vwz]]::$Class[[A]] $Variable[[B]];
142144
::$Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable[[BB]];
145+
)cpp",
146+
R"cpp(
147+
struct $Class[[D]] {
148+
double $Field[[C]];
149+
};
150+
struct $Class[[A]] {
151+
double $Field[[B]];
152+
$Class[[D]] $Field[[E]];
153+
static double $Variable[[S]];
154+
void $Method[[foo]]() {
155+
$Field[[B]] = 123;
156+
this->$Field[[B]] = 156;
157+
this->$Method[[foo]]();
158+
$Method[[foo]]();
159+
$Variable[[S]] = 90.1;
160+
}
161+
};
162+
void $Function[[foo]]() {
163+
$Class[[A]] $Variable[[AA]];
164+
$Variable[[AA]].$Field[[B]] += 2;
165+
$Variable[[AA]].$Method[[foo]]();
166+
$Variable[[AA]].$Field[[E]].$Field[[C]];
167+
$Class[[A]]::$Variable[[S]] = 90;
168+
}
143169
)cpp"};
144170
for (const auto &TestCase : TestCases) {
145171
checkHighlightings(TestCase);

0 commit comments

Comments
 (0)
Please sign in to comment.