2017-12-06 03:32:33 +00:00
|
|
|
#include "message_handler.h"
|
|
|
|
#include "query_utils.h"
|
2017-12-29 16:29:47 +00:00
|
|
|
#include "queue_manager.h"
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2017-12-06 05:03:38 +00:00
|
|
|
namespace {
|
2017-12-23 23:41:09 +00:00
|
|
|
|
|
|
|
lsWorkspaceEdit BuildWorkspaceEdit(QueryDatabase* db,
|
|
|
|
WorkingFiles* working_files,
|
2018-02-10 20:53:18 +00:00
|
|
|
const std::vector<Use>& uses,
|
2017-12-23 23:41:09 +00:00
|
|
|
const std::string& new_text) {
|
|
|
|
std::unordered_map<QueryFileId, lsTextDocumentEdit> path_to_edit;
|
|
|
|
|
2018-02-10 20:53:18 +00:00
|
|
|
for (Use use : uses) {
|
2017-12-23 23:41:09 +00:00
|
|
|
optional<lsLocation> ls_location =
|
2018-02-10 20:53:18 +00:00
|
|
|
GetLsLocation(db, working_files, use);
|
2017-12-23 23:41:09 +00:00
|
|
|
if (!ls_location)
|
|
|
|
continue;
|
|
|
|
|
2018-02-12 04:22:47 +00:00
|
|
|
QueryFileId file_id = use.file;
|
|
|
|
if (path_to_edit.find(file_id) == path_to_edit.end()) {
|
|
|
|
path_to_edit[file_id] = lsTextDocumentEdit();
|
2017-12-23 23:41:09 +00:00
|
|
|
|
2018-02-12 04:22:47 +00:00
|
|
|
QueryFile& file = db->files[file_id.id];
|
2017-12-23 23:41:09 +00:00
|
|
|
if (!file.def)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const std::string& path = file.def->path;
|
2018-02-12 04:22:47 +00:00
|
|
|
path_to_edit[file_id].textDocument.uri =
|
2017-12-23 23:41:09 +00:00
|
|
|
lsDocumentUri::FromPath(path);
|
|
|
|
|
|
|
|
WorkingFile* working_file = working_files->GetFileByFilename(path);
|
|
|
|
if (working_file)
|
2018-02-12 04:22:47 +00:00
|
|
|
path_to_edit[file_id].textDocument.version =
|
2017-12-23 23:41:09 +00:00
|
|
|
working_file->version;
|
|
|
|
}
|
|
|
|
|
|
|
|
lsTextEdit edit;
|
|
|
|
edit.range = ls_location->range;
|
|
|
|
edit.newText = new_text;
|
|
|
|
|
|
|
|
// vscode complains if we submit overlapping text edits.
|
2018-02-12 04:22:47 +00:00
|
|
|
auto& edits = path_to_edit[file_id].edits;
|
2017-12-23 23:41:09 +00:00
|
|
|
if (std::find(edits.begin(), edits.end(), edit) == edits.end())
|
|
|
|
edits.push_back(edit);
|
|
|
|
}
|
|
|
|
|
|
|
|
lsWorkspaceEdit edit;
|
|
|
|
for (const auto& changes : path_to_edit)
|
|
|
|
edit.documentChanges.push_back(changes.second);
|
|
|
|
return edit;
|
|
|
|
}
|
|
|
|
|
2018-01-19 08:56:09 +00:00
|
|
|
struct Ipc_TextDocumentRename : public RequestMessage<Ipc_TextDocumentRename> {
|
|
|
|
const static IpcId kIpcId = IpcId::TextDocumentRename;
|
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;
|
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Ipc_TextDocumentRename::Params,
|
|
|
|
textDocument,
|
|
|
|
position,
|
|
|
|
newName);
|
|
|
|
MAKE_REFLECT_STRUCT(Ipc_TextDocumentRename, id, params);
|
|
|
|
REGISTER_IPC_MESSAGE(Ipc_TextDocumentRename);
|
|
|
|
|
|
|
|
struct Out_TextDocumentRename : public lsOutMessage<Out_TextDocumentRename> {
|
|
|
|
lsRequestId id;
|
|
|
|
lsWorkspaceEdit result;
|
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Out_TextDocumentRename, jsonrpc, id, result);
|
|
|
|
|
2017-12-06 03:32:33 +00:00
|
|
|
struct TextDocumentRenameHandler : BaseMessageHandler<Ipc_TextDocumentRename> {
|
|
|
|
void Run(Ipc_TextDocumentRename* request) override {
|
|
|
|
QueryFileId file_id;
|
|
|
|
QueryFile* file;
|
2017-12-31 03:18:33 +00:00
|
|
|
if (!FindFileOrFail(db, project, request->id,
|
2017-12-06 03:32:33 +00:00
|
|
|
request->params.textDocument.uri.GetPath(), &file,
|
|
|
|
&file_id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkingFile* working_file =
|
|
|
|
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-09 05:11:35 +00:00
|
|
|
out.result = BuildWorkspaceEdit(db, working_files,
|
2018-02-09 17:42:10 +00:00
|
|
|
GetUsesOfSymbol(db, sym, true),
|
2018-02-09 05:11:35 +00:00
|
|
|
request->params.newName);
|
2017-12-06 03:32:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-12-24 00:25:18 +00:00
|
|
|
QueueManager::WriteStdout(IpcId::TextDocumentRename, out);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_MESSAGE_HANDLER(TextDocumentRenameHandler);
|
2017-12-31 03:18:33 +00:00
|
|
|
} // namespace
|