Distinguish between BufferRanges and InclusiveBufferRanges

Fixes #1257
This commit is contained in:
Maxime Coste 2017-05-08 12:34:57 +01:00
parent f9a609e479
commit 12c498a0bd
4 changed files with 44 additions and 39 deletions

View File

@ -10,37 +10,6 @@
namespace Kakoune namespace Kakoune
{ {
String option_to_string(BufferRange range)
{
return format("{}.{},{}.{}",
range.begin.line+1, range.begin.column+1,
range.end.line+1, range.end.column);
}
void option_from_string(StringView str, BufferRange& opt)
{
auto sep = find_if(str, [](char c){ return c == ',' or c == '+'; });
auto dot_beg = find(StringView{str.begin(), sep}, '.');
auto dot_end = find(StringView{sep, str.end()}, '.');
if (sep == str.end() or dot_beg == sep or
(*sep == ',' and dot_end == str.end()))
throw runtime_error(format("'{}' does not follow <line>.<column>,<line>.<column> or <line>.<column>+<len> format", str));
const BufferCoord beg{str_to_int({str.begin(), dot_beg}) - 1,
str_to_int({dot_beg+1, sep}) - 1};
const bool len = (*sep == '+');
const BufferCoord end{len ? beg.line : str_to_int({sep+1, dot_end}) - 1,
len ? beg.column + str_to_int({sep+1, str.end()})
: str_to_int({dot_end+1, str.end()}) };
if (beg.line < 0 or beg.column < 0 or end.line < 0 or end.column < 0)
throw runtime_error("coordinates elements should be >= 1");
opt = { beg, end };
}
StringView DisplayAtom::content() const StringView DisplayAtom::content() const
{ {
switch (m_type) switch (m_type)

View File

@ -14,9 +14,6 @@ namespace Kakoune
class Buffer; class Buffer;
struct BufferRange{ BufferCoord begin, end; }; struct BufferRange{ BufferCoord begin, end; };
String option_to_string(BufferRange range);
void option_from_string(StringView str, BufferRange& opt);
inline bool operator==(const BufferRange& lhs, const BufferRange& rhs) inline bool operator==(const BufferRange& lhs, const BufferRange& rhs)
{ {
return lhs.begin == rhs.begin and lhs.end == rhs.end; return lhs.begin == rhs.begin and lhs.end == rhs.end;

View File

@ -1274,9 +1274,39 @@ private:
String m_default_face; String m_default_face;
}; };
String option_to_string(InclusiveBufferRange range)
{
return format("{}.{},{}.{}",
range.first.line+1, range.first.column+1,
range.last.line+1, range.last.column+1);
}
BufferCoord& get_first(RangeAndFace& r) { return std::get<0>(r).begin; } void option_from_string(StringView str, InclusiveBufferRange& opt)
BufferCoord& get_last(RangeAndFace& r) { return std::get<0>(r).end; } {
auto sep = find_if(str, [](char c){ return c == ',' or c == '+'; });
auto dot_beg = find(StringView{str.begin(), sep}, '.');
auto dot_end = find(StringView{sep, str.end()}, '.');
if (sep == str.end() or dot_beg == sep or
(*sep == ',' and dot_end == str.end()))
throw runtime_error(format("'{}' does not follow <line>.<column>,<line>.<column> or <line>.<column>+<len> format", str));
const BufferCoord first{str_to_int({str.begin(), dot_beg}) - 1,
str_to_int({dot_beg+1, sep}) - 1};
const bool len = (*sep == '+');
const BufferCoord last{len ? first.line : str_to_int({sep+1, dot_end}) - 1,
len ? first.column + str_to_int({sep+1, str.end()}) - 1
: str_to_int({dot_end+1, str.end()}) - 1 };
if (first.line < 0 or first.column < 0 or last.line < 0 or last.column < 0)
throw runtime_error("coordinates elements should be >= 1");
opt = { first, last };
}
BufferCoord& get_first(RangeAndFace& r) { return std::get<0>(r).first; }
BufferCoord& get_last(RangeAndFace& r) { return std::get<0>(r).last; }
struct RangesHighlighter : Highlighter struct RangesHighlighter : Highlighter
{ {
@ -1308,8 +1338,8 @@ private:
try try
{ {
auto& r = std::get<0>(range); auto& r = std::get<0>(range);
if (buffer.is_valid(r.begin) and buffer.is_valid(r.end)) if (buffer.is_valid(r.first) and buffer.is_valid(r.last))
highlight_range(display_buffer, r.begin, r.end, true, highlight_range(display_buffer, r.first, buffer.char_next(r.last), true,
apply_face(get_face(std::get<1>(range)))); apply_face(get_face(std::get<1>(range))));
} }
catch (runtime_error&) catch (runtime_error&)

View File

@ -9,8 +9,17 @@ namespace Kakoune
void register_highlighters(); void register_highlighters();
struct InclusiveBufferRange{ BufferCoord first, last; };
inline bool operator==(const InclusiveBufferRange& lhs, const InclusiveBufferRange& rhs)
{
return lhs.first == rhs.first and lhs.last == rhs.last;
}
String option_to_string(InclusiveBufferRange range);
void option_from_string(StringView str, InclusiveBufferRange& opt);
using LineAndFlag = std::tuple<LineCount, String>; using LineAndFlag = std::tuple<LineCount, String>;
using RangeAndFace = std::tuple<BufferRange, String>; using RangeAndFace = std::tuple<InclusiveBufferRange, String>;
} }