ccls/src/messages/textDocument_signatureHelp.cc

185 lines
6.8 KiB
C++
Raw Normal View History

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.
==============================================================================*/
2017-12-29 16:29:47 +00:00
#include "clang_complete.h"
2017-12-06 03:32:33 +00:00
#include "message_handler.h"
2018-05-28 00:50:02 +00:00
#include "pipeline.hh"
using namespace ccls;
2017-12-06 03:32:33 +00:00
#include <stdint.h>
namespace {
MethodType kMethodType = "textDocument/signatureHelp";
2018-03-22 05:01:21 +00:00
struct In_TextDocumentSignatureHelp : public RequestInMessage {
MethodType GetMethodType() const override { return kMethodType; }
2017-12-06 04:39:44 +00:00
lsTextDocumentPositionParams params;
};
MAKE_REFLECT_STRUCT(In_TextDocumentSignatureHelp, id, params);
REGISTER_IN_MESSAGE(In_TextDocumentSignatureHelp);
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 {
// The label of this parameter. Will be shown in
// the UI.
std::string label;
// The human-readable doc-comment of this parameter. Will be shown
// in the UI but can be omitted.
2018-03-31 03:16:33 +00:00
std::optional<std::string> documentation;
2017-12-06 04:39:44 +00:00
};
MAKE_REFLECT_STRUCT(lsParameterInformation, label, documentation);
// 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 {
// The label of this signature. Will be shown in
// the UI.
std::string label;
// The human-readable doc-comment of this signature. Will be shown
// in the UI but can be omitted.
2018-03-31 03:16:33 +00:00
std::optional<std::string> documentation;
2017-12-06 04:39:44 +00:00
// The parameters of this signature.
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 {
// One or more signatures.
2017-12-12 05:20:29 +00:00
std::vector<lsSignatureInformation> signatures;
2017-12-06 04:39:44 +00:00
// The active signature. If omitted or the value lies outside the
// range of `signatures` the value defaults to zero or is ignored if
// `signatures.length === 0`. Whenever possible implementors should
// make an active decision about the active signature and shouldn't
// rely on a default value.
// In future version of the protocol this property might become
// mandantory to better express this.
2018-03-31 03:16:33 +00:00
std::optional<int> activeSignature;
2017-12-06 04:39:44 +00:00
// The active parameter of the active signature. If omitted or the value
// lies outside the range of `signatures[activeSignature].parameters`
// defaults to 0 if the active signature has parameters. If
// the active signature has no parameters it is ignored.
// In future version of the protocol this property might become
// mandantory to better express the active parameter if the
// active signature does have any.
2018-03-31 03:16:33 +00:00
std::optional<int> activeParameter;
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);
struct Out_TextDocumentSignatureHelp
: public lsOutMessage<Out_TextDocumentSignatureHelp> {
lsRequestId id;
lsSignatureHelp result;
};
MAKE_REFLECT_STRUCT(Out_TextDocumentSignatureHelp, jsonrpc, id, result);
struct Handler_TextDocumentSignatureHelp : MessageHandler {
MethodType GetMethodType() const override { return kMethodType; }
2017-12-06 03:32:33 +00:00
2018-03-22 05:01:21 +00:00
void Run(std::unique_ptr<InMessage> message) override {
2018-08-09 17:08:14 +00:00
auto request = static_cast<In_TextDocumentSignatureHelp *>(message.get());
lsTextDocumentPositionParams &params = request->params;
WorkingFile *file =
2017-12-06 03:32:33 +00:00
working_files->GetFileByFilename(params.textDocument.uri.GetPath());
std::string search;
int active_param = 0;
if (file) {
lsPosition completion_position;
search = file->FindClosestCallNameInBuffer(params.position, &active_param,
&completion_position);
params.position = completion_position;
}
if (search.empty())
return;
ClangCompleteManager::OnComplete callback = std::bind(
2018-08-09 17:08:14 +00:00
[this](InMessage *message, std::string search, int active_param,
const std::vector<lsCompletionItem> &results,
2017-12-06 03:32:33 +00:00
bool is_cached_result) {
2018-08-09 17:08:14 +00:00
auto msg = static_cast<In_TextDocumentSignatureHelp *>(message);
2017-12-06 03:32:33 +00:00
Out_TextDocumentSignatureHelp out;
out.id = msg->id;
2018-08-09 17:08:14 +00:00
for (auto &result : results) {
2017-12-06 03:32:33 +00:00
if (result.label != search)
continue;
lsSignatureInformation signature;
signature.label = result.detail;
2018-08-09 17:08:14 +00:00
for (auto &parameter : result.parameters_) {
2017-12-06 03:32:33 +00:00
lsParameterInformation ls_param;
ls_param.label = parameter;
signature.parameters.push_back(ls_param);
}
out.result.signatures.push_back(signature);
}
// Prefer the signature with least parameter count but still larger
// than active_param.
2017-12-06 03:32:33 +00:00
out.result.activeSignature = 0;
if (out.result.signatures.size()) {
size_t num_parameters = SIZE_MAX;
for (size_t i = 0; i < out.result.signatures.size(); ++i) {
size_t t = out.result.signatures[i].parameters.size();
if (active_param < t && t < num_parameters) {
out.result.activeSignature = int(i);
num_parameters = t;
}
2017-12-06 03:32:33 +00:00
}
}
// Set signature to what we parsed from the working file.
out.result.activeParameter = active_param;
2018-05-28 00:50:02 +00:00
pipeline::WriteStdout(kMethodType, out);
2017-12-06 03:32:33 +00:00
if (!is_cached_result) {
signature_cache->WithLock([&]() {
signature_cache->cached_path_ =
msg->params.textDocument.uri.GetPath();
signature_cache->cached_completion_position_ =
msg->params.position;
signature_cache->cached_results_ = results;
});
}
delete message;
},
message.release(), search, active_param, std::placeholders::_1,
std::placeholders::_2);
if (signature_cache->IsCacheValid(params)) {
signature_cache->WithLock([&]() {
callback(signature_cache->cached_results_, true /*is_cached_result*/);
});
} else {
clang_complete->CodeComplete(request->id, params, std::move(callback));
2017-12-06 03:32:33 +00:00
}
}
};
REGISTER_MESSAGE_HANDLER(Handler_TextDocumentSignatureHelp);
2018-08-09 17:08:14 +00:00
} // namespace