ccls/src/messages/textDocument_rename.cc

80 lines
2.5 KiB
C++
Raw Normal View History

2018-08-21 05:27:52 +00:00
/* Copyright 2017-2018 ccls Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "message_handler.hh"
2018-10-29 04:21:21 +00:00
#include "query_utils.hh"
2017-12-06 03:32:33 +00:00
namespace ccls {
namespace {
2018-10-29 04:21:21 +00:00
lsWorkspaceEdit BuildWorkspaceEdit(DB *db, WorkingFiles *wfiles,
2018-08-09 17:08:14 +00:00
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 =
2018-10-29 04:21:21 +00:00
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] = 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-10-29 04:21:21 +00:00
WorkingFile *working_file = wfiles->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;
}
} // namespace
void MessageHandler::textDocument_rename(RenameParam &param, ReplyOnce &reply) {
int file_id;
QueryFile *file = FindFile(reply, param.textDocument.uri.GetPath(), &file_id);
if (!file)
return;
2018-10-29 04:21:21 +00:00
WorkingFile *wfile = wfiles->GetFileByFilename(file->def->path);
lsWorkspaceEdit result;
for (SymbolRef sym : FindSymbolsAtLocation(wfile, 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