diff options
author | Douglas Gregor <dgregor@apple.com> | 2010-08-26 13:48:20 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2010-08-26 13:48:20 +0000 |
commit | 1e5e6684b0f27701e6f7c65f8c6a32a10cbcc3ed (patch) | |
tree | e6ffa9f08a0b7e237c01aef2000cfbbd1f4404fa /tools | |
parent | f29815affe5d28c499d21c0be9bb078c52025ae6 (diff) |
Move the sorting of code-completion results out of the main path and
into the clients, e.g., the printing code-completion consumer and
c-index-test. Clients may want to re-sort the results anyway.
Provide a libclang function that sorts the results.
3rd try. How embarrassing.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@112180 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools')
-rw-r--r-- | tools/c-index-test/c-index-test.c | 26 | ||||
-rw-r--r-- | tools/libclang/CIndexCodeCompletion.cpp | 35 | ||||
-rw-r--r-- | tools/libclang/libclang.darwin.exports | 1 | ||||
-rw-r--r-- | tools/libclang/libclang.exports | 1 |
4 files changed, 62 insertions, 1 deletions
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c index 3f702904a3..67e0c7724f 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -1,6 +1,7 @@ /* c-index-test.c */ #include "clang-c/Index.h" +#include <ctype.h> #include <stdlib.h> #include <stdio.h> #include <string.h> @@ -903,6 +904,25 @@ void print_completion_result(CXCompletionResult *completion_result, fprintf(file, "\n"); } +int my_stricmp(const char *s1, const char *s2) { + while (*s1 && *s2) { + int c1 = tolower(*s1), c2 = tolower(*s2); + if (c1 < c2) + return -1; + else if (c1 > c2) + return 1; + + ++s1; + ++s2; + } + + if (*s1) + return 1; + else if (*s2) + return -1; + return 0; +} + int perform_code_completion(int argc, const char **argv, int timing_only) { const char *input = argv[1]; char *filename = 0; @@ -958,9 +978,13 @@ int perform_code_completion(int argc, const char **argv, int timing_only) { if (results) { unsigned i, n = results->NumResults; - if (!timing_only) + if (!timing_only) { + /* Sort the code-completion results based on the typed text. */ + clang_sortCodeCompletionResults(results->Results, results->NumResults); + for (i = 0; i != n; ++i) print_completion_result(results->Results + i, stdout); + } n = clang_codeCompleteGetNumDiagnostics(results); for (i = 0; i != n; ++i) { CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i); diff --git a/tools/libclang/CIndexCodeCompletion.cpp b/tools/libclang/CIndexCodeCompletion.cpp index 026d0be4ba..825031bd4b 100644 --- a/tools/libclang/CIndexCodeCompletion.cpp +++ b/tools/libclang/CIndexCodeCompletion.cpp @@ -582,6 +582,8 @@ namespace { AllocatedResults.Results[I].CompletionString = StoredCompletion; } } + + // FIXME: Add ProcessOverloadCandidates? }; } @@ -784,3 +786,36 @@ clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn, } // end extern "C" + +namespace { + struct OrderCompletionResults { + bool operator()(const CXCompletionResult &XR, + const CXCompletionResult &YR) const { + CXStoredCodeCompletionString *X + = (CXStoredCodeCompletionString *)XR.CompletionString; + CXStoredCodeCompletionString *Y + = (CXStoredCodeCompletionString *)YR.CompletionString; + + const char *XText = X->getTypedText(); + const char *YText = Y->getTypedText(); + if (!XText || !YText) + return XText != 0; + + int result = llvm::StringRef(XText).compare_lower(YText); + if (result < 0) + return true; + if (result > 0) + return false; + + result = llvm::StringRef(XText).compare(YText); + return result; + } + }; +} + +extern "C" { + void clang_sortCodeCompletionResults(CXCompletionResult *Results, + unsigned NumResults) { + std::stable_sort(Results, Results + NumResults, OrderCompletionResults()); + } +}
\ No newline at end of file diff --git a/tools/libclang/libclang.darwin.exports b/tools/libclang/libclang.darwin.exports index 6a36966501..d779190cc1 100644 --- a/tools/libclang/libclang.darwin.exports +++ b/tools/libclang/libclang.darwin.exports @@ -99,5 +99,6 @@ _clang_parseTranslationUnit _clang_reparseTranslationUnit _clang_saveTranslationUnit _clang_setUseExternalASTGeneration +_clang_sortCodeCompletionResults _clang_tokenize _clang_visitChildren diff --git a/tools/libclang/libclang.exports b/tools/libclang/libclang.exports index 8f1d626957..df93a799f7 100644 --- a/tools/libclang/libclang.exports +++ b/tools/libclang/libclang.exports @@ -99,5 +99,6 @@ clang_parseTranslationUnit clang_reparseTranslationUnit clang_saveTranslationUnit clang_setUseExternalASTGeneration +clang_sortCodeCompletionResults clang_tokenize clang_visitChildren |