ccls/src/messages/textDocument_rename.cc

61 lines
1.6 KiB
C++
Raw Normal View History

2018-08-21 05:27:52 +00:00
// Copyright 2017-2018 ccls Authors
// SPDX-License-Identifier: Apache-2.0
#include "message_handler.hh"
2018-11-27 06:29:28 +00:00
#include "query.hh"
2017-12-06 03:32:33 +00:00
namespace ccls {
namespace {
WorkspaceEdit BuildWorkspaceEdit(DB *db, WorkingFiles *wfiles, SymbolRef sym,
const std::string &new_text) {
std::unordered_map<int, TextDocumentEdit> path_to_edit;
EachOccurrence(db, sym, true, [&](Use use) {
std::optional<Location> ls_location = GetLsLocation(db, wfiles, 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] = TextDocumentEdit();
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;
path_to_edit[file_id].textDocument.uri = DocumentUri::FromPath(path);
WorkingFile *wf = wfiles->GetFile(path);
if (wf)
path_to_edit[file_id].textDocument.version = wf->version;
}
TextEdit &edit = path_to_edit[file_id].edits.emplace_back();
edit.range = ls_location->range;
edit.newText = new_text;
2018-02-21 01:50:48 +00:00
});
WorkspaceEdit edit;
2018-08-09 17:08:14 +00:00
for (const auto &changes : path_to_edit)
edit.documentChanges.push_back(changes.second);
return edit;
}
} // namespace
void MessageHandler::textDocument_rename(RenameParam &param, ReplyOnce &reply) {
auto [file, wf] = FindOrFail(param.textDocument.uri.GetPath(), reply);
if (!wf) {
return;
}
WorkspaceEdit result;
for (SymbolRef sym : FindSymbolsAtLocation(wf, file, param.position)) {
2018-10-29 04:21:21 +00:00
result = BuildWorkspaceEdit(db, wfiles, sym, param.newName);
break;
2017-12-06 03:32:33 +00:00
}
reply(result);
}
} // namespace ccls