From 5408540158f70e53c536afe1d7d4689b8dcf0143 Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Thu, 6 Apr 2017 23:20:30 -0700 Subject: [PATCH] cleanup query location usage --- src/command_line.cc | 46 +++++++++++++-------------- src/position.h | 5 --- src/query.cc | 28 +++++++---------- src/query.h | 76 ++++++++++++++++----------------------------- 4 files changed, 59 insertions(+), 96 deletions(-) diff --git a/src/command_line.cc b/src/command_line.cc index 0c7e3a95..1cf1728b 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -104,7 +104,7 @@ std::string Join(const std::vector& elements, std::string sep) { } -optional GetDefinitionSpellingOfUsr(QueryableDatabase* db, const Usr& usr) { +optional GetDefinitionSpellingOfUsr(QueryableDatabase* db, const Usr& usr) { SymbolIdx symbol = db->usr_to_symbol[usr]; switch (symbol.kind) { case SymbolKind::Type: { @@ -128,7 +128,7 @@ optional GetDefinitionSpellingOfUsr(QueryableDatabase* db, const return nullopt; } -optional GetDefinitionExtentOfUsr(QueryableDatabase* db, const Usr& usr) { +optional GetDefinitionExtentOfUsr(QueryableDatabase* db, const Usr& usr) { SymbolIdx symbol = db->usr_to_symbol[usr]; switch (symbol.kind) { case SymbolKind::Type: { @@ -332,38 +332,38 @@ QueryableFile* FindFile(QueryableDatabase* db, const std::string& filename) { return nullptr; } -lsRange GetLsRange(const QueryableRange& location) { +lsRange GetLsRange(const Range& location) { return lsRange( lsPosition(location.start.line - 1, location.start.column - 1), lsPosition(location.end.line - 1, location.end.column - 1)); } -lsLocation GetLsLocation(const QueryableRange& location) { +lsLocation GetLsLocation(const QueryableLocation& location) { return lsLocation( - lsDocumentUri::FromPath(location.start.path), - GetLsRange(location)); + lsDocumentUri::FromPath(location.path), + GetLsRange(location.range)); } void AddCodeLens(std::vector* result, - QueryableRange loc, - const std::vector& uses, + QueryableLocation loc, + const std::vector& uses, bool exclude_loc, bool only_interesting, const char* singular, const char* plural) { TCodeLens code_lens; - code_lens.range = GetLsRange(loc); + code_lens.range = GetLsRange(loc.range); code_lens.command = lsCommand(); code_lens.command->command = "superindex.showReferences"; - code_lens.command->arguments.uri = lsDocumentUri::FromPath(loc.start.path); + code_lens.command->arguments.uri = lsDocumentUri::FromPath(loc.path); code_lens.command->arguments.position = code_lens.range.start; // Add unique uses. std::unordered_set unique_uses; - for (const QueryableRange& use : uses) { + for (const QueryableLocation& use : uses) { if (exclude_loc && use == loc) continue; - if (only_interesting && !use.start.interesting) + if (only_interesting && !use.range.interesting) continue; unique_uses.insert(GetLsLocation(use)); } @@ -383,13 +383,13 @@ void AddCodeLens(std::vector* result, } void AddCodeLens(std::vector* result, - QueryableRange loc, + QueryableLocation loc, const std::vector& uses, bool exclude_loc, bool only_interesting, const char* singular, const char* plural) { - std::vector uses0; + std::vector uses0; uses0.reserve(uses.size()); for (const UsrRef& use : uses) uses0.push_back(use.loc); @@ -398,16 +398,16 @@ void AddCodeLens(std::vector* result, void AddCodeLens(std::vector* result, QueryableDatabase* db, - QueryableRange loc, + QueryableLocation loc, const std::vector& usrs, bool exclude_loc, bool only_interesting, const char* singular, const char* plural) { - std::vector uses0; + std::vector uses0; uses0.reserve(usrs.size()); for (const Usr& usr : usrs) { - optional loc = GetDefinitionSpellingOfUsr(db, usr); + optional loc = GetDefinitionSpellingOfUsr(db, usr); if (loc) uses0.push_back(loc.value()); } @@ -517,9 +517,9 @@ void QueryDbMainLoop( int target_column = msg->params.position.character + 1; for (const UsrRef& ref : file->def.all_symbols) { - if (ref.loc.start.line >= target_line && ref.loc.end.line <= target_line && - ref.loc.start.column <= target_column && ref.loc.end.column >= target_column) { - optional location = GetDefinitionSpellingOfUsr(db, ref.usr); + if (ref.loc.range.start.line >= target_line && ref.loc.range.end.line <= target_line && + ref.loc.range.start.column <= target_column && ref.loc.range.end.column >= target_column) { + optional location = GetDefinitionSpellingOfUsr(db, ref.usr); if (location) response.result.push_back(GetLsLocation(location.value())); break; @@ -688,10 +688,8 @@ void QueryDbMainLoop( info.name = def.def.qualified_name; info.kind = lsSymbolKind::Class; - if (def.def.definition_extent.has_value()) { - info.location.uri.SetPath(def.def.definition_extent->start.path); - info.location.range = GetLsRange(def.def.definition_extent.value()); - } + if (def.def.definition_extent.has_value()) + info.location = GetLsLocation(def.def.definition_extent.value()); break; } case SymbolKind::Func: { diff --git a/src/position.h b/src/position.h index 4880c8ac..229d42af 100644 --- a/src/position.h +++ b/src/position.h @@ -37,9 +37,4 @@ struct Range { bool operator==(const Range& that) const; bool operator!=(const Range& that) const; bool operator<(const Range& that) const; -}; - -struct Location { - std::string path; - Range range; }; \ No newline at end of file diff --git a/src/query.cc b/src/query.cc index 589c3592..44905fa7 100644 --- a/src/query.cc +++ b/src/query.cc @@ -39,12 +39,8 @@ Usr MapIdToUsr(const IdCache& id_cache, const VarId& id) { assert(id_cache.var_id_to_usr.find(id) != id_cache.var_id_to_usr.end()); return id_cache.var_id_to_usr.find(id)->second; } -QueryableRange MapIdToUsr(const IdCache& id_cache, const Range& id) { - QueryableLocation start(id_cache.primary_file, id.start.line, id.start.column, id.interesting); - QueryableLocation end(id_cache.primary_file, id.end.line, id.end.column, id.interesting); - return QueryableRange(start, end); - //assert(id_cache.file_id_to_file_path.find(id.file_id()) != id_cache.file_id_to_file_path.end()); - //return QueryableLocation(id_cache.file_id_to_file_path.find(id.file_id())->second, id.line, id.column, id.interesting); +QueryableLocation MapIdToUsr(const IdCache& id_cache, const Range& range) { + return QueryableLocation(id_cache.primary_file, range); } std::vector MapIdToUsr(const IdCache& id_cache, const std::vector& ids) { @@ -75,11 +71,9 @@ std::vector MapIdToUsr(const IdCache& id_cache, const std::vector MapIdToUsr(const IdCache& id_cache, const std::vector& ids) { - return Transform(ids, [&](Range id) { - QueryableLocation start(id_cache.primary_file, id.start.line, id.start.column, id.interesting); - QueryableLocation end(id_cache.primary_file, id.end.line, id.end.column, id.interesting); - return QueryableRange(start, end); +std::vector MapIdToUsr(const IdCache& id_cache, const std::vector& ids) { + return Transform(ids, [&](Range range) { + return QueryableLocation(id_cache.primary_file, range); }); } QueryableTypeDef::DefUpdate MapIdToUsr(const IdCache& id_cache, const IndexedTypeDef::Def& def) { @@ -172,10 +166,10 @@ QueryableFile::Def BuildFileDef(const IndexedFile& indexed) { } std::sort(def.outline.begin(), def.outline.end(), [](const UsrRef& a, const UsrRef& b) { - return a.loc.start < b.loc.start; + return a.loc.range.start < b.loc.range.start; }); std::sort(def.all_symbols.begin(), def.all_symbols.end(), [](const UsrRef& a, const UsrRef& b) { - return a.loc.start < b.loc.start; + return a.loc.range.start < b.loc.range.start; }); return def; @@ -434,7 +428,7 @@ IndexUpdate::IndexUpdate(IndexedFile& previous_file, IndexedFile& current_file) PROCESS_UPDATE_DIFF(types_derived, derived, Usr); PROCESS_UPDATE_DIFF(types_instantiations, instantiations, Usr); - PROCESS_UPDATE_DIFF(types_uses, uses, QueryableRange); + PROCESS_UPDATE_DIFF(types_uses, uses, QueryableLocation); }); // Functions @@ -456,10 +450,10 @@ IndexUpdate::IndexUpdate(IndexedFile& previous_file, IndexedFile& current_file) if (previous_remapped_def != current_remapped_def) funcs_def_update.push_back(current_remapped_def); - PROCESS_UPDATE_DIFF(funcs_declarations, declarations, QueryableRange); + PROCESS_UPDATE_DIFF(funcs_declarations, declarations, QueryableLocation); PROCESS_UPDATE_DIFF(funcs_derived, derived, Usr); PROCESS_UPDATE_DIFF(funcs_callers, callers, UsrRef); - PROCESS_UPDATE_DIFF(funcs_uses, uses, QueryableRange); + PROCESS_UPDATE_DIFF(funcs_uses, uses, QueryableLocation); }); // Variables @@ -478,7 +472,7 @@ IndexUpdate::IndexUpdate(IndexedFile& previous_file, IndexedFile& current_file) if (previous_remapped_def != current_remapped_def) vars_def_update.push_back(current_remapped_def); - PROCESS_UPDATE_DIFF(vars_uses, uses, QueryableRange); + PROCESS_UPDATE_DIFF(vars_uses, uses, QueryableLocation); }); #undef PROCESS_UPDATE_DIFF diff --git a/src/query.h b/src/query.h index 224199cb..ee4d483e 100644 --- a/src/query.h +++ b/src/query.h @@ -16,70 +16,46 @@ using Usr = std::string; // paths. struct QueryableLocation { Usr path; - int line; - int column; - bool interesting; - + Range range; + QueryableLocation() - : path(""), line(-1), column(-1), interesting(false) {} - QueryableLocation(Usr path, int line, int column, bool interesting) - : path(path), line(line), column(column), interesting(interesting) {} + : path("") {} + QueryableLocation(Usr path, Range range) + : path(path), range(range) {} - QueryableLocation OffsetColumn(int offset) const { - return QueryableLocation(path, line, column + offset, interesting); + QueryableLocation OffsetStartColumn(int offset) const { + QueryableLocation result = *this; + result.range.start.column += offset; + return result; } bool operator==(const QueryableLocation& other) const { // Note: We ignore |is_interesting|. return path == other.path && - line == other.line && - column == other.column; + range == other.range; } bool operator!=(const QueryableLocation& other) const { return !(*this == other); } bool operator<(const QueryableLocation& o) const { return path < o.path && - line < o.line && - column < o.column && - interesting < o.interesting; - } -}; - -struct QueryableRange { - QueryableLocation start; - QueryableLocation end; - - QueryableRange() {} - QueryableRange(QueryableLocation start, QueryableLocation end) : start(start), end(end) {} - - QueryableRange OffsetStartColumn(int offset) const { - return QueryableRange(start.OffsetColumn(offset), end); - } - - bool operator==(const QueryableRange& other) const { - // Note: We ignore |is_interesting|. - return start == other.start && end == other.end; - } - bool operator!=(const QueryableRange& other) const { return !(*this == other); } - bool operator<(const QueryableRange& o) const { - return start < o.start; + range < o.range; } }; struct UsrRef { Usr usr; - QueryableRange loc; + QueryableLocation loc; UsrRef() {} - UsrRef(Usr usr, QueryableRange loc) : usr(usr), loc(loc) {} + UsrRef(Usr usr, QueryableLocation loc) : usr(usr), loc(loc) {} bool operator==(const UsrRef& other) const { - return usr == other.usr && loc.start == other.loc.start; + return usr == other.usr && loc.range.start == other.loc.range.start; } bool operator!=(const UsrRef& other) const { return !(*this == other); } bool operator<(const UsrRef& other) const { - return usr < other.usr && loc.start < other.loc.start; + return usr < other.usr && loc.range.start < other.loc.range.start; } }; @@ -136,43 +112,43 @@ struct QueryableFile { }; struct QueryableTypeDef { - using DefUpdate = TypeDefDefinitionData; + using DefUpdate = TypeDefDefinitionData; using DerivedUpdate = MergeableUpdate; using InstantiationsUpdate = MergeableUpdate; - using UsesUpdate = MergeableUpdate; + using UsesUpdate = MergeableUpdate; DefUpdate def; std::vector derived; std::vector instantiations; - std::vector uses; + std::vector uses; QueryableTypeDef() : def("") {} QueryableTypeDef(IdCache& id_cache, const IndexedTypeDef& indexed); }; struct QueryableFuncDef { - using DefUpdate = FuncDefDefinitionData; - using DeclarationsUpdate = MergeableUpdate; + using DefUpdate = FuncDefDefinitionData; + using DeclarationsUpdate = MergeableUpdate; using DerivedUpdate = MergeableUpdate; using CallersUpdate = MergeableUpdate; - using UsesUpdate = MergeableUpdate; + using UsesUpdate = MergeableUpdate; DefUpdate def; - std::vector declarations; + std::vector declarations; std::vector derived; std::vector callers; - std::vector uses; + std::vector uses; QueryableFuncDef() : def("") {} QueryableFuncDef(IdCache& id_cache, const IndexedFuncDef& indexed); }; struct QueryableVarDef { - using DefUpdate = VarDefDefinitionData; - using UsesUpdate = MergeableUpdate; + using DefUpdate = VarDefDefinitionData; + using UsesUpdate = MergeableUpdate; DefUpdate def; - std::vector uses; + std::vector uses; QueryableVarDef() : def("") {} QueryableVarDef(IdCache& id_cache, const IndexedVarDef& indexed);