[clang-tools-extra] 297a9da - [CodeComplete] Don't replace the rest of line in #include completion.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 26 03:16:49 PDT 2020
Author: Haojian Wu
Date: 2020-03-26T11:03:04+01:00
New Revision: 297a9dac43f31cdbc811de5ec63ad20812433f98
URL: https://github.com/llvm/llvm-project/commit/297a9dac43f31cdbc811de5ec63ad20812433f98
DIFF: https://github.com/llvm/llvm-project/commit/297a9dac43f31cdbc811de5ec63ad20812433f98.diff
LOG: [CodeComplete] Don't replace the rest of line in #include completion.
Summary:
The previous behavior was aggressive,
#include "abc/f^/abc.h"
foo/ -> candidate
"f/abc.h" is replaced with "foo/", this patch will preserve the "abc.h".
Reviewers: sammccall
Subscribers: jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D76770
Added:
Modified:
clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
clang/lib/Lex/Lexer.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
index f5c90a4677cb..c691c6d9e61a 100644
--- a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
@@ -1860,6 +1860,7 @@ TEST(CompletionTest, RenderWithFixItNonMerged) {
TEST(CompletionTest, CompletionTokenRange) {
MockFSProvider FS;
MockCompilationDatabase CDB;
+ FS.Files["foo/abc/foo.h"] = "";
ClangdServer Server(CDB, FS, ClangdServer::optsForTest());
constexpr const char *TestCodes[] = {
@@ -1882,7 +1883,14 @@ TEST(CompletionTest, CompletionTokenRange) {
Auxilary x;
x.[[]]^;
}
- )cpp"};
+ )cpp",
+ R"cpp(
+ #include "foo/[[a^/]]foo.h"
+ )cpp",
+ R"cpp(
+ #include "foo/abc/[[fo^o.h"]]
+ )cpp",
+ };
for (const auto &Text : TestCodes) {
Annotations TestCode(Text);
auto Results = completions(Server, TestCode.code(), TestCode.point());
diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp
index a51745697b11..f0d2d4332b37 100644
--- a/clang/lib/Lex/Lexer.cpp
+++ b/clang/lib/Lex/Lexer.cpp
@@ -29,6 +29,7 @@
#include "clang/Basic/TokenKinds.h"
#include "llvm/ADT/None.h"
#include "llvm/ADT/Optional.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/ADT/StringRef.h"
@@ -2092,7 +2093,8 @@ void Lexer::codeCompleteIncludedFile(const char *PathStart,
bool IsAngled) {
// Completion only applies to the filename, after the last slash.
StringRef PartialPath(PathStart, CompletionPoint - PathStart);
- auto Slash = PartialPath.find_last_of(LangOpts.MSVCCompat ? "/\\" : "/");
+ llvm::StringRef SlashChars = LangOpts.MSVCCompat ? "/\\" : "/";
+ auto Slash = PartialPath.find_last_of(SlashChars);
StringRef Dir =
(Slash == StringRef::npos) ? "" : PartialPath.take_front(Slash);
const char *StartOfFilename =
@@ -2100,7 +2102,8 @@ void Lexer::codeCompleteIncludedFile(const char *PathStart,
// Code completion filter range is the filename only, up to completion point.
PP->setCodeCompletionIdentifierInfo(&PP->getIdentifierTable().get(
StringRef(StartOfFilename, CompletionPoint - StartOfFilename)));
- // We should replace the characters up to the closing quote, if any.
+ // We should replace the characters up to the closing quote or closest slash,
+ // if any.
while (CompletionPoint < BufferEnd) {
char Next = *(CompletionPoint + 1);
if (Next == 0 || Next == '\r' || Next == '\n')
@@ -2108,7 +2111,10 @@ void Lexer::codeCompleteIncludedFile(const char *PathStart,
++CompletionPoint;
if (Next == (IsAngled ? '>' : '"'))
break;
+ if (llvm::is_contained(SlashChars, Next))
+ break;
}
+
PP->setCodeCompletionTokenRange(
FileLoc.getLocWithOffset(StartOfFilename - BufferStart),
FileLoc.getLocWithOffset(CompletionPoint - BufferStart));
More information about the cfe-commits
mailing list