Skip to content

Commit 8a0714c

Browse files
committed
[clangd] Add HeaderInsertion yaml config option
This is the yaml config equivalent of `--header-insertion` CLI option
1 parent aca8a5c commit 8a0714c

File tree

9 files changed

+41
-8
lines changed

9 files changed

+41
-8
lines changed

clang-tools-extra/clangd/ClangdServer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ void ClangdServer::codeComplete(PathRef File, Position Pos,
455455
CodeCompleteOpts.MainFileSignals = IP->Signals;
456456
CodeCompleteOpts.AllScopes = Config::current().Completion.AllScopes;
457457
CodeCompleteOpts.ArgumentLists = Config::current().Completion.ArgumentLists;
458+
CodeCompleteOpts.InsertIncludes = Config::current().HeaderInsertion.Policy;
458459
// FIXME(ibiryukov): even if Preamble is non-null, we may want to check
459460
// both the old and the new version in case only one of them matches.
460461
CodeCompleteResult Result = clangd::codeComplete(

clang-tools-extra/clangd/CodeComplete.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@ struct CompletionCandidate {
294294
std::optional<llvm::StringRef>
295295
headerToInsertIfAllowed(const CodeCompleteOptions &Opts,
296296
CodeCompletionContext::Kind ContextKind) const {
297-
if (Opts.InsertIncludes == CodeCompleteOptions::NeverInsert ||
297+
if (Opts.InsertIncludes ==
298+
CodeCompleteOptions::IncludeInsertion::NeverInsert ||
298299
RankedIncludeHeaders.empty() ||
299300
!contextAllowsHeaderInsertion(ContextKind))
300301
return std::nullopt;

clang-tools-extra/clangd/CodeComplete.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,8 @@ struct CodeCompleteOptions {
7171
/// Whether to present doc comments as plain-text or markdown.
7272
MarkupKind DocumentationFormat = MarkupKind::PlainText;
7373

74-
enum IncludeInsertion {
75-
IWYU,
76-
NeverInsert,
77-
} InsertIncludes = IncludeInsertion::IWYU;
74+
using IncludeInsertion = Config::HeaderInsertionPolicy;
75+
Config::HeaderInsertionPolicy InsertIncludes = IncludeInsertion::IWYU;
7876

7977
/// Whether include insertions for Objective-C code should use #import instead
8078
/// of #include.

clang-tools-extra/clangd/Config.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ struct Config {
151151
ArgumentListsPolicy ArgumentLists = ArgumentListsPolicy::FullPlaceholders;
152152
} Completion;
153153

154+
enum class HeaderInsertionPolicy {
155+
IWYU, // Include what you use
156+
NeverInsert // Never insert headers as part of code completion
157+
};
158+
159+
struct {
160+
HeaderInsertionPolicy Policy = HeaderInsertionPolicy::IWYU;
161+
} HeaderInsertion;
162+
154163
/// Configures hover feature.
155164
struct {
156165
/// Whether hover show a.k.a type.

clang-tools-extra/clangd/ConfigCompile.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,17 @@ struct FragmentCompiler {
685685
C.Completion.ArgumentLists = *Val;
686686
});
687687
}
688+
if (F.HeaderInsertion) {
689+
if (auto Val =
690+
compileEnum<Config::HeaderInsertionPolicy>("HeaderInsertion",
691+
*F.HeaderInsertion)
692+
.map("IWYU", Config::HeaderInsertionPolicy::IWYU)
693+
.map("Never", Config::HeaderInsertionPolicy::NeverInsert)
694+
.value())
695+
Out.Apply.push_back([Val](const Params &, Config &C) {
696+
C.HeaderInsertion.Policy = *Val;
697+
});
698+
}
688699
}
689700

690701
void compile(Fragment::HoverBlock &&F) {

clang-tools-extra/clangd/ConfigFragment.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,14 @@ struct Fragment {
333333
/// Delimiters: empty pair of delimiters "()" or "<>"
334334
/// FullPlaceholders: full name of both type and parameter
335335
std::optional<Located<std::string>> ArgumentLists;
336+
/// Add #include directives when accepting code completions. Config
337+
/// equivalent of the CLI option '--header-insertion'
338+
/// Valid values are enum Config::HeaderInsertionPolicy values:
339+
/// "IWYU": Include what you use. Insert the owning header for top-level
340+
/// symbols, unless the header is already directly included or the
341+
/// symbol is forward-declared
342+
/// "NeverInsert": Never insert headers
343+
std::optional<Located<std::string>> HeaderInsertion;
336344
};
337345
CompletionBlock Completion;
338346

clang-tools-extra/clangd/ConfigYAML.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ class Parser {
241241
if (auto ArgumentLists = scalarValue(N, "ArgumentLists"))
242242
F.ArgumentLists = *ArgumentLists;
243243
});
244+
Dict.handle("HeaderInsertion", [&](Node &N) {
245+
if (auto HeaderInsertion = scalarValue(N, "HeaderInsertion"))
246+
F.HeaderInsertion = *HeaderInsertion;
247+
});
244248
Dict.parse(N);
245249
}
246250

clang-tools-extra/clangd/tool/ClangdMain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,13 @@ opt<CodeCompleteOptions::IncludeInsertion> HeaderInsertion{
257257
desc("Add #include directives when accepting code completions"),
258258
init(CodeCompleteOptions().InsertIncludes),
259259
values(
260-
clEnumValN(CodeCompleteOptions::IWYU, "iwyu",
260+
clEnumValN(CodeCompleteOptions::IncludeInsertion::IWYU, "iwyu",
261261
"Include what you use. "
262262
"Insert the owning header for top-level symbols, unless the "
263263
"header is already directly included or the symbol is "
264264
"forward-declared"),
265265
clEnumValN(
266-
CodeCompleteOptions::NeverInsert, "never",
266+
CodeCompleteOptions::IncludeInsertion::NeverInsert, "never",
267267
"Never insert #include directives as part of code completion")),
268268
};
269269

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,8 @@ TEST(CompletionTest, IncludeInsertionPreprocessorIntegrationTests) {
882882
ElementsAre(AllOf(named("X"), insertInclude("\"bar.h\""))));
883883
// Can be disabled via option.
884884
CodeCompleteOptions NoInsertion;
885-
NoInsertion.InsertIncludes = CodeCompleteOptions::NeverInsert;
885+
NoInsertion.InsertIncludes =
886+
CodeCompleteOptions::IncludeInsertion::NeverInsert;
886887
Results = completions(TU, Test.point(), {Sym}, NoInsertion);
887888
EXPECT_THAT(Results.Completions,
888889
ElementsAre(AllOf(named("X"), Not(insertInclude()))));

0 commit comments

Comments
 (0)