Format code

This commit is contained in:
Jacob Dufault 2017-12-27 07:53:35 -08:00
parent a84c863e5e
commit b8e7a5bcb3
7 changed files with 58 additions and 45 deletions

View File

@ -198,15 +198,18 @@ optional<std::string> ClangCursor::get_comments() const {
// Get associated comment text. // Get associated comment text.
CXString cx_raw = clang_Cursor_getRawCommentText(cx_cursor); 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; 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; auto skip = start_column - 1;
for (; skip > 0 && (*p == ' ' || *p == '\t'); p++) for (; skip > 0 && (*p == ' ' || *p == '\t'); p++)
skip--; skip--;
const char* q = p; const char* q = p;
while (*q != '\n' && *q) q++; while (*q != '\n' && *q)
if (*q) q++; q++;
if (*q)
q++;
unindented.insert(unindented.end(), p, q); unindented.insert(unindented.end(), p, q);
p = q; p = q;
} }

View File

@ -402,7 +402,7 @@ bool IsFunctionCallContext(CXCursorKind kind) {
case CXCursor_ConversionFunction: case CXCursor_ConversionFunction:
case CXCursor_FunctionTemplate: case CXCursor_FunctionTemplate:
case CXCursor_OverloadedDeclRef: case CXCursor_OverloadedDeclRef:
// TODO: we need to test lambdas // TODO: we need to test lambdas
case CXCursor_LambdaExpr: case CXCursor_LambdaExpr:
return true; return true;
@ -1040,7 +1040,7 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor,
default: default:
if (!IsFunctionCallContext(cursor.get_kind())) if (!IsFunctionCallContext(cursor.get_kind()))
cursor.VisitChildren(&TemplateVisitor, data); cursor.VisitChildren(&TemplateVisitor, data);
/* fallthrough */ /* fallthrough */
// TODO Add other containers not covered by IsFunctionCallContext // TODO Add other containers not covered by IsFunctionCallContext
case CXCursor_ClassTemplate: case CXCursor_ClassTemplate:
break; break;
@ -1058,10 +1058,8 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor,
IndexFunc* called = data->db->Resolve(called_id); IndexFunc* called = data->db->Resolve(called_id);
OnIndexReference_Function(data->db, OnIndexReference_Function(data->db,
ResolveSpelling(cursor.cx_cursor), ResolveSpelling(cursor.cx_cursor),
data->container, data->container, called_id, called,
called_id, /*implicit=*/false);
called,
/*implicit=*/ false);
break; break;
} }
} }
@ -1072,7 +1070,7 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor,
return ClangCursor::VisitResult::Continue; return ClangCursor::VisitResult::Continue;
} }
} // namespace } // namespace
void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
if (!kIndexStdDeclarations && if (!kIndexStdDeclarations &&
@ -1654,11 +1652,8 @@ void OnIndexReference(CXClientData client_data, const CXIdxEntityRefInfo* ref) {
!CursorSpellingContainsString(ref->cursor, param->tu->cx_tu, !CursorSpellingContainsString(ref->cursor, param->tu->cx_tu,
called->def.short_name))); called->def.short_name)));
OnIndexReference_Function(db, loc_spelling, OnIndexReference_Function(db, loc_spelling, ref->container->cursor,
ref->container->cursor, called_id, called, is_implicit);
called_id,
called,
is_implicit);
// Checks if |str| starts with |start|. Ignores case. // Checks if |str| starts with |start|. Ignores case.
auto str_begin = [](const char* start, const char* str) { auto str_begin = [](const char* start, const char* str) {

View File

@ -112,7 +112,7 @@ void EmitSemanticHighlighting(QueryDatabase* db,
case VarClass::Member: case VarClass::Member:
break; break;
default: default:
continue; // applies to for loop continue; // applies to for loop
} }
is_type_member = var->def->declaring_type.has_value(); is_type_member = var->def->declaring_type.has_value();
detailed_name = var->def->short_name; detailed_name = var->def->short_name;

View File

@ -50,7 +50,7 @@ void FilterCompletionResponse(Out_TextDocumentComplete* complete_response,
} }
#endif #endif
auto &items = complete_response->result.items; auto& items = complete_response->result.items;
// If the text doesn't start with underscore, // If the text doesn't start with underscore,
// remove all candidates that 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(), items.erase(std::remove_if(items.begin(), items.end(),
[](const lsCompletionItem& item) { [](const lsCompletionItem& item) {
return item.label[0] == '_'; return item.label[0] == '_';
}), items.end()); }),
items.end());
} }
// find the exact text // find the exact text
@ -72,7 +73,8 @@ void FilterCompletionResponse(Out_TextDocumentComplete* complete_response,
items.erase(std::remove_if(items.begin(), items.end(), items.erase(std::remove_if(items.begin(), items.end(),
[&](const lsCompletionItem& item) { [&](const lsCompletionItem& item) {
return item.label.find(complete_text) != 0; return item.label.find(complete_text) != 0;
}), items.end()); }),
items.end());
} }
const size_t kMaxResultSize = 100u; const size_t kMaxResultSize = 100u;

View File

@ -57,7 +57,6 @@ struct Out_WorkspaceSymbol : public lsOutMessage<Out_WorkspaceSymbol> {
}; };
MAKE_REFLECT_STRUCT(Out_WorkspaceSymbol, jsonrpc, id, result); MAKE_REFLECT_STRUCT(Out_WorkspaceSymbol, jsonrpc, id, result);
///// Fuzzy matching ///// Fuzzy matching
// Negative but far from INT_MIN so that intermediate results are hard to // 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 }; enum class CharClass { Lower, Upper, Digit, NonWord };
static CharClass GetCharClass(int c) { static CharClass GetCharClass(int c) {
if (islower(c)) return CharClass::Lower; if (islower(c))
if (isupper(c)) return CharClass::Upper; return CharClass::Lower;
if (isdigit(c)) return CharClass::Digit; if (isupper(c))
return CharClass::Upper;
if (isdigit(c))
return CharClass::Digit;
return CharClass::NonWord; 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 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 - 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 - The first characters of words of `pattern` receive bonus because they usually
- Superfluous characters in `str` will reduce the score (gap penalty). See kGapScore 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 - Prefer early occurrence of the first character. See kLeadingGapScore/kGapScore
The recurrence of the dynamic programming: The recurrence of the dynamic programming:
dp[i][j]: maximum accumulated score by aligning pattern[0..i] to str[0..j] 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[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)) dp[i][j] = max(dp[i-1][j-1] + CONSECUTIVE_SCORE, max(dp[i-1][k] +
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) 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, int FuzzyEvaluate(const std::string& pattern,
const std::string& str, const std::string& str,
@ -136,7 +145,7 @@ int FuzzyEvaluate(const std::string& pattern,
std::fill_n(dp.begin(), str.size(), kMinScore); std::fill_n(dp.begin(), str.size(), kMinScore);
// Align each character of pattern. // Align each character of pattern.
for (unsigned char pc: pattern) { for (unsigned char pc : pattern) {
if (isspace(pc)) { if (isspace(pc)) {
pstart = true; pstart = true;
continue; continue;
@ -192,7 +201,8 @@ struct WorkspaceSymbolHandler : BaseMessageHandler<Ipc_WorkspaceSymbol> {
if (!inserted_results.insert(db->detailed_names[i]).second) if (!inserted_results.insert(db->detailed_names[i]).second)
continue; 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); result_indices.push_back(i);
if (unsorted_results.size() >= config->maxWorkspaceSearchResults) if (unsorted_results.size() >= config->maxWorkspaceSearchResults)
break; break;
@ -204,7 +214,7 @@ struct WorkspaceSymbolHandler : BaseMessageHandler<Ipc_WorkspaceSymbol> {
if (unsorted_results.size() < config->maxWorkspaceSearchResults) { if (unsorted_results.size() < config->maxWorkspaceSearchResults) {
std::string query_without_space; std::string query_without_space;
query_without_space.reserve(query.size()); query_without_space.reserve(query.size());
for (char c: query) for (char c : query)
if (!isspace(c)) if (!isspace(c))
query_without_space += c; query_without_space += c;
@ -214,7 +224,8 @@ struct WorkspaceSymbolHandler : BaseMessageHandler<Ipc_WorkspaceSymbol> {
if (!inserted_results.insert(db->detailed_names[i]).second) if (!inserted_results.insert(db->detailed_names[i]).second)
continue; 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); result_indices.push_back(i);
if (unsorted_results.size() >= config->maxWorkspaceSearchResults) if (unsorted_results.size() >= config->maxWorkspaceSearchResults)
break; break;
@ -225,16 +236,15 @@ struct WorkspaceSymbolHandler : BaseMessageHandler<Ipc_WorkspaceSymbol> {
// Sort results with a fuzzy matching algorithm. // Sort results with a fuzzy matching algorithm.
int longest = 0; int longest = 0;
for (int i: result_indices) for (int i : result_indices)
longest = std::max(longest, int(db->short_names[i].size())); longest = std::max(longest, int(db->short_names[i].size()));
std::vector<int> score(longest), // score for each position std::vector<int> score(longest), // score for each position
dp(longest); // dp[i]: maximum value by aligning pattern to str[0..i] dp(longest); // dp[i]: maximum value by aligning pattern to str[0..i]
std::vector<std::pair<int, int>> permutation(result_indices.size()); std::vector<std::pair<int, int>> permutation(result_indices.size());
for (int i = 0; i < int(result_indices.size()); i++) { for (int i = 0; i < int(result_indices.size()); i++) {
permutation[i] = { permutation[i] = {
FuzzyEvaluate(query, db->short_names[result_indices[i]], score, FuzzyEvaluate(query, db->short_names[result_indices[i]], score, dp),
dp),
i}; i};
} }
std::sort(permutation.begin(), permutation.end(), std::sort(permutation.begin(), permutation.end(),

View File

@ -781,7 +781,8 @@ void QueryDatabase::ImportOrUpdate(
existing.def = def.value; existing.def = def.value;
UpdateDetailedNames(&existing.detailed_name_idx, SymbolKind::Type, 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; existing.def = def.value;
UpdateDetailedNames(&existing.detailed_name_idx, SymbolKind::Func, 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; existing.def = def.value;
if (!def.value.is_local()) if (!def.value.is_local())
UpdateDetailedNames(&existing.detailed_name_idx, SymbolKind::Var, 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);
} }
} }

View File

@ -340,9 +340,9 @@ void WorkingFiles::OnChange(const lsTextDocumentDidChangeParams& change) {
diff.rangeLength diff.rangeLength
? start_offset + *diff.rangeLength ? start_offset + *diff.rangeLength
: GetOffsetForPosition(diff.range->end, file->buffer_content); : GetOffsetForPosition(diff.range->end, file->buffer_content);
file->buffer_content.replace( file->buffer_content.replace(file->buffer_content.begin() + start_offset,
file->buffer_content.begin() + start_offset, file->buffer_content.begin() + end_offset,
file->buffer_content.begin() + end_offset, diff.text); diff.text);
file->OnBufferContentUpdated(); file->OnBufferContentUpdated();
} }
} }