From d61cc7a077da1e49897f80f0b81b603e7c92f0f0 Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Sat, 8 Apr 2017 00:21:00 -0700 Subject: [PATCH] QueryableLocation now stores a file index --- src/command_line.cc | 52 +++++++++++++++++++++++++++------------------ src/indexer.cc | 9 +++++++- src/query.cc | 4 ++-- src/query.h | 7 +++--- 4 files changed, 44 insertions(+), 28 deletions(-) diff --git a/src/command_line.cc b/src/command_line.cc index 3ec9d2c1..534b539e 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -378,13 +378,20 @@ lsRange GetLsRange(const Range& location) { lsPosition(location.end.line - 1, location.end.column - 1)); } -lsLocation GetLsLocation(const QueryableLocation& location) { +lsDocumentUri GetLsDocumentUri(QueryableDatabase* db, QueryFileId file_id) { + std::string path = db->files[file_id.id].def.usr; + return lsDocumentUri::FromPath(path); +} + +lsLocation GetLsLocation(QueryableDatabase* db, const QueryableLocation& location) { return lsLocation( - lsDocumentUri::FromPath(location.path), + GetLsDocumentUri(db, location.path), GetLsRange(location.range)); } -void AddCodeLens(std::vector* result, +void AddCodeLens( + QueryableDatabase* db, + std::vector* result, QueryableLocation loc, const std::vector& uses, bool exclude_loc, @@ -395,7 +402,7 @@ void AddCodeLens(std::vector* result, code_lens.range = GetLsRange(loc.range); code_lens.command = lsCommand(); code_lens.command->command = "superindex.showReferences"; - code_lens.command->arguments.uri = lsDocumentUri::FromPath(loc.path); + code_lens.command->arguments.uri = GetLsDocumentUri(db, loc.path); code_lens.command->arguments.position = code_lens.range.start; // Add unique uses. @@ -405,7 +412,7 @@ void AddCodeLens(std::vector* result, continue; if (only_interesting && !use.range.interesting) continue; - unique_uses.insert(GetLsLocation(use)); + unique_uses.insert(GetLsLocation(db, use)); } code_lens.command->arguments.locations.assign(unique_uses.begin(), unique_uses.end()); @@ -422,7 +429,9 @@ void AddCodeLens(std::vector* result, result->push_back(code_lens); } -void AddCodeLens(std::vector* result, +void AddCodeLens( + QueryableDatabase* db, + std::vector* result, QueryableLocation loc, const std::vector& uses, bool exclude_loc, @@ -433,11 +442,12 @@ void AddCodeLens(std::vector* result, uses0.reserve(uses.size()); for (const UsrRef& use : uses) uses0.push_back(use.loc); - AddCodeLens(result, loc, uses0, exclude_loc, only_interesting, singular, plural); + AddCodeLens(db, result, loc, uses0, exclude_loc, only_interesting, singular, plural); } -void AddCodeLens(std::vector* result, +void AddCodeLens( QueryableDatabase* db, + std::vector* result, QueryableLocation loc, const std::vector& usrs, bool exclude_loc, @@ -451,7 +461,7 @@ void AddCodeLens(std::vector* result, if (loc) uses0.push_back(loc.value()); } - AddCodeLens(result, loc, uses0, exclude_loc, only_interesting, singular, plural); + AddCodeLens(db, result, loc, uses0, exclude_loc, only_interesting, singular, plural); } void QueryDbMainLoop( @@ -563,7 +573,7 @@ void QueryDbMainLoop( ref.loc.range.start.column <= target_column && ref.loc.range.end.column >= target_column) { optional location = GetDefinitionSpellingOfSymbol(db, ref.idx); if (location) - response.result.push_back(GetLsLocation(location.value())); + response.result.push_back(GetLsLocation(db, location.value())); break; } } @@ -589,7 +599,7 @@ void QueryDbMainLoop( SymbolIdx symbol = ref.idx; lsSymbolInformation info; - info.location = GetLsLocation(ref.loc); + info.location = GetLsLocation(db, ref.loc); switch (symbol.kind) { case SymbolKind::Type: { @@ -655,15 +665,15 @@ void QueryDbMainLoop( switch (symbol.kind) { case SymbolKind::Type: { QueryableTypeDef& def = db->types[symbol.idx]; - AddCodeLens(&response.result, ref.loc.OffsetStartColumn(0), def.uses, + AddCodeLens(db, &response.result, ref.loc.OffsetStartColumn(0), def.uses, false /*exclude_loc*/, false /*only_interesting*/, "ref", "refs"); - AddCodeLens(&response.result, ref.loc.OffsetStartColumn(1), def.uses, + AddCodeLens(db, &response.result, ref.loc.OffsetStartColumn(1), def.uses, false /*exclude_loc*/, true /*only_interesting*/, "iref", "irefs"); - AddCodeLens(&response.result, db, ref.loc.OffsetStartColumn(2), def.derived, + AddCodeLens(db, &response.result, ref.loc.OffsetStartColumn(2), def.derived, false /*exclude_loc*/, false /*only_interesting*/, "derived", "derived"); - AddCodeLens(&response.result, db, ref.loc.OffsetStartColumn(3), def.instantiations, + AddCodeLens(db, &response.result, ref.loc.OffsetStartColumn(3), def.instantiations, false /*exclude_loc*/, false /*only_interesting*/, "instantiation", "instantiations"); break; } @@ -672,17 +682,17 @@ void QueryDbMainLoop( //AddCodeLens(&response.result, ref.loc.OffsetStartColumn(0), def.uses, // false /*exclude_loc*/, false /*only_interesting*/, "reference", // "references"); - AddCodeLens(&response.result, ref.loc.OffsetStartColumn(1), def.callers, + AddCodeLens(db, &response.result, ref.loc.OffsetStartColumn(1), def.callers, true /*exclude_loc*/, false /*only_interesting*/, "caller", "callers"); //AddCodeLens(&response.result, ref.loc.OffsetColumn(2), def.def.callees, // false /*exclude_loc*/, false /*only_interesting*/, "callee", "callees"); - AddCodeLens(&response.result, db, ref.loc.OffsetStartColumn(3), def.derived, + AddCodeLens(db, &response.result, ref.loc.OffsetStartColumn(3), def.derived, false /*exclude_loc*/, false /*only_interesting*/, "derived", "derived"); break; } case SymbolKind::Var: { QueryableVarDef& def = db->vars[symbol.idx]; - AddCodeLens(&response.result, ref.loc.OffsetStartColumn(0), def.uses, + AddCodeLens(db, &response.result, ref.loc.OffsetStartColumn(0), def.uses, true /*exclude_loc*/, false /*only_interesting*/, "reference", "references"); break; @@ -742,7 +752,7 @@ void QueryDbMainLoop( info.kind = lsSymbolKind::Class; if (def.def.definition_extent.has_value()) - info.location = GetLsLocation(def.def.definition_extent.value()); + info.location = GetLsLocation(db, def.def.definition_extent.value()); break; } case SymbolKind::Func: { @@ -760,7 +770,7 @@ void QueryDbMainLoop( } if (def.def.definition_extent.has_value()) { - info.location = GetLsLocation(def.def.definition_extent.value()); + info.location = GetLsLocation(db, def.def.definition_extent.value()); } break; } @@ -770,7 +780,7 @@ void QueryDbMainLoop( info.kind = lsSymbolKind::Variable; if (def.def.definition_extent.has_value()) { - info.location = GetLsLocation(def.def.definition_extent.value()); + info.location = GetLsLocation(db, def.def.definition_extent.value()); } break; } diff --git a/src/indexer.cc b/src/indexer.cc index a538e267..7d74b660 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -86,6 +86,13 @@ IndexedTypeDef::IndexedTypeDef(IndexTypeId id, const std::string& usr) void AddUsage(std::vector& uses, Range loc, bool insert_if_not_present = true) { + // cannot sub 1 from size_t in loop below; check explicitly here + if (uses.empty()) { + if (insert_if_not_present) + uses.push_back(loc); + return; + } + // TODO: think about if we need to also consider |uses[i].end| // First thought makes me think no, we don't. for (int i = uses.size() - 1; i >= 0; --i) { @@ -749,7 +756,7 @@ void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { // Don't treat enum definition variables as instantiations. bool is_enum_member = decl->semanticContainer && decl->semanticContainer->cursor.kind == CXCursor_EnumDecl; if (!is_enum_member) - db->Resolve(var_type.value())->instantiations.push_back(var_id.id); + db->Resolve(var_type.value())->instantiations.push_back(var_id); var_def->def.variable_type = var_type.value(); } diff --git a/src/query.cc b/src/query.cc index f78771f7..3c4937c5 100644 --- a/src/query.cc +++ b/src/query.cc @@ -32,7 +32,7 @@ Usr MapIdToUsr(const IdMap& id_map, const IndexVarId& id) { return id_map.local_ids.var_id_to_usr.find(id)->second; } QueryableLocation MapIdToUsr(const IdMap& id_map, const Range& range) { - return QueryableLocation(id_map.local_ids.primary_file, range); + return QueryableLocation(id_map.primary_file, range); } UsrRef MapIdToUsr(const IdMap& id_map, const FuncRef& id) { assert(id_map.local_ids.func_id_to_usr.find(id.id) != id_map.local_ids.func_id_to_usr.end()); @@ -403,7 +403,7 @@ QueryVarId GetQueryVarIdFromUsr(QueryableDatabase* query_db, const Usr& usr) { IdMap::IdMap(QueryableDatabase* query_db, const IdCache& local_ids) : local_ids(local_ids) { - index_file_id = GetQueryFileIdFromUsr(query_db, local_ids.primary_file); + primary_file = GetQueryFileIdFromUsr(query_db, local_ids.primary_file); cached_type_ids_.set_empty_key(-1); cached_type_ids_.resize(local_ids.type_id_to_usr.size()); diff --git a/src/query.h b/src/query.h index ef8763ce..cad33624 100644 --- a/src/query.h +++ b/src/query.h @@ -30,10 +30,10 @@ struct IdMap; // 'interesting' from location when that is cleaned up. struct QueryableLocation { - Usr path; + QueryFileId path; Range range; - QueryableLocation(Usr path, Range range) + QueryableLocation(QueryFileId path, Range range) : path(path), range(range) {} QueryableLocation OffsetStartColumn(int offset) const { @@ -291,6 +291,7 @@ struct IdMap { // Then lookup in cached_* should *never* fail. const IdCache& local_ids; + QueryFileId primary_file; IdMap(QueryableDatabase* query_db, const IdCache& local_ids); @@ -301,9 +302,7 @@ struct IdMap { SymbolIdx ToSymbol(IndexFuncId id) const; SymbolIdx ToSymbol(IndexVarId id) const; - // TODO private: - QueryFileId index_file_id; // TODO: make these type safe google::dense_hash_map cached_type_ids_; // IndexTypeId -> QueryTypeId google::dense_hash_map cached_func_ids_; // IndexFuncId -> QueryFuncId