From 45317da25a3ff4872c193fc59e6f8576fbb477ed Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Thu, 27 Oct 2022 11:11:22 +0200 Subject: [PATCH] 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: Reviewed-by: David Schulz --- .../clangcodemodel/clangdcompletion.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/plugins/clangcodemodel/clangdcompletion.cpp b/src/plugins/clangcodemodel/clangdcompletion.cpp index 921b3aa19e8..36dbb16009d 100644 --- a/src/plugins/clangcodemodel/clangdcompletion.cpp +++ b/src/plugins/clangcodemodel/clangdcompletion.cpp @@ -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; }