ccls/src/messages/textDocument_rename.cc

108 lines
3.3 KiB
C++
Raw Normal View History

2017-12-06 03:32:33 +00:00
#include "message_handler.h"
2018-05-28 00:50:02 +00:00
#include "pipeline.hh"
2018-08-09 17:08:14 +00:00
#include "query_utils.h"
2018-05-28 00:50:02 +00:00
using namespace ccls;
2017-12-06 03:32:33 +00:00
namespace {
MethodType kMethodType = "textDocument/rename";
2018-08-09 17:08:14 +00:00
lsWorkspaceEdit BuildWorkspaceEdit(DB *db, WorkingFiles *working_files,
SymbolRef sym, const std::string &new_text) {
std::unordered_map<int, lsTextDocumentEdit> path_to_edit;
EachOccurrence(db, sym, true, [&](Use use) {
2018-08-09 17:08:14 +00:00
std::optional<lsLocation> ls_location =
GetLsLocation(db, working_files, use);
if (!ls_location)
2018-02-21 01:50:48 +00:00
return;
int file_id = use.file_id;
if (path_to_edit.find(file_id) == path_to_edit.end()) {
path_to_edit[file_id] = lsTextDocumentEdit();
2018-08-09 17:08:14 +00:00
QueryFile &file = db->files[file_id];
if (!file.def)
2018-02-21 01:50:48 +00:00
return;
2018-08-09 17:08:14 +00:00
const std::string &path = file.def->path;
2018-02-21 01:50:48 +00:00
path_to_edit[file_id].textDocument.uri = lsDocumentUri::FromPath(path);
2018-08-09 17:08:14 +00:00
WorkingFile *working_file = working_files->GetFileByFilename(path);
if (working_file)
2018-02-21 01:50:48 +00:00
path_to_edit[file_id].textDocument.version = working_file->version;
}
lsTextEdit edit;
edit.range = ls_location->range;
edit.newText = new_text;
// vscode complains if we submit overlapping text edits.
2018-08-09 17:08:14 +00:00
auto &edits = path_to_edit[file_id].edits;
if (std::find(edits.begin(), edits.end(), edit) == edits.end())
edits.push_back(edit);
2018-02-21 01:50:48 +00:00
});
lsWorkspaceEdit edit;
2018-08-09 17:08:14 +00:00
for (const auto &changes : path_to_edit)
edit.documentChanges.push_back(changes.second);
return edit;
}
2018-03-22 05:01:21 +00:00
struct In_TextDocumentRename : public RequestInMessage {
MethodType GetMethodType() const override { return kMethodType; }
2017-12-06 04:39:44 +00:00
struct Params {
// The document to format.
lsTextDocumentIdentifier textDocument;
// The position at which this request was sent.
lsPosition position;
// The new name of the symbol. If the given name is not valid the
// request must return a [ResponseError](#ResponseError) with an
// appropriate message set.
std::string newName;
};
Params params;
};
2018-08-09 17:08:14 +00:00
MAKE_REFLECT_STRUCT(In_TextDocumentRename::Params, textDocument, position,
2017-12-06 04:39:44 +00:00
newName);
MAKE_REFLECT_STRUCT(In_TextDocumentRename, id, params);
REGISTER_IN_MESSAGE(In_TextDocumentRename);
2017-12-06 04:39:44 +00:00
struct Out_TextDocumentRename : public lsOutMessage<Out_TextDocumentRename> {
lsRequestId id;
lsWorkspaceEdit result;
};
MAKE_REFLECT_STRUCT(Out_TextDocumentRename, jsonrpc, id, result);
struct Handler_TextDocumentRename : BaseMessageHandler<In_TextDocumentRename> {
MethodType GetMethodType() const override { return kMethodType; }
2018-08-09 17:08:14 +00:00
void Run(In_TextDocumentRename *request) override {
int file_id;
2018-08-09 17:08:14 +00:00
QueryFile *file;
if (!FindFileOrFail(db, project, request->id,
2017-12-06 03:32:33 +00:00
request->params.textDocument.uri.GetPath(), &file,
&file_id)) {
return;
}
2018-08-09 17:08:14 +00:00
WorkingFile *working_file =
2017-12-06 03:32:33 +00:00
working_files->GetFileByFilename(file->def->path);
Out_TextDocumentRename out;
out.id = request->id;
2018-02-10 06:51:58 +00:00
for (SymbolRef sym :
2017-12-06 03:32:33 +00:00
FindSymbolsAtLocation(working_file, file, request->params.position)) {
// Found symbol. Return references to rename.
2018-02-21 01:50:48 +00:00
out.result =
BuildWorkspaceEdit(db, working_files, sym, request->params.newName);
2017-12-06 03:32:33 +00:00
break;
}
2018-05-28 00:50:02 +00:00
pipeline::WriteStdout(kMethodType, out);
2017-12-06 03:32:33 +00:00
}
};
REGISTER_MESSAGE_HANDLER(Handler_TextDocumentRename);
2018-08-09 17:08:14 +00:00
} // namespace