From 12c498a0bd7fc03dcef88ef2d3564d0c1419eb8a Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 8 May 2017 12:34:57 +0100 Subject: [PATCH] Distinguish between BufferRanges and InclusiveBufferRanges Fixes #1257 --- src/display_buffer.cc | 31 ------------------------------- src/display_buffer.hh | 3 --- src/highlighters.cc | 38 ++++++++++++++++++++++++++++++++++---- src/highlighters.hh | 11 ++++++++++- 4 files changed, 44 insertions(+), 39 deletions(-) diff --git a/src/display_buffer.cc b/src/display_buffer.cc index 1fb82d19..3796cf2a 100644 --- a/src/display_buffer.cc +++ b/src/display_buffer.cc @@ -10,37 +10,6 @@ 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 .,. or .+ 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 { switch (m_type) diff --git a/src/display_buffer.hh b/src/display_buffer.hh index 17c2602f..18b238d8 100644 --- a/src/display_buffer.hh +++ b/src/display_buffer.hh @@ -14,9 +14,6 @@ namespace Kakoune class Buffer; 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) { return lhs.begin == rhs.begin and lhs.end == rhs.end; diff --git a/src/highlighters.cc b/src/highlighters.cc index bd1ba929..595cd3de 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -1274,9 +1274,39 @@ private: 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; } -BufferCoord& get_last(RangeAndFace& r) { return std::get<0>(r).end; } +void option_from_string(StringView str, InclusiveBufferRange& 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 .,. or .+ 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 { @@ -1308,8 +1338,8 @@ private: try { auto& r = std::get<0>(range); - if (buffer.is_valid(r.begin) and buffer.is_valid(r.end)) - highlight_range(display_buffer, r.begin, r.end, true, + if (buffer.is_valid(r.first) and buffer.is_valid(r.last)) + highlight_range(display_buffer, r.first, buffer.char_next(r.last), true, apply_face(get_face(std::get<1>(range)))); } catch (runtime_error&) diff --git a/src/highlighters.hh b/src/highlighters.hh index e4063c52..6435efdc 100644 --- a/src/highlighters.hh +++ b/src/highlighters.hh @@ -9,8 +9,17 @@ namespace Kakoune 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; -using RangeAndFace = std::tuple; +using RangeAndFace = std::tuple; }