[clangd] findNearbyIdentifier(): fix the word search in the token stream.

Without this patch the word occurrence search always returns the first token of the file.
Despite of that, `findNeardyIdentifier()` returns the correct result (but inefficently) until there are several matched tokens with the same value `floor(log2(<token line> - <word line>))` (e.g. several matched tokens on the same line).

Reviewed By: kadircet

Differential Revision: https://reviews.llvm.org/D84912
This commit is contained in:
Aleksandr Platonov 2020-07-30 12:45:07 +03:00
parent 3da6a974db
commit 05b1734661
2 changed files with 9 additions and 2 deletions

View File

@ -518,7 +518,7 @@ const syntax::Token *findNearbyIdentifier(const SpelledWord &Word,
// Find where the word occurred in the token stream, to search forward & back.
auto *I = llvm::partition_point(SpelledTokens, [&](const syntax::Token &T) {
assert(SM.getFileID(T.location()) == SM.getFileID(Word.Location));
return T.location() >= Word.Location; // Comparison OK: same file.
return T.location() < Word.Location; // Comparison OK: same file.
});
// Search for matches after the cursor.
for (const syntax::Token &Tok : llvm::makeArrayRef(I, SpelledTokens.end()))

View File

@ -1197,7 +1197,14 @@ TEST(LocateSymbol, NearbyIdentifier) {
// h^i
)cpp",
};
R"cpp(
// prefer nearest occurrence even if several matched tokens
// have the same value of `floor(log2(<token line> - <word line>))`.
int hello;
int x = hello, y = hello;
int z = [[hello]];
// h^ello
)cpp"};
for (const char *Test : Tests) {
Annotations T(Test);
auto AST = TestTU::withCode(T.code()).build();