From b8e7a5bcb3167849ada5e14c05805e87f94f4d95 Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Wed, 27 Dec 2017 07:53:35 -0800 Subject: [PATCH] Format code --- src/clang_cursor.cc | 11 ++++-- src/indexer.cc | 19 ++++------ src/message_handler.cc | 2 +- src/messages/text_document_completion.cc | 8 ++-- src/messages/workspace_symbol.cc | 48 ++++++++++++++---------- src/query.cc | 9 +++-- src/working_files.cc | 6 +-- 7 files changed, 58 insertions(+), 45 deletions(-) diff --git a/src/clang_cursor.cc b/src/clang_cursor.cc index 908a2902..704566d2 100644 --- a/src/clang_cursor.cc +++ b/src/clang_cursor.cc @@ -198,15 +198,18 @@ optional ClangCursor::get_comments() const { // Get associated comment text. CXString cx_raw = clang_Cursor_getRawCommentText(cx_cursor); - // The first line starts with a comment marker, but the rest needs un-indenting. + // The first line starts with a comment marker, but the rest needs + // un-indenting. std::string unindented; - for (const char* p = clang_getCString(cx_raw); *p; ) { + for (const char* p = clang_getCString(cx_raw); *p;) { auto skip = start_column - 1; for (; skip > 0 && (*p == ' ' || *p == '\t'); p++) skip--; const char* q = p; - while (*q != '\n' && *q) q++; - if (*q) q++; + while (*q != '\n' && *q) + q++; + if (*q) + q++; unindented.insert(unindented.end(), p, q); p = q; } diff --git a/src/indexer.cc b/src/indexer.cc index c497aec8..c5436ac5 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -402,7 +402,7 @@ bool IsFunctionCallContext(CXCursorKind kind) { case CXCursor_ConversionFunction: case CXCursor_FunctionTemplate: case CXCursor_OverloadedDeclRef: - // TODO: we need to test lambdas + // TODO: we need to test lambdas case CXCursor_LambdaExpr: return true; @@ -1040,7 +1040,7 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor, default: if (!IsFunctionCallContext(cursor.get_kind())) cursor.VisitChildren(&TemplateVisitor, data); - /* fallthrough */ + /* fallthrough */ // TODO Add other containers not covered by IsFunctionCallContext case CXCursor_ClassTemplate: break; @@ -1058,10 +1058,8 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor, IndexFunc* called = data->db->Resolve(called_id); OnIndexReference_Function(data->db, ResolveSpelling(cursor.cx_cursor), - data->container, - called_id, - called, - /*implicit=*/ false); + data->container, called_id, called, + /*implicit=*/false); break; } } @@ -1072,7 +1070,7 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor, return ClangCursor::VisitResult::Continue; } -} // namespace +} // namespace void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { if (!kIndexStdDeclarations && @@ -1654,11 +1652,8 @@ void OnIndexReference(CXClientData client_data, const CXIdxEntityRefInfo* ref) { !CursorSpellingContainsString(ref->cursor, param->tu->cx_tu, called->def.short_name))); - OnIndexReference_Function(db, loc_spelling, - ref->container->cursor, - called_id, - called, - is_implicit); + OnIndexReference_Function(db, loc_spelling, ref->container->cursor, + called_id, called, is_implicit); // Checks if |str| starts with |start|. Ignores case. auto str_begin = [](const char* start, const char* str) { diff --git a/src/message_handler.cc b/src/message_handler.cc index ae748023..cb18cca1 100644 --- a/src/message_handler.cc +++ b/src/message_handler.cc @@ -112,7 +112,7 @@ void EmitSemanticHighlighting(QueryDatabase* db, case VarClass::Member: break; default: - continue; // applies to for loop + continue; // applies to for loop } is_type_member = var->def->declaring_type.has_value(); detailed_name = var->def->short_name; diff --git a/src/messages/text_document_completion.cc b/src/messages/text_document_completion.cc index f42bf94c..aa79e00d 100644 --- a/src/messages/text_document_completion.cc +++ b/src/messages/text_document_completion.cc @@ -50,7 +50,7 @@ void FilterCompletionResponse(Out_TextDocumentComplete* complete_response, } #endif - auto &items = complete_response->result.items; + auto& items = complete_response->result.items; // If the text doesn't start with underscore, // remove all candidates that start with underscore. @@ -58,7 +58,8 @@ void FilterCompletionResponse(Out_TextDocumentComplete* complete_response, items.erase(std::remove_if(items.begin(), items.end(), [](const lsCompletionItem& item) { return item.label[0] == '_'; - }), items.end()); + }), + items.end()); } // find the exact text @@ -72,7 +73,8 @@ void FilterCompletionResponse(Out_TextDocumentComplete* complete_response, items.erase(std::remove_if(items.begin(), items.end(), [&](const lsCompletionItem& item) { return item.label.find(complete_text) != 0; - }), items.end()); + }), + items.end()); } const size_t kMaxResultSize = 100u; diff --git a/src/messages/workspace_symbol.cc b/src/messages/workspace_symbol.cc index 623ef8c2..7d803d4b 100644 --- a/src/messages/workspace_symbol.cc +++ b/src/messages/workspace_symbol.cc @@ -57,7 +57,6 @@ struct Out_WorkspaceSymbol : public lsOutMessage { }; MAKE_REFLECT_STRUCT(Out_WorkspaceSymbol, jsonrpc, id, result); - ///// Fuzzy matching // Negative but far from INT_MIN so that intermediate results are hard to @@ -83,9 +82,12 @@ constexpr int kCamelScore = kWordStartScore + kGapScore - 1; enum class CharClass { Lower, Upper, Digit, NonWord }; static CharClass GetCharClass(int c) { - if (islower(c)) return CharClass::Lower; - if (isupper(c)) return CharClass::Upper; - if (isdigit(c)) return CharClass::Digit; + if (islower(c)) + return CharClass::Lower; + if (isupper(c)) + return CharClass::Upper; + if (isdigit(c)) + return CharClass::Digit; return CharClass::NonWord; } @@ -101,20 +103,27 @@ static int GetScoreFor(CharClass prev, CharClass curr) { } /* -fuzzyEvaluate implements a global sequence alignment algorithm to find the maximum accumulated score by aligning `pattern` to `str`. It applies when `pattern` is a subsequence of `str`. +fuzzyEvaluate implements a global sequence alignment algorithm to find the +maximum accumulated score by aligning `pattern` to `str`. It applies when +`pattern` is a subsequence of `str`. Scoring criteria -- Prefer matches at the start of a word, or the start of subwords in CamelCase/camelCase/camel123 words. See kWordStartScore/kCamelScore +- Prefer matches at the start of a word, or the start of subwords in +CamelCase/camelCase/camel123 words. See kWordStartScore/kCamelScore - Non-word characters matter. See kNonWordScore -- The first characters of words of `pattern` receive bonus because they usually have more significance than the rest. See kPatternStartMultiplier -- Superfluous characters in `str` will reduce the score (gap penalty). See kGapScore +- The first characters of words of `pattern` receive bonus because they usually +have more significance than the rest. See kPatternStartMultiplier +- Superfluous characters in `str` will reduce the score (gap penalty). See +kGapScore - Prefer early occurrence of the first character. See kLeadingGapScore/kGapScore The recurrence of the dynamic programming: dp[i][j]: maximum accumulated score by aligning pattern[0..i] to str[0..j] dp[0][j] = leading_gap_penalty(0, j) + score[j] -dp[i][j] = max(dp[i-1][j-1] + CONSECUTIVE_SCORE, max(dp[i-1][k] + gap_penalty(k+1, j) + score[j] : k < j)) -The first dimension can be suppressed since we do not need a matching scheme, which reduces the space complexity from O(N*M) to O(M) +dp[i][j] = max(dp[i-1][j-1] + CONSECUTIVE_SCORE, max(dp[i-1][k] + +gap_penalty(k+1, j) + score[j] : k < j)) +The first dimension can be suppressed since we do not need a matching scheme, +which reduces the space complexity from O(N*M) to O(M) */ int FuzzyEvaluate(const std::string& pattern, const std::string& str, @@ -136,7 +145,7 @@ int FuzzyEvaluate(const std::string& pattern, std::fill_n(dp.begin(), str.size(), kMinScore); // Align each character of pattern. - for (unsigned char pc: pattern) { + for (unsigned char pc : pattern) { if (isspace(pc)) { pstart = true; continue; @@ -192,7 +201,8 @@ struct WorkspaceSymbolHandler : BaseMessageHandler { if (!inserted_results.insert(db->detailed_names[i]).second) continue; - if (InsertSymbolIntoResult(db, working_files, db->symbols[i], &unsorted_results)) { + if (InsertSymbolIntoResult(db, working_files, db->symbols[i], + &unsorted_results)) { result_indices.push_back(i); if (unsorted_results.size() >= config->maxWorkspaceSearchResults) break; @@ -204,7 +214,7 @@ struct WorkspaceSymbolHandler : BaseMessageHandler { if (unsorted_results.size() < config->maxWorkspaceSearchResults) { std::string query_without_space; query_without_space.reserve(query.size()); - for (char c: query) + for (char c : query) if (!isspace(c)) query_without_space += c; @@ -214,7 +224,8 @@ struct WorkspaceSymbolHandler : BaseMessageHandler { if (!inserted_results.insert(db->detailed_names[i]).second) continue; - if (InsertSymbolIntoResult(db, working_files, db->symbols[i], &unsorted_results)) { + if (InsertSymbolIntoResult(db, working_files, db->symbols[i], + &unsorted_results)) { result_indices.push_back(i); if (unsorted_results.size() >= config->maxWorkspaceSearchResults) break; @@ -225,16 +236,15 @@ struct WorkspaceSymbolHandler : BaseMessageHandler { // Sort results with a fuzzy matching algorithm. int longest = 0; - for (int i: result_indices) + for (int i : result_indices) longest = std::max(longest, int(db->short_names[i].size())); - std::vector score(longest), // score for each position - dp(longest); // dp[i]: maximum value by aligning pattern to str[0..i] + std::vector score(longest), // score for each position + dp(longest); // dp[i]: maximum value by aligning pattern to str[0..i] std::vector> permutation(result_indices.size()); for (int i = 0; i < int(result_indices.size()); i++) { permutation[i] = { - FuzzyEvaluate(query, db->short_names[result_indices[i]], score, - dp), + FuzzyEvaluate(query, db->short_names[result_indices[i]], score, dp), i}; } std::sort(permutation.begin(), permutation.end(), diff --git a/src/query.cc b/src/query.cc index de6afbfe..de5c46ee 100644 --- a/src/query.cc +++ b/src/query.cc @@ -781,7 +781,8 @@ void QueryDatabase::ImportOrUpdate( existing.def = def.value; UpdateDetailedNames(&existing.detailed_name_idx, SymbolKind::Type, - it->second.id, def.value.short_name, def.value.detailed_name); + it->second.id, def.value.short_name, + def.value.detailed_name); } } @@ -805,7 +806,8 @@ void QueryDatabase::ImportOrUpdate( existing.def = def.value; UpdateDetailedNames(&existing.detailed_name_idx, SymbolKind::Func, - it->second.id, def.value.short_name, def.value.detailed_name); + it->second.id, def.value.short_name, + def.value.detailed_name); } } @@ -830,7 +832,8 @@ void QueryDatabase::ImportOrUpdate( existing.def = def.value; if (!def.value.is_local()) UpdateDetailedNames(&existing.detailed_name_idx, SymbolKind::Var, - it->second.id, def.value.short_name, def.value.detailed_name); + it->second.id, def.value.short_name, + def.value.detailed_name); } } diff --git a/src/working_files.cc b/src/working_files.cc index 37b68e6c..19a4aeb4 100644 --- a/src/working_files.cc +++ b/src/working_files.cc @@ -340,9 +340,9 @@ void WorkingFiles::OnChange(const lsTextDocumentDidChangeParams& change) { diff.rangeLength ? start_offset + *diff.rangeLength : GetOffsetForPosition(diff.range->end, file->buffer_content); - file->buffer_content.replace( - file->buffer_content.begin() + start_offset, - file->buffer_content.begin() + end_offset, diff.text); + file->buffer_content.replace(file->buffer_content.begin() + start_offset, + file->buffer_content.begin() + end_offset, + diff.text); file->OnBufferContentUpdated(); } }