Skip to content

Commit

Permalink
[clangd] Return empty results on spurious completion triggers
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ilya-biryukov committed Jun 7, 2019
1 parent e490e4a commit a7a1147
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
18 changes: 6 additions & 12 deletions clang-tools-extra/clangd/ClangdLSPServer.cpp
Expand Up @@ -25,16 +25,6 @@
namespace clang {
namespace clangd {
namespace {
class IgnoreCompletionError : public llvm::ErrorInfo<CancelledError> {
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,
Expand Down Expand Up @@ -738,8 +728,12 @@ void ClangdLSPServer::onCodeAction(const CodeActionParams &Params,

void ClangdLSPServer::onCompletion(const CompletionParams &Params,
Callback<CompletionList> Reply) {
if (!shouldRunCompletion(Params))
return Reply(llvm::make_error<IgnoreCompletionError>());
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,
Expand Down
22 changes: 12 additions & 10 deletions clang-tools-extra/clangd/test/completion-auto-trigger.test
Expand Up @@ -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,
Expand Down Expand Up @@ -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":":"}}}
---
Expand Down

0 comments on commit a7a1147

Please sign in to comment.