-
Notifications
You must be signed in to change notification settings - Fork 276
Expand file tree
/
Copy pathmessage_handler.cc
More file actions
249 lines (223 loc) · 9.58 KB
/
Copy pathmessage_handler.cc
File metadata and controls
249 lines (223 loc) · 9.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2017-2018 ccls Authors
// SPDX-License-Identifier: Apache-2.0
#include "message_handler.hh"
#include "log.hh"
#include "pipeline.hh"
#include "project.hh"
#include "query.hh"
#include <rapidjson/document.h>
#include <rapidjson/reader.h>
#include <algorithm>
#include <stdexcept>
using namespace clang;
MAKE_HASHABLE(ccls::SymbolIdx, t.usr, t.kind);
namespace ccls {
REFLECT_STRUCT(CodeActionParam::Context, diagnostics);
REFLECT_STRUCT(CodeActionParam, textDocument, range, context);
void reflect(JsonReader &, EmptyParam &) {}
REFLECT_STRUCT(TextDocumentParam, textDocument);
REFLECT_STRUCT(DidOpenTextDocumentParam, textDocument);
REFLECT_STRUCT(TextDocumentContentChangeEvent, range, rangeLength, text);
REFLECT_STRUCT(TextDocumentDidChangeParam, textDocument, contentChanges);
REFLECT_STRUCT(TextDocumentPositionParam, textDocument, position);
REFLECT_STRUCT(RenameParam, textDocument, position, newName);
// completion
REFLECT_UNDERLYING(CompletionTriggerKind);
REFLECT_STRUCT(CompletionContext, triggerKind, triggerCharacter);
REFLECT_STRUCT(CompletionParam, textDocument, position, context);
// formatting
REFLECT_STRUCT(FormattingOptions, tabSize, insertSpaces);
REFLECT_STRUCT(DocumentFormattingParam, textDocument, options);
REFLECT_STRUCT(DocumentOnTypeFormattingParam, textDocument, position, ch,
options);
REFLECT_STRUCT(DocumentRangeFormattingParam, textDocument, range, options);
// workspace
REFLECT_UNDERLYING(FileChangeType);
REFLECT_STRUCT(DidChangeWatchedFilesParam::Event, uri, type);
REFLECT_STRUCT(DidChangeWatchedFilesParam, changes);
REFLECT_STRUCT(DidChangeWorkspaceFoldersParam::Event, added, removed);
REFLECT_STRUCT(DidChangeWorkspaceFoldersParam, event);
REFLECT_STRUCT(WorkspaceSymbolParam, query, folders);
namespace {
struct CclsSetSkippedRanges {
DocumentUri uri;
std::vector<lsRange> skippedRanges;
};
REFLECT_STRUCT(CclsSetSkippedRanges, uri, skippedRanges);
} // namespace
void ReplyOnce::notOpened(std::string_view path) {
error(ErrorCode::InvalidRequest, std::string(path) + " is not opened");
}
void ReplyOnce::replyLocationLink(std::vector<LocationLink> &result) {
std::sort(result.begin(), result.end());
result.erase(std::unique(result.begin(), result.end()), result.end());
if (result.size() > g_config->xref.maxNum)
result.resize(g_config->xref.maxNum);
if (g_config->client.linkSupport) {
(*this)(result);
} else {
(*this)(std::vector<Location>(std::make_move_iterator(result.begin()),
std::make_move_iterator(result.end())));
}
}
void MessageHandler::bind(const char *method,
void (MessageHandler::*handler)(JsonReader &)) {
method2notification[method] = [this, handler](JsonReader &reader) {
(this->*handler)(reader);
};
}
template <typename Param>
void MessageHandler::bind(const char *method,
void (MessageHandler::*handler)(Param &)) {
method2notification[method] = [this, handler](JsonReader &reader) {
Param param{};
reflect(reader, param);
(this->*handler)(param);
};
}
void MessageHandler::bind(const char *method,
void (MessageHandler::*handler)(JsonReader &,
ReplyOnce &)) {
method2request[method] = [this, handler](JsonReader &reader,
ReplyOnce &reply) {
(this->*handler)(reader, reply);
};
}
template <typename Param>
void MessageHandler::bind(const char *method,
void (MessageHandler::*handler)(Param &,
ReplyOnce &)) {
method2request[method] = [this, handler](JsonReader &reader,
ReplyOnce &reply) {
Param param{};
reflect(reader, param);
(this->*handler)(param, reply);
};
}
MessageHandler::MessageHandler() {
// clang-format off
bind("$ccls/call", &MessageHandler::ccls_call);
bind("$ccls/fileInfo", &MessageHandler::ccls_fileInfo);
bind("$ccls/info", &MessageHandler::ccls_info);
bind("$ccls/inheritance", &MessageHandler::ccls_inheritance);
bind("$ccls/member", &MessageHandler::ccls_member);
bind("$ccls/navigate", &MessageHandler::ccls_navigate);
bind("$ccls/reload", &MessageHandler::ccls_reload);
bind("$ccls/vars", &MessageHandler::ccls_vars);
bind("exit", &MessageHandler::exit);
bind("initialize", &MessageHandler::initialize);
bind("initialized", &MessageHandler::initialized);
bind("shutdown", &MessageHandler::shutdown);
bind("textDocument/codeAction", &MessageHandler::textDocument_codeAction);
bind("textDocument/codeLens", &MessageHandler::textDocument_codeLens);
bind("textDocument/completion", &MessageHandler::textDocument_completion);
bind("textDocument/declaration", &MessageHandler::textDocument_declaration);
bind("textDocument/definition", &MessageHandler::textDocument_definition);
bind("textDocument/didChange", &MessageHandler::textDocument_didChange);
bind("textDocument/didClose", &MessageHandler::textDocument_didClose);
bind("textDocument/didOpen", &MessageHandler::textDocument_didOpen);
bind("textDocument/didSave", &MessageHandler::textDocument_didSave);
bind("textDocument/documentHighlight", &MessageHandler::textDocument_documentHighlight);
bind("textDocument/documentLink", &MessageHandler::textDocument_documentLink);
bind("textDocument/documentSymbol", &MessageHandler::textDocument_documentSymbol);
bind("textDocument/foldingRange", &MessageHandler::textDocument_foldingRange);
bind("textDocument/formatting", &MessageHandler::textDocument_formatting);
bind("textDocument/hover", &MessageHandler::textDocument_hover);
bind("textDocument/implementation", &MessageHandler::textDocument_implementation);
bind("textDocument/onTypeFormatting", &MessageHandler::textDocument_onTypeFormatting);
bind("textDocument/rangeFormatting", &MessageHandler::textDocument_rangeFormatting);
bind("textDocument/references", &MessageHandler::textDocument_references);
bind("textDocument/rename", &MessageHandler::textDocument_rename);
bind("textDocument/signatureHelp", &MessageHandler::textDocument_signatureHelp);
bind("textDocument/typeDefinition", &MessageHandler::textDocument_typeDefinition);
bind("textDocument/semanticTokens/full", &MessageHandler::textDocument_semanticTokensFull);
bind("textDocument/semanticTokens/range", &MessageHandler::textDocument_semanticTokensRange);
bind("workspace/didChangeConfiguration", &MessageHandler::workspace_didChangeConfiguration);
bind("workspace/didChangeWatchedFiles", &MessageHandler::workspace_didChangeWatchedFiles);
bind("workspace/didChangeWorkspaceFolders", &MessageHandler::workspace_didChangeWorkspaceFolders);
bind("workspace/executeCommand", &MessageHandler::workspace_executeCommand);
bind("workspace/symbol", &MessageHandler::workspace_symbol);
// clang-format on
}
void MessageHandler::run(InMessage &msg) {
rapidjson::Document &doc = *msg.document;
rapidjson::Value null;
auto it = doc.FindMember("params");
JsonReader reader(it != doc.MemberEnd() ? &it->value : &null);
if (msg.id.valid()) {
ReplyOnce reply{*this, msg.id};
auto it = method2request.find(msg.method);
if (it != method2request.end()) {
try {
it->second(reader, reply);
} catch (std::invalid_argument &ex) {
reply.error(ErrorCode::InvalidParams,
"invalid params of " + msg.method + ": expected " +
ex.what() + " for " + reader.getPath());
} catch (NotIndexed &) {
throw;
} catch (...) {
reply.error(ErrorCode::InternalError,
"failed to process " + msg.method);
}
} else {
reply.error(ErrorCode::MethodNotFound, "unknown request " + msg.method);
}
} else {
auto it = method2notification.find(msg.method);
if (it != method2notification.end())
try {
it->second(reader);
} catch (...) {
ShowMessageParam param{MessageType::Error,
std::string("failed to process ") + msg.method};
pipeline::notify(window_showMessage, param);
}
}
}
QueryFile *MessageHandler::findFile(const std::string &path, int *out_file_id) {
QueryFile *ret = nullptr;
auto it = db->name2file_id.find(lowerPathIfInsensitive(path));
if (it != db->name2file_id.end()) {
QueryFile &file = db->files[it->second];
if (file.def) {
ret = &file;
if (out_file_id)
*out_file_id = it->second;
return ret;
}
}
if (out_file_id)
*out_file_id = -1;
return ret;
}
std::pair<QueryFile *, WorkingFile *>
MessageHandler::findOrFail(const std::string &path, ReplyOnce &reply,
int *out_file_id, bool allow_unopened) {
WorkingFile *wf = wfiles->getFile(path);
if (!wf && !allow_unopened) {
reply.notOpened(path);
return {nullptr, nullptr};
}
QueryFile *file = findFile(path, out_file_id);
if (!file) {
if (!overdue)
throw NotIndexed{path};
reply.error(ErrorCode::InvalidRequest, "not indexed");
return {nullptr, nullptr};
}
return {file, wf};
}
void emitSkippedRanges(WorkingFile *wfile, QueryFile &file) {
CclsSetSkippedRanges params;
params.uri = DocumentUri::fromPath(wfile->filename);
for (Range skipped : file.def->skipped_ranges)
if (auto ls_skipped = getLsRange(wfile, skipped))
params.skippedRanges.push_back(*ls_skipped);
pipeline::notify("$ccls/publishSkippedRanges", params);
}
void emitSemanticHighlightRefresh() {
std::vector<int> emptyParameters{}; // notification with no parameters (empty list)
pipeline::notify("workspace/semanticTokens/refresh", emptyParameters);
}
} // namespace ccls