From a7a1147d4f0901841dc0432b1988debc0ce09b31 Mon Sep 17 00:00:00 2001 From: Ilya Biryukov Date: Fri, 7 Jun 2019 16:24:38 +0000 Subject: [PATCH] [clangd] Return empty results on spurious completion triggers Summary: We currently return an error, this causes `coc.nvim` and VSCode to show an error message in the logs. Returning empty list of completions allows to avod the error message without altering other user-visible behavior. Reviewers: sammccall Reviewed By: sammccall Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D62999 llvm-svn: 362811 --- clang-tools-extra/clangd/ClangdLSPServer.cpp | 18 ++++++------------ .../clangd/test/completion-auto-trigger.test | 22 ++++++++++++---------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp index 0c09eaa..0b8f762 100644 --- a/clang-tools-extra/clangd/ClangdLSPServer.cpp +++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp @@ -25,16 +25,6 @@ namespace clang { namespace clangd { namespace { -class IgnoreCompletionError : public llvm::ErrorInfo { -public: - void log(llvm::raw_ostream &OS) const override { - OS << "ignored auto-triggered completion, preceding char did not match"; - } - std::error_code convertToErrorCode() const override { - return std::make_error_code(std::errc::operation_canceled); - } -}; - /// Transforms a tweak into a code action that would apply it if executed. /// EXPECTS: T.prepare() was called and returned true. CodeAction toCodeAction(const ClangdServer::TweakRef &T, const URIForFile &File, @@ -738,8 +728,12 @@ void ClangdLSPServer::onCodeAction(const CodeActionParams &Params, void ClangdLSPServer::onCompletion(const CompletionParams &Params, Callback Reply) { - if (!shouldRunCompletion(Params)) - return Reply(llvm::make_error()); + if (!shouldRunCompletion(Params)) { + // Clients sometimes auto-trigger completions in undesired places (e.g. + // 'a >^ '), we return empty results in those cases. + vlog("ignored auto-triggered completion, preceding char did not match"); + return Reply(CompletionList()); + } Server->codeComplete(Params.textDocument.uri.file(), Params.position, CCOpts, Bind( [this](decltype(Reply) Reply, diff --git a/clang-tools-extra/clangd/test/completion-auto-trigger.test b/clang-tools-extra/clangd/test/completion-auto-trigger.test index 9683028..46871b9 100644 --- a/clang-tools-extra/clangd/test/completion-auto-trigger.test +++ b/clang-tools-extra/clangd/test/completion-auto-trigger.test @@ -4,11 +4,12 @@ {"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"test:///main.cpp","languageId":"cpp","version":1,"text":"namespace ns { int ns_member; } struct vector { int size; static int default_capacity; };\nvoid test(vector *a, vector *b) {\n if (a > b) {} \n a->size = 10;\n\n a ? a : b;\n ns::ns_member = 10;\n}"}}} --- {"jsonrpc":"2.0","id":1,"method":"textDocument/completion","params":{"textDocument":{"uri":"test:///main.cpp"},"position":{"line":2,"character":9},"context":{"triggerKind":2,"triggerCharacter":">"}}} -# CHECK: "error": { -# CHECK-NEXT: "code": -32001, -# CHECK-NEXT: "message": "ignored auto-triggered completion, preceding char did not match" -# CHECK-NEXT: }, -# CHECK-NEXT: "id": 1, +# CHECK: "id": 1, +# CHECK-NEXT: "jsonrpc": "2.0" +# CHECK-NEXT: "result": { +# CHECK-NEXT: "isIncomplete": false, +# CHECK-NEXT: "items": [] +# CHECK-NEXT: } --- {"jsonrpc":"2.0","id":2,"method":"textDocument/completion","params":{"textDocument":{"uri":"test:///main.cpp"},"position":{"line":3,"character":5},"context":{"triggerKind":2,"triggerCharacter":">"}}} # CHECK: "id": 2, @@ -64,11 +65,12 @@ # CHECK-NEXT: } --- {"jsonrpc":"2.0","id":3,"method":"textDocument/completion","params":{"textDocument":{"uri":"test:///main.cpp"},"position":{"line":5,"character":9},"context":{"triggerKind":2,"triggerCharacter":":"}}} -# CHECK: "error": { -# CHECK-NEXT: "code": -32001, -# CHECK-NEXT: "message": "ignored auto-triggered completion, preceding char did not match" -# CHECK-NEXT: }, -# CHECK-NEXT: "id": 3, +# CHECK: "id": 3, +# CHECK-NEXT: "jsonrpc": "2.0", +# CHECK-NEXT: "result": { +# CHECK-NEXT: "isIncomplete": false, +# CHECK-NEXT: "items": [] +# CHECK-NEXT: } --- {"jsonrpc":"2.0","id":4,"method":"textDocument/completion","params":{"textDocument":{"uri":"test:///main.cpp"},"position":{"line":6,"character":6},"context":{"triggerKind":2,"triggerCharacter":":"}}} --- -- 2.7.4