2018-08-21 05:27:52 +00:00
|
|
|
// Copyright 2017-2018 ccls Authors
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-02-02 05:31:56 +00:00
|
|
|
#include "maybe.h"
|
2017-07-19 07:12:04 +00:00
|
|
|
#include "utils.h"
|
2017-05-19 07:02:01 +00:00
|
|
|
|
2018-02-02 05:31:56 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string>
|
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
struct Position {
|
2018-04-08 06:32:35 +00:00
|
|
|
int16_t line = -1;
|
|
|
|
int16_t column = -1;
|
2017-04-05 08:06:18 +00:00
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
static Position FromString(const std::string &encoded);
|
2017-04-05 08:06:18 +00:00
|
|
|
|
2018-04-08 06:32:35 +00:00
|
|
|
bool Valid() const { return line >= 0; }
|
2017-04-05 08:06:18 +00:00
|
|
|
std::string ToString();
|
|
|
|
|
|
|
|
// Compare two Positions and check if they are equal. Ignores the value of
|
|
|
|
// |interesting|.
|
2018-08-09 17:08:14 +00:00
|
|
|
bool operator==(const Position &o) const {
|
2018-04-08 06:32:35 +00:00
|
|
|
return line == o.line && column == o.column;
|
|
|
|
}
|
2018-08-09 17:08:14 +00:00
|
|
|
bool operator<(const Position &o) const {
|
2018-04-08 06:32:35 +00:00
|
|
|
if (line != o.line)
|
|
|
|
return line < o.line;
|
|
|
|
return column < o.column;
|
|
|
|
}
|
2017-04-05 08:06:18 +00:00
|
|
|
};
|
2017-07-19 07:12:04 +00:00
|
|
|
MAKE_HASHABLE(Position, t.line, t.column);
|
2017-04-05 08:06:18 +00:00
|
|
|
|
|
|
|
struct Range {
|
|
|
|
Position start;
|
|
|
|
Position end;
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
static Range FromString(const std::string &encoded);
|
2017-04-05 08:06:18 +00:00
|
|
|
|
2018-04-08 06:32:35 +00:00
|
|
|
bool Valid() const { return start.Valid(); }
|
2017-04-11 05:43:01 +00:00
|
|
|
bool Contains(int line, int column) const;
|
2018-02-01 05:01:31 +00:00
|
|
|
Range RemovePrefix(Position position) const;
|
2017-04-11 05:43:01 +00:00
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
std::string ToString();
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
bool operator==(const Range &o) const {
|
2018-04-08 06:32:35 +00:00
|
|
|
return start == o.start && end == o.end;
|
|
|
|
}
|
2018-08-09 17:08:14 +00:00
|
|
|
bool operator<(const Range &o) const {
|
2018-04-08 06:32:35 +00:00
|
|
|
return !(start == o.start) ? start < o.start : end < o.end;
|
|
|
|
}
|
2017-05-19 07:02:01 +00:00
|
|
|
};
|
|
|
|
|
2018-05-05 03:40:52 +00:00
|
|
|
namespace std {
|
2018-08-09 17:08:14 +00:00
|
|
|
template <> struct hash<Range> {
|
2018-05-05 03:40:52 +00:00
|
|
|
std::size_t operator()(Range x) const {
|
|
|
|
union U {
|
|
|
|
Range range = {};
|
|
|
|
uint64_t u64;
|
|
|
|
} u;
|
|
|
|
static_assert(sizeof(Range) == 8);
|
|
|
|
u.range = x;
|
|
|
|
return hash<uint64_t>()(u.u64);
|
|
|
|
}
|
|
|
|
};
|
2018-08-09 17:08:14 +00:00
|
|
|
} // namespace std
|
2018-05-05 03:40:52 +00:00
|
|
|
|
2017-05-19 07:02:01 +00:00
|
|
|
// Reflection
|
2018-04-08 06:32:35 +00:00
|
|
|
class Reader;
|
|
|
|
class Writer;
|
2018-08-09 17:08:14 +00:00
|
|
|
void Reflect(Reader &visitor, Position &value);
|
|
|
|
void Reflect(Writer &visitor, Position &value);
|
|
|
|
void Reflect(Reader &visitor, Range &value);
|
|
|
|
void Reflect(Writer &visitor, Range &value);
|