Add bool include_decl parameter to GetUsesOfSymbol

Fixes #350

Function declarations/definitions are not counted if include_decl is
false.
We should do similar thing to Var and Type.
This commit is contained in:
Fangrui Song 2018-01-26 17:31:50 -08:00
parent 723c78409f
commit 37809def7c
5 changed files with 14 additions and 18 deletions

View File

@ -38,7 +38,7 @@ struct TextDocumentDocumentHighlightHandler
for (const SymbolRef& ref :
FindSymbolsAtLocation(working_file, file, request->params.position)) {
// Found symbol. Return references to highlight.
std::vector<QueryLocation> uses = GetUsesOfSymbol(db, ref.idx);
std::vector<QueryLocation> uses = GetUsesOfSymbol(db, ref.idx, true);
out.result.reserve(uses.size());
for (const QueryLocation& use : uses) {
if (use.path != file_id)

View File

@ -53,19 +53,11 @@ struct TextDocumentReferencesHandler
for (const SymbolRef& ref :
FindSymbolsAtLocation(working_file, file, request->params.position)) {
optional<QueryLocation> excluded_declaration;
if (!request->params.context.includeDeclaration) {
LOG_S(INFO) << "Excluding declaration in references";
excluded_declaration = GetDefinitionSpellingOfSymbol(db, ref.idx);
}
// Found symbol. Return references.
std::vector<QueryLocation> uses = GetUsesOfSymbol(db, ref.idx);
std::vector<QueryLocation> uses = GetUsesOfSymbol(
db, ref.idx, request->params.context.includeDeclaration);
out.result.reserve(uses.size());
for (const QueryLocation& use : uses) {
if (excluded_declaration.has_value() && use == *excluded_declaration)
continue;
optional<lsLocation> ls_location =
GetLsLocation(db, working_files, use);
if (ls_location)

View File

@ -97,7 +97,7 @@ struct TextDocumentRenameHandler : BaseMessageHandler<Ipc_TextDocumentRename> {
for (const SymbolRef& ref :
FindSymbolsAtLocation(working_file, file, request->params.position)) {
// Found symbol. Return references to rename.
std::vector<QueryLocation> uses = GetUsesOfSymbol(db, ref.idx);
std::vector<QueryLocation> uses = GetUsesOfSymbol(db, ref.idx, true);
out.result =
BuildWorkspaceEdit(db, working_files, uses, request->params.newName);
break;

View File

@ -182,7 +182,8 @@ std::vector<QueryLocation> ToQueryLocation(QueryDatabase* db,
}
std::vector<QueryLocation> GetUsesOfSymbol(QueryDatabase* db,
const SymbolIdx& symbol) {
const SymbolIdx& symbol,
bool include_decl) {
switch (symbol.kind) {
case SymbolKind::Type: {
QueryType& type = db->types[symbol.idx];
@ -192,9 +193,11 @@ std::vector<QueryLocation> GetUsesOfSymbol(QueryDatabase* db,
// TODO: the vector allocation could be avoided.
QueryFunc& func = db->funcs[symbol.idx];
std::vector<QueryLocation> result = ToQueryLocation(db, func.callers);
AddRange(&result, func.declarations);
if (func.def && func.def->definition_spelling)
result.push_back(*func.def->definition_spelling);
if (include_decl) {
AddRange(&result, func.declarations);
if (func.def && func.def->definition_spelling)
result.push_back(*func.def->definition_spelling);
}
return result;
}
case SymbolKind::Var: {

View File

@ -29,7 +29,8 @@ std::vector<QueryLocation> ToQueryLocation(QueryDatabase* db,
std::vector<QueryLocation> ToQueryLocation(QueryDatabase* db,
const std::vector<QueryVarId>& ids);
std::vector<QueryLocation> GetUsesOfSymbol(QueryDatabase* db,
const SymbolIdx& symbol);
const SymbolIdx& symbol,
bool include_decl);
std::vector<QueryLocation> GetDeclarationsOfSymbolForGotoDefinition(
QueryDatabase* db,
const SymbolIdx& symbol);
@ -65,4 +66,4 @@ std::vector<SymbolRef> FindSymbolsAtLocation(WorkingFile* working_file,
void EmitDiagnostics(WorkingFiles* working_files,
std::string path,
std::vector<lsDiagnostic> diagnostics);
std::vector<lsDiagnostic> diagnostics);