2018-08-21 05:27:52 +00:00
|
|
|
/* Copyright 2017-2018 ccls Authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==============================================================================*/
|
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-10-29 04:21:21 +00:00
|
|
|
#include "maybe.hh"
|
|
|
|
#include "utils.hh"
|
2017-05-19 07:02:01 +00:00
|
|
|
|
2018-02-02 05:31:56 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string>
|
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
namespace ccls {
|
2018-11-04 18:30:18 +00:00
|
|
|
struct Pos {
|
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-11-04 18:30:18 +00:00
|
|
|
static Pos 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-11-04 18:30:18 +00:00
|
|
|
bool operator==(const Pos &o) const {
|
2018-04-08 06:32:35 +00:00
|
|
|
return line == o.line && column == o.column;
|
|
|
|
}
|
2018-11-04 18:30:18 +00:00
|
|
|
bool operator<(const Pos &o) const {
|
2018-04-08 06:32:35 +00:00
|
|
|
if (line != o.line)
|
|
|
|
return line < o.line;
|
|
|
|
return column < o.column;
|
|
|
|
}
|
2018-11-04 18:30:18 +00:00
|
|
|
bool operator<=(const Pos &o) const { return !(o < *this); }
|
2017-04-05 08:06:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Range {
|
2018-11-04 18:30:18 +00:00
|
|
|
Pos start;
|
|
|
|
Pos end;
|
2017-04-05 08:06:18 +00:00
|
|
|
|
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;
|
|
|
|
|
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-10-28 17:49:31 +00:00
|
|
|
// Reflection
|
|
|
|
class Reader;
|
|
|
|
class Writer;
|
2018-11-04 18:30:18 +00:00
|
|
|
void Reflect(Reader &visitor, Pos &value);
|
|
|
|
void Reflect(Writer &visitor, Pos &value);
|
2018-10-28 17:49:31 +00:00
|
|
|
void Reflect(Reader &visitor, Range &value);
|
|
|
|
void Reflect(Writer &visitor, Range &value);
|
|
|
|
} // namespace ccls
|
|
|
|
|
2018-05-05 03:40:52 +00:00
|
|
|
namespace std {
|
2018-10-28 17:49:31 +00:00
|
|
|
template <> struct hash<ccls::Range> {
|
|
|
|
std::size_t operator()(ccls::Range x) const {
|
2018-11-11 09:10:15 +00:00
|
|
|
union {
|
|
|
|
ccls::Range range;
|
2018-05-05 03:40:52 +00:00
|
|
|
uint64_t u64;
|
2018-11-11 09:10:15 +00:00
|
|
|
} u{x};
|
2018-10-28 17:49:31 +00:00
|
|
|
static_assert(sizeof(ccls::Range) == 8);
|
2018-05-05 03:40:52 +00:00
|
|
|
return hash<uint64_t>()(u.u64);
|
|
|
|
}
|
|
|
|
};
|
2018-08-09 17:08:14 +00:00
|
|
|
} // namespace std
|
2018-05-05 03:40:52 +00:00
|
|
|
|
2018-11-04 18:30:18 +00:00
|
|
|
MAKE_HASHABLE(ccls::Pos, t.line, t.column);
|