[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
This commit is contained in:
Haojian Wu 2020-03-25 14:17:23 +01:00
parent 4673699a47
commit 297a9dac43
2 changed files with 17 additions and 3 deletions

View File

@ -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());

View File

@ -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));