diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp --- a/clang-tools-extra/clangd/CodeComplete.cpp +++ b/clang-tools-extra/clangd/CodeComplete.cpp @@ -1123,7 +1123,11 @@ // skip all includes in this case; these completions are really simple. PreambleBounds PreambleRegion = ComputePreambleBounds(*CI->getLangOpts(), *ContentsBuffer, 0); - bool CompletingInPreamble = PreambleRegion.Size > Input.Offset; + // If preamble doesn't end on a new line, include the following byte in + // preamble to enable include completion on a new file. e.g: #include "^ + bool CompletingInPreamble = + PreambleRegion.Size + !PreambleRegion.PreambleEndsAtStartOfLine > + Input.Offset; if (Input.Patch) Input.Patch->apply(*CI); // NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise diff --git a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp --- a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp +++ b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp @@ -2573,7 +2573,7 @@ } TEST(CompletionTest, IncludedCompletionKinds) { - Annotations Test(R"cpp(#include "^")cpp"); + Annotations Test(R"cpp(#include "^)cpp"); auto TU = TestTU::withCode(Test.code()); TU.AdditionalFiles["sub/bar.h"] = ""; TU.ExtraArgs.push_back("-I" + testPath("sub"));