Fix highlighting empty ranges at end of line and begin of line

This commit is contained in:
Maxime Coste 2020-04-25 12:17:39 +10:00
parent fde4c60dd9
commit 0fdfbdfd15
5 changed files with 32 additions and 13 deletions

View File

@ -89,13 +89,18 @@ void replace_range(DisplayBuffer& display_buffer,
BufferCoord begin, BufferCoord end, T func) BufferCoord begin, BufferCoord end, T func)
{ {
// tolerate begin > end as that can be triggered by wrong encodings // tolerate begin > end as that can be triggered by wrong encodings
if (begin > end or end <= display_buffer.range().begin if (begin > end or end < display_buffer.range().begin or begin > display_buffer.range().end)
or begin >= display_buffer.range().end)
return; return;
for (auto& line : display_buffer.lines()) for (auto& line : display_buffer.lines())
{ {
auto& range = line.range(); auto& range = line.range();
if ((begin == end) and begin == range.end)
{
func(line, line.atoms().size(), line.atoms().size());
continue;
}
if (range.end <= begin or end < range.begin) if (range.end <= begin or end < range.begin)
continue; continue;
@ -103,7 +108,7 @@ void replace_range(DisplayBuffer& display_buffer,
for (auto atom_it = line.begin(); atom_it != line.end(); ++atom_it) for (auto atom_it = line.begin(); atom_it != line.end(); ++atom_it)
{ {
if (not atom_it->has_buffer_range() or if (not atom_it->has_buffer_range() or
end <= atom_it->begin() or begin >= atom_it->end()) end < atom_it->begin() or begin >= atom_it->end())
continue; continue;
if (begin >= atom_it->begin()) if (begin >= atom_it->begin())
@ -1573,21 +1578,23 @@ private:
auto& range_and_faces = get_option(context); auto& range_and_faces = get_option(context);
update_ranges(buffer, range_and_faces.prefix, range_and_faces.list); update_ranges(buffer, range_and_faces.prefix, range_and_faces.list);
auto is_valid = [&buffer](BufferCoord c) {
return c.line >= 0 and c.column >= 0 and c.line < buffer.line_count() and c.column <= buffer[c.line].length();
};
for (auto& [range, spec] : range_and_faces.list) for (auto& [range, spec] : range_and_faces.list)
{ {
try try
{ {
if (buffer.is_valid(range.first) and (buffer.is_valid(range.last) or is_empty(range))) if (!is_valid(range.first) or (!is_empty(range) and !is_valid(range.last)))
{ continue;
auto replacement = parse_display_line(spec, context.context.faces()); auto replacement = parse_display_line(spec, context.context.faces());
replace_range(display_buffer, range.first, is_empty(range) ? range.first : buffer.char_next(range.last), replace_range(display_buffer, range.first, is_empty(range) ? range.first : buffer.char_next(range.last),
[&](DisplayLine& line, int beg_idx, int end_idx){ [&](DisplayLine& line, int beg_idx, int end_idx){
auto it = line.erase(line.begin() + beg_idx, line.begin() + end_idx); auto it = line.erase(line.begin() + beg_idx, line.begin() + end_idx);
for (auto& atom : replacement) std::move(replacement.begin(), replacement.end(), std::inserter(line, it));
it = ++line.insert(it, std::move(atom));
}); });
} }
}
catch (runtime_error&) catch (runtime_error&)
{} {}
} }

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,2 @@
12345
12345

View File

@ -0,0 +1,2 @@
declare-option range-specs test_ranges %val{timestamp} '1.1+0|A' '1.7+0|B' '2.3+0|C'
add-highlighter window/ replace-ranges test_ranges

View File

@ -0,0 +1,7 @@
ui_out '{ "jsonrpc": "2.0", "method": "set_ui_options", "params": [{}] }'
ui_out '{ "jsonrpc": "2.0", "method": "draw", "params": [[[{ "face": { "fg": "default", "bg": "default", "attributes": [] }, "contents": "A" }, { "face": { "fg": "black", "bg": "white", "attributes": [] }, "contents": "1" }, { "face": { "fg": "default", "bg": "default", "attributes": [] }, "contents": "2345\u000a" }, { "face": { "fg": "default", "bg": "default", "attributes": [] }, "contents": "B" }], [{ "face": { "fg": "default", "bg": "default", "attributes": [] }, "contents": "12" }, { "face": { "fg": "default", "bg": "default", "attributes": [] }, "contents": "C" }, { "face": { "fg": "default", "bg": "default", "attributes": [] }, "contents": "345\u000a" }]], { "fg": "default", "bg": "default", "attributes": [] }, { "fg": "blue", "bg": "default", "attributes": [] }] }'
ui_out '{ "jsonrpc": "2.0", "method": "menu_hide", "params": [] }'
ui_out '{ "jsonrpc": "2.0", "method": "info_hide", "params": [] }'
ui_out '{ "jsonrpc": "2.0", "method": "draw_status", "params": [[], [{ "face": { "fg": "default", "bg": "default", "attributes": [] }, "contents": "out 1:1 " }, { "face": { "fg": "black", "bg": "yellow", "attributes": [] }, "contents": "" }, { "face": { "fg": "default", "bg": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "blue", "bg": "default", "attributes": [] }, "contents": "1 sel" }, { "face": { "fg": "default", "bg": "default", "attributes": [] }, "contents": " - client0@[kak-tests]" }], { "fg": "cyan", "bg": "default", "attributes": [] }] }'
ui_out '{ "jsonrpc": "2.0", "method": "set_cursor", "params": ["buffer", { "line": 0, "column": 1 }] }'
ui_out '{ "jsonrpc": "2.0", "method": "refresh", "params": [true] }'