ClangCodeModel: Auto-start header file completion on trigger characters

That is, recognize '"', '<' and '/' as activation characters, like the
built-in code model.
Note that we do not actually start the LSP completion procedure for
these unless we really are in an #include directive.

Fixes: QTCREATORBUG-28203
Change-Id: I85727d0e392a27efa8549e7d6fa5f18f953774b4
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2022-10-27 11:11:22 +02:00
parent 4e9eef339c
commit 45317da25a

View File

@@ -145,6 +145,20 @@ IAssistProcessor *ClangdCompletionAssistProvider::createProcessor(
default:
break;
}
if (interface->reason() == ActivationCharacter) {
switch (interface->characterAt(interface->position() - 1).toLatin1()) {
case '"': case '<': case '/':
if (contextAnalyzer.completionAction()
!= ClangCompletionContextAnalyzer::CompleteIncludePath) {
class NoOpProcessor : public IAssistProcessor {
IAssistProposal *perform(const AssistInterface *) override { return nullptr; }
};
return new NoOpProcessor;
}
}
}
const QString snippetsGroup = contextAnalyzer.addSnippets() && !isInCommentOrString(interface)
? CppEditor::Constants::CPP_SNIPPETS_GROUP_ID
: QString();
@@ -166,10 +180,11 @@ bool ClangdCompletionAssistProvider::isActivationCharSequence(const QString &seq
// We want to minimize unneeded completion requests, as those trigger document updates,
// which trigger re-highlighting and diagnostics, which we try to delay.
// Therefore, we do not trigger on syntax elements that often occur in non-applicable
// contexts, such as '(', '<' or '/'.
// Therefore, for '"', '<', and '/', a follow-up check will verify whether we are in
// an include completion context and otherwise not start the LSP completion procedure.
switch (kind) {
case T_DOT: case T_COLON_COLON: case T_ARROW: case T_DOT_STAR: case T_ARROW_STAR: case T_POUND:
case T_STRING_LITERAL: case T_ANGLE_STRING_LITERAL: case T_SLASH:
qCDebug(clangdLogCompletion) << "detected" << sequence << "as activation char sequence";
return true;
}