From 4c895bef0b64679490c7ee3274eb328088481508 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Tue, 30 Jan 2018 23:48:34 -0800 Subject: [PATCH] Make optional {hover,comments} non-optional --- src/clang_cursor.cc | 6 +++--- src/clang_cursor.h | 2 +- src/indexer.cc | 4 ++-- src/indexer.h | 16 +++++++-------- src/language_server_api.h | 6 +++--- src/messages/text_document_hover.cc | 31 ++++++++++++++++------------- src/serializer.cc | 23 +++++++++++++++------ src/serializer.h | 9 ++------- 8 files changed, 53 insertions(+), 44 deletions(-) diff --git a/src/clang_cursor.cc b/src/clang_cursor.cc index aa5e965c..8bde2949 100644 --- a/src/clang_cursor.cc +++ b/src/clang_cursor.cc @@ -224,12 +224,12 @@ std::string ClangCursor::get_type_description() const { return ::ToString(clang_getTypeSpelling(type)); } -optional ClangCursor::get_comments() const { +std::string ClangCursor::get_comments() const { if (!g_enable_comments) - return nullopt; + return ""; CXSourceRange range = clang_Cursor_getCommentRange(cx_cursor); if (clang_Range_isNull(range)) - return nullopt; + return ""; unsigned start_column; clang_getSpellingLocation(clang_getRangeStart(range), nullptr, nullptr, diff --git a/src/clang_cursor.h b/src/clang_cursor.h index 6ff9e878..9f36d2a2 100644 --- a/src/clang_cursor.h +++ b/src/clang_cursor.h @@ -78,7 +78,7 @@ class ClangCursor { bool is_valid_kind() const; std::string get_type_description() const; - optional get_comments() const; + std::string get_comments() const; std::string ToString() const; diff --git a/src/indexer.cc b/src/indexer.cc index 1d0e72c9..8edb14ba 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -565,8 +565,8 @@ void OnIndexReference_Function(IndexFile* db, } // namespace // static -const int IndexFile::kMajorVersion = 10; -const int IndexFile::kMinorVersion = 1; +const int IndexFile::kMajorVersion = 11; +const int IndexFile::kMinorVersion = 0; IndexFile::IndexFile(const std::string& path, const std::string& contents) diff --git a/src/indexer.h b/src/indexer.h index 83a19a5c..b374ab25 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -155,9 +155,8 @@ template struct TypeDefDefinitionData { // General metadata. std::string detailed_name; - ClangSymbolKind kind = ClangSymbolKind::Unknown; - optional hover; - optional comments; + std::string hover; + std::string comments; // While a class/type can technically have a separate declaration/definition, // it doesn't really happen in practice. The declaration never contains @@ -185,10 +184,11 @@ struct TypeDefDefinitionData { int16_t short_name_offset = 0; int16_t short_name_size = 0; + ClangSymbolKind kind = ClangSymbolKind::Unknown; bool operator==( const TypeDefDefinitionData& other) const { - return detailed_name == other.detailed_name && hover == other.hover && + return detailed_name == other.detailed_name && definition_spelling == other.definition_spelling && definition_extent == other.definition_extent && alias_of == other.alias_of && parents == other.parents && @@ -264,8 +264,8 @@ template hover; - optional comments; + std::string hover; + std::string comments; optional definition_spelling; optional definition_extent; @@ -388,8 +388,8 @@ template struct VarDefDefinitionData { // General metadata. std::string detailed_name; - optional hover; - optional comments; + std::string hover; + std::string comments; // TODO: definitions should be a list of ranges, since there can be more // than one - when?? optional definition_spelling; diff --git a/src/language_server_api.h b/src/language_server_api.h index 6eb5046f..f27b99a5 100644 --- a/src/language_server_api.h +++ b/src/language_server_api.h @@ -595,10 +595,10 @@ MAKE_REFLECT_STRUCT(Out_TextDocumentPublishDiagnostics::Params, // Note that markdown strings will be sanitized - that means html will be // escaped. struct lsMarkedString1 { - std::string language; - std::string value; + std::string_view language; + std::string_view value; }; -using lsMarkedString = std::variant; +using lsMarkedString = std::variant; MAKE_REFLECT_STRUCT(lsMarkedString1, language, value); struct lsTextDocumentContentChangeEvent { diff --git a/src/messages/text_document_hover.cc b/src/messages/text_document_hover.cc index 6280b68c..02b65b45 100644 --- a/src/messages/text_document_hover.cc +++ b/src/messages/text_document_hover.cc @@ -4,29 +4,32 @@ namespace { -std::pair, std::string> GetCommentsAndHover( +std::pair GetCommentsAndHover( QueryDatabase* db, const SymbolIdx& symbol) { switch (symbol.kind) { case SymbolKind::Type: { QueryType& type = db->types[symbol.idx]; if (type.def) - return {type.def->comments, - type.def->hover.value_or(type.def->detailed_name)}; + return {type.def->comments, type.def->hover.size() + ? type.def->hover + : type.def->detailed_name}; break; } case SymbolKind::Func: { QueryFunc& func = db->funcs[symbol.idx]; if (func.def) - return {func.def->comments, - func.def->hover.value_or(func.def->detailed_name)}; + return {func.def->comments, func.def->hover.size() + ? func.def->hover + : func.def->detailed_name}; break; } case SymbolKind::Var: { QueryVar& var = db->vars[symbol.idx]; if (var.def) - return {var.def->comments, - var.def->hover.value_or(var.def->detailed_name)}; + return {var.def->comments, var.def->hover.size() + ? var.def->hover + : var.def->detailed_name}; break; } case SymbolKind::File: @@ -35,7 +38,7 @@ std::pair, std::string> GetCommentsAndHover( break; } } - return {nullopt, ""}; + return {"", ""}; } struct Ipc_TextDocumentHover : public RequestMessage { @@ -92,16 +95,16 @@ struct TextDocumentHoverHandler : BaseMessageHandler { if (!ls_range) continue; - std::pair, std::string> comments_hover = + std::pair comments_hover = GetCommentsAndHover(db, ref.idx); - if (comments_hover.first || comments_hover.second.size()) { + if (comments_hover.first.size() || comments_hover.second.size()) { out.result = Out_TextDocumentHover::Result(); - if (comments_hover.first) { - out.result->contents.emplace_back(*comments_hover.first); + if (comments_hover.first.size()) { + out.result->contents.emplace_back(comments_hover.first); } if (comments_hover.second.size()) { - out.result->contents.emplace_back( - lsMarkedString1{file->def->language, comments_hover.second}); + out.result->contents.emplace_back(lsMarkedString1{ + std::string_view(file->def->language), comments_hover.second}); } out.result->range = *ls_range; break; diff --git a/src/serializer.cc b/src/serializer.cc index 3876ee44..4f5fc1f3 100644 --- a/src/serializer.cc +++ b/src/serializer.cc @@ -150,6 +150,20 @@ void Reflect(Writer& visitor, IndexInclude& value) { REFLECT_MEMBER_END(); } +template +void ReflectHoverAndComments(Reader& visitor, Def& def) { + ReflectMember(visitor, "hover", def.hover); + ReflectMember(visitor, "comments", def.comments); +} + +template +void ReflectHoverAndComments(Writer& visitor, Def& def) { + if (!gTestOutputMode || def.hover.size()) + ReflectMember(visitor, "hover", def.hover); + if (!gTestOutputMode || def.comments.size()) + ReflectMember(visitor, "comments", def.comments); +} + template void Reflect(TVisitor& visitor, IndexType& value) { REFLECT_MEMBER_START(); @@ -159,8 +173,7 @@ void Reflect(TVisitor& visitor, IndexType& value) { REFLECT_MEMBER2("short_name_offset", value.def.short_name_offset); REFLECT_MEMBER2("short_name_size", value.def.short_name_size); REFLECT_MEMBER2("kind", value.def.kind); - REFLECT_MEMBER2("hover", value.def.hover); - REFLECT_MEMBER2("comments", value.def.comments); + ReflectHoverAndComments(visitor, value.def); REFLECT_MEMBER2("definition_spelling", value.def.definition_spelling); REFLECT_MEMBER2("definition_extent", value.def.definition_extent); REFLECT_MEMBER2("alias_of", value.def.alias_of); @@ -184,8 +197,7 @@ void Reflect(TVisitor& visitor, IndexFunc& value) { REFLECT_MEMBER2("short_name_size", value.def.short_name_size); REFLECT_MEMBER2("kind", value.def.kind); REFLECT_MEMBER2("storage", value.def.storage); - REFLECT_MEMBER2("hover", value.def.hover); - REFLECT_MEMBER2("comments", value.def.comments); + ReflectHoverAndComments(visitor, value.def); REFLECT_MEMBER2("declarations", value.declarations); REFLECT_MEMBER2("definition_spelling", value.def.definition_spelling); REFLECT_MEMBER2("definition_extent", value.def.definition_extent); @@ -206,8 +218,7 @@ void Reflect(TVisitor& visitor, IndexVar& value) { REFLECT_MEMBER2("detailed_name", value.def.detailed_name); REFLECT_MEMBER2("short_name_offset", value.def.short_name_offset); REFLECT_MEMBER2("short_name_size", value.def.short_name_size); - REFLECT_MEMBER2("hover", value.def.hover); - REFLECT_MEMBER2("comments", value.def.comments); + ReflectHoverAndComments(visitor, value.def); REFLECT_MEMBER2("declarations", value.declarations); REFLECT_MEMBER2("definition_spelling", value.def.definition_spelling); REFLECT_MEMBER2("definition_extent", value.def.definition_extent); diff --git a/src/serializer.h b/src/serializer.h index 7af897ca..51cc2223 100644 --- a/src/serializer.h +++ b/src/serializer.h @@ -68,12 +68,7 @@ class Writer { struct IndexFile; -#define REFLECT_MEMBER_START() \ - if (!ReflectMemberStart(visitor, value)) \ - return -#define REFLECT_MEMBER_START1(value) \ - if (!ReflectMemberStart(visitor, value)) \ - return +#define REFLECT_MEMBER_START() ReflectMemberStart(visitor, value) #define REFLECT_MEMBER_END() ReflectMemberEnd(visitor, value); #define REFLECT_MEMBER_END1(value) ReflectMemberEnd(visitor, value); #define REFLECT_MEMBER(name) ReflectMember(visitor, #name, value.name) @@ -319,7 +314,7 @@ inline void DefaultReflectMemberStart(Reader& visitor) {} template bool ReflectMemberStart(Reader& visitor, T& value) { - return true; + return false; } template bool ReflectMemberStart(Writer& visitor, T& value) {