From 28ad5b1ed1ada089be28ef4dbfd4f407ac2c272a Mon Sep 17 00:00:00 2001 From: topisani Date: Thu, 30 Nov 2017 04:47:29 +0100 Subject: [PATCH] Index file language and send it on hover --- src/command_line.cc | 4 +++- src/indexer.cc | 18 ++++++++++++++++++ src/indexer.h | 2 ++ src/language_server_api.h | 20 +++++++++++++++++++- src/query.cc | 1 + src/query.h | 3 +++ src/serializer.cc | 1 + 7 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/command_line.cc b/src/command_line.cc index 0767f9c9..b7b49526 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -2328,7 +2328,9 @@ bool QueryDbMainLoop(Config* config, if (!ls_range) continue; - response.result.contents = GetHoverForSymbol(db, ref.idx); + response.result.contents.value = GetHoverForSymbol(db, ref.idx); + response.result.contents.language = file->def->language; + response.result.range = *ls_range; break; } diff --git a/src/indexer.cc b/src/indexer.cc index 51ba4ffc..2c4099b1 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -995,6 +995,24 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { if (!db) return; + + // update the file language + // TODO: only do this when |is_first_ownership| in ConsumeFile is true + switch (clang_getCursorLanguage(decl->cursor)) { + case CXLanguage_C: + db->language = "c"; + break; + case CXLanguage_CPlusPlus: + db->language = "c++"; + break; + case CXLanguage_ObjC: + db->language = "objc"; + break; + case CXLanguage_Invalid: + db->language = "invalid"; + break; + } + NamespaceHelper* ns = ¶m->ns; // std::cerr << "DECL kind=" << decl->entityInfo->kind << " at " << diff --git a/src/indexer.h b/src/indexer.h index 9c5e8593..efd2c987 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -473,6 +473,8 @@ struct IndexFile { std::string path; std::vector args; int64_t last_modification_time = 0; + // "c++", "c", "obj-c", "invalid" or "unknown" + std::string language; // The path to the translation unit cc file which caused the creation of this // IndexFile. When parsing a translation unit we generate many IndexFile diff --git a/src/language_server_api.h b/src/language_server_api.h index 86c3a893..cc209849 100644 --- a/src/language_server_api.h +++ b/src/language_server_api.h @@ -1179,6 +1179,24 @@ MAKE_REFLECT_STRUCT(lsSignatureHelp, signatures, activeSignature, activeParameter); + +// MarkedString can be used to render human readable text. It is either a markdown string +// or a code-block that provides a language and a code snippet. The language identifier +// is sematically equal to the optional language identifier in fenced code blocks in GitHub +// issues. See https://help.github.com/articles/creating-and-highlighting-code-blocks/#syntax-highlighting +// +// The pair of a language and a value is an equivalent to markdown: +// ```${language} +// ${value} +// ``` +// +// Note that markdown strings will be sanitized - that means html will be escaped. +struct lsMarkedString { + std::string language; + std::string value; +}; +MAKE_REFLECT_STRUCT(lsMarkedString, language, value); + struct Out_TextDocumentSignatureHelp : public lsOutMessage { lsRequestId id; @@ -1228,7 +1246,7 @@ struct Ipc_TextDocumentHover : public IpcMessage { MAKE_REFLECT_STRUCT(Ipc_TextDocumentHover, id, params); struct Out_TextDocumentHover : public lsOutMessage { struct Result { - std::string contents; + lsMarkedString contents; optional range; }; diff --git a/src/query.cc b/src/query.cc index ea8e7e52..76ac9155 100644 --- a/src/query.cc +++ b/src/query.cc @@ -171,6 +171,7 @@ void CompareGroups(std::vector& previous_data, QueryFile::Def BuildFileDef(const IdMap& id_map, const IndexFile& indexed) { QueryFile::Def def; def.path = indexed.path; + def.language = indexed.language; def.includes = indexed.includes; def.inactive_regions = indexed.skipped_by_preprocessor; diff --git a/src/query.h b/src/query.h index 9d23177d..f3ec38ce 100644 --- a/src/query.h +++ b/src/query.h @@ -169,6 +169,8 @@ void Reflect(TVisitor& visitor, MergeableUpdate& value) { struct QueryFile { struct Def { std::string path; + // Language identifier + std::string language; // Includes in the file. std::vector includes; // Outline of the file (ie, for code lens). @@ -191,6 +193,7 @@ struct QueryFile { }; MAKE_REFLECT_STRUCT(QueryFile::Def, path, + language, outline, all_symbols, inactive_regions); diff --git a/src/serializer.cc b/src/serializer.cc index a76bbbd0..34397a98 100644 --- a/src/serializer.cc +++ b/src/serializer.cc @@ -169,6 +169,7 @@ void Reflect(TVisitor& visitor, IndexFile& value) { if (!gTestOutputMode) { REFLECT_MEMBER(version); REFLECT_MEMBER(last_modification_time); + REFLECT_MEMBER(language); REFLECT_MEMBER(import_file); REFLECT_MEMBER(args); }