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-07-29 04:32:41 +00:00
|
|
|
#include "message_handler.h"
|
|
|
|
#include "pipeline.hh"
|
|
|
|
#include "working_files.h"
|
|
|
|
using namespace ccls;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
MethodType kMethodType = "textDocument/codeAction";
|
|
|
|
|
2019-01-09 07:19:17 +00:00
|
|
|
struct In_TextDocumentCodeAction : public RequestMessage {
|
2018-07-29 04:32:41 +00:00
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
|
|
|
|
|
|
|
// Contains additional diagnostic information about the context in which
|
|
|
|
// a code action is run.
|
|
|
|
struct lsCodeActionContext {
|
|
|
|
// An array of diagnostics.
|
|
|
|
std::vector<lsDiagnostic> diagnostics;
|
|
|
|
};
|
|
|
|
// Params for the CodeActionRequest
|
|
|
|
struct lsCodeActionParams {
|
|
|
|
// The document in which the command was invoked.
|
|
|
|
lsTextDocumentIdentifier textDocument;
|
|
|
|
// The range for which the command was invoked.
|
|
|
|
lsRange range;
|
|
|
|
// Context carrying additional information.
|
|
|
|
lsCodeActionContext context;
|
|
|
|
} params;
|
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(In_TextDocumentCodeAction::lsCodeActionContext,
|
|
|
|
diagnostics);
|
2018-08-09 17:08:14 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_TextDocumentCodeAction::lsCodeActionParams, textDocument,
|
|
|
|
range, context);
|
2018-07-29 04:32:41 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_TextDocumentCodeAction, id, params);
|
|
|
|
REGISTER_IN_MESSAGE(In_TextDocumentCodeAction);
|
|
|
|
|
2018-09-29 08:10:56 +00:00
|
|
|
struct lsCodeAction {
|
|
|
|
std::string title;
|
|
|
|
const char *kind = "quickfix";
|
|
|
|
lsWorkspaceEdit edit;
|
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(lsCodeAction, title, kind, edit);
|
|
|
|
|
2018-07-29 04:32:41 +00:00
|
|
|
struct Handler_TextDocumentCodeAction
|
|
|
|
: BaseMessageHandler<In_TextDocumentCodeAction> {
|
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
void Run(In_TextDocumentCodeAction *request) override {
|
2018-07-29 04:32:41 +00:00
|
|
|
const auto ¶ms = request->params;
|
|
|
|
WorkingFile *wfile =
|
|
|
|
working_files->GetFileByFilename(params.textDocument.uri.GetPath());
|
|
|
|
if (!wfile)
|
|
|
|
return;
|
2019-01-09 07:19:17 +00:00
|
|
|
std::vector<lsCodeAction> result;
|
2018-07-29 04:32:41 +00:00
|
|
|
std::vector<lsDiagnostic> diagnostics;
|
|
|
|
working_files->DoAction([&]() { diagnostics = wfile->diagnostics_; });
|
|
|
|
for (lsDiagnostic &diag : diagnostics)
|
|
|
|
if (diag.fixits_.size()) {
|
2019-01-09 07:19:17 +00:00
|
|
|
lsCodeAction &cmd = result.emplace_back();
|
2018-09-29 08:10:56 +00:00
|
|
|
cmd.title = "FixIt: " + diag.message;
|
|
|
|
auto &edit = cmd.edit.documentChanges.emplace_back();
|
|
|
|
edit.textDocument.uri = params.textDocument.uri;
|
|
|
|
edit.textDocument.version = wfile->version;
|
|
|
|
edit.edits = diag.fixits_;
|
2018-07-29 04:32:41 +00:00
|
|
|
}
|
2019-01-09 07:19:17 +00:00
|
|
|
pipeline::Reply(request->id, result);
|
2018-07-29 04:32:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_MESSAGE_HANDLER(Handler_TextDocumentCodeAction);
|
2018-08-09 17:08:14 +00:00
|
|
|
} // namespace
|