ccls/src/position.cc

181 lines
4.2 KiB
C++
Raw Normal View History

2017-04-05 08:06:18 +00:00
#include "position.h"
2018-03-15 04:12:55 +00:00
#include <stdlib.h>
2017-04-07 06:10:17 +00:00
Position::Position() : line(-1), column(-1) {}
2017-04-05 08:06:18 +00:00
2017-09-22 01:14:57 +00:00
Position::Position(int16_t line, int16_t column) : line(line), column(column) {}
2017-04-05 08:06:18 +00:00
Position::Position(const char* encoded) {
2018-03-15 04:12:55 +00:00
char* p = const_cast<char*>(encoded);
line = int16_t(strtol(p, &p, 10)) - 1;
assert(*p == ':');
p++;
column = int16_t(strtol(p, &p, 10)) - 1;
2017-04-05 08:06:18 +00:00
}
std::string Position::ToString() {
// Output looks like this:
//
2017-04-07 06:10:17 +00:00
// 1:2
2017-04-05 08:06:18 +00:00
//
// 1 => line
// 2 => column
std::string result;
result += std::to_string(line + 1);
2017-04-05 08:06:18 +00:00
result += ':';
result += std::to_string(column + 1);
2017-04-05 08:06:18 +00:00
return result;
}
std::string Position::ToPrettyString(const std::string& filename) {
// Output looks like this:
//
2017-04-07 06:10:17 +00:00
// 1:2:3
2017-04-05 08:06:18 +00:00
//
2017-04-07 06:10:17 +00:00
// 1 => filename
// 2 => line
// 3 => column
2017-04-05 08:06:18 +00:00
std::string result;
result += filename;
result += ':';
result += std::to_string(line + 1);
2017-04-05 08:06:18 +00:00
result += ':';
result += std::to_string(column + 1);
2017-04-05 08:06:18 +00:00
return result;
}
bool Position::operator==(const Position& that) const {
return line == that.line && column == that.column;
}
2017-09-22 01:14:57 +00:00
bool Position::operator!=(const Position& that) const {
return !(*this == that);
}
2017-04-05 08:06:18 +00:00
bool Position::operator<(const Position& that) const {
if (line != that.line)
return line < that.line;
return column < that.column;
2017-04-05 08:06:18 +00:00
}
Range::Range() {}
2017-05-15 03:51:53 +00:00
Range::Range(Position position) : Range(position, position) {}
Range::Range(Position start, Position end) : start(start), end(end) {}
2017-04-05 08:06:18 +00:00
2017-04-07 06:10:17 +00:00
Range::Range(const char* encoded) {
char* p = const_cast<char*>(encoded);
start.line = int16_t(strtol(p, &p, 10)) - 1;
assert(*p == ':');
p++;
start.column = int16_t(strtol(p, &p, 10)) - 1;
assert(*p == '-');
p++;
end.line = int16_t(strtol(p, &p, 10)) - 1;
assert(*p == ':');
p++;
end.column = int16_t(strtol(p, nullptr, 10)) - 1;
2017-04-05 08:06:18 +00:00
}
bool Range::Contains(int line, int column) const {
if (line == start.line && line == end.line)
return column >= start.column && column < end.column;
if (line == start.line)
return column >= start.column;
if (line == end.line)
return column < end.column;
if (line > start.line && line < end.line)
return true;
return false;
}
Range Range::RemovePrefix(Position position) const {
return {std::min(std::max(position, start), end), end};
}
2017-04-05 08:06:18 +00:00
std::string Range::ToString() {
2017-04-07 06:10:17 +00:00
// Output looks like this:
//
2018-03-15 04:12:55 +00:00
// 1:2-3:4
2017-04-07 06:10:17 +00:00
//
// 1 => start line
// 2 => start column
// 3 => end line
// 4 => end column
2017-04-05 08:06:18 +00:00
std::string output;
2017-04-05 08:29:15 +00:00
output += std::to_string(start.line + 1);
2017-04-07 06:10:17 +00:00
output += ':';
output += std::to_string(start.column + 1);
2017-04-07 06:10:17 +00:00
output += '-';
output += std::to_string(end.line + 1);
2017-04-07 06:10:17 +00:00
output += ':';
output += std::to_string(end.column + 1);
2017-04-05 08:29:15 +00:00
2017-04-05 08:06:18 +00:00
return output;
}
bool Range::operator==(const Range& that) const {
return start == that.start && end == that.end;
}
2017-09-22 01:14:57 +00:00
bool Range::operator!=(const Range& that) const {
return !(*this == that);
}
2017-04-05 08:06:18 +00:00
bool Range::operator<(const Range& that) const {
if (start != that.start)
return start < that.start;
return end < that.end;
2017-05-19 07:02:01 +00:00
}
// Position
void Reflect(Reader& visitor, Position& value) {
if (visitor.Format() == SerializeFormat::Json) {
std::string s = visitor.GetString();
value = Position(s.c_str());
2018-01-07 09:27:14 +00:00
} else {
Reflect(visitor, value.line);
Reflect(visitor, value.column);
}
2017-05-19 07:02:01 +00:00
}
void Reflect(Writer& visitor, Position& value) {
2018-01-07 09:27:14 +00:00
if (visitor.Format() == SerializeFormat::Json) {
std::string output = value.ToString();
visitor.String(output.c_str(), output.size());
} else {
Reflect(visitor, value.line);
Reflect(visitor, value.column);
}
2017-05-19 07:02:01 +00:00
}
// Range
void Reflect(Reader& visitor, Range& value) {
if (visitor.Format() == SerializeFormat::Json) {
std::string s = visitor.GetString();
value = Range(s.c_str());
2018-01-07 09:27:14 +00:00
} else {
Reflect(visitor, value.start.line);
Reflect(visitor, value.start.column);
Reflect(visitor, value.end.line);
Reflect(visitor, value.end.column);
}
2017-05-19 07:02:01 +00:00
}
void Reflect(Writer& visitor, Range& value) {
2018-01-07 09:27:14 +00:00
if (visitor.Format() == SerializeFormat::Json) {
std::string output = value.ToString();
visitor.String(output.c_str(), output.size());
} else {
Reflect(visitor, value.start.line);
Reflect(visitor, value.start.column);
Reflect(visitor, value.end.line);
Reflect(visitor, value.end.column);
}
}