cleanup query location usage

This commit is contained in:
Jacob Dufault 2017-04-06 23:20:30 -07:00
parent 547ef3fc3d
commit 5408540158
4 changed files with 59 additions and 96 deletions

View File

@ -104,7 +104,7 @@ std::string Join(const std::vector<std::string>& elements, std::string sep) {
} }
optional<QueryableRange> GetDefinitionSpellingOfUsr(QueryableDatabase* db, const Usr& usr) { optional<QueryableLocation> GetDefinitionSpellingOfUsr(QueryableDatabase* db, const Usr& usr) {
SymbolIdx symbol = db->usr_to_symbol[usr]; SymbolIdx symbol = db->usr_to_symbol[usr];
switch (symbol.kind) { switch (symbol.kind) {
case SymbolKind::Type: { case SymbolKind::Type: {
@ -128,7 +128,7 @@ optional<QueryableRange> GetDefinitionSpellingOfUsr(QueryableDatabase* db, const
return nullopt; return nullopt;
} }
optional<QueryableRange> GetDefinitionExtentOfUsr(QueryableDatabase* db, const Usr& usr) { optional<QueryableLocation> GetDefinitionExtentOfUsr(QueryableDatabase* db, const Usr& usr) {
SymbolIdx symbol = db->usr_to_symbol[usr]; SymbolIdx symbol = db->usr_to_symbol[usr];
switch (symbol.kind) { switch (symbol.kind) {
case SymbolKind::Type: { case SymbolKind::Type: {
@ -332,38 +332,38 @@ QueryableFile* FindFile(QueryableDatabase* db, const std::string& filename) {
return nullptr; return nullptr;
} }
lsRange GetLsRange(const QueryableRange& location) { lsRange GetLsRange(const Range& location) {
return lsRange( return lsRange(
lsPosition(location.start.line - 1, location.start.column - 1), lsPosition(location.start.line - 1, location.start.column - 1),
lsPosition(location.end.line - 1, location.end.column - 1)); lsPosition(location.end.line - 1, location.end.column - 1));
} }
lsLocation GetLsLocation(const QueryableRange& location) { lsLocation GetLsLocation(const QueryableLocation& location) {
return lsLocation( return lsLocation(
lsDocumentUri::FromPath(location.start.path), lsDocumentUri::FromPath(location.path),
GetLsRange(location)); GetLsRange(location.range));
} }
void AddCodeLens(std::vector<TCodeLens>* result, void AddCodeLens(std::vector<TCodeLens>* result,
QueryableRange loc, QueryableLocation loc,
const std::vector<QueryableRange>& uses, const std::vector<QueryableLocation>& uses,
bool exclude_loc, bool exclude_loc,
bool only_interesting, bool only_interesting,
const char* singular, const char* singular,
const char* plural) { const char* plural) {
TCodeLens code_lens; TCodeLens code_lens;
code_lens.range = GetLsRange(loc); code_lens.range = GetLsRange(loc.range);
code_lens.command = lsCommand<lsCodeLensCommandArguments>(); code_lens.command = lsCommand<lsCodeLensCommandArguments>();
code_lens.command->command = "superindex.showReferences"; 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; code_lens.command->arguments.position = code_lens.range.start;
// Add unique uses. // Add unique uses.
std::unordered_set<lsLocation> unique_uses; std::unordered_set<lsLocation> unique_uses;
for (const QueryableRange& use : uses) { for (const QueryableLocation& use : uses) {
if (exclude_loc && use == loc) if (exclude_loc && use == loc)
continue; continue;
if (only_interesting && !use.start.interesting) if (only_interesting && !use.range.interesting)
continue; continue;
unique_uses.insert(GetLsLocation(use)); unique_uses.insert(GetLsLocation(use));
} }
@ -383,13 +383,13 @@ void AddCodeLens(std::vector<TCodeLens>* result,
} }
void AddCodeLens(std::vector<TCodeLens>* result, void AddCodeLens(std::vector<TCodeLens>* result,
QueryableRange loc, QueryableLocation loc,
const std::vector<UsrRef>& uses, const std::vector<UsrRef>& uses,
bool exclude_loc, bool exclude_loc,
bool only_interesting, bool only_interesting,
const char* singular, const char* singular,
const char* plural) { const char* plural) {
std::vector<QueryableRange> uses0; std::vector<QueryableLocation> uses0;
uses0.reserve(uses.size()); uses0.reserve(uses.size());
for (const UsrRef& use : uses) for (const UsrRef& use : uses)
uses0.push_back(use.loc); uses0.push_back(use.loc);
@ -398,16 +398,16 @@ void AddCodeLens(std::vector<TCodeLens>* result,
void AddCodeLens(std::vector<TCodeLens>* result, void AddCodeLens(std::vector<TCodeLens>* result,
QueryableDatabase* db, QueryableDatabase* db,
QueryableRange loc, QueryableLocation loc,
const std::vector<Usr>& usrs, const std::vector<Usr>& usrs,
bool exclude_loc, bool exclude_loc,
bool only_interesting, bool only_interesting,
const char* singular, const char* singular,
const char* plural) { const char* plural) {
std::vector<QueryableRange> uses0; std::vector<QueryableLocation> uses0;
uses0.reserve(usrs.size()); uses0.reserve(usrs.size());
for (const Usr& usr : usrs) { for (const Usr& usr : usrs) {
optional<QueryableRange> loc = GetDefinitionSpellingOfUsr(db, usr); optional<QueryableLocation> loc = GetDefinitionSpellingOfUsr(db, usr);
if (loc) if (loc)
uses0.push_back(loc.value()); uses0.push_back(loc.value());
} }
@ -517,9 +517,9 @@ void QueryDbMainLoop(
int target_column = msg->params.position.character + 1; int target_column = msg->params.position.character + 1;
for (const UsrRef& ref : file->def.all_symbols) { for (const UsrRef& ref : file->def.all_symbols) {
if (ref.loc.start.line >= target_line && ref.loc.end.line <= target_line && if (ref.loc.range.start.line >= target_line && ref.loc.range.end.line <= target_line &&
ref.loc.start.column <= target_column && ref.loc.end.column >= target_column) { ref.loc.range.start.column <= target_column && ref.loc.range.end.column >= target_column) {
optional<QueryableRange> location = GetDefinitionSpellingOfUsr(db, ref.usr); optional<QueryableLocation> location = GetDefinitionSpellingOfUsr(db, ref.usr);
if (location) if (location)
response.result.push_back(GetLsLocation(location.value())); response.result.push_back(GetLsLocation(location.value()));
break; break;
@ -688,10 +688,8 @@ void QueryDbMainLoop(
info.name = def.def.qualified_name; info.name = def.def.qualified_name;
info.kind = lsSymbolKind::Class; info.kind = lsSymbolKind::Class;
if (def.def.definition_extent.has_value()) { if (def.def.definition_extent.has_value())
info.location.uri.SetPath(def.def.definition_extent->start.path); info.location = GetLsLocation(def.def.definition_extent.value());
info.location.range = GetLsRange(def.def.definition_extent.value());
}
break; break;
} }
case SymbolKind::Func: { case SymbolKind::Func: {

View File

@ -38,8 +38,3 @@ struct Range {
bool operator!=(const Range& that) const; 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;
};

View File

@ -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()); 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; return id_cache.var_id_to_usr.find(id)->second;
} }
QueryableRange MapIdToUsr(const IdCache& id_cache, const Range& id) { QueryableLocation MapIdToUsr(const IdCache& id_cache, const Range& range) {
QueryableLocation start(id_cache.primary_file, id.start.line, id.start.column, id.interesting); return QueryableLocation(id_cache.primary_file, range);
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);
} }
std::vector<Usr> MapIdToUsr(const IdCache& id_cache, const std::vector<TypeId>& ids) { std::vector<Usr> MapIdToUsr(const IdCache& id_cache, const std::vector<TypeId>& ids) {
@ -75,11 +71,9 @@ std::vector<UsrRef> MapIdToUsr(const IdCache& id_cache, const std::vector<FuncRe
return result; return result;
}); });
} }
std::vector<QueryableRange> MapIdToUsr(const IdCache& id_cache, const std::vector<Range>& ids) { std::vector<QueryableLocation> MapIdToUsr(const IdCache& id_cache, const std::vector<Range>& ids) {
return Transform<Range, QueryableRange>(ids, [&](Range id) { return Transform<Range, QueryableLocation>(ids, [&](Range range) {
QueryableLocation start(id_cache.primary_file, id.start.line, id.start.column, id.interesting); return QueryableLocation(id_cache.primary_file, range);
QueryableLocation end(id_cache.primary_file, id.end.line, id.end.column, id.interesting);
return QueryableRange(start, end);
}); });
} }
QueryableTypeDef::DefUpdate MapIdToUsr(const IdCache& id_cache, const IndexedTypeDef::Def& def) { 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) { 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) { 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; 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_derived, derived, Usr);
PROCESS_UPDATE_DIFF(types_instantiations, instantiations, Usr); PROCESS_UPDATE_DIFF(types_instantiations, instantiations, Usr);
PROCESS_UPDATE_DIFF(types_uses, uses, QueryableRange); PROCESS_UPDATE_DIFF(types_uses, uses, QueryableLocation);
}); });
// Functions // Functions
@ -456,10 +450,10 @@ IndexUpdate::IndexUpdate(IndexedFile& previous_file, IndexedFile& current_file)
if (previous_remapped_def != current_remapped_def) if (previous_remapped_def != current_remapped_def)
funcs_def_update.push_back(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_derived, derived, Usr);
PROCESS_UPDATE_DIFF(funcs_callers, callers, UsrRef); PROCESS_UPDATE_DIFF(funcs_callers, callers, UsrRef);
PROCESS_UPDATE_DIFF(funcs_uses, uses, QueryableRange); PROCESS_UPDATE_DIFF(funcs_uses, uses, QueryableLocation);
}); });
// Variables // Variables
@ -478,7 +472,7 @@ IndexUpdate::IndexUpdate(IndexedFile& previous_file, IndexedFile& current_file)
if (previous_remapped_def != current_remapped_def) if (previous_remapped_def != current_remapped_def)
vars_def_update.push_back(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 #undef PROCESS_UPDATE_DIFF

View File

@ -16,70 +16,46 @@ using Usr = std::string;
// paths. // paths.
struct QueryableLocation { struct QueryableLocation {
Usr path; Usr path;
int line; Range range;
int column;
bool interesting;
QueryableLocation() QueryableLocation()
: path(""), line(-1), column(-1), interesting(false) {} : path("") {}
QueryableLocation(Usr path, int line, int column, bool interesting) QueryableLocation(Usr path, Range range)
: path(path), line(line), column(column), interesting(interesting) {} : path(path), range(range) {}
QueryableLocation OffsetColumn(int offset) const { QueryableLocation OffsetStartColumn(int offset) const {
return QueryableLocation(path, line, column + offset, interesting); QueryableLocation result = *this;
result.range.start.column += offset;
return result;
} }
bool operator==(const QueryableLocation& other) const { bool operator==(const QueryableLocation& other) const {
// Note: We ignore |is_interesting|. // Note: We ignore |is_interesting|.
return return
path == other.path && path == other.path &&
line == other.line && range == other.range;
column == other.column;
} }
bool operator!=(const QueryableLocation& other) const { return !(*this == other); } bool operator!=(const QueryableLocation& other) const { return !(*this == other); }
bool operator<(const QueryableLocation& o) const { bool operator<(const QueryableLocation& o) const {
return return
path < o.path && path < o.path &&
line < o.line && range < o.range;
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;
} }
}; };
struct UsrRef { struct UsrRef {
Usr usr; Usr usr;
QueryableRange loc; QueryableLocation loc;
UsrRef() {} 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 { 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 !(*this == other); }
bool operator<(const UsrRef& other) const { 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 { struct QueryableTypeDef {
using DefUpdate = TypeDefDefinitionData<Usr, Usr, Usr, QueryableRange, QueryableRange>; using DefUpdate = TypeDefDefinitionData<Usr, Usr, Usr, QueryableLocation, QueryableLocation>;
using DerivedUpdate = MergeableUpdate<Usr>; using DerivedUpdate = MergeableUpdate<Usr>;
using InstantiationsUpdate = MergeableUpdate<Usr>; using InstantiationsUpdate = MergeableUpdate<Usr>;
using UsesUpdate = MergeableUpdate<QueryableRange>; using UsesUpdate = MergeableUpdate<QueryableLocation>;
DefUpdate def; DefUpdate def;
std::vector<Usr> derived; std::vector<Usr> derived;
std::vector<Usr> instantiations; std::vector<Usr> instantiations;
std::vector<QueryableRange> uses; std::vector<QueryableLocation> uses;
QueryableTypeDef() : def("") {} QueryableTypeDef() : def("") {}
QueryableTypeDef(IdCache& id_cache, const IndexedTypeDef& indexed); QueryableTypeDef(IdCache& id_cache, const IndexedTypeDef& indexed);
}; };
struct QueryableFuncDef { struct QueryableFuncDef {
using DefUpdate = FuncDefDefinitionData<Usr, Usr, Usr, UsrRef, QueryableRange, QueryableRange>; using DefUpdate = FuncDefDefinitionData<Usr, Usr, Usr, UsrRef, QueryableLocation, QueryableLocation>;
using DeclarationsUpdate = MergeableUpdate<QueryableRange>; using DeclarationsUpdate = MergeableUpdate<QueryableLocation>;
using DerivedUpdate = MergeableUpdate<Usr>; using DerivedUpdate = MergeableUpdate<Usr>;
using CallersUpdate = MergeableUpdate<UsrRef>; using CallersUpdate = MergeableUpdate<UsrRef>;
using UsesUpdate = MergeableUpdate<QueryableRange>; using UsesUpdate = MergeableUpdate<QueryableLocation>;
DefUpdate def; DefUpdate def;
std::vector<QueryableRange> declarations; std::vector<QueryableLocation> declarations;
std::vector<Usr> derived; std::vector<Usr> derived;
std::vector<UsrRef> callers; std::vector<UsrRef> callers;
std::vector<QueryableRange> uses; std::vector<QueryableLocation> uses;
QueryableFuncDef() : def("") {} QueryableFuncDef() : def("") {}
QueryableFuncDef(IdCache& id_cache, const IndexedFuncDef& indexed); QueryableFuncDef(IdCache& id_cache, const IndexedFuncDef& indexed);
}; };
struct QueryableVarDef { struct QueryableVarDef {
using DefUpdate = VarDefDefinitionData<Usr, Usr, Usr, QueryableRange, QueryableRange>; using DefUpdate = VarDefDefinitionData<Usr, Usr, Usr, QueryableLocation, QueryableLocation>;
using UsesUpdate = MergeableUpdate<QueryableRange>; using UsesUpdate = MergeableUpdate<QueryableLocation>;
DefUpdate def; DefUpdate def;
std::vector<QueryableRange> uses; std::vector<QueryableLocation> uses;
QueryableVarDef() : def("") {} QueryableVarDef() : def("") {}
QueryableVarDef(IdCache& id_cache, const IndexedVarDef& indexed); QueryableVarDef(IdCache& id_cache, const IndexedVarDef& indexed);