Make optional<string> {hover,comments} non-optional

This commit is contained in:
Fangrui Song 2018-01-30 23:48:34 -08:00
parent 77dca1936b
commit 4c895bef0b
8 changed files with 53 additions and 44 deletions

View File

@ -224,12 +224,12 @@ std::string ClangCursor::get_type_description() const {
return ::ToString(clang_getTypeSpelling(type));
}
optional<std::string> 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,

View File

@ -78,7 +78,7 @@ class ClangCursor {
bool is_valid_kind() const;
std::string get_type_description() const;
optional<std::string> get_comments() const;
std::string get_comments() const;
std::string ToString() const;

View File

@ -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)

View File

@ -155,9 +155,8 @@ template <typename TypeId, typename FuncId, typename VarId, typename Range>
struct TypeDefDefinitionData {
// General metadata.
std::string detailed_name;
ClangSymbolKind kind = ClangSymbolKind::Unknown;
optional<std::string> hover;
optional<std::string> 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<TypeId, FuncId, VarId, Range>& 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 <typename TypeId,
struct FuncDefDefinitionData {
// General metadata.
std::string detailed_name;
optional<std::string> hover;
optional<std::string> comments;
std::string hover;
std::string comments;
optional<Range> definition_spelling;
optional<Range> definition_extent;
@ -388,8 +388,8 @@ template <typename TypeId, typename FuncId, typename VarId, typename Range>
struct VarDefDefinitionData {
// General metadata.
std::string detailed_name;
optional<std::string> hover;
optional<std::string> comments;
std::string hover;
std::string comments;
// TODO: definitions should be a list of ranges, since there can be more
// than one - when??
optional<Range> definition_spelling;

View File

@ -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<std::string, lsMarkedString1>;
using lsMarkedString = std::variant<std::string_view, lsMarkedString1>;
MAKE_REFLECT_STRUCT(lsMarkedString1, language, value);
struct lsTextDocumentContentChangeEvent {

View File

@ -4,29 +4,32 @@
namespace {
std::pair<optional<std::string>, std::string> GetCommentsAndHover(
std::pair<std::string_view, std::string_view> 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<optional<std::string>, std::string> GetCommentsAndHover(
break;
}
}
return {nullopt, ""};
return {"", ""};
}
struct Ipc_TextDocumentHover : public RequestMessage<Ipc_TextDocumentHover> {
@ -92,16 +95,16 @@ struct TextDocumentHoverHandler : BaseMessageHandler<Ipc_TextDocumentHover> {
if (!ls_range)
continue;
std::pair<optional<std::string>, std::string> comments_hover =
std::pair<std::string_view, std::string_view> 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;

View File

@ -150,6 +150,20 @@ void Reflect(Writer& visitor, IndexInclude& value) {
REFLECT_MEMBER_END();
}
template <typename Def>
void ReflectHoverAndComments(Reader& visitor, Def& def) {
ReflectMember(visitor, "hover", def.hover);
ReflectMember(visitor, "comments", def.comments);
}
template <typename Def>
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 <typename TVisitor>
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);

View File

@ -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 <typename T>
bool ReflectMemberStart(Reader& visitor, T& value) {
return true;
return false;
}
template <typename T>
bool ReflectMemberStart(Writer& visitor, T& value) {