mirror of
https://github.com/MaskRay/ccls.git
synced 2024-11-25 00:55:08 +00:00
19341c18cd
Still some important TODOs to address: - Improve the algorithm that converts between offsets and line/column pairs. Right now it's extremely naive. - Add proper support for a .clang-format file that specifies the coding style.
53 lines
1.8 KiB
C++
53 lines
1.8 KiB
C++
#include "clang_format.h"
|
|
#include "message_handler.h"
|
|
|
|
namespace {
|
|
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;
|
|
std::vector<lsTextEdit> result;
|
|
};
|
|
MAKE_REFLECT_STRUCT(Out_TextDocumentFormatting, jsonrpc, id, result);
|
|
|
|
struct TextDocumentFormattingHandler
|
|
: BaseMessageHandler<Ipc_TextDocumentFormatting> {
|
|
void Run(Ipc_TextDocumentFormatting* request) override {
|
|
QueryFile* file;
|
|
if (!FindFileOrFail(db, project, request->id,
|
|
request->params.textDocument.uri.GetPath(), &file)) {
|
|
return;
|
|
}
|
|
|
|
WorkingFile* working_file =
|
|
working_files->GetFileByFilename(file->def->path);
|
|
|
|
int tab_size = request->params.formattingOptions.tabSize;
|
|
bool insert_spaces = request->params.formattingOptions.insertSpaces;
|
|
|
|
Out_TextDocumentFormatting response;
|
|
response.id = request->id;
|
|
|
|
const auto clang_format = std::unique_ptr<ClangFormat>(new ClangFormat(
|
|
working_file->filename, working_file->buffer_content,
|
|
{clang::tooling::Range(0, working_file->buffer_content.size())},
|
|
tab_size, insert_spaces));
|
|
const auto replacements = clang_format->FormatWholeDocument();
|
|
response.result = ConvertClangReplacementsIntoTextEdits(
|
|
working_file->buffer_content, replacements);
|
|
|
|
QueueManager::WriteStdout(IpcId::TextDocumentFormatting, response);
|
|
}
|
|
};
|
|
REGISTER_MESSAGE_HANDLER(TextDocumentFormattingHandler);
|
|
} // namespace
|