Skip to content

Commit d2c2ef6

Browse files
committed
[clangd] Fix bordering tokens wrong order
Fixed an issue where if two tokens are bordering clangd would always prefer the right one, but if the right one is a comma this would lead to unintuitive and wrong results. See issue #2457 for more details. Also adjusted a test to also test for this.
1 parent 90d1d23 commit d2c2ef6

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

clang-tools-extra/clangd/Selection.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,14 +1039,20 @@ pointBounds(unsigned Offset, const syntax::TokenBuffer &Tokens) {
10391039
const auto &SM = Tokens.sourceManager();
10401040
SourceLocation Loc = SM.getComposedLoc(SM.getMainFileID(), Offset);
10411041
llvm::SmallVector<std::pair<unsigned, unsigned>, 2> Result;
1042-
// Prefer right token over left.
1042+
llvm::SmallVector<std::pair<unsigned, unsigned>, 2> ResultLowPrio;
1043+
// Prefer right token over left, also non comma over comma.
10431044
for (const syntax::Token &Tok :
10441045
llvm::reverse(spelledTokensTouching(Loc, Tokens))) {
10451046
if (shouldIgnore(Tok))
10461047
continue;
10471048
unsigned Offset = Tokens.sourceManager().getFileOffset(Tok.location());
1048-
Result.emplace_back(Offset, Offset + Tok.length());
1049+
if (Tok.kind() == tok::comma){
1050+
ResultLowPrio.emplace_back(Offset, Offset + Tok.length());
1051+
} else {
1052+
Result.emplace_back(Offset, Offset + Tok.length());
1053+
}
10491054
}
1055+
Result.append(ResultLowPrio);
10501056
if (Result.empty())
10511057
Result.emplace_back(Offset, Offset);
10521058
return Result;

clang-tools-extra/clangd/unittests/CallHierarchyTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ TEST(CallHierarchy, HierarchyOnVar) {
636636
TEST(CallHierarchy, HierarchyOnEnumConstant) {
637637
// Tests that the call hierarchy works on enum constants.
638638
Annotations Source(R"cpp(
639-
enum class Coin { heads$Heads^ , tai$Tails^ls };
639+
enum class Coin { heads$Heads^, tai$Tails^ls };
640640
void caller() {
641641
Coin::$CallerH[[heads]];
642642
Coin::$CallerT[[tails]];

0 commit comments

Comments
 (0)