mirror of
https://github.com/MaskRay/ccls.git
synced 2024-11-22 15:45:08 +00:00
cleanup query location usage
This commit is contained in:
parent
547ef3fc3d
commit
5408540158
@ -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];
|
||||
switch (symbol.kind) {
|
||||
case SymbolKind::Type: {
|
||||
@ -128,7 +128,7 @@ optional<QueryableRange> GetDefinitionSpellingOfUsr(QueryableDatabase* db, const
|
||||
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];
|
||||
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<TCodeLens>* result,
|
||||
QueryableRange loc,
|
||||
const std::vector<QueryableRange>& uses,
|
||||
QueryableLocation loc,
|
||||
const std::vector<QueryableLocation>& 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<lsCodeLensCommandArguments>();
|
||||
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<lsLocation> 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<TCodeLens>* result,
|
||||
}
|
||||
|
||||
void AddCodeLens(std::vector<TCodeLens>* result,
|
||||
QueryableRange loc,
|
||||
QueryableLocation loc,
|
||||
const std::vector<UsrRef>& uses,
|
||||
bool exclude_loc,
|
||||
bool only_interesting,
|
||||
const char* singular,
|
||||
const char* plural) {
|
||||
std::vector<QueryableRange> uses0;
|
||||
std::vector<QueryableLocation> uses0;
|
||||
uses0.reserve(uses.size());
|
||||
for (const UsrRef& use : uses)
|
||||
uses0.push_back(use.loc);
|
||||
@ -398,16 +398,16 @@ void AddCodeLens(std::vector<TCodeLens>* result,
|
||||
|
||||
void AddCodeLens(std::vector<TCodeLens>* result,
|
||||
QueryableDatabase* db,
|
||||
QueryableRange loc,
|
||||
QueryableLocation loc,
|
||||
const std::vector<Usr>& usrs,
|
||||
bool exclude_loc,
|
||||
bool only_interesting,
|
||||
const char* singular,
|
||||
const char* plural) {
|
||||
std::vector<QueryableRange> uses0;
|
||||
std::vector<QueryableLocation> uses0;
|
||||
uses0.reserve(usrs.size());
|
||||
for (const Usr& usr : usrs) {
|
||||
optional<QueryableRange> loc = GetDefinitionSpellingOfUsr(db, usr);
|
||||
optional<QueryableLocation> 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<QueryableRange> 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<QueryableLocation> 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: {
|
||||
|
@ -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;
|
||||
};
|
28
src/query.cc
28
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<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;
|
||||
});
|
||||
}
|
||||
std::vector<QueryableRange> MapIdToUsr(const IdCache& id_cache, const std::vector<Range>& ids) {
|
||||
return Transform<Range, QueryableRange>(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<QueryableLocation> MapIdToUsr(const IdCache& id_cache, const std::vector<Range>& ids) {
|
||||
return Transform<Range, QueryableLocation>(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
|
||||
|
76
src/query.h
76
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<Usr, Usr, Usr, QueryableRange, QueryableRange>;
|
||||
using DefUpdate = TypeDefDefinitionData<Usr, Usr, Usr, QueryableLocation, QueryableLocation>;
|
||||
using DerivedUpdate = MergeableUpdate<Usr>;
|
||||
using InstantiationsUpdate = MergeableUpdate<Usr>;
|
||||
using UsesUpdate = MergeableUpdate<QueryableRange>;
|
||||
using UsesUpdate = MergeableUpdate<QueryableLocation>;
|
||||
|
||||
DefUpdate def;
|
||||
std::vector<Usr> derived;
|
||||
std::vector<Usr> instantiations;
|
||||
std::vector<QueryableRange> uses;
|
||||
std::vector<QueryableLocation> uses;
|
||||
|
||||
QueryableTypeDef() : def("") {}
|
||||
QueryableTypeDef(IdCache& id_cache, const IndexedTypeDef& indexed);
|
||||
};
|
||||
|
||||
struct QueryableFuncDef {
|
||||
using DefUpdate = FuncDefDefinitionData<Usr, Usr, Usr, UsrRef, QueryableRange, QueryableRange>;
|
||||
using DeclarationsUpdate = MergeableUpdate<QueryableRange>;
|
||||
using DefUpdate = FuncDefDefinitionData<Usr, Usr, Usr, UsrRef, QueryableLocation, QueryableLocation>;
|
||||
using DeclarationsUpdate = MergeableUpdate<QueryableLocation>;
|
||||
using DerivedUpdate = MergeableUpdate<Usr>;
|
||||
using CallersUpdate = MergeableUpdate<UsrRef>;
|
||||
using UsesUpdate = MergeableUpdate<QueryableRange>;
|
||||
using UsesUpdate = MergeableUpdate<QueryableLocation>;
|
||||
|
||||
DefUpdate def;
|
||||
std::vector<QueryableRange> declarations;
|
||||
std::vector<QueryableLocation> declarations;
|
||||
std::vector<Usr> derived;
|
||||
std::vector<UsrRef> callers;
|
||||
std::vector<QueryableRange> uses;
|
||||
std::vector<QueryableLocation> uses;
|
||||
|
||||
QueryableFuncDef() : def("") {}
|
||||
QueryableFuncDef(IdCache& id_cache, const IndexedFuncDef& indexed);
|
||||
};
|
||||
|
||||
struct QueryableVarDef {
|
||||
using DefUpdate = VarDefDefinitionData<Usr, Usr, Usr, QueryableRange, QueryableRange>;
|
||||
using UsesUpdate = MergeableUpdate<QueryableRange>;
|
||||
using DefUpdate = VarDefDefinitionData<Usr, Usr, Usr, QueryableLocation, QueryableLocation>;
|
||||
using UsesUpdate = MergeableUpdate<QueryableLocation>;
|
||||
|
||||
DefUpdate def;
|
||||
std::vector<QueryableRange> uses;
|
||||
std::vector<QueryableLocation> uses;
|
||||
|
||||
QueryableVarDef() : def("") {}
|
||||
QueryableVarDef(IdCache& id_cache, const IndexedVarDef& indexed);
|
||||
|
Loading…
Reference in New Issue
Block a user