2017-12-25 17:04:10 +00:00
|
|
|
#include "clang_format.h"
|
2017-12-25 13:08:36 +00:00
|
|
|
#include "message_handler.h"
|
2017-12-29 21:19:30 +00:00
|
|
|
#include "queue_manager.h"
|
|
|
|
#include "working_files.h"
|
2017-12-25 13:08:36 +00:00
|
|
|
|
2017-12-31 22:15:03 +00:00
|
|
|
#include <loguru.hpp>
|
|
|
|
|
2017-12-25 13:08:36 +00:00
|
|
|
namespace {
|
2018-01-01 04:03:43 +00:00
|
|
|
|
|
|
|
struct lsTextDocumentFormattingParams {
|
|
|
|
lsTextDocumentIdentifier textDocument;
|
|
|
|
lsFormattingOptions options;
|
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(lsTextDocumentFormattingParams, textDocument, options);
|
|
|
|
|
2017-12-25 13:08:36 +00:00
|
|
|
struct Ipc_TextDocumentFormatting
|
|
|
|
: public IpcMessage<Ipc_TextDocumentFormatting> {
|
|
|
|
const static IpcId kIpcId = IpcId::TextDocumentFormatting;
|
|
|
|
|
|
|
|
lsRequestId id;
|
|
|
|
lsTextDocumentFormattingParams params;
|
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Ipc_TextDocumentFormatting, id, params);
|
|
|
|
REGISTER_IPC_MESSAGE(Ipc_TextDocumentFormatting);
|
|
|
|
|
|
|
|
struct Out_TextDocumentFormatting
|
|
|
|
: public lsOutMessage<Out_TextDocumentFormatting> {
|
|
|
|
lsRequestId id;
|
2017-12-25 17:04:10 +00:00
|
|
|
std::vector<lsTextEdit> result;
|
2017-12-25 13:08:36 +00:00
|
|
|
};
|
2017-12-25 17:04:10 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Out_TextDocumentFormatting, jsonrpc, id, result);
|
|
|
|
|
|
|
|
struct TextDocumentFormattingHandler
|
|
|
|
: BaseMessageHandler<Ipc_TextDocumentFormatting> {
|
|
|
|
void Run(Ipc_TextDocumentFormatting* request) override {
|
2017-12-31 22:15:03 +00:00
|
|
|
Out_TextDocumentFormatting response;
|
|
|
|
response.id = request->id;
|
|
|
|
#if USE_CLANG_CXX
|
2017-12-25 17:04:10 +00:00
|
|
|
QueryFile* file;
|
|
|
|
if (!FindFileOrFail(db, project, request->id,
|
|
|
|
request->params.textDocument.uri.GetPath(), &file)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkingFile* working_file =
|
|
|
|
working_files->GetFileByFilename(file->def->path);
|
|
|
|
|
|
|
|
response.result = ConvertClangReplacementsIntoTextEdits(
|
2018-01-01 03:33:28 +00:00
|
|
|
working_file->buffer_content,
|
|
|
|
ClangFormatDocument(working_file, 0,
|
|
|
|
working_file->buffer_content.size(),
|
|
|
|
request->params.options));
|
2017-12-31 22:15:03 +00:00
|
|
|
#else
|
|
|
|
LOG_S(WARNING) << "You must compile cquery with --use-clang-cxx to use "
|
2018-01-01 03:33:28 +00:00
|
|
|
"textDocument/formatting.";
|
2017-12-31 22:15:03 +00:00
|
|
|
// TODO: Fallback to execute the clang-format binary?
|
|
|
|
response.result = {};
|
|
|
|
#endif
|
2017-12-25 17:04:10 +00:00
|
|
|
|
|
|
|
QueueManager::WriteStdout(IpcId::TextDocumentFormatting, response);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_MESSAGE_HANDLER(TextDocumentFormattingHandler);
|
2017-12-25 13:08:36 +00:00
|
|
|
} // namespace
|