From 93fb74699267ec8bfb52b963923c7515f8c745cb Mon Sep 17 00:00:00 2001 From: Yuxuan Shui Date: Wed, 6 Dec 2017 12:46:24 -0500 Subject: [PATCH] Convert priority to a string properly for sortText Cast the number to a char directly sometimes results in a invalid UTF-8 string, which is not good for some clients. --- src/clang_complete.cc | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/clang_complete.cc b/src/clang_complete.cc index a736277a..560344ef 100644 --- a/src/clang_complete.cc +++ b/src/clang_complete.cc @@ -27,10 +27,12 @@ unsigned Flags() { ; } -int GetCompletionPriority(const CXCompletionString& str, +unsigned GetCompletionPriority(const CXCompletionString& str, CXCursorKind result_kind, const std::string& label) { - int priority = clang_getCompletionPriority(str); + unsigned priority = clang_getCompletionPriority(str); + + // XXX: What happens if priority overflows? if (result_kind == CXCursor_Destructor) { priority *= 100; } @@ -44,6 +46,20 @@ int GetCompletionPriority(const CXCompletionString& str, return priority; } +template +char *tofixedbase64(T input, char *out) { + const char *digits = "./0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; + int len = (sizeof(T)*8-1)/6+1; + for (int i = len-1; i >= 0; i--) { + out[i] = digits[input%64]; + input /= 64; + } + out[len] = '\0'; + return out; +} + /* bool IsCallKind(CXCursorKind kind) { switch (kind) { @@ -431,10 +447,12 @@ void CompletionQueryMain(ClangCompleteManager* completion_manager) { ls_completion_item.documentation = ToString( clang_getCompletionBriefComment(result.CompletionString)); + + char buf[16]; ls_completion_item.sortText = - (const char)uint64_t(GetCompletionPriority( + tofixedbase64(GetCompletionPriority( result.CompletionString, result.CursorKind, - ls_completion_item.label)); + ls_completion_item.label), buf); ls_result.push_back(ls_completion_item); }