ccls/src/message_handler.h

144 lines
4.3 KiB
C
Raw Normal View History

2018-08-21 05:27:52 +00:00
// Copyright 2017-2018 ccls Authors
// SPDX-License-Identifier: Apache-2.0
#pragma once
2018-03-31 05:05:21 +00:00
#include "lru_cache.h"
#include "lsp.h"
2018-03-31 05:05:21 +00:00
#include "match.h"
2018-03-22 05:01:21 +00:00
#include "method.h"
#include "query.h"
2017-12-29 16:29:47 +00:00
#include <memory>
2018-08-09 17:08:14 +00:00
#include <optional>
2018-03-31 05:05:21 +00:00
#include <unordered_map>
2017-12-29 16:29:47 +00:00
#include <vector>
struct ClangCompleteManager;
struct CodeCompleteCache;
struct Config;
class DiagnosticsPublisher;
struct VFS;
2017-12-29 16:29:47 +00:00
struct ImportManager;
struct IncludeComplete;
struct MultiQueueWaiter;
2018-01-06 21:40:33 +00:00
struct Project;
struct DB;
2017-12-29 16:29:47 +00:00
struct WorkingFile;
struct WorkingFiles;
2018-03-31 05:05:21 +00:00
// Caches symbols for a single file for semantic highlighting to provide
// relatively stable ids. Only supports xxx files at a time.
struct SemanticHighlightSymbolCache {
struct Entry {
2018-08-09 17:08:14 +00:00
SemanticHighlightSymbolCache *all_caches_ = nullptr;
2018-03-31 05:05:21 +00:00
// The path this cache belongs to.
std::string path;
// Detailed symbol name to stable id.
using TNameToId = std::unordered_map<std::string, int>;
TNameToId detailed_type_name_to_stable_id;
TNameToId detailed_func_name_to_stable_id;
TNameToId detailed_var_name_to_stable_id;
2018-08-09 17:08:14 +00:00
Entry(SemanticHighlightSymbolCache *all_caches, const std::string &path);
2018-03-31 05:05:21 +00:00
std::optional<int> TryGetStableId(SymbolKind kind,
2018-08-09 17:08:14 +00:00
const std::string &detailed_name);
int GetStableId(SymbolKind kind, const std::string &detailed_name);
2018-03-31 05:05:21 +00:00
2018-08-09 17:08:14 +00:00
TNameToId *GetMapForSymbol_(SymbolKind kind);
2018-03-31 05:05:21 +00:00
};
constexpr static int kCacheSize = 10;
LruCache<std::string, Entry> cache_;
uint32_t next_stable_id_ = 0;
std::unique_ptr<GroupMatch> match_;
SemanticHighlightSymbolCache();
2018-04-04 06:05:41 +00:00
void Init();
2018-08-09 17:08:14 +00:00
std::shared_ptr<Entry> GetCacheForFile(const std::string &path);
2018-03-31 05:05:21 +00:00
};
2018-03-31 03:16:33 +00:00
struct Out_CclsPublishSemanticHighlighting
: public lsOutMessage<Out_CclsPublishSemanticHighlighting> {
struct Symbol {
int stableId = 0;
lsSymbolKind parentKind;
lsSymbolKind kind;
uint8_t storage;
std::vector<std::pair<int, int>> ranges;
// `lsRanges` is used to compute `ranges`.
std::vector<lsRange> lsRanges;
};
struct Params {
lsDocumentUri uri;
std::vector<Symbol> symbols;
};
2018-03-31 03:16:33 +00:00
std::string method = "$ccls/publishSemanticHighlighting";
Params params;
};
MAKE_REFLECT_STRUCT(Out_CclsPublishSemanticHighlighting::Symbol, stableId,
parentKind, kind, storage, ranges, lsRanges);
MAKE_REFLECT_STRUCT(Out_CclsPublishSemanticHighlighting::Params, uri, symbols);
2018-08-09 17:08:14 +00:00
MAKE_REFLECT_STRUCT(Out_CclsPublishSemanticHighlighting, jsonrpc, method,
params);
// Usage:
//
// struct FooHandler : MessageHandler {
// // ...
// };
// REGISTER_MESSAGE_HANDLER(FooHandler);
//
// Then there will be a global FooHandler instance in
// |MessageHandler::message_handlers|.
2018-08-09 17:08:14 +00:00
#define REGISTER_MESSAGE_HANDLER(type) \
static type type##message_handler_instance_;
struct MessageHandler {
2018-08-09 17:08:14 +00:00
DB *db = nullptr;
MultiQueueWaiter *waiter = nullptr;
Project *project = nullptr;
DiagnosticsPublisher *diag_pub = nullptr;
VFS *vfs = nullptr;
ImportManager *import_manager = nullptr;
SemanticHighlightSymbolCache *semantic_cache = nullptr;
WorkingFiles *working_files = nullptr;
ClangCompleteManager *clang_complete = nullptr;
IncludeComplete *include_complete = nullptr;
CodeCompleteCache *global_code_complete_cache = nullptr;
CodeCompleteCache *non_global_code_complete_cache = nullptr;
CodeCompleteCache *signature_cache = nullptr;
virtual MethodType GetMethodType() const = 0;
2018-03-22 05:01:21 +00:00
virtual void Run(std::unique_ptr<InMessage> message) = 0;
2018-08-09 17:08:14 +00:00
static std::vector<MessageHandler *> *message_handlers;
2018-08-09 17:08:14 +00:00
protected:
MessageHandler();
};
2018-08-09 17:08:14 +00:00
template <typename TMessage> struct BaseMessageHandler : MessageHandler {
virtual void Run(TMessage *message) = 0;
// MessageHandler:
2018-03-22 05:01:21 +00:00
void Run(std::unique_ptr<InMessage> message) override {
2018-08-09 17:08:14 +00:00
Run(static_cast<TMessage *>(message.get()));
}
};
2017-12-06 03:32:33 +00:00
2018-08-09 17:08:14 +00:00
bool FindFileOrFail(DB *db, Project *project, std::optional<lsRequestId> id,
const std::string &absolute_path,
QueryFile **out_query_file, int *out_file_id = nullptr);
2017-12-06 03:32:33 +00:00
void EmitSkippedRanges(WorkingFile *working_file,
const std::vector<Range> &skipped_ranges);
2017-12-06 03:32:33 +00:00
2018-08-09 17:08:14 +00:00
void EmitSemanticHighlighting(DB *db,
SemanticHighlightSymbolCache *semantic_cache,
WorkingFile *working_file, QueryFile *file);