decorations: right_align and win_col

This commit is contained in:
Björn Linse
2021-04-15 20:57:23 +02:00
parent 59eae3b38f
commit 0865f1238f
5 changed files with 227 additions and 60 deletions

View File

@@ -1426,6 +1426,10 @@ Array nvim_buf_get_extmarks(Buffer buffer, Integer ns_id,
/// - "eol": right after eol character (default) /// - "eol": right after eol character (default)
/// - "overlay": display over the specified column, without /// - "overlay": display over the specified column, without
/// shifting the underlying text. /// shifting the underlying text.
/// - "right_align": display right aligned in the window.
/// - virt_text_win_col : position the virtual text at a fixed
/// window column (starting from the first
/// text column)
/// - virt_text_hide : hide the virtual text when the background /// - virt_text_hide : hide the virtual text when the background
/// text is selected or hidden due to /// text is selected or hidden due to
/// horizontal scroll 'nowrap' /// horizontal scroll 'nowrap'
@@ -1574,11 +1578,22 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id,
decor.virt_text_pos = kVTEndOfLine; decor.virt_text_pos = kVTEndOfLine;
} else if (strequal("overlay", str.data)) { } else if (strequal("overlay", str.data)) {
decor.virt_text_pos = kVTOverlay; decor.virt_text_pos = kVTOverlay;
} else if (strequal("right_align", str.data)) {
decor.virt_text_pos = kVTRightAlign;
} else { } else {
api_set_error(err, kErrorTypeValidation, api_set_error(err, kErrorTypeValidation,
"virt_text_pos: invalid value"); "virt_text_pos: invalid value");
goto error; goto error;
} }
} else if (strequal("virt_text_win_col", k.data)) {
if (v->type != kObjectTypeInteger) {
api_set_error(err, kErrorTypeValidation,
"virt_text_win_col is not a Number of the correct size");
goto error;
}
decor.col = (int)v->data.integer;
decor.virt_text_pos = kVTWinCol;
} else if (strequal("virt_text_hide", k.data)) { } else if (strequal("virt_text_hide", k.data)) {
decor.virt_text_hide = api_object_to_bool(*v, decor.virt_text_hide = api_object_to_bool(*v,
"virt_text_hide", false, err); "virt_text_hide", false, err);
@@ -1673,6 +1688,14 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id,
col2 = 0; col2 = 0;
} }
if (decor.virt_text_pos == kVTRightAlign) {
decor.col = 0;
for (size_t i = 0; i < kv_size(decor.virt_text); i++) {
decor.col += mb_string2cells((char_u *)kv_A(decor.virt_text, i).text);
}
}
Decoration *d = NULL; Decoration *d = NULL;
if (ephemeral) { if (ephemeral) {

View File

@@ -230,6 +230,10 @@ static void decor_add(DecorState *state, int start_row, int start_col,
*decor, attr_id, *decor, attr_id,
kv_size(decor->virt_text) && owned, -1 }; kv_size(decor->virt_text) && owned, -1 };
if (decor->virt_text_pos == kVTEndOfLine) {
range.win_col = -2; // handled separately
}
kv_pushp(state->active); kv_pushp(state->active);
size_t index; size_t index;
for (index = kv_size(state->active)-1; index > 0; index--) { for (index = kv_size(state->active)-1; index > 0; index--) {
@@ -242,7 +246,7 @@ static void decor_add(DecorState *state, int start_row, int start_col,
kv_A(state->active, index) = range; kv_A(state->active, index) = range;
} }
int decor_redraw_col(buf_T *buf, int col, int virt_col, bool hidden, int decor_redraw_col(buf_T *buf, int col, int win_col, bool hidden,
DecorState *state) DecorState *state)
{ {
if (col <= state->col_until) { if (col <= state->col_until) {
@@ -321,8 +325,9 @@ next_mark:
attr = hl_combine_attr(attr, item.attr_id); attr = hl_combine_attr(attr, item.attr_id);
} }
if ((item.start_row == state->row && item.start_col <= col) if ((item.start_row == state->row && item.start_col <= col)
&& kv_size(item.decor.virt_text) && item.virt_col == -1) { && kv_size(item.decor.virt_text)
item.virt_col = (item.decor.virt_text_hide && hidden) ? -2 : virt_col; && item.decor.virt_text_pos == kVTOverlay && item.win_col == -1) {
item.win_col = (item.decor.virt_text_hide && hidden) ? -2 : win_col;
} }
if (keep) { if (keep) {
kv_A(state->active, j++) = item; kv_A(state->active, j++) = item;
@@ -340,17 +345,22 @@ void decor_redraw_end(DecorState *state)
state->buf = NULL; state->buf = NULL;
} }
VirtText decor_redraw_eol(buf_T *buf, DecorState *state, int *eol_attr) VirtText decor_redraw_eol(buf_T *buf, DecorState *state, int *eol_attr,
bool *aligned)
{ {
decor_redraw_col(buf, MAXCOL, MAXCOL, false, state); decor_redraw_col(buf, MAXCOL, MAXCOL, false, state);
VirtText text = VIRTTEXT_EMPTY; VirtText text = VIRTTEXT_EMPTY;
for (size_t i = 0; i < kv_size(state->active); i++) { for (size_t i = 0; i < kv_size(state->active); i++) {
DecorRange item = kv_A(state->active, i); DecorRange item = kv_A(state->active, i);
if (!kv_size(text) if (item.start_row == state->row && kv_size(item.decor.virt_text)) {
&& item.start_row == state->row && kv_size(item.decor.virt_text) if (!kv_size(text) && item.decor.virt_text_pos == kVTEndOfLine) {
&& item.decor.virt_text_pos == kVTEndOfLine) {
text = item.decor.virt_text; text = item.decor.virt_text;
} else if (item.decor.virt_text_pos == kVTRightAlign
|| item.decor.virt_text_pos == kVTWinCol) {
*aligned = true;
} }
}
if (item.decor.hl_eol && item.start_row <= state->row) { if (item.decor.hl_eol && item.start_row <= state->row) {
*eol_attr = hl_combine_attr(*eol_attr, item.attr_id); *eol_attr = hl_combine_attr(*eol_attr, item.attr_id);

View File

@@ -21,6 +21,8 @@ typedef uint16_t DecorPriority;
typedef enum { typedef enum {
kVTEndOfLine, kVTEndOfLine,
kVTOverlay, kVTOverlay,
kVTWinCol,
kVTRightAlign,
} VirtTextPos; } VirtTextPos;
typedef enum { typedef enum {
@@ -41,9 +43,10 @@ struct Decoration
// TODO(bfredl): style, signs, etc // TODO(bfredl): style, signs, etc
DecorPriority priority; DecorPriority priority;
bool shared; // shared decoration, don't free bool shared; // shared decoration, don't free
int col; // fixed col value, like win_col
}; };
#define DECORATION_INIT { 0, KV_INITIAL_VALUE, kVTEndOfLine, false, \ #define DECORATION_INIT { 0, KV_INITIAL_VALUE, kVTEndOfLine, false, \
kHlModeUnknown, false, DECOR_PRIORITY_BASE, false } kHlModeUnknown, false, DECOR_PRIORITY_BASE, false, 0 }
typedef struct { typedef struct {
int start_row; int start_row;
@@ -53,7 +56,7 @@ typedef struct {
Decoration decor; Decoration decor;
int attr_id; // cached lookup of decor.hl_id int attr_id; // cached lookup of decor.hl_id
bool virt_text_owned; bool virt_text_owned;
int virt_col; int win_col;
} DecorRange; } DecorRange;
typedef struct { typedef struct {

View File

@@ -2101,6 +2101,7 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
bool search_attr_from_match = false; // if search_attr is from :match bool search_attr_from_match = false; // if search_attr is from :match
bool has_decor = false; // this buffer has decoration bool has_decor = false; // this buffer has decoration
bool do_virttext = false; // draw virtual text for this line bool do_virttext = false; // draw virtual text for this line
int win_col_offset; // offsett for window columns
char_u buf_fold[FOLD_TEXT_LEN + 1]; // Hold value returned by get_foldtext char_u buf_fold[FOLD_TEXT_LEN + 1]; // Hold value returned by get_foldtext
@@ -2790,6 +2791,10 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
} }
} }
if (draw_state == WL_NR && n_extra == 0) {
win_col_offset = off;
}
if (wp->w_briopt_sbr && draw_state == WL_BRI - 1 if (wp->w_briopt_sbr && draw_state == WL_BRI - 1
&& n_extra == 0 && *p_sbr != NUL) { && n_extra == 0 && *p_sbr != NUL) {
// draw indent after showbreak value // draw indent after showbreak value
@@ -2904,7 +2909,7 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
&& vcol >= (long)wp->w_virtcol) && vcol >= (long)wp->w_virtcol)
|| (number_only && draw_state > WL_NR)) || (number_only && draw_state > WL_NR))
&& filler_todo <= 0) { && filler_todo <= 0) {
draw_virt_text(buf, &col, grid->Columns); draw_virt_text(buf, win_col_offset, &col, grid->Columns);
grid_put_linebuf(grid, row, 0, col, -grid->Columns, wp->w_p_rl, wp, grid_put_linebuf(grid, row, 0, col, -grid->Columns, wp->w_p_rl, wp,
wp->w_hl_attr_normal, false); wp->w_hl_attr_normal, false);
// Pretend we have finished updating the window. Except when // Pretend we have finished updating the window. Except when
@@ -3945,13 +3950,15 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
draw_color_col = advance_color_col(VCOL_HLC, &color_cols); draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
VirtText virt_text = KV_INITIAL_VALUE; VirtText virt_text = KV_INITIAL_VALUE;
bool has_aligned = false;
if (err_text) { if (err_text) {
int hl_err = syn_check_group((char_u *)S_LEN("ErrorMsg")); int hl_err = syn_check_group((char_u *)S_LEN("ErrorMsg"));
kv_push(virt_text, ((VirtTextChunk){ .text = err_text, kv_push(virt_text, ((VirtTextChunk){ .text = err_text,
.hl_id = hl_err })); .hl_id = hl_err }));
do_virttext = true; do_virttext = true;
} else if (has_decor) { } else if (has_decor) {
virt_text = decor_redraw_eol(wp->w_buffer, &decor_state, &line_attr); virt_text = decor_redraw_eol(wp->w_buffer, &decor_state, &line_attr,
&has_aligned);
if (kv_size(virt_text)) { if (kv_size(virt_text)) {
do_virttext = true; do_virttext = true;
} }
@@ -3963,7 +3970,8 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
grid->Columns * (row - startrow + 1) + v grid->Columns * (row - startrow + 1) + v
&& lnum != wp->w_cursor.lnum) && lnum != wp->w_cursor.lnum)
|| draw_color_col || line_attr_lowprio || line_attr || draw_color_col || line_attr_lowprio || line_attr
|| diff_hlf != (hlf_T)0 || do_virttext)) { || diff_hlf != (hlf_T)0 || do_virttext
|| has_aligned)) {
int rightmost_vcol = 0; int rightmost_vcol = 0;
int i; int i;
@@ -4001,7 +4009,7 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
} }
int base_attr = hl_combine_attr(line_attr_lowprio, diff_attr); int base_attr = hl_combine_attr(line_attr_lowprio, diff_attr);
if (base_attr || line_attr) { if (base_attr || line_attr || has_aligned) {
rightmost_vcol = INT_MAX; rightmost_vcol = INT_MAX;
} }
@@ -4079,7 +4087,7 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
} }
} }
draw_virt_text(buf, &col, grid->Columns); draw_virt_text(buf, win_col_offset, &col, grid->Columns);
grid_put_linebuf(grid, row, 0, col, grid->Columns, wp->w_p_rl, wp, grid_put_linebuf(grid, row, 0, col, grid->Columns, wp->w_p_rl, wp,
wp->w_hl_attr_normal, false); wp->w_hl_attr_normal, false);
row++; row++;
@@ -4300,7 +4308,7 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
&& !wp->w_p_rl; // Not right-to-left. && !wp->w_p_rl; // Not right-to-left.
int draw_col = col - boguscols; int draw_col = col - boguscols;
draw_virt_text(buf, &draw_col, grid->Columns); draw_virt_text(buf, win_col_offset, &draw_col, grid->Columns);
grid_put_linebuf(grid, row, 0, draw_col, grid->Columns, wp->w_p_rl, grid_put_linebuf(grid, row, 0, draw_col, grid->Columns, wp->w_p_rl,
wp, wp->w_hl_attr_normal, wrap); wp, wp->w_hl_attr_normal, wrap);
if (wrap) { if (wrap) {
@@ -4377,21 +4385,31 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow,
return row; return row;
} }
void draw_virt_text(buf_T *buf, int *end_col, int max_col) void draw_virt_text(buf_T *buf, int col_off, int *end_col, int max_col)
{ {
DecorState *state = &decor_state; DecorState *state = &decor_state;
int right_pos = max_col;
for (size_t i = 0; i < kv_size(state->active); i++) { for (size_t i = 0; i < kv_size(state->active); i++) {
DecorRange *item = &kv_A(state->active, i); DecorRange *item = &kv_A(state->active, i);
if (item->start_row == state->row && kv_size(item->decor.virt_text) if (item->start_row == state->row && kv_size(item->decor.virt_text)) {
&& item->decor.virt_text_pos == kVTOverlay if (item->win_col == -1) {
&& item->virt_col >= 0) { if (item->decor.virt_text_pos == kVTRightAlign) {
right_pos -= item->decor.col;
item->win_col = right_pos;
} else if (item->decor.virt_text_pos == kVTWinCol) {
item->win_col = MAX(item->decor.col+col_off, 0);
}
}
if (item->win_col < 0) {
continue;
}
VirtText vt = item->decor.virt_text; VirtText vt = item->decor.virt_text;
HlMode hl_mode = item->decor.hl_mode; HlMode hl_mode = item->decor.hl_mode;
LineState s = LINE_STATE(""); LineState s = LINE_STATE("");
int virt_attr = 0; int virt_attr = 0;
int col = item->virt_col; int col = item->win_col;
size_t virt_pos = 0; size_t virt_pos = 0;
item->virt_col = -2; // deactivate item->win_col = -2; // deactivate
while (col < max_col) { while (col < max_col) {
if (!*s.p) { if (!*s.p) {

View File

@@ -333,6 +333,35 @@ describe('decorations providers', function()
]]} ]]}
end) end)
it('can have virtual text of the style: right_align', function()
insert(mulholland)
setup_provider [[
local hl = a.nvim_get_hl_id_by_name "ErrorMsg"
local test_ns = a.nvim_create_namespace "mulholland"
function on_do(event, ...)
if event == "line" then
local win, buf, line = ...
a.nvim_buf_set_extmark(buf, test_ns, line, 0, {
virt_text = {{'+'}, {string.rep(' ', line+1), 'ErrorMsg'}};
virt_text_pos='right_align';
ephemeral = true;
})
end
end
]]
screen:expect{grid=[[
// just to see if there was an acciden+{2: }|
// on Mulholland Drive +{2: }|
try_start(); +{2: }|
bufref_T save_buf; +{2: }|
switch_buffer(&save_buf, buf); +{2: }|
posp = getmark(mark, false); +{2: }|
restore_buffer(&save_buf);^ +{2: }|
|
]]}
end)
it('can highlight beyond EOL', function() it('can highlight beyond EOL', function()
insert(mulholland) insert(mulholland)
setup_provider [[ setup_provider [[
@@ -366,7 +395,7 @@ describe('decorations providers', function()
end) end)
describe('extmark decorations', function() describe('extmark decorations', function()
local screen local screen, ns
before_each( function() before_each( function()
clear() clear()
screen = Screen.new(50, 15) screen = Screen.new(50, 15)
@@ -397,6 +426,8 @@ describe('extmark decorations', function()
[23] = {foreground = Screen.colors.Magenta1, background = Screen.colors.LightGrey}; [23] = {foreground = Screen.colors.Magenta1, background = Screen.colors.LightGrey};
[24] = {bold = true}; [24] = {bold = true};
} }
ns = meths.create_namespace 'test'
end) end)
local example_text = [[ local example_text = [[
@@ -417,7 +448,6 @@ end]]
insert(example_text) insert(example_text)
feed 'gg' feed 'gg'
local ns = meths.create_namespace 'test'
for i = 1,9 do for i = 1,9 do
meths.buf_set_extmark(0, ns, i, 0, { virt_text={{'|', 'LineNr'}}, virt_text_pos='overlay'}) meths.buf_set_extmark(0, ns, i, 0, { virt_text={{'|', 'LineNr'}}, virt_text_pos='overlay'})
if i == 3 or (i >= 6 and i <= 9) then if i == 3 or (i >= 6 and i <= 9) then
@@ -484,7 +514,6 @@ end]]
it('can have virtual text of overlay position and styling', function() it('can have virtual text of overlay position and styling', function()
insert(example_text) insert(example_text)
feed 'gg' feed 'gg'
local ns = meths.create_namespace 'test'
command 'set ft=lua' command 'set ft=lua'
command 'syntax on' command 'syntax on'
@@ -572,4 +601,88 @@ end]]
{24:-- VISUAL LINE --} | {24:-- VISUAL LINE --} |
]]} ]]}
end) end)
it('can have virtual text of fixed win_col position', function()
insert(example_text)
feed 'gg'
meths.buf_set_extmark(0, ns, 1, 0, { virt_text={{'Very', 'ErrorMsg'}}, virt_text_win_col=31, hl_mode='blend'})
meths.buf_set_extmark(0, ns, 2, 10, { virt_text={{'Much', 'ErrorMsg'}}, virt_text_win_col=31, hl_mode='blend'})
meths.buf_set_extmark(0, ns, 3, 15, { virt_text={{'Error', 'ErrorMsg'}}, virt_text_win_col=31, hl_mode='blend'})
meths.buf_set_extmark(0, ns, 7, 21, { virt_text={{'-', 'NonText'}}, virt_text_win_col=4, hl_mode='blend'})
screen:expect{grid=[[
^for _,item in ipairs(items) do |
local text, hl_id_cell, cou{4:Very} unpack(item) |
if hl_id_cell ~= nil then {4:Much} |
hl_id = hl_id_cell {4:Error} |
end |
for _ = 1, (count or 1) do |
local cell = line[colpos] |
{1:-} cell.text = text |
cell.hl_id = hl_id |
colpos = colpos+1 |
end |
end |
{1:~ }|
{1:~ }|
|
]]}
feed '3G12|i<cr><esc>'
screen:expect{grid=[[
for _,item in ipairs(items) do |
local text, hl_id_cell, cou{4:Very} unpack(item) |
if hl_i {4:Much} |
^d_cell ~= nil then |
hl_id = hl_id_cell {4:Error} |
end |
for _ = 1, (count or 1) do |
local cell = line[colpos] |
{1:-} cell.text = text |
cell.hl_id = hl_id |
colpos = colpos+1 |
end |
end |
{1:~ }|
|
]]}
feed 'u:<cr>'
screen:expect{grid=[[
for _,item in ipairs(items) do |
local text, hl_id_cell, cou{4:Very} unpack(item) |
if hl_i^d_cell ~= nil then {4:Much} |
hl_id = hl_id_cell {4:Error} |
end |
for _ = 1, (count or 1) do |
local cell = line[colpos] |
{1:-} cell.text = text |
cell.hl_id = hl_id |
colpos = colpos+1 |
end |
end |
{1:~ }|
{1:~ }|
: |
]]}
feed '8|i<cr><esc>'
screen:expect{grid=[[
for _,item in ipairs(items) do |
local text, hl_id_cell, cou{4:Very} unpack(item) |
if |
^hl_id_cell ~= nil then {4:Much} |
hl_id = hl_id_cell {4:Error} |
end |
for _ = 1, (count or 1) do |
local cell = line[colpos] |
{1:-} cell.text = text |
cell.hl_id = hl_id |
colpos = colpos+1 |
end |
end |
{1:~ }|
|
]]}
end)
end) end)