Re-work line trimming to fix issues with column highighters

Instead of triming only buffer ranges, add a trim_from method to
display line to keep the initial N columns, we know how many columns
are used by non-trimable widgets in DisplaySetup::widget_columns so
we can just pass this.

Also restore the previous logic for face merging

Fixes #4670
This commit is contained in:
Maxime Coste 2022-07-13 12:24:14 +10:00
parent 195fe8fd29
commit c7fbf1f390
8 changed files with 44 additions and 40 deletions

View File

@ -193,29 +193,45 @@ ColumnCount DisplayLine::length() const
return len;
}
bool DisplayLine::trim(ColumnCount first_col, ColumnCount col_count, bool only_buffer)
bool DisplayLine::trim(ColumnCount front, ColumnCount col_count)
{
for (auto it = begin(); first_col > 0 and it != end(); )
{
if (only_buffer and !it->has_buffer_range())
{
++it;
continue;
}
return trim_from(0_col, front, col_count);
}
bool DisplayLine::trim_from(ColumnCount first_col, ColumnCount front, ColumnCount col_count)
{
auto it = begin();
while (first_col > 0 and it != end())
{
auto len = it->length();
if (len <= first_col)
{
m_atoms.erase(it);
++it;
first_col -= len;
}
else
{
it->trim_begin(first_col);
it = ++split(it, front);
first_col = 0;
}
}
auto it = begin();
while (front > 0 and it != end())
{
auto len = it->length();
if (len <= front)
{
m_atoms.erase(it);
front -= len;
}
else
{
it->trim_begin(front);
front = 0;
}
}
it = begin();
for (; it != end() and col_count > 0; ++it)
col_count -= it->length();

View File

@ -144,9 +144,13 @@ public:
iterator erase(iterator beg, iterator end);
DisplayAtom& push_back(DisplayAtom atom);
// remove first_col from the begining of the line, and make sure
// remove front from the begining of the line, and make sure
// the line is less that col_count character
bool trim(ColumnCount first_col, ColumnCount col_count, bool only_buffer = false);
bool trim(ColumnCount front, ColumnCount col_count);
// remove front from the begining of the line + first_col, and make sure
// the line is less that col_count character
bool trim_from(ColumnCount first_col, ColumnCount front, ColumnCount col_count);
// Merge together consecutive atoms sharing the same display attributes
void optimize();

View File

@ -65,10 +65,10 @@ inline Face merge_faces(const Face& base, const Face& face)
};
auto choose = [&](Color Face::*color, Attribute final_attr) {
if (base.attributes & final_attr)
return base.*color;
if (face.attributes & final_attr)
return face.*color;
if (base.attributes & final_attr)
return base.*color;
if (face.*color == Color::Default)
return base.*color;
if ((base.*color).isRGB() and (face.*color).isRGB() and (face.*color).a != 255)

View File

@ -558,17 +558,6 @@ std::unique_ptr<Highlighter> create_dynamic_regex_highlighter(HighlighterParamet
return make_hl(get_regex, get_face);
}
namespace
{
Face make_final(Face face)
{
face.attributes |= Attribute::Final;
return face;
}
}
const HighlighterDesc line_desc = {
"Parameters: <value string> <face>\n"
"Highlight the line given by evaluating <value string> with <face>",
@ -615,7 +604,7 @@ std::unique_ptr<Highlighter> create_line_highlighter(HighlighterParameters param
}
const ColumnCount remaining = context.context.window().dimensions().column - column;
if (remaining > 0)
it->push_back({ String{' ', remaining}, make_final(face) });
it->push_back({String{' ', remaining}, face});
};
return make_highlighter(std::move(func));
@ -652,18 +641,13 @@ std::unique_ptr<Highlighter> create_column_highlighter(HighlighterParameters par
if (column < context.setup.first_column or column >= context.setup.first_column + context.context.window().dimensions().column)
return;
const Buffer& buffer = context.context.buffer();
column += context.setup.widget_columns;
for (auto& line : display_buffer.lines())
{
auto remaining_col = column;
bool found = false;
auto first_buf = find_if(line, [](auto& atom) { return atom.has_buffer_range(); });
BufferCoord last_pos{};
for (auto atom_it = first_buf; atom_it != line.end(); ++atom_it)
for (auto atom_it = line.begin(); atom_it != line.end(); ++atom_it)
{
if (atom_it->has_buffer_range())
last_pos = atom_it->end();
const auto atom_len = atom_it->length();
if (remaining_col < atom_len)
{
@ -681,8 +665,8 @@ std::unique_ptr<Highlighter> create_column_highlighter(HighlighterParameters par
continue;
if (remaining_col > 0)
line.push_back({buffer, last_pos, last_pos, String{' ', remaining_col}, make_final(Face{})});
line.push_back({buffer, last_pos, last_pos, " ", make_final(face)});
line.push_back({String{' ', remaining_col}, Face{}});
line.push_back({" ", face});
}
};

View File

@ -159,7 +159,7 @@ const DisplayBuffer& Window::update_display_buffer(const Context& context)
m_builtin_highlighters.highlight({context, setup, pass, {}}, m_display_buffer, range);
for (auto& line : m_display_buffer.lines())
line.trim(setup.first_column, m_dimensions.column, true);
line.trim_from(setup.widget_columns, setup.first_column, m_dimensions.column);
m_display_buffer.optimize();

View File

@ -1,2 +1,2 @@
ui_out -ignore 1
ui_out '{ "jsonrpc": "2.0", "method": "draw", "params": [[[{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " 1│" }, { "face": { "fg": "black", "bg": "white", "underline": "default", "attributes": [] }, "contents": "a" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "\u000a" }, { "face": { "fg": "default", "bg": "red", "underline": "default", "attributes": ["final_fg","final_bg","final_attr"] }, "contents": " " }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": ["final_fg","final_bg","final_attr"] }, "contents": " " }, { "face": { "fg": "default", "bg": "green", "underline": "default", "attributes": ["final_fg","final_bg","final_attr"] }, "contents": " " }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " 2│" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "default", "bg": "red", "underline": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "default", "bg": "blue", "underline": "default", "attributes": [] }, "contents": "a" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "b" }, { "face": { "fg": "default", "bg": "green", "underline": "default", "attributes": [] }, "contents": "\u000a" }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " 3│" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "ab" }, { "face": { "fg": "default", "bg": "red", "underline": "default", "attributes": [] }, "contents": "c" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "\u000a" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": ["final_fg","final_bg","final_attr"] }, "contents": " " }, { "face": { "fg": "default", "bg": "green", "underline": "default", "attributes": ["final_fg","final_bg","final_attr"] }, "contents": " " }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " 4│" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "ab" }, { "face": { "fg": "default", "bg": "red", "underline": "default", "attributes": [] }, "contents": "c" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "d" }, { "face": { "fg": "default", "bg": "blue", "underline": "default", "attributes": [] }, "contents": "\u000a" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": ["final_fg","final_bg","final_attr"] }, "contents": " " }, { "face": { "fg": "default", "bg": "green", "underline": "default", "attributes": ["final_fg","final_bg","final_attr"] }, "contents": " " }]], { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, { "fg": "blue", "bg": "default", "underline": "default", "attributes": [] }] }'
ui_out '{ "jsonrpc": "2.0", "method": "draw", "params": [[[{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " 1│" }, { "face": { "fg": "black", "bg": "white", "underline": "default", "attributes": [] }, "contents": "a" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "\u000a" }, { "face": { "fg": "default", "bg": "red", "underline": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "default", "bg": "blue", "underline": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "default", "bg": "green", "underline": "default", "attributes": [] }, "contents": " " }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " 2│" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "default", "bg": "red", "underline": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "default", "bg": "blue", "underline": "default", "attributes": [] }, "contents": "a" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "b" }, { "face": { "fg": "default", "bg": "green", "underline": "default", "attributes": [] }, "contents": "\u000a" }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " 3│" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "ab" }, { "face": { "fg": "default", "bg": "red", "underline": "default", "attributes": [] }, "contents": "c" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "\u000a" }, { "face": { "fg": "default", "bg": "blue", "underline": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "default", "bg": "green", "underline": "default", "attributes": [] }, "contents": " " }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " 4│" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "ab" }, { "face": { "fg": "default", "bg": "red", "underline": "default", "attributes": [] }, "contents": "c" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "d" }, { "face": { "fg": "default", "bg": "blue", "underline": "default", "attributes": [] }, "contents": "\u000a" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "default", "bg": "green", "underline": "default", "attributes": [] }, "contents": " " }]], { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, { "fg": "blue", "bg": "default", "underline": "default", "attributes": [] }] }'

View File

@ -1,2 +1,2 @@
ui_out -ignore 1
ui_out '{ "jsonrpc": "2.0", "method": "draw", "params": [[[{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "xxxxxxxxxx" }, { "face": { "fg": "default", "bg": "blue", "underline": "default", "attributes": [] }, "contents": "x" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" }, { "face": { "fg": "black", "bg": "white", "underline": "default", "attributes": [] }, "contents": "x" }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "xxx\u000a" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": ["final_fg","final_bg","final_attr"] }, "contents": " " }, { "face": { "fg": "default", "bg": "blue", "underline": "default", "attributes": ["final_fg","final_bg","final_attr"] }, "contents": " " }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": ["final_fg","final_bg","final_attr"] }, "contents": " " }, { "face": { "fg": "default", "bg": "blue", "underline": "default", "attributes": ["final_fg","final_bg","final_attr"] }, "contents": " " }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": ["final_fg","final_bg","final_attr"] }, "contents": " " }, { "face": { "fg": "default", "bg": "blue", "underline": "default", "attributes": ["final_fg","final_bg","final_attr"] }, "contents": " " }]], { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, { "fg": "blue", "bg": "default", "underline": "default", "attributes": [] }] }'
ui_out '{ "jsonrpc": "2.0", "method": "draw", "params": [[[{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "xxxxxxxxxx" }, { "face": { "fg": "default", "bg": "blue", "underline": "default", "attributes": [] }, "contents": "x" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" }, { "face": { "fg": "black", "bg": "white", "underline": "default", "attributes": [] }, "contents": "x" }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "xxx\u000a" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "default", "bg": "blue", "underline": "default", "attributes": [] }, "contents": " " }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "default", "bg": "blue", "underline": "default", "attributes": [] }, "contents": " " }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "default", "bg": "blue", "underline": "default", "attributes": [] }, "contents": " " }]], { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, { "fg": "blue", "bg": "default", "underline": "default", "attributes": [] }] }'

View File

@ -1,2 +1,2 @@
ui_out -ignore 1
ui_out '{ "jsonrpc": "2.0", "method": "draw", "params": [[[{ "face": { "fg": "white", "bg": "blue", "underline": "default", "attributes": [] }, "contents": "foo\u000a" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": ["final_fg","final_bg","final_attr"] }, "contents": " " }, { "face": { "fg": "default", "bg": "red", "underline": "default", "attributes": ["final_fg","final_bg","final_attr"] }, "contents": " " }], [{ "face": { "fg": "black", "bg": "white", "underline": "default", "attributes": [] }, "contents": "b" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "ar\u000a" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": ["final_fg","final_bg","final_attr"] }, "contents": " " }, { "face": { "fg": "default", "bg": "red", "underline": "default", "attributes": ["final_fg","final_bg","final_attr"] }, "contents": " " }]], { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, { "fg": "blue", "bg": "default", "underline": "default", "attributes": [] }] }'
ui_out '{ "jsonrpc": "2.0", "method": "draw", "params": [[[{ "face": { "fg": "white", "bg": "blue", "underline": "default", "attributes": [] }, "contents": "foo\u000a" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "default", "bg": "red", "underline": "default", "attributes": [] }, "contents": " " }], [{ "face": { "fg": "black", "bg": "white", "underline": "default", "attributes": [] }, "contents": "b" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "ar\u000a" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": " " }, { "face": { "fg": "default", "bg": "red", "underline": "default", "attributes": [] }, "contents": " " }]], { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, { "fg": "blue", "bg": "default", "underline": "default", "attributes": [] }] }'