2018-08-21 05:27:52 +00:00
|
|
|
/* Copyright 2017-2018 ccls Authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==============================================================================*/
|
|
|
|
|
2018-09-08 19:07:43 +00:00
|
|
|
#include "clang_complete.hh"
|
2017-12-06 03:32:33 +00:00
|
|
|
#include "message_handler.h"
|
2018-05-28 00:50:02 +00:00
|
|
|
#include "pipeline.hh"
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-09-11 05:37:01 +00:00
|
|
|
#include <clang/Sema/Sema.h>
|
|
|
|
|
2018-09-23 19:10:40 +00:00
|
|
|
using namespace ccls;
|
|
|
|
using namespace clang;
|
2018-01-21 00:17:28 +00:00
|
|
|
|
2017-12-06 05:03:38 +00:00
|
|
|
namespace {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType kMethodType = "textDocument/signatureHelp";
|
|
|
|
|
2017-12-06 04:39:44 +00:00
|
|
|
// Represents a parameter of a callable-signature. A parameter can
|
|
|
|
// have a label and a doc-comment.
|
|
|
|
struct lsParameterInformation {
|
|
|
|
std::string label;
|
2018-09-11 05:37:01 +00:00
|
|
|
// Not available in clang
|
|
|
|
// std::optional<std::string> documentation;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
2018-09-11 05:37:01 +00:00
|
|
|
MAKE_REFLECT_STRUCT(lsParameterInformation, label);
|
2017-12-06 04:39:44 +00:00
|
|
|
|
|
|
|
// Represents the signature of something callable. A signature
|
|
|
|
// can have a label, like a function-name, a doc-comment, and
|
|
|
|
// a set of parameters.
|
|
|
|
struct lsSignatureInformation {
|
|
|
|
std::string label;
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<std::string> documentation;
|
2017-12-06 04:39:44 +00:00
|
|
|
std::vector<lsParameterInformation> parameters;
|
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(lsSignatureInformation, label, documentation, parameters);
|
|
|
|
|
|
|
|
// Signature help represents the signature of something
|
|
|
|
// callable. There can be multiple signature but only one
|
|
|
|
// active and only one active parameter.
|
|
|
|
struct lsSignatureHelp {
|
2017-12-12 05:20:29 +00:00
|
|
|
std::vector<lsSignatureInformation> signatures;
|
2018-09-11 05:37:01 +00:00
|
|
|
int activeSignature = 0;
|
2018-09-13 06:07:47 +00:00
|
|
|
int activeParameter = 0;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
2018-08-09 17:08:14 +00:00
|
|
|
MAKE_REFLECT_STRUCT(lsSignatureHelp, signatures, activeSignature,
|
2017-12-06 04:39:44 +00:00
|
|
|
activeParameter);
|
|
|
|
|
2019-01-09 07:19:17 +00:00
|
|
|
struct In_TextDocumentSignatureHelp : public RequestMessage {
|
2018-09-11 05:37:01 +00:00
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
|
|
|
lsTextDocumentPositionParams params;
|
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(In_TextDocumentSignatureHelp, id, params);
|
|
|
|
REGISTER_IN_MESSAGE(In_TextDocumentSignatureHelp);
|
|
|
|
|
|
|
|
std::string BuildOptional(const CodeCompletionString &CCS,
|
|
|
|
std::vector<lsParameterInformation> &ls_params) {
|
|
|
|
std::string ret;
|
|
|
|
for (const auto &Chunk : CCS) {
|
|
|
|
switch (Chunk.Kind) {
|
|
|
|
case CodeCompletionString::CK_Optional:
|
|
|
|
ret += BuildOptional(*Chunk.Optional, ls_params);
|
|
|
|
break;
|
|
|
|
case CodeCompletionString::CK_Placeholder:
|
|
|
|
// A string that acts as a placeholder for, e.g., a function call
|
|
|
|
// argument.
|
|
|
|
// Intentional fallthrough here.
|
|
|
|
case CodeCompletionString::CK_CurrentParameter: {
|
|
|
|
// A piece of text that describes the parameter that corresponds to
|
|
|
|
// the code-completion location within a function call, message send,
|
|
|
|
// macro invocation, etc.
|
|
|
|
ret += Chunk.Text;
|
|
|
|
ls_params.push_back(lsParameterInformation{Chunk.Text});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CodeCompletionString::CK_VerticalSpace:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret += Chunk.Text;
|
|
|
|
break;
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
2018-09-11 05:37:01 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
class SignatureHelpConsumer : public CodeCompleteConsumer {
|
|
|
|
std::shared_ptr<GlobalCodeCompletionAllocator> Alloc;
|
|
|
|
CodeCompletionTUInfo CCTUInfo;
|
|
|
|
public:
|
|
|
|
bool from_cache;
|
|
|
|
lsSignatureHelp ls_sighelp;
|
|
|
|
SignatureHelpConsumer(const clang::CodeCompleteOptions &CCOpts,
|
|
|
|
bool from_cache)
|
|
|
|
: CodeCompleteConsumer(CCOpts, false),
|
|
|
|
Alloc(std::make_shared<GlobalCodeCompletionAllocator>()),
|
|
|
|
CCTUInfo(Alloc), from_cache(from_cache) {}
|
|
|
|
void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
|
|
|
|
OverloadCandidate *Candidates,
|
|
|
|
unsigned NumCandidates
|
|
|
|
#if LLVM_VERSION_MAJOR >= 8
|
|
|
|
,
|
|
|
|
SourceLocation OpenParLoc
|
|
|
|
#endif
|
|
|
|
) override {
|
|
|
|
ls_sighelp.activeParameter = (int)CurrentArg;
|
|
|
|
for (unsigned i = 0; i < NumCandidates; i++) {
|
|
|
|
OverloadCandidate Cand = Candidates[i];
|
|
|
|
// We want to avoid showing instantiated signatures, because they may be
|
|
|
|
// long in some cases (e.g. when 'T' is substituted with 'std::string', we
|
|
|
|
// would get 'std::basic_string<char>').
|
|
|
|
if (auto *Func = Cand.getFunction())
|
|
|
|
if (auto *Pattern = Func->getTemplateInstantiationPattern())
|
|
|
|
Cand = OverloadCandidate(Pattern);
|
|
|
|
|
|
|
|
const auto *CCS =
|
|
|
|
Cand.CreateSignatureString(CurrentArg, S, *Alloc, CCTUInfo, true);
|
|
|
|
|
|
|
|
const char *ret_type = nullptr;
|
|
|
|
lsSignatureInformation &ls_sig = ls_sighelp.signatures.emplace_back();
|
|
|
|
#if LLVM_VERSION_MAJOR >= 8
|
|
|
|
const RawComment *RC = getCompletionComment(S.getASTContext(), Cand.getFunction());
|
|
|
|
ls_sig.documentation = RC ? RC->getBriefText(S.getASTContext()) : "";
|
|
|
|
#endif
|
|
|
|
for (const auto &Chunk : *CCS)
|
|
|
|
switch (Chunk.Kind) {
|
|
|
|
case CodeCompletionString::CK_ResultType:
|
|
|
|
ret_type = Chunk.Text;
|
|
|
|
break;
|
|
|
|
case CodeCompletionString::CK_Placeholder:
|
|
|
|
case CodeCompletionString::CK_CurrentParameter: {
|
|
|
|
ls_sig.label += Chunk.Text;
|
|
|
|
ls_sig.parameters.push_back(lsParameterInformation{Chunk.Text});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CodeCompletionString::CK_Optional:
|
|
|
|
ls_sig.label += BuildOptional(*Chunk.Optional, ls_sig.parameters);
|
|
|
|
break;
|
|
|
|
case CodeCompletionString::CK_VerticalSpace:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ls_sig.label += Chunk.Text;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ret_type) {
|
|
|
|
ls_sig.label += " -> ";
|
|
|
|
ls_sig.label += ret_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::sort(
|
|
|
|
ls_sighelp.signatures.begin(), ls_sighelp.signatures.end(),
|
|
|
|
[](const lsSignatureInformation &l, const lsSignatureInformation &r) {
|
|
|
|
if (l.parameters.size() != r.parameters.size())
|
|
|
|
return l.parameters.size() < r.parameters.size();
|
|
|
|
if (l.label.size() != r.label.size())
|
|
|
|
return l.label.size() < r.label.size();
|
|
|
|
return l.label < r.label;
|
|
|
|
});
|
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-09-11 05:37:01 +00:00
|
|
|
CodeCompletionAllocator &getAllocator() override { return *Alloc; }
|
|
|
|
CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
|
|
|
|
};
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-09-11 05:37:01 +00:00
|
|
|
struct Handler_TextDocumentSignatureHelp
|
|
|
|
: BaseMessageHandler<In_TextDocumentSignatureHelp> {
|
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-09-11 05:37:01 +00:00
|
|
|
void Run(In_TextDocumentSignatureHelp *request) override {
|
|
|
|
static CompleteConsumerCache<lsSignatureHelp> cache;
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-10-06 22:23:23 +00:00
|
|
|
const auto ¶ms = request->params;
|
2018-09-11 05:37:01 +00:00
|
|
|
std::string path = params.textDocument.uri.GetPath();
|
2018-10-06 22:23:23 +00:00
|
|
|
lsPosition begin_pos = params.position;
|
2018-09-11 05:37:01 +00:00
|
|
|
if (WorkingFile *file = working_files->GetFileByFilename(path)) {
|
|
|
|
std::string completion_text;
|
|
|
|
lsPosition end_pos = params.position;
|
2018-10-06 22:23:23 +00:00
|
|
|
begin_pos = file->FindStableCompletionSource(
|
2018-09-11 05:37:01 +00:00
|
|
|
request->params.position, &completion_text, &end_pos);
|
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-09-11 05:37:01 +00:00
|
|
|
CompletionManager::OnComplete callback =
|
2018-10-06 22:23:23 +00:00
|
|
|
[id = request->id, path, begin_pos](CodeCompleteConsumer *OptConsumer) {
|
2018-09-11 05:37:01 +00:00
|
|
|
if (!OptConsumer)
|
|
|
|
return;
|
|
|
|
auto *Consumer = static_cast<SignatureHelpConsumer *>(OptConsumer);
|
2019-01-09 07:19:17 +00:00
|
|
|
pipeline::Reply(id, Consumer->ls_sighelp);
|
2018-09-11 05:37:01 +00:00
|
|
|
if (!Consumer->from_cache) {
|
|
|
|
cache.WithLock([&]() {
|
|
|
|
cache.path = path;
|
2018-10-06 22:23:23 +00:00
|
|
|
cache.position = begin_pos;
|
2018-09-11 05:37:01 +00:00
|
|
|
cache.result = Consumer->ls_sighelp;
|
2017-12-06 03:32:33 +00:00
|
|
|
});
|
|
|
|
}
|
2018-09-11 05:37:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CodeCompleteOptions CCOpts;
|
|
|
|
CCOpts.IncludeGlobals = false;
|
|
|
|
CCOpts.IncludeMacros = false;
|
|
|
|
CCOpts.IncludeBriefComments = false;
|
2018-10-06 22:23:23 +00:00
|
|
|
if (cache.IsCacheValid(path, begin_pos)) {
|
2018-09-11 05:37:01 +00:00
|
|
|
SignatureHelpConsumer Consumer(CCOpts, true);
|
|
|
|
cache.WithLock([&]() { Consumer.ls_sighelp = cache.result; });
|
|
|
|
callback(&Consumer);
|
2017-12-06 03:32:33 +00:00
|
|
|
} else {
|
2018-09-11 05:37:01 +00:00
|
|
|
clang_complete->completion_request_.PushBack(
|
|
|
|
std::make_unique<CompletionManager::CompletionRequest>(
|
|
|
|
request->id, params.textDocument, params.position,
|
|
|
|
std::make_unique<SignatureHelpConsumer>(CCOpts, false), CCOpts,
|
|
|
|
callback));
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-03-22 04:05:25 +00:00
|
|
|
REGISTER_MESSAGE_HANDLER(Handler_TextDocumentSignatureHelp);
|
2018-08-09 17:08:14 +00:00
|
|
|
} // namespace
|