1 #include "message_handler.h"
2 #include "query_utils.h"
3 #include "queue_manager.h"
4 
5 namespace {
6 MethodType kMethodType = "textDocument/rename";
7 
BuildWorkspaceEdit(QueryDatabase * db,WorkingFiles * working_files,QueryId::SymbolRef sym,const std::string & new_text)8 lsWorkspaceEdit BuildWorkspaceEdit(QueryDatabase* db,
9                                    WorkingFiles* working_files,
10                                    QueryId::SymbolRef sym,
11                                    const std::string& new_text) {
12   std::unordered_map<QueryId::File, lsTextDocumentEdit> path_to_edit;
13 
14   EachOccurrence(db, sym, true, [&](QueryId::LexicalRef ref) {
15     optional<lsLocation> ls_location = GetLsLocation(db, working_files, ref);
16     if (!ls_location)
17       return;
18 
19     QueryId::File file_id = ref.file;
20     if (path_to_edit.find(file_id) == path_to_edit.end()) {
21       path_to_edit[file_id] = lsTextDocumentEdit();
22 
23       QueryFile& file = db->files[file_id.id];
24       if (!file.def)
25         return;
26 
27       const std::string& path = file.def->path;
28       path_to_edit[file_id].textDocument.uri = lsDocumentUri::FromPath(path);
29 
30       WorkingFile* working_file = working_files->GetFileByFilename(path);
31       if (working_file)
32         path_to_edit[file_id].textDocument.version = working_file->version;
33     }
34 
35     lsTextEdit edit;
36     edit.range = ls_location->range;
37     edit.newText = new_text;
38 
39     // vscode complains if we submit overlapping text edits.
40     auto& edits = path_to_edit[file_id].edits;
41     if (std::find(edits.begin(), edits.end(), edit) == edits.end())
42       edits.push_back(edit);
43   });
44 
45   lsWorkspaceEdit edit;
46   for (const auto& changes : path_to_edit)
47     edit.documentChanges.push_back(changes.second);
48   return edit;
49 }
50 
51 struct In_TextDocumentRename : public RequestInMessage {
GetMethodType__anone2f7b2cd0111::In_TextDocumentRename52   MethodType GetMethodType() const override { return kMethodType; }
53   struct Params {
54     // The document to format.
55     lsTextDocumentIdentifier textDocument;
56 
57     // The position at which this request was sent.
58     lsPosition position;
59 
60     // The new name of the symbol. If the given name is not valid the
61     // request must return a [ResponseError](#ResponseError) with an
62     // appropriate message set.
63     std::string newName;
64   };
65   Params params;
66 };
67 MAKE_REFLECT_STRUCT(In_TextDocumentRename::Params,
68                     textDocument,
69                     position,
70                     newName);
71 MAKE_REFLECT_STRUCT(In_TextDocumentRename, id, params);
72 REGISTER_IN_MESSAGE(In_TextDocumentRename);
73 
74 struct Out_TextDocumentRename : public lsOutMessage<Out_TextDocumentRename> {
75   lsRequestId id;
76   lsWorkspaceEdit result;
77 };
78 MAKE_REFLECT_STRUCT(Out_TextDocumentRename, jsonrpc, id, result);
79 
80 struct Handler_TextDocumentRename : BaseMessageHandler<In_TextDocumentRename> {
GetMethodType__anone2f7b2cd0111::Handler_TextDocumentRename81   MethodType GetMethodType() const override { return kMethodType; }
Run__anone2f7b2cd0111::Handler_TextDocumentRename82   void Run(In_TextDocumentRename* request) override {
83     QueryId::File file_id;
84     QueryFile* file;
85     if (!FindFileOrFail(db, project, request->id,
86                         request->params.textDocument.uri.GetAbsolutePath(),
87                         &file, &file_id)) {
88       return;
89     }
90 
91     WorkingFile* working_file =
92         working_files->GetFileByFilename(file->def->path);
93 
94     Out_TextDocumentRename out;
95     out.id = request->id;
96 
97     for (QueryId::SymbolRef sym :
98          FindSymbolsAtLocation(working_file, file, request->params.position)) {
99       // Found symbol. Return references to rename.
100       out.result =
101           BuildWorkspaceEdit(db, working_files, sym, request->params.newName);
102       break;
103     }
104 
105     QueueManager::WriteStdout(kMethodType, out);
106   }
107 };
108 REGISTER_MESSAGE_HANDLER(Handler_TextDocumentRename);
109 }  // namespace
110