From 0d715e7bcfdc8f21dbdf26435341997a804f80ff Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Thu, 1 Feb 2018 12:21:58 -0800 Subject: [PATCH] Handle int8_t int16_t uint8_t uint16_t enum constants Clean up clang_complete --- src/clang_complete.cc | 83 +++++++++++-------------------------------- src/indexer.cc | 9 +++-- 2 files changed, 24 insertions(+), 68 deletions(-) diff --git a/src/clang_complete.cc b/src/clang_complete.cc index a30c137b..b76a0692 100644 --- a/src/clang_complete.cc +++ b/src/clang_complete.cc @@ -160,6 +160,11 @@ void BuildDetailString(CXCompletionString completion_string, std::vector* parameters, bool include_snippets) { int num_chunks = clang_getNumCompletionChunks(completion_string); + auto append = [&](const char* text) { + detail += text; + if (do_insert) + insert += text; + }; for (int i = 0; i < num_chunks; ++i) { CXCompletionChunkKind kind = clang_getCompletionChunkKind(completion_string, i); @@ -224,71 +229,23 @@ void BuildDetailString(CXCompletionString completion_string, break; } - case CXCompletionChunk_LeftParen: - detail += "("; - if (do_insert) - insert += "("; - break; - case CXCompletionChunk_RightParen: - detail += ")"; - if (do_insert) - insert += ")"; - break; - case CXCompletionChunk_LeftBracket: - detail += "["; - if (do_insert) - insert += "["; - break; - case CXCompletionChunk_RightBracket: - detail += "]"; - if (do_insert) - insert += "]"; - break; - case CXCompletionChunk_LeftBrace: - detail += "{"; - if (do_insert) - insert += "{"; - break; - case CXCompletionChunk_RightBrace: - detail += "}"; - if (do_insert) - insert += "}"; - break; - case CXCompletionChunk_LeftAngle: - detail += "<"; - if (do_insert) - insert += "<"; - break; - case CXCompletionChunk_RightAngle: - detail += ">"; - if (do_insert) - insert += ">"; - break; - case CXCompletionChunk_Comma: - detail += ", "; - if (do_insert) - insert += ", "; - break; - case CXCompletionChunk_Colon: - detail += ":"; - if (do_insert) - insert += ":"; - break; - case CXCompletionChunk_SemiColon: - detail += ";"; - if (do_insert) - insert += ";"; - break; - case CXCompletionChunk_Equal: - detail += "="; - if (do_insert) - insert += "="; - break; + // clang-format off + case CXCompletionChunk_LeftParen: append("("); break; + case CXCompletionChunk_RightParen: append(")"); break; + case CXCompletionChunk_LeftBracket: append("["); break; + case CXCompletionChunk_RightBracket: append("]"); break; + case CXCompletionChunk_LeftBrace: append("{"); break; + case CXCompletionChunk_RightBrace: append("}"); break; + case CXCompletionChunk_LeftAngle: append("<"); break; + case CXCompletionChunk_RightAngle: append(">"); break; + case CXCompletionChunk_Comma: append(", "); break; + case CXCompletionChunk_Colon: append(":"); break; + case CXCompletionChunk_SemiColon: append(";"); break; + case CXCompletionChunk_Equal: append("="); break; + // clang-format on case CXCompletionChunk_HorizontalSpace: case CXCompletionChunk_VerticalSpace: - detail += " "; - if (do_insert) - insert += " "; + append(" "); break; } } diff --git a/src/indexer.cc b/src/indexer.cc index e328af49..ab865bb3 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -490,13 +490,12 @@ void SetVarDetail(IndexVar* var, CXType enum_type = clang_getCanonicalType( clang_getEnumDeclIntegerType(semanticContainer->cursor)); std::string hover = qualified_name + " = "; - if (enum_type.kind == CXType_Int || enum_type.kind == CXType_Long || - enum_type.kind == CXType_LongLong) - hover += std::to_string(clang_getEnumConstantDeclValue(cursor.cx_cursor)); - else if (enum_type.kind == CXType_UInt || enum_type.kind == CXType_ULong || - enum_type.kind == CXType_ULongLong) + if (enum_type.kind == CXType_UInt || enum_type.kind == CXType_ULong || + enum_type.kind == CXType_ULongLong) hover += std::to_string( clang_getEnumConstantDeclUnsignedValue(cursor.cx_cursor)); + else + hover += std::to_string(clang_getEnumConstantDeclValue(cursor.cx_cursor)); def.detailed_name = std::move(qualified_name); def.hover = hover; } else {