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.
|
|
|
|
==============================================================================*/
|
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
#include "message_handler.hh"
|
2018-11-27 06:29:28 +00:00
|
|
|
#include "query.hh"
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
namespace ccls {
|
2017-12-06 05:03:38 +00:00
|
|
|
namespace {
|
2018-11-03 20:52:43 +00:00
|
|
|
WorkspaceEdit BuildWorkspaceEdit(DB *db, WorkingFiles *wfiles, SymbolRef sym,
|
|
|
|
const std::string &new_text) {
|
|
|
|
std::unordered_map<int, TextDocumentEdit> path_to_edit;
|
2017-12-23 23:41:09 +00:00
|
|
|
|
2018-02-23 23:27:21 +00:00
|
|
|
EachOccurrence(db, sym, true, [&](Use use) {
|
2018-11-03 20:52:43 +00:00
|
|
|
std::optional<Location> ls_location = GetLsLocation(db, wfiles, use);
|
2017-12-23 23:41:09 +00:00
|
|
|
if (!ls_location)
|
2018-02-21 01:50:48 +00:00
|
|
|
return;
|
2017-12-23 23:41:09 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
int file_id = use.file_id;
|
2018-02-12 04:22:47 +00:00
|
|
|
if (path_to_edit.find(file_id) == path_to_edit.end()) {
|
2018-11-03 20:52:43 +00:00
|
|
|
path_to_edit[file_id] = TextDocumentEdit();
|
2017-12-23 23:41:09 +00:00
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
QueryFile &file = db->files[file_id];
|
2017-12-23 23:41:09 +00:00
|
|
|
if (!file.def)
|
2018-02-21 01:50:48 +00:00
|
|
|
return;
|
2017-12-23 23:41:09 +00:00
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
const std::string &path = file.def->path;
|
2018-11-03 20:52:43 +00:00
|
|
|
path_to_edit[file_id].textDocument.uri = DocumentUri::FromPath(path);
|
2017-12-23 23:41:09 +00:00
|
|
|
|
2018-12-02 00:55:51 +00:00
|
|
|
WorkingFile *wf = wfiles->GetFile(path);
|
|
|
|
if (wf)
|
|
|
|
path_to_edit[file_id].textDocument.version = wf->version;
|
2017-12-23 23:41:09 +00:00
|
|
|
}
|
|
|
|
|
2018-11-04 18:30:18 +00:00
|
|
|
TextEdit &edit = path_to_edit[file_id].edits.emplace_back();
|
2017-12-23 23:41:09 +00:00
|
|
|
edit.range = ls_location->range;
|
|
|
|
edit.newText = new_text;
|
2018-02-21 01:50:48 +00:00
|
|
|
});
|
2017-12-23 23:41:09 +00:00
|
|
|
|
2018-11-03 20:52:43 +00:00
|
|
|
WorkspaceEdit edit;
|
2018-08-09 17:08:14 +00:00
|
|
|
for (const auto &changes : path_to_edit)
|
2017-12-23 23:41:09 +00:00
|
|
|
edit.documentChanges.push_back(changes.second);
|
|
|
|
return edit;
|
|
|
|
}
|
2018-10-28 17:49:31 +00:00
|
|
|
} // namespace
|
2017-12-23 23:41:09 +00:00
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
void MessageHandler::textDocument_rename(RenameParam ¶m, ReplyOnce &reply) {
|
2018-12-02 00:55:51 +00:00
|
|
|
QueryFile *file = FindFile(param.textDocument.uri.GetPath());
|
2018-12-01 06:44:52 +00:00
|
|
|
WorkingFile *wf = file ? wfiles->GetFile(file->def->path) : nullptr;
|
2018-12-02 00:55:51 +00:00
|
|
|
if (!wf) {
|
|
|
|
reply.NotReady(file);
|
2018-10-28 17:49:31 +00:00
|
|
|
return;
|
2018-12-02 00:55:51 +00:00
|
|
|
}
|
2018-10-28 17:49:31 +00:00
|
|
|
|
2018-11-03 20:52:43 +00:00
|
|
|
WorkspaceEdit result;
|
2018-12-01 06:44:52 +00:00
|
|
|
for (SymbolRef sym : FindSymbolsAtLocation(wf, file, param.position)) {
|
2018-10-29 04:21:21 +00:00
|
|
|
result = BuildWorkspaceEdit(db, wfiles, sym, param.newName);
|
2018-10-28 17:49:31 +00:00
|
|
|
break;
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
2018-10-28 17:49:31 +00:00
|
|
|
|
|
|
|
reply(result);
|
|
|
|
}
|
|
|
|
} // namespace ccls
|