mirror of
https://github.com/MaskRay/ccls.git
synced 2025-02-22 00:19:28 +00:00
simplify position part1
This commit is contained in:
parent
69b1dcadaa
commit
547ef3fc3d
@ -93,8 +93,8 @@ void AddUsage(std::vector<Range>& uses,
|
|||||||
// First thought makes me think no, we don't.
|
// First thought makes me think no, we don't.
|
||||||
for (int i = uses.size() - 1; i >= 0; --i) {
|
for (int i = uses.size() - 1; i >= 0; --i) {
|
||||||
if (uses[i].start == loc.start) {
|
if (uses[i].start == loc.start) {
|
||||||
if (loc.start.interesting)
|
if (loc.interesting)
|
||||||
uses[i].start.interesting = true;
|
uses[i].interesting = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -115,9 +115,9 @@ Range IdCache::ForceResolve(const CXSourceRange& range, bool interesting) {
|
|||||||
unsigned int end_line, end_column;
|
unsigned int end_line, end_column;
|
||||||
clang_getSpellingLocation(end, nullptr, &end_line, &end_column, nullptr);
|
clang_getSpellingLocation(end, nullptr, &end_line, &end_column, nullptr);
|
||||||
|
|
||||||
return Range(
|
return Range(interesting,
|
||||||
Position(interesting, start_line, start_column) /*start*/,
|
Position(start_line, start_column) /*start*/,
|
||||||
Position(interesting, end_line, end_column) /*end*/);
|
Position(end_line, end_column) /*end*/);
|
||||||
}
|
}
|
||||||
|
|
||||||
Range IdCache::ForceResolveSpelling(const CXCursor& cx_cursor, bool interesting) {
|
Range IdCache::ForceResolveSpelling(const CXCursor& cx_cursor, bool interesting) {
|
||||||
|
@ -1,23 +1,25 @@
|
|||||||
#include "position.h"
|
#include "position.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
// Skips until the character immediately following |skip_after|.
|
||||||
|
const char* SkipAfter(const char* input, char skip_after) {
|
||||||
|
while (*input && *input != skip_after)
|
||||||
|
++input;
|
||||||
|
++input;
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
Position::Position() {}
|
Position::Position() {}
|
||||||
|
|
||||||
Position::Position(bool interesting, int32_t line, int32_t column)
|
Position::Position(int32_t line, int32_t column)
|
||||||
: interesting(interesting), line(line), column(column) {}
|
: line(line), column(column) {}
|
||||||
|
|
||||||
Position::Position(const char* encoded) {
|
Position::Position(const char* encoded) {
|
||||||
if (*encoded == '*') {
|
|
||||||
interesting = true;
|
|
||||||
++encoded;
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(encoded);
|
assert(encoded);
|
||||||
line = atoi(encoded);
|
line = atoi(encoded);
|
||||||
while (*encoded && *encoded != ':')
|
|
||||||
++encoded;
|
|
||||||
if (*encoded == ':')
|
|
||||||
++encoded;
|
|
||||||
|
|
||||||
|
encoded = SkipAfter(encoded, ':');
|
||||||
assert(encoded);
|
assert(encoded);
|
||||||
column = atoi(encoded);
|
column = atoi(encoded);
|
||||||
}
|
}
|
||||||
@ -25,15 +27,12 @@ Position::Position(const char* encoded) {
|
|||||||
std::string Position::ToString() {
|
std::string Position::ToString() {
|
||||||
// Output looks like this:
|
// Output looks like this:
|
||||||
//
|
//
|
||||||
// *1:2
|
// 1:2
|
||||||
//
|
//
|
||||||
// * => interesting
|
|
||||||
// 1 => line
|
// 1 => line
|
||||||
// 2 => column
|
// 2 => column
|
||||||
|
|
||||||
std::string result;
|
std::string result;
|
||||||
if (interesting)
|
|
||||||
result += '*';
|
|
||||||
result += std::to_string(line);
|
result += std::to_string(line);
|
||||||
result += ':';
|
result += ':';
|
||||||
result += std::to_string(column);
|
result += std::to_string(column);
|
||||||
@ -43,15 +42,13 @@ std::string Position::ToString() {
|
|||||||
std::string Position::ToPrettyString(const std::string& filename) {
|
std::string Position::ToPrettyString(const std::string& filename) {
|
||||||
// Output looks like this:
|
// Output looks like this:
|
||||||
//
|
//
|
||||||
// *1:2
|
// 1:2:3
|
||||||
//
|
//
|
||||||
// * => interesting
|
// 1 => filename
|
||||||
// 1 => line
|
// 2 => line
|
||||||
// 2 => column
|
// 3 => column
|
||||||
|
|
||||||
std::string result;
|
std::string result;
|
||||||
if (interesting)
|
|
||||||
result += '*';
|
|
||||||
result += filename;
|
result += filename;
|
||||||
result += ':';
|
result += ':';
|
||||||
result += std::to_string(line);
|
result += std::to_string(line);
|
||||||
@ -60,12 +57,6 @@ std::string Position::ToPrettyString(const std::string& filename) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Position Position::WithInteresting(bool interesting) {
|
|
||||||
Position result = *this;
|
|
||||||
result.interesting = interesting;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Position::operator==(const Position& that) const {
|
bool Position::operator==(const Position& that) const {
|
||||||
return line == that.line && column == that.column;
|
return line == that.line && column == that.column;
|
||||||
}
|
}
|
||||||
@ -73,45 +64,66 @@ bool Position::operator==(const Position& that) const {
|
|||||||
bool Position::operator!=(const Position& that) const { return !(*this == that); }
|
bool Position::operator!=(const Position& that) const { return !(*this == that); }
|
||||||
|
|
||||||
bool Position::operator<(const Position& that) const {
|
bool Position::operator<(const Position& that) const {
|
||||||
return interesting < that.interesting && line < that.line && column < that.column;
|
return line < that.line && column < that.column;
|
||||||
}
|
}
|
||||||
|
|
||||||
Range::Range() {}
|
Range::Range() {}
|
||||||
|
|
||||||
Range::Range(Position start, Position end) : start(start), end(end) {}
|
Range::Range(bool interesting, Position start, Position end) : interesting(interesting), start(start), end(end) {}
|
||||||
|
|
||||||
Range::Range(const char* encoded) : start(encoded) {
|
Range::Range(const char* encoded) {
|
||||||
end = start;
|
end = start;
|
||||||
|
|
||||||
|
if (*encoded == '*') {
|
||||||
|
interesting = true;
|
||||||
|
++encoded;
|
||||||
|
}
|
||||||
|
|
||||||
|
start.line = atoi(encoded);
|
||||||
|
|
||||||
|
encoded = SkipAfter(encoded, ':');
|
||||||
|
assert(encoded);
|
||||||
|
start.column = atoi(encoded);
|
||||||
|
|
||||||
|
encoded = SkipAfter(encoded, '-');
|
||||||
assert(encoded);
|
assert(encoded);
|
||||||
while (*encoded && *encoded != '-')
|
|
||||||
++encoded;
|
|
||||||
if (*encoded == '-')
|
|
||||||
++encoded;
|
|
||||||
end.line = atoi(encoded);
|
end.line = atoi(encoded);
|
||||||
|
|
||||||
|
encoded = SkipAfter(encoded, ':');
|
||||||
assert(encoded);
|
assert(encoded);
|
||||||
while (*encoded && *encoded != ':')
|
|
||||||
++encoded;
|
|
||||||
if (*encoded == ':')
|
|
||||||
++encoded;
|
|
||||||
end.column = atoi(encoded);
|
end.column = atoi(encoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Range::ToString() {
|
std::string Range::ToString() {
|
||||||
std::string output;
|
// Output looks like this:
|
||||||
output += start.ToString();
|
//
|
||||||
|
// *1:2-3:4
|
||||||
|
//
|
||||||
|
// * => if present, range is interesting
|
||||||
|
// 1 => start line
|
||||||
|
// 2 => start column
|
||||||
|
// 3 => end line
|
||||||
|
// 4 => end column
|
||||||
|
|
||||||
output += "-";
|
std::string output;
|
||||||
|
|
||||||
|
if (interesting)
|
||||||
|
output += '*';
|
||||||
|
output += std::to_string(start.line);
|
||||||
|
output += ':';
|
||||||
|
output += std::to_string(start.column);
|
||||||
|
output += '-';
|
||||||
output += std::to_string(end.line);
|
output += std::to_string(end.line);
|
||||||
output += ":";
|
output += ':';
|
||||||
output += std::to_string(end.column);
|
output += std::to_string(end.column);
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
Range Range::WithInteresting(bool interesting) {
|
Range Range::WithInteresting(bool interesting) {
|
||||||
return Range(start.WithInteresting(interesting), end.WithInteresting(interesting));
|
Range result = *this;
|
||||||
|
result.interesting = interesting;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Range::operator==(const Range& that) const {
|
bool Range::operator==(const Range& that) const {
|
||||||
|
@ -5,19 +5,16 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
struct Position {
|
struct Position {
|
||||||
bool interesting = false;
|
|
||||||
int32_t line = -1;
|
int32_t line = -1;
|
||||||
int32_t column = -1;
|
int32_t column = -1;
|
||||||
|
|
||||||
Position();
|
Position();
|
||||||
Position(bool interesting, int32_t line, int32_t column);
|
Position(int32_t line, int32_t column);
|
||||||
explicit Position(const char* encoded);
|
explicit Position(const char* encoded);
|
||||||
|
|
||||||
std::string ToString();
|
std::string ToString();
|
||||||
std::string ToPrettyString(const std::string& filename);
|
std::string ToPrettyString(const std::string& filename);
|
||||||
|
|
||||||
Position WithInteresting(bool interesting);
|
|
||||||
|
|
||||||
// Compare two Positions and check if they are equal. Ignores the value of
|
// Compare two Positions and check if they are equal. Ignores the value of
|
||||||
// |interesting|.
|
// |interesting|.
|
||||||
bool operator==(const Position& that) const;
|
bool operator==(const Position& that) const;
|
||||||
@ -26,11 +23,12 @@ struct Position {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct Range {
|
struct Range {
|
||||||
|
bool interesting = false;
|
||||||
Position start;
|
Position start;
|
||||||
Position end;
|
Position end;
|
||||||
|
|
||||||
Range();
|
Range();
|
||||||
Range(Position start, Position end);
|
Range(bool interesting, Position start, Position end);
|
||||||
explicit Range(const char* encoded);
|
explicit Range(const char* encoded);
|
||||||
|
|
||||||
std::string ToString();
|
std::string ToString();
|
||||||
@ -39,4 +37,9 @@ 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;
|
||||||
bool operator<(const Range& that) const;
|
bool operator<(const Range& that) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Location {
|
||||||
|
std::string path;
|
||||||
|
Range range;
|
||||||
};
|
};
|
40
src/query.cc
40
src/query.cc
@ -40,8 +40,8 @@ Usr MapIdToUsr(const IdCache& id_cache, const VarId& id) {
|
|||||||
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) {
|
QueryableRange MapIdToUsr(const IdCache& id_cache, const Range& id) {
|
||||||
QueryableLocation start(id_cache.primary_file, id.start.line, id.start.column, id.start.interesting);
|
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.end.interesting);
|
QueryableLocation end(id_cache.primary_file, id.end.line, id.end.column, id.interesting);
|
||||||
return QueryableRange(start, end);
|
return QueryableRange(start, end);
|
||||||
//assert(id_cache.file_id_to_file_path.find(id.file_id()) != id_cache.file_id_to_file_path.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);
|
//return QueryableLocation(id_cache.file_id_to_file_path.find(id.file_id())->second, id.line, id.column, id.interesting);
|
||||||
@ -77,8 +77,8 @@ std::vector<UsrRef> MapIdToUsr(const IdCache& id_cache, const std::vector<FuncRe
|
|||||||
}
|
}
|
||||||
std::vector<QueryableRange> MapIdToUsr(const IdCache& id_cache, const std::vector<Range>& ids) {
|
std::vector<QueryableRange> MapIdToUsr(const IdCache& id_cache, const std::vector<Range>& ids) {
|
||||||
return Transform<Range, QueryableRange>(ids, [&](Range id) {
|
return Transform<Range, QueryableRange>(ids, [&](Range id) {
|
||||||
QueryableLocation start(id_cache.primary_file, id.start.line, id.start.column, id.start.interesting);
|
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.end.interesting);
|
QueryableLocation end(id_cache.primary_file, id.end.line, id.end.column, id.interesting);
|
||||||
return QueryableRange(start, end);
|
return QueryableRange(start, end);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -379,38 +379,6 @@ void CompareGroups(
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if false
|
|
||||||
IndexUpdate::IndexUpdate(IndexedFile& file) {
|
|
||||||
// TODO: use delta constructor with an empty file.
|
|
||||||
|
|
||||||
auto file_def = BuildFileDef(file);
|
|
||||||
files_def_update.push_back(file_def);
|
|
||||||
|
|
||||||
for (const IndexedTypeDef& indexed : file.types) {
|
|
||||||
QueryableTypeDef query(file.id_cache, indexed);
|
|
||||||
types_def_update.push_back(query.def);
|
|
||||||
types_derived.push_back(QueryableTypeDef::DerivedUpdate(query.def.usr, query.derived));
|
|
||||||
types_instantiations.push_back(QueryableTypeDef::InstantiationsUpdate(query.def.usr, query.instantiations));
|
|
||||||
types_uses.push_back(QueryableTypeDef::UsesUpdate(query.def.usr, query.uses));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const IndexedFuncDef& indexed : file.funcs) {
|
|
||||||
QueryableFuncDef query(file.id_cache, indexed);
|
|
||||||
funcs_def_update.push_back(query.def);
|
|
||||||
funcs_declarations.push_back(QueryableFuncDef::DeclarationsUpdate(query.def.usr, query.declarations));
|
|
||||||
funcs_derived.push_back(QueryableFuncDef::DerivedUpdate(query.def.usr, query.derived));
|
|
||||||
funcs_callers.push_back(QueryableFuncDef::CallersUpdate(query.def.usr, query.callers));
|
|
||||||
funcs_uses.push_back(QueryableFuncDef::UsesUpdate(query.def.usr, query.uses));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const IndexedVarDef& indexed : file.vars) {
|
|
||||||
QueryableVarDef query(file.id_cache, indexed);
|
|
||||||
vars_def_update.push_back(query.def);
|
|
||||||
vars_uses.push_back(QueryableVarDef::UsesUpdate(query.def.usr, query.uses));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// static
|
// static
|
||||||
IndexUpdate IndexUpdate::CreateImport(IndexedFile& file) {
|
IndexUpdate IndexUpdate::CreateImport(IndexedFile& file) {
|
||||||
// Return standard diff constructor but with an empty file so everything is
|
// Return standard diff constructor but with an empty file so everything is
|
||||||
|
Loading…
Reference in New Issue
Block a user