|
3 | 3 |
|
4 | 4 | #include "message_handler.hh"
|
5 | 5 | #include "pipeline.hh"
|
| 6 | +#include "project.hh" |
6 | 7 | #include "query.hh"
|
7 | 8 |
|
| 9 | +#include <llvm/ADT/STLExtras.h> |
| 10 | +#include <llvm/ADT/StringRef.h> |
| 11 | +#include <llvm/Support/Path.h> |
| 12 | + |
8 | 13 | #include <algorithm>
|
9 | 14 |
|
| 15 | +using namespace llvm; |
| 16 | + |
10 | 17 | MAKE_HASHABLE(ccls::SymbolIdx, t.usr, t.kind);
|
11 | 18 |
|
12 | 19 | namespace ccls {
|
@@ -226,4 +233,66 @@ void MessageHandler::textDocument_documentSymbol(JsonReader &reader,
|
226 | 233 | reply(result);
|
227 | 234 | }
|
228 | 235 | }
|
| 236 | + |
| 237 | +void MessageHandler::textDocument_switchSourceHeader(TextDocumentIdentifier ¶m, ReplyOnce &reply) { |
| 238 | + QueryFile *file; |
| 239 | + WorkingFile *wf; |
| 240 | + std::tie(file, wf) = findOrFail(param.uri.getPath(), reply); |
| 241 | + if (!wf) |
| 242 | + return reply(JsonNull{}); |
| 243 | + int file_id = file->id; |
| 244 | + |
| 245 | + DocumentUri result; |
| 246 | + const std::string &path = wf->filename; |
| 247 | + bool is_hdr = lookupExtension(path).second; |
| 248 | + |
| 249 | + // Vote for each interesting symbol's definitions (for header) or declarations (for non-header). |
| 250 | + // Select the file with the most votes. |
| 251 | + // Ignore Type symbols to skip class forward declarations and namespaces. |
| 252 | + std::unordered_map<int, int> file_id2cnt; |
| 253 | + for (auto [sym, refcnt] : file->symbol2refcnt) { |
| 254 | + if (refcnt <= 0 || !sym.extent.valid() || sym.kind == Kind::Type) |
| 255 | + continue; |
| 256 | + |
| 257 | + if (is_hdr) { |
| 258 | + withEntity(db, sym, [&](const auto &entity) { |
| 259 | + for (auto &def : entity.def) |
| 260 | + if (def.spell && def.file_id != file_id) |
| 261 | + ++file_id2cnt[def.file_id]; |
| 262 | + }); |
| 263 | + } else { |
| 264 | + for (DeclRef dr : getNonDefDeclarations(db, sym)) |
| 265 | + if (dr.file_id != file_id) |
| 266 | + ++file_id2cnt[dr.file_id]; |
| 267 | + } |
| 268 | + } |
| 269 | + if (file_id2cnt.size()) { |
| 270 | + auto best = file_id2cnt.begin(); |
| 271 | + for (auto it = file_id2cnt.begin(); it != file_id2cnt.end(); ++it) |
| 272 | + if (it->second > best->second || (it->second == best->second && it->first < best->first)) |
| 273 | + best = it; |
| 274 | + if (auto &def = db->files[best->first].def) |
| 275 | + return reply(DocumentUri::fromPath(def->path)); |
| 276 | + } |
| 277 | + |
| 278 | + if (is_hdr) { |
| 279 | + // Check if `path` is in a #include entry. |
| 280 | + for (QueryFile &file1 : db->files) { |
| 281 | + auto &def = file1.def; |
| 282 | + if (!def || lookupExtension(def->path).second) |
| 283 | + continue; |
| 284 | + for (IndexInclude &include : def->includes) |
| 285 | + if (path == include.resolved_path) |
| 286 | + return reply(DocumentUri::fromPath(def->path)); |
| 287 | + } |
| 288 | + return reply(JsonNull{}); |
| 289 | + } |
| 290 | + |
| 291 | + // Otherwise, find the #include with the same stem. |
| 292 | + StringRef stem = sys::path::stem(path); |
| 293 | + for (IndexInclude &include : file->def->includes) |
| 294 | + if (sys::path::stem(include.resolved_path) == stem) |
| 295 | + return reply(DocumentUri::fromPath(std::string(include.resolved_path))); |
| 296 | + reply(JsonNull{}); |
| 297 | +} |
229 | 298 | } // namespace ccls
|
0 commit comments