From 64f094d3471286f6f6db3d60a44580cf4be7b99e Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Sat, 23 Dec 2017 15:41:09 -0800 Subject: [PATCH] Move BuildWorkspaceEdit to text_document_rename.cc --- src/messages/text_document_rename.cc | 46 ++++++++++++++++++++++++++++ src/query_utils.cc | 45 --------------------------- src/query_utils.h | 5 --- 3 files changed, 46 insertions(+), 50 deletions(-) diff --git a/src/messages/text_document_rename.cc b/src/messages/text_document_rename.cc index 4f41ba1c..9433563c 100644 --- a/src/messages/text_document_rename.cc +++ b/src/messages/text_document_rename.cc @@ -2,6 +2,52 @@ #include "query_utils.h" namespace { + +lsWorkspaceEdit BuildWorkspaceEdit(QueryDatabase* db, + WorkingFiles* working_files, + const std::vector& locations, + const std::string& new_text) { + std::unordered_map path_to_edit; + + for (auto& location : locations) { + optional ls_location = + GetLsLocation(db, working_files, location); + if (!ls_location) + continue; + + if (path_to_edit.find(location.path) == path_to_edit.end()) { + path_to_edit[location.path] = lsTextDocumentEdit(); + + QueryFile& file = db->files[location.path.id]; + if (!file.def) + continue; + + const std::string& path = file.def->path; + path_to_edit[location.path].textDocument.uri = + lsDocumentUri::FromPath(path); + + WorkingFile* working_file = working_files->GetFileByFilename(path); + if (working_file) + path_to_edit[location.path].textDocument.version = + working_file->version; + } + + lsTextEdit edit; + edit.range = ls_location->range; + edit.newText = new_text; + + // vscode complains if we submit overlapping text edits. + auto& edits = path_to_edit[location.path].edits; + 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; +} + struct Ipc_TextDocumentRename : public IpcMessage { struct Params { // The document to format. diff --git a/src/query_utils.cc b/src/query_utils.cc index 33752406..d14be871 100644 --- a/src/query_utils.cc +++ b/src/query_utils.cc @@ -470,51 +470,6 @@ optional GetSymbolInfo(QueryDatabase* db, return nullopt; } -lsWorkspaceEdit BuildWorkspaceEdit(QueryDatabase* db, - WorkingFiles* working_files, - const std::vector& locations, - const std::string& new_text) { - std::unordered_map path_to_edit; - - for (auto& location : locations) { - optional ls_location = - GetLsLocation(db, working_files, location); - if (!ls_location) - continue; - - if (path_to_edit.find(location.path) == path_to_edit.end()) { - path_to_edit[location.path] = lsTextDocumentEdit(); - - QueryFile& file = db->files[location.path.id]; - if (!file.def) - continue; - - const std::string& path = file.def->path; - path_to_edit[location.path].textDocument.uri = - lsDocumentUri::FromPath(path); - - WorkingFile* working_file = working_files->GetFileByFilename(path); - if (working_file) - path_to_edit[location.path].textDocument.version = - working_file->version; - } - - lsTextEdit edit; - edit.range = ls_location->range; - edit.newText = new_text; - - // vscode complains if we submit overlapping text edits. - auto& edits = path_to_edit[location.path].edits; - 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; -} - std::vector FindSymbolsAtLocation(WorkingFile* working_file, QueryFile* file, lsPosition position) { diff --git a/src/query_utils.h b/src/query_utils.h index daa3156c..5dc454f8 100644 --- a/src/query_utils.h +++ b/src/query_utils.h @@ -59,11 +59,6 @@ optional GetSymbolInfo(QueryDatabase* db, SymbolIdx symbol, bool use_short_name); -lsWorkspaceEdit BuildWorkspaceEdit(QueryDatabase* db, - WorkingFiles* working_files, - const std::vector& locations, - const std::string& new_text); - std::vector FindSymbolsAtLocation(WorkingFile* working_file, QueryFile* file, lsPosition position);