Skip to content

Commit 60ffbf0

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

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
@@ -156,6 +156,15 @@ struct Config {
156156
ArgumentListsPolicy ArgumentLists = ArgumentListsPolicy::FullPlaceholders;
157157
} Completion;
158158

159+
enum class HeaderInsertionPolicy {
160+
IWYU, // Include what you use
161+
NeverInsert // Never insert headers as part of code completion
162+
};
163+
164+
struct {
165+
HeaderInsertionPolicy Policy = HeaderInsertionPolicy::IWYU;
166+
} HeaderInsertion;
167+
159168
/// Configures hover feature.
160169
struct {
161170
/// 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
@@ -697,6 +697,17 @@ struct FragmentCompiler {
697697
C.Completion.ArgumentLists = *Val;
698698
});
699699
}
700+
if (F.HeaderInsertion) {
701+
if (auto Val =
702+
compileEnum<Config::HeaderInsertionPolicy>("HeaderInsertion",
703+
*F.HeaderInsertion)
704+
.map("IWYU", Config::HeaderInsertionPolicy::IWYU)
705+
.map("Never", Config::HeaderInsertionPolicy::NeverInsert)
706+
.value())
707+
Out.Apply.push_back([Val](const Params &, Config &C) {
708+
C.HeaderInsertion.Policy = *Val;
709+
});
710+
}
700711
}
701712

702713
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
@@ -341,6 +341,14 @@ struct Fragment {
341341
/// Delimiters: empty pair of delimiters "()" or "<>"
342342
/// FullPlaceholders: full name of both type and parameter
343343
std::optional<Located<std::string>> ArgumentLists;
344+
/// Add #include directives when accepting code completions. Config
345+
/// equivalent of the CLI option '--header-insertion'
346+
/// Valid values are enum Config::HeaderInsertionPolicy values:
347+
/// "IWYU": Include what you use. Insert the owning header for top-level
348+
/// symbols, unless the header is already directly included or the
349+
/// symbol is forward-declared
350+
/// "NeverInsert": Never insert headers
351+
std::optional<Located<std::string>> HeaderInsertion;
344352
};
345353
CompletionBlock Completion;
346354

clang-tools-extra/clangd/ConfigYAML.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ class Parser {
245245
if (auto ArgumentLists = scalarValue(N, "ArgumentLists"))
246246
F.ArgumentLists = *ArgumentLists;
247247
});
248+
Dict.handle("HeaderInsertion", [&](Node &N) {
249+
if (auto HeaderInsertion = scalarValue(N, "HeaderInsertion"))
250+
F.HeaderInsertion = *HeaderInsertion;
251+
});
248252
Dict.parse(N);
249253
}
250254

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)