Skip to content

[clangd] Add HeaderInsertion yaml config option #128503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang-tools-extra/clangd/ClangdServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,8 @@ void ClangdServer::codeComplete(PathRef File, Position Pos,
CodeCompleteOpts.MainFileSignals = IP->Signals;
CodeCompleteOpts.AllScopes = Config::current().Completion.AllScopes;
CodeCompleteOpts.ArgumentLists = Config::current().Completion.ArgumentLists;
CodeCompleteOpts.InsertIncludes =
Config::current().Completion.HeaderInsertion;
// FIXME(ibiryukov): even if Preamble is non-null, we may want to check
// both the old and the new version in case only one of them matches.
CodeCompleteResult Result = clangd::codeComplete(
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clangd/CodeComplete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ struct CompletionCandidate {
std::optional<llvm::StringRef>
headerToInsertIfAllowed(const CodeCompleteOptions &Opts,
CodeCompletionContext::Kind ContextKind) const {
if (Opts.InsertIncludes == CodeCompleteOptions::NeverInsert ||
if (Opts.InsertIncludes == Config::HeaderInsertionPolicy::NeverInsert ||
RankedIncludeHeaders.empty() ||
!contextAllowsHeaderInsertion(ContextKind))
return std::nullopt;
Expand Down
6 changes: 2 additions & 4 deletions clang-tools-extra/clangd/CodeComplete.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,8 @@ struct CodeCompleteOptions {
/// Whether to present doc comments as plain-text or markdown.
MarkupKind DocumentationFormat = MarkupKind::PlainText;

enum IncludeInsertion {
IWYU,
NeverInsert,
} InsertIncludes = IncludeInsertion::IWYU;
Config::HeaderInsertionPolicy InsertIncludes =
Config::HeaderInsertionPolicy::IWYU;

/// Whether include insertions for Objective-C code should use #import instead
/// of #include.
Expand Down
7 changes: 7 additions & 0 deletions clang-tools-extra/clangd/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,20 @@ struct Config {
FullPlaceholders,
};

enum class HeaderInsertionPolicy {
IWYU, // Include what you use
NeverInsert // Never insert headers as part of code completion
};

/// Configures code completion feature.
struct {
/// Whether code completion includes results that are not visible in current
/// scopes.
bool AllScopes = true;
/// controls the completion options for argument lists.
ArgumentListsPolicy ArgumentLists = ArgumentListsPolicy::FullPlaceholders;
/// Controls if headers should be inserted when completions are accepted
HeaderInsertionPolicy HeaderInsertion = HeaderInsertionPolicy::IWYU;
} Completion;

/// Configures hover feature.
Expand Down
11 changes: 11 additions & 0 deletions clang-tools-extra/clangd/ConfigCompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,17 @@ struct FragmentCompiler {
C.Completion.ArgumentLists = *Val;
});
}
if (F.HeaderInsertion) {
if (auto Val =
compileEnum<Config::HeaderInsertionPolicy>("HeaderInsertion",
*F.HeaderInsertion)
.map("IWYU", Config::HeaderInsertionPolicy::IWYU)
.map("Never", Config::HeaderInsertionPolicy::NeverInsert)
.value())
Out.Apply.push_back([Val](const Params &, Config &C) {
C.Completion.HeaderInsertion = *Val;
});
}
}

void compile(Fragment::HoverBlock &&F) {
Expand Down
8 changes: 8 additions & 0 deletions clang-tools-extra/clangd/ConfigFragment.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,14 @@ struct Fragment {
/// Delimiters: empty pair of delimiters "()" or "<>"
/// FullPlaceholders: full name of both type and parameter
std::optional<Located<std::string>> ArgumentLists;
/// Add #include directives when accepting code completions. Config
/// equivalent of the CLI option '--header-insertion'
/// Valid values are enum Config::HeaderInsertionPolicy values:
/// "IWYU": Include what you use. Insert the owning header for top-level
/// symbols, unless the header is already directly included or the
/// symbol is forward-declared
/// "NeverInsert": Never insert headers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like there's a slight discrepancy here; value is documented as NeverInsert here, but config-compiler expects Never. Can you amend one of those?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, my mistake! Do I make a new PR or push a new amend-ed commit to the existing branch?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a new PR/branch is preferred (I am not an expert on how github PRs work though, hence I'd prefer the most straight-forward approach :D)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me neither :D

Will create a new PR with the proposed fix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created a PR at #135921

std::optional<Located<std::string>> HeaderInsertion;
};
CompletionBlock Completion;

Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/clangd/ConfigYAML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ class Parser {
if (auto ArgumentLists = scalarValue(N, "ArgumentLists"))
F.ArgumentLists = *ArgumentLists;
});
Dict.handle("HeaderInsertion", [&](Node &N) {
if (auto HeaderInsertion = scalarValue(N, "HeaderInsertion"))
F.HeaderInsertion = *HeaderInsertion;
});
Dict.parse(N);
}

Expand Down
14 changes: 11 additions & 3 deletions clang-tools-extra/clangd/tool/ClangdMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,19 @@ opt<std::string> EnableFunctionArgSnippets{
init("-1"),
};

opt<CodeCompleteOptions::IncludeInsertion> HeaderInsertion{
opt<Config::HeaderInsertionPolicy> HeaderInsertion{
"header-insertion",
cat(Features),
desc("Add #include directives when accepting code completions"),
init(CodeCompleteOptions().InsertIncludes),
values(
clEnumValN(CodeCompleteOptions::IWYU, "iwyu",
clEnumValN(Config::HeaderInsertionPolicy::IWYU, "iwyu",
"Include what you use. "
"Insert the owning header for top-level symbols, unless the "
"header is already directly included or the symbol is "
"forward-declared"),
clEnumValN(
CodeCompleteOptions::NeverInsert, "never",
Config::HeaderInsertionPolicy::NeverInsert, "never",
"Never insert #include directives as part of code completion")),
};

Expand Down Expand Up @@ -668,6 +668,7 @@ class FlagsConfigProvider : public config::Provider {
std::optional<Config::ExternalIndexSpec> IndexSpec;
std::optional<Config::BackgroundPolicy> BGPolicy;
std::optional<Config::ArgumentListsPolicy> ArgumentLists;
std::optional<Config::HeaderInsertionPolicy> HeaderInsertionPolicy;

// If --compile-commands-dir arg was invoked, check value and override
// default path.
Expand Down Expand Up @@ -712,6 +713,11 @@ class FlagsConfigProvider : public config::Provider {
BGPolicy = Config::BackgroundPolicy::Skip;
}

// If CLI has set never, use that regardless of what the config files have
if (HeaderInsertion == Config::HeaderInsertionPolicy::NeverInsert) {
HeaderInsertionPolicy = Config::HeaderInsertionPolicy::NeverInsert;
}

if (std::optional<bool> Enable = shouldEnableFunctionArgSnippets()) {
ArgumentLists = *Enable ? Config::ArgumentListsPolicy::FullPlaceholders
: Config::ArgumentListsPolicy::Delimiters;
Expand All @@ -726,6 +732,8 @@ class FlagsConfigProvider : public config::Provider {
C.Index.Background = *BGPolicy;
if (ArgumentLists)
C.Completion.ArgumentLists = *ArgumentLists;
if (HeaderInsertionPolicy)
C.Completion.HeaderInsertion = *HeaderInsertionPolicy;
if (AllScopesCompletion.getNumOccurrences())
C.Completion.AllScopes = AllScopesCompletion;

Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ TEST(CompletionTest, IncludeInsertionPreprocessorIntegrationTests) {
ElementsAre(AllOf(named("X"), insertInclude("\"bar.h\""))));
// Can be disabled via option.
CodeCompleteOptions NoInsertion;
NoInsertion.InsertIncludes = CodeCompleteOptions::NeverInsert;
NoInsertion.InsertIncludes = Config::HeaderInsertionPolicy::NeverInsert;
Results = completions(TU, Test.point(), {Sym}, NoInsertion);
EXPECT_THAT(Results.Completions,
ElementsAre(AllOf(named("X"), Not(insertInclude()))));
Expand Down Expand Up @@ -1191,7 +1191,7 @@ TEST(CompletionTest, CommentsOnMembersFromHeaderOverloadBundling) {
int delta(int i);

void epsilon(long l);

/// This one has a comment.
void epsilon(int i);
};
Expand Down