feat(extmark): window scoped extmark

Co-authored-by: zeertzjq <zeertzjq@outlook.com>
This commit is contained in:
altermo 2024-02-06 11:52:42 +01:00
parent 6d8bbfe19d
commit 1c032ad703
15 changed files with 520 additions and 21 deletions

View File

@ -2791,6 +2791,8 @@ nvim_buf_set_extmark({buffer}, {ns_id}, {line}, {col}, {*opts})
• url: A URL to associate with this extmark. In the TUI, the
OSC 8 control sequence is used to generate a clickable
hyperlink to this URL.
• scoped: boolean that indicates that the extmark should
only be displayed in the namespace scope. (experimental)
Return: ~
Id of the created/updated extmark
@ -2865,6 +2867,35 @@ nvim_set_decoration_provider({ns_id}, {*opts})
winid, bufnr, row]
• on_end: called at the end of a redraw cycle ["end", tick]
nvim_win_add_ns({window}, {ns_id}) *nvim_win_add_ns()*
Adds the namespace scope to the window.
Parameters: ~
• {window} Window handle, or 0 for current window
• {ns_id} the namespace to add
Return: ~
true if the namespace was added, else false
nvim_win_get_ns({window}) *nvim_win_get_ns()*
Gets all the namespaces scopes associated with a window.
Parameters: ~
• {window} Window handle, or 0 for current window
Return: ~
a list of namespaces ids
nvim_win_remove_ns({window}, {ns_id}) *nvim_win_remove_ns()*
Removes the namespace scope from the window.
Parameters: ~
• {window} Window handle, or 0 for current window
• {ns_id} the namespace to remove
Return: ~
true if the namespace was removed, else false
==============================================================================
Window Functions *api-window*

View File

@ -318,6 +318,10 @@ The following new APIs and features were added.
• Clicking on a tabpage in the tabline with the middle mouse button closes it.
• |namespace| can now have window scopes. |nvim_win_add_ns()|
• |extmarks| option `scoped`: only show the extmarks in its namespace's scope.
==============================================================================
CHANGED FEATURES *news-changed*

View File

@ -613,6 +613,8 @@ function vim.api.nvim_buf_line_count(buffer) end
--- • url: A URL to associate with this extmark. In the TUI, the
--- OSC 8 control sequence is used to generate a clickable
--- hyperlink to this URL.
--- • scoped: boolean that indicates that the extmark should
--- only be displayed in the namespace scope. (experimental)
--- @return integer
function vim.api.nvim_buf_set_extmark(buffer, ns_id, line, col, opts) end
@ -1982,6 +1984,13 @@ function vim.api.nvim_tabpage_set_var(tabpage, name, value) end
--- @param win integer Window handle, must already belong to {tabpage}
function vim.api.nvim_tabpage_set_win(tabpage, win) end
--- Adds the namespace scope to the window.
---
--- @param window integer Window handle, or 0 for current window
--- @param ns_id integer the namespace to add
--- @return boolean
function vim.api.nvim_win_add_ns(window, ns_id) end
--- Calls a function with window as temporary current window.
---
--- @param window integer Window handle, or 0 for current window
@ -2032,6 +2041,12 @@ function vim.api.nvim_win_get_cursor(window) end
--- @return integer
function vim.api.nvim_win_get_height(window) end
--- Gets all the namespaces scopes associated with a window.
---
--- @param window integer Window handle, or 0 for current window
--- @return integer[]
function vim.api.nvim_win_get_ns(window) end
--- Gets the window number
---
--- @param window integer Window handle, or 0 for current window
@ -2084,6 +2099,13 @@ function vim.api.nvim_win_hide(window) end
--- @return boolean
function vim.api.nvim_win_is_valid(window) end
--- Removes the namespace scope from the window.
---
--- @param window integer Window handle, or 0 for current window
--- @param ns_id integer the namespace to remove
--- @return boolean
function vim.api.nvim_win_remove_ns(window, ns_id) end
--- Sets the current buffer in a window, without side effects
---
--- @param window integer Window handle, or 0 for current window

View File

@ -252,6 +252,7 @@ error('Cannot require a meta file')
--- @field ui_watched? boolean
--- @field undo_restore? boolean
--- @field url? string
--- @field scoped? boolean
--- @field _subpriority? integer
--- @class vim.api.keyset.user_command

View File

@ -169,7 +169,7 @@ Integer nvim_buf_set_virtual_text(Buffer buffer, Integer src_id, Integer line, A
DecorInline decor = { .ext = true, .data.ext.vt = vt, .data.ext.sh_idx = DECOR_ID_INVALID };
extmark_set(buf, ns_id, NULL, (int)line, 0, -1, -1, decor, 0, true,
false, false, false, NULL);
false, false, false, false, NULL);
return src_id;
}

View File

@ -20,6 +20,7 @@
#include "nvim/extmark.h"
#include "nvim/grid.h"
#include "nvim/highlight_group.h"
#include "nvim/map_defs.h"
#include "nvim/marktree.h"
#include "nvim/marktree_defs.h"
#include "nvim/mbyte.h"
@ -177,6 +178,10 @@ static Array extmark_to_array(MTPair extmark, bool id, bool add_dict, bool hl_na
PUT_C(dict, "invalid", BOOLEAN_OBJ(true));
}
if (mt_scoped(start)) {
PUT_C(dict, "scoped", BOOLEAN_OBJ(true));
}
decor_to_dict_legacy(&dict, mt_decor(start), hl_name, arena);
ADD_C(rv, DICTIONARY_OBJ(dict));
@ -493,6 +498,8 @@ Array nvim_buf_get_extmarks(Buffer buffer, Integer ns_id, Object start, Object e
/// used together with virt_text.
/// - url: A URL to associate with this extmark. In the TUI, the OSC 8 control
/// sequence is used to generate a clickable hyperlink to this URL.
/// - scoped: boolean that indicates that the extmark should only be
/// displayed in the namespace scope. (experimental)
///
/// @param[out] err Error details, if any
/// @return Id of the created/updated extmark
@ -751,6 +758,11 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
}
if (opts->ephemeral && decor_state.win && decor_state.win->w_buffer == buf) {
if (opts->scoped) {
api_set_error(err, kErrorTypeException, "not yet implemented");
goto error;
}
int r = (int)line;
int c = (int)col;
if (line2 == -1) {
@ -843,7 +855,7 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
extmark_set(buf, (uint32_t)ns_id, &id, (int)line, (colnr_T)col, line2, col2,
decor, decor_flags, right_gravity, opts->end_right_gravity,
!GET_BOOL_OR_TRUE(opts, set_extmark, undo_restore),
opts->invalidate, err);
opts->invalidate, opts->scoped, err);
if (ERROR_SET(err)) {
decor_free(decor);
return 0;
@ -969,7 +981,7 @@ Integer nvim_buf_add_highlight(Buffer buffer, Integer ns_id, String hl_group, In
decor.data.hl.hl_id = hl_id;
extmark_set(buf, ns, NULL, (int)line, (colnr_T)col_start, end_line, (colnr_T)col_end,
decor, MT_FLAG_DECOR_HL, true, false, false, false, NULL);
decor, MT_FLAG_DECOR_HL, true, false, false, false, false, NULL);
return ns_id;
}
@ -1212,3 +1224,72 @@ String nvim__buf_debug_extmarks(Buffer buffer, Boolean keys, Boolean dot, Error
return mt_inspect(buf->b_marktree, keys, dot);
}
/// Adds the namespace scope to the window.
///
/// @param window Window handle, or 0 for current window
/// @param ns_id the namespace to add
/// @return true if the namespace was added, else false
Boolean nvim_win_add_ns(Window window, Integer ns_id, Error *err)
FUNC_API_SINCE(12)
{
win_T *win = find_window_by_handle(window, err);
if (!win) {
return false;
}
VALIDATE_INT(ns_initialized((uint32_t)ns_id), "ns_id", ns_id, {
return false;
});
set_put(uint32_t, &win->w_ns_set, (uint32_t)ns_id);
redraw_all_later(UPD_NOT_VALID); // TODO(altermo): only need to redraw the window
return true;
}
/// Gets all the namespaces scopes associated with a window.
///
/// @param window Window handle, or 0 for current window
/// @return a list of namespaces ids
ArrayOf(Integer) nvim_win_get_ns(Window window, Error *err)
FUNC_API_SINCE(12)
{
Array rv = ARRAY_DICT_INIT;
win_T *win = find_window_by_handle(window, err);
if (!win) {
return rv;
}
uint32_t i;
set_foreach(&win->w_ns_set, i, {
ADD(rv, INTEGER_OBJ((Integer)(i)));
});
return rv;
}
/// Removes the namespace scope from the window.
///
/// @param window Window handle, or 0 for current window
/// @param ns_id the namespace to remove
/// @return true if the namespace was removed, else false
Boolean nvim_win_remove_ns(Window window, Integer ns_id, Error *err)
FUNC_API_SINCE(12)
{
win_T *win = find_window_by_handle(window, err);
if (!win) {
return false;
}
if (!set_has(uint32_t, &win->w_ns_set, (uint32_t)ns_id)) {
return false;
}
set_del_uint32_t(&win->w_ns_set, (uint32_t)ns_id);
redraw_later(win, UPD_NOT_VALID);
return true;
}

View File

@ -55,6 +55,7 @@ typedef struct {
Boolean ui_watched;
Boolean undo_restore;
String url;
Boolean scoped;
Integer _subpriority;
} Dict(set_extmark);

View File

@ -1020,6 +1020,8 @@ struct window_S {
int w_ns_hl_active;
int *w_ns_hl_attr;
Set(uint32_t) w_ns_set;
int w_hl_id_normal; ///< 'winhighlight' normal id
int w_hl_attr_normal; ///< 'winhighlight' normal final attrs
int w_hl_attr_normalnc; ///< 'winhighlight' NormalNC final attrs

View File

@ -88,7 +88,7 @@ void bufhl_add_hl_pos_offset(buf_T *buf, int src_id, int hl_id, lpos_T pos_start
extmark_set(buf, (uint32_t)src_id, NULL,
(int)lnum - 1, hl_start, (int)lnum - 1 + end_off, hl_end,
decor, MT_FLAG_DECOR_HL, true, false, true, false, NULL);
decor, MT_FLAG_DECOR_HL, true, false, true, false, false, NULL);
}
}
@ -582,6 +582,10 @@ int decor_redraw_col(win_T *wp, int col, int win_col, bool hidden, DecorState *s
break;
}
if (!mt_scoped_in_win(mark, wp)) {
goto next_mark;
}
if (mt_invalid(mark) || mt_end(mark) || !mt_decor_any(mark)) {
goto next_mark;
}
@ -726,7 +730,8 @@ void decor_redraw_signs(win_T *wp, buf_T *buf, int row, SignTextAttrs sattrs[],
if (mark.pos.row != row) {
break;
}
if (!mt_end(mark) && !mt_invalid(mark) && mt_decor_sign(mark)) {
if (!mt_end(mark) && !mt_invalid(mark) && mt_decor_sign(mark)
&& mt_scoped_in_win(mark, wp)) {
DecorSignHighlight *sh = decor_find_sign(mt_decor(mark));
num_text += (sh->text[0] != NUL);
kv_push(signs, ((SignItem){ sh, mark.id }));
@ -906,18 +911,20 @@ int decor_virt_lines(win_T *wp, linenr_T lnum, VirtLines *lines, TriState has_fo
while (true) {
MTKey mark = marktree_itr_current(itr);
DecorVirtText *vt = mt_decor_virt(mark);
while (vt) {
if (vt->flags & kVTIsLines) {
bool above = vt->flags & kVTLinesAbove;
int draw_row = mark.pos.row + (above ? 0 : 1);
if (draw_row == row) {
virt_lines += (int)kv_size(vt->data.virt_lines);
if (lines) {
kv_splice(*lines, vt->data.virt_lines);
if (mt_scoped_in_win(mark, wp)) {
while (vt) {
if (vt->flags & kVTIsLines) {
bool above = vt->flags & kVTLinesAbove;
int draw_row = mark.pos.row + (above ? 0 : 1);
if (draw_row == row) {
virt_lines += (int)kv_size(vt->data.virt_lines);
if (lines) {
kv_splice(*lines, vt->data.virt_lines);
}
}
}
vt = vt->next;
}
vt = vt->next;
}
if (!marktree_itr_next_filter(buf->b_marktree, itr, end_row, 0, lines_filter)) {

View File

@ -54,12 +54,12 @@
/// must not be used during iteration!
void extmark_set(buf_T *buf, uint32_t ns_id, uint32_t *idp, int row, colnr_T col, int end_row,
colnr_T end_col, DecorInline decor, uint16_t decor_flags, bool right_gravity,
bool end_right_gravity, bool no_undo, bool invalidate, Error *err)
bool end_right_gravity, bool no_undo, bool invalidate, bool scoped, Error *err)
{
uint32_t *ns = map_put_ref(uint32_t, uint32_t)(buf->b_extmark_ns, ns_id, NULL, NULL);
uint32_t id = idp ? *idp : 0;
uint16_t flags = mt_flags(right_gravity, no_undo, invalidate, decor.ext) | decor_flags;
uint16_t flags = mt_flags(right_gravity, no_undo, invalidate, decor.ext, scoped) | decor_flags;
if (id == 0) {
id = ++*ns;
} else {

View File

@ -2286,7 +2286,7 @@ static void marktree_itr_fix_pos(MarkTree *b, MarkTreeIter *itr)
void marktree_put_test(MarkTree *b, uint32_t ns, uint32_t id, int row, int col, bool right_gravity,
int end_row, int end_col, bool end_right, bool meta_inline)
{
uint16_t flags = mt_flags(right_gravity, false, false, false);
uint16_t flags = mt_flags(right_gravity, false, false, false, false);
// The specific choice is irrelevant here, we pick one counted decor
// type to test the counting and filtering logic.
flags |= meta_inline ? MT_FLAG_DECOR_VIRT_TEXT_INLINE : 0;

View File

@ -4,6 +4,7 @@
#include <stddef.h> // IWYU pragma: keep
#include <stdint.h>
#include "nvim/buffer_defs.h"
#include "nvim/decoration_defs.h"
#include "nvim/marktree_defs.h" // IWYU pragma: keep
#include "nvim/pos_defs.h" // IWYU pragma: keep
@ -34,6 +35,8 @@
#define MT_FLAG_DECOR_VIRT_LINES (((uint16_t)1) << 11)
#define MT_FLAG_DECOR_VIRT_TEXT_INLINE (((uint16_t)1) << 12)
#define MT_FLAG_SCOPED (((uint16_t)1) << 13)
// These _must_ be last to preserve ordering of marks
#define MT_FLAG_RIGHT_GRAVITY (((uint16_t)1) << 14)
#define MT_FLAG_LAST (((uint16_t)1) << 15)
@ -43,7 +46,7 @@
| MT_FLAG_DECOR_VIRT_TEXT_INLINE)
#define MT_FLAG_EXTERNAL_MASK (MT_FLAG_DECOR_MASK | MT_FLAG_NO_UNDO \
| MT_FLAG_INVALIDATE | MT_FLAG_INVALID)
| MT_FLAG_INVALIDATE | MT_FLAG_INVALID | MT_FLAG_SCOPED)
// this is defined so that start and end of the same range have adjacent ids
#define MARKTREE_END_FLAG ((uint64_t)1)
@ -107,12 +110,24 @@ static inline bool mt_decor_sign(MTKey key)
return key.flags & (MT_FLAG_DECOR_SIGNTEXT | MT_FLAG_DECOR_SIGNHL);
}
static inline uint16_t mt_flags(bool right_gravity, bool no_undo, bool invalidate, bool decor_ext)
static inline bool mt_scoped(MTKey key)
{
return key.flags & MT_FLAG_SCOPED;
}
static inline bool mt_scoped_in_win(MTKey key, win_T *wp)
{
return !mt_scoped(key) || set_has(uint32_t, &wp->w_ns_set, key.ns);
}
static inline uint16_t mt_flags(bool right_gravity, bool no_undo, bool invalidate, bool decor_ext,
bool scoped)
{
return (uint16_t)((right_gravity ? MT_FLAG_RIGHT_GRAVITY : 0)
| (no_undo ? MT_FLAG_NO_UNDO : 0)
| (invalidate ? MT_FLAG_INVALIDATE : 0)
| (decor_ext ? MT_FLAG_DECOR_EXT : 0));
| (decor_ext ? MT_FLAG_DECOR_EXT : 0)
| (scoped ? MT_FLAG_SCOPED : 0));
}
static inline MTPair mtpair_from(MTKey start, MTKey end)

View File

@ -126,7 +126,7 @@ static void buf_set_sign(buf_T *buf, uint32_t *id, char *group, int prio, linenr
DecorInline decor = { .ext = true, .data.ext = { .vt = NULL, .sh_idx = decor_put_sh(sign) } };
extmark_set(buf, ns, id, lnum - 1, 0, -1, -1, decor, decor_flags, true,
false, true, true, NULL);
false, true, true, false, NULL);
}
/// For an existing, placed sign with "id", modify the sign, group or priority.

View File

@ -4989,6 +4989,9 @@ win_T *win_alloc(win_T *after, bool hidden)
new_wp->w_ns_hl = -1;
Set(uint32_t) ns_set = SET_INIT;
new_wp->w_ns_set = ns_set;
// use global option for global-local options
new_wp->w_allbuf_opt.wo_so = new_wp->w_p_so = -1;
new_wp->w_allbuf_opt.wo_siso = new_wp->w_p_siso = -1;
@ -5027,6 +5030,8 @@ void win_free(win_T *wp, tabpage_T *tp)
// Don't execute autocommands while the window is halfway being deleted.
block_autocmds();
set_destroy(uint32_t, &wp->w_ns_set);
clear_winopt(&wp->w_onebuf_opt);
clear_winopt(&wp->w_allbuf_opt);

View File

@ -5468,3 +5468,333 @@ describe('decorations: virt_text', function()
]]}
end)
end)
describe('decorations: window scoped', function()
local screen, ns
before_each(function()
clear()
screen = Screen.new(20, 10)
screen:attach()
screen:set_default_attr_ids {
[1] = { foreground = Screen.colors.Blue1 },
[2] = { foreground = Screen.colors.Blue1, bold = true },
}
ns = api.nvim_create_namespace 'test'
insert('12345')
end)
local noextmarks = {
grid = [[
1234^5 |
{2:~ }|*8
|
]]}
local function set_scoped_extmark(line, col, opts)
return api.nvim_buf_set_extmark(0, ns, line, col, vim.tbl_extend('error', { scoped = true }, opts))
end
it('hl_group', function()
set_scoped_extmark(0, 0, {
hl_group = 'Comment',
end_col = 3,
})
screen:expect(noextmarks)
api.nvim_win_add_ns(0, ns)
screen:expect {
grid = [[
{1:123}4^5 |
{2:~ }|*8
|
]]}
command 'split'
command 'only'
screen:expect(noextmarks)
end)
it('virt_text', function()
set_scoped_extmark(0, 0, {
virt_text = { { 'a', 'Comment' } },
virt_text_pos = 'eol',
})
set_scoped_extmark(0, 5, {
virt_text = { { 'b', 'Comment' } },
virt_text_pos = 'inline',
})
set_scoped_extmark(0, 1, {
virt_text = { { 'c', 'Comment' } },
virt_text_pos = 'overlay',
})
set_scoped_extmark(0, 1, {
virt_text = { { 'd', 'Comment' } },
virt_text_pos = 'right_align',
})
screen:expect(noextmarks)
api.nvim_win_add_ns(0, ns)
screen:expect {
grid = [[
1{1:c}34^5{1:b} {1:a} {1:d}|
{2:~ }|*8
|
]]}
command 'split'
command 'only'
screen:expect(noextmarks)
end)
it('virt_lines', function()
set_scoped_extmark(0, 0, {
virt_lines = { { { 'a', 'Comment' } } },
})
screen:expect(noextmarks)
api.nvim_win_add_ns(0, ns)
screen:expect {
grid = [[
1234^5 |
{1:a} |
{2:~ }|*7
|
]]}
command 'split'
command 'only'
screen:expect(noextmarks)
end)
pending('sign_text', function()
-- TODO(altermo): The window signcolumn width is calculated wrongly (when `signcolumn=auto`)
-- This happens in function `win_redraw_signcols` on line containing `buf_meta_total(buf, kMTMetaSignText) > 0`
set_scoped_extmark(0, 0, {
sign_text = 'a',
sign_hl_group = 'Comment',
})
screen:expect(noextmarks)
api.nvim_win_add_ns(0, ns)
screen:expect {
grid = [[
a 1234^5 |
{2:~ }|*8
|
]]}
command 'split'
command 'only'
screen:expect(noextmarks)
end)
it('statuscolumn hl group', function()
local attrs = screen:get_default_attr_ids()
table.insert(attrs, {
foreground = Screen.colors.Brown,
})
screen:set_default_attr_ids(attrs)
set_scoped_extmark(0, 0, {
number_hl_group='comment',
})
set_scoped_extmark(0, 0, {
line_hl_group='comment',
})
command 'set number'
screen:expect {
grid = [[
{3: 1 }1234^5 |
{2:~ }|*8
|
]]}
api.nvim_win_add_ns(0, ns)
screen:expect {
grid = [[
{1: 1 1234^5 }|
{2:~ }|*8
|
]]}
command 'split'
command 'only'
screen:expect {
grid = [[
{3: 1 }1234^5 |
{2:~ }|*8
|
]]}
end)
it('spell', function()
local attrs = screen:get_default_attr_ids()
table.insert(attrs, {
special = Screen.colors.Red, undercurl = true
})
screen:set_default_attr_ids(attrs)
api.nvim_buf_set_lines(0,0,-1,true,{'aa'})
set_scoped_extmark(0, 0, {
spell=true,
end_col=2,
})
command 'set spelloptions=noplainbuffer'
command 'set spell'
command 'syntax off'
screen:expect {
grid = [[
a^a |
{2:~ }|*8
|
]]}
api.nvim_win_add_ns(0, ns)
screen:expect {
grid = [[
{3:a^a} |
{2:~ }|*8
|
]]}
command 'split'
command 'only'
screen:expect {
grid = [[
a^a |
{2:~ }|*8
|
]]}
end)
it('url', function()
local url = 'https://example.com'
local attrs = screen:get_default_attr_ids()
table.insert(attrs, {
url = url,
})
screen:set_default_attr_ids(attrs)
set_scoped_extmark(0, 0, {
end_col=3,
url=url,
})
screen:expect(noextmarks)
api.nvim_win_add_ns(0, ns)
screen:expect {
grid = [[
{3:123}4^5 |
{2:~ }|*8
|
]]}
command 'split'
command 'only'
screen:expect(noextmarks)
end)
it('change extmarks scoped option', function()
local id = set_scoped_extmark(0, 0, {
hl_group = 'Comment',
end_col = 3,
})
api.nvim_win_add_ns(0, ns)
screen:expect {
grid = [[
{1:123}4^5 |
{2:~ }|*8
|
]]}
command 'split'
command 'only'
screen:expect(noextmarks)
api.nvim_buf_set_extmark(0, ns, 0, 0, {
id = id,
hl_group = 'Comment',
end_col = 3,
scoped = false,
})
screen:expect {
grid = [[
{1:123}4^5 |
{2:~ }|*8
|
]]}
api.nvim_buf_set_extmark(0, ns, 0, 0, {
id = id,
hl_group = 'Comment',
end_col = 3,
scoped = true,
})
screen:expect(noextmarks)
end)
it('change namespace scope', function()
set_scoped_extmark(0, 0, {
hl_group = 'Comment',
end_col = 3,
})
api.nvim_win_add_ns(0, ns)
screen:expect {
grid = [[
{1:123}4^5 |
{2:~ }|*8
|
]]}
command 'split'
command 'only'
screen:expect(noextmarks)
api.nvim_win_add_ns(0, ns)
screen:expect {
grid = [[
{1:123}4^5 |
{2:~ }|*8
|
]]}
eq(true, api.nvim_win_remove_ns(0, ns))
eq({}, api.nvim_win_get_ns(0))
screen:expect(noextmarks)
end)
end)