ui: use an array for mode styles

This commit is contained in:
Björn Linse 2017-04-18 13:42:04 +02:00
parent 2c5751b9b2
commit 7ea5c78687
9 changed files with 156 additions and 157 deletions

View File

@ -70,7 +70,7 @@ void nvim_ui_attach(uint64_t channel_id, Integer width, Integer height,
ui->clear = remote_ui_clear;
ui->eol_clear = remote_ui_eol_clear;
ui->cursor_goto = remote_ui_cursor_goto;
ui->cursor_style_set = remote_ui_cursor_style_set;
ui->mode_info_set = remote_ui_mode_info_set;
ui->update_menu = remote_ui_update_menu;
ui->busy_start = remote_ui_busy_start;
ui->busy_stop = remote_ui_busy_stop;
@ -295,12 +295,12 @@ static void remote_ui_scroll(UI *ui, int count)
push_call(ui, "scroll", args);
}
static void remote_ui_cursor_style_set(UI *ui, bool enabled, Dictionary data)
static void remote_ui_mode_info_set(UI *ui, bool guicursor_enabled, Array data)
{
Array args = ARRAY_DICT_INIT;
ADD(args, BOOLEAN_OBJ(enabled));
ADD(args, copy_object(DICTIONARY_OBJ(data)));
push_call(ui, "cursor_style_set", args);
ADD(args, BOOLEAN_OBJ(guicursor_enabled));
ADD(args, copy_object(ARRAY_OBJ(data)));
push_call(ui, "mode_info_set", args);
}
static void remote_ui_highlight_set(UI *ui, HlAttrs attrs)

View File

@ -34,11 +34,11 @@ cursorentry_T shape_table[SHAPE_IDX_COUNT] =
{ "showmatch", 0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR },
};
/// Converts cursor_shapes into a Dictionary of dictionaries
/// @return dictionary of the form {"normal" : { "cursor_shape": ... }, ...}
Dictionary cursor_shape_dict(void)
/// Converts cursor_shapes into an Array of Dictionaries
/// @return Array of the form {[ "cursor_shape": ... ], ...}
Array mode_style_array(void)
{
Dictionary all = ARRAY_DICT_INIT;
Array all = ARRAY_DICT_INIT;
for (int i = 0; i < SHAPE_IDX_COUNT; i++) {
Dictionary dic = ARRAY_DICT_INIT;
@ -62,10 +62,10 @@ Dictionary cursor_shape_dict(void)
PUT(dic, "hl_id", INTEGER_OBJ(cur->id));
PUT(dic, "id_lm", INTEGER_OBJ(cur->id_lm));
}
PUT(dic, "name", STRING_OBJ(cstr_to_string(cur->full_name)));
PUT(dic, "short_name", STRING_OBJ(cstr_to_string(cur->name)));
PUT(dic, "mode_idx", INTEGER_OBJ(i));
PUT(all, cur->full_name, DICTIONARY_OBJ(dic));
ADD(all, DICTIONARY_OBJ(dic));
}
return all;
@ -241,7 +241,7 @@ char_u *parse_shape_opt(int what)
shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
}
}
ui_cursor_style_set();
ui_mode_info_set();
return NULL;
}

View File

@ -101,7 +101,7 @@ UI *tui_start(void)
ui->clear = tui_clear;
ui->eol_clear = tui_eol_clear;
ui->cursor_goto = tui_cursor_goto;
ui->cursor_style_set = tui_cursor_style_set;
ui->mode_info_set = tui_mode_info_set;
ui->update_menu = tui_update_menu;
ui->busy_start = tui_busy_start;
ui->busy_stop = tui_busy_stop;
@ -451,7 +451,7 @@ CursorShape tui_cursor_decode_shape(const char *shape_str)
return shape;
}
static cursorentry_T decode_cursor_entry(Dictionary args, int *mode_idx)
static cursorentry_T decode_cursor_entry(Dictionary args)
{
cursorentry_T r;
@ -467,31 +467,26 @@ static cursorentry_T decode_cursor_entry(Dictionary args, int *mode_idx)
r.blinkoff = (int)value.data.integer;
} else if (strequal(key, "hl_id")) {
r.id = (int)value.data.integer;
} else if (strequal(key, "mode_idx")) {
*mode_idx = (int)value.data.integer;
}
}
return r;
}
static void tui_cursor_style_set(UI *ui, bool enabled, Dictionary args)
static void tui_mode_info_set(UI *ui, bool guicursor_enabled, Array args)
{
cursor_style_enabled = enabled;
if (!enabled) {
cursor_style_enabled = guicursor_enabled;
if (!guicursor_enabled) {
return; // Do not send cursor style control codes.
}
TUIData *data = ui->data;
assert(args.size);
// Keys: as defined by `shape_table`.
// cursor style entries as defined by `shape_table`.
for (size_t i = 0; i < args.size; i++) {
char *mode_name = args.items[i].key.data;
int mode_idx;
cursorentry_T r = decode_cursor_entry(args.items[i].value.data.dictionary,
&mode_idx);
assert(mode_idx >= 0);
r.full_name = mode_name;
data->cursor_shapes[mode_idx] = r;
assert(args.items[i].type == kObjectTypeDictionary);
cursorentry_T r = decode_cursor_entry(args.items[i].data.dictionary);
data->cursor_shapes[i] = r;
}
tui_set_mode(ui, data->showing_mode);

View File

@ -175,7 +175,7 @@ void ui_refresh(void)
row = col = 0;
screen_resize(width, height);
pum_set_external(pum_external);
ui_cursor_style_set();
ui_mode_info_set();
old_mode_idx = -1;
ui_cursor_shape();
}
@ -375,12 +375,12 @@ void ui_cursor_goto(int new_row, int new_col)
pending_cursor_update = true;
}
void ui_cursor_style_set(void)
void ui_mode_info_set(void)
{
Dictionary style = cursor_shape_dict();
Array style = mode_style_array();
bool enabled = (*p_guicursor != NUL);
UI_CALL(cursor_style_set, enabled, style);
api_free_dictionary(style);
UI_CALL(mode_info_set, enabled, style);
api_free_array(style);
}
void ui_update_menu(void)

0
src/nvim/ui.ch Normal file
View File

View File

@ -22,7 +22,7 @@ struct ui_t {
void (*clear)(UI *ui);
void (*eol_clear)(UI *ui);
void (*cursor_goto)(UI *ui, int row, int col);
void (*cursor_style_set)(UI *ui, bool enabled, Dictionary cursor_styles);
void (*mode_info_set)(UI *ui, bool enabled, Array cursor_styles);
void (*update_menu)(UI *ui);
void (*busy_start)(UI *ui);
void (*busy_stop)(UI *ui);

View File

@ -60,7 +60,7 @@ UI *ui_bridge_attach(UI *ui, ui_main_fn ui_main, event_scheduler scheduler)
rv->bridge.clear = ui_bridge_clear;
rv->bridge.eol_clear = ui_bridge_eol_clear;
rv->bridge.cursor_goto = ui_bridge_cursor_goto;
rv->bridge.cursor_style_set = ui_bridge_cursor_style_set;
rv->bridge.mode_info_set = ui_bridge_mode_info_set;
rv->bridge.update_menu = ui_bridge_update_menu;
rv->bridge.busy_start = ui_bridge_busy_start;
rv->bridge.busy_stop = ui_bridge_busy_stop;
@ -180,23 +180,23 @@ static void ui_bridge_cursor_goto_event(void **argv)
ui->cursor_goto(ui, PTR2INT(argv[1]), PTR2INT(argv[2]));
}
static void ui_bridge_cursor_style_set(UI *b, bool enabled, Dictionary styles)
static void ui_bridge_mode_info_set(UI *b, bool enabled, Array modes)
{
bool *enabledp = xmalloc(sizeof(*enabledp));
Object *stylesp = xmalloc(sizeof(*stylesp));
Object *modesp = xmalloc(sizeof(*modesp));
*enabledp = enabled;
*stylesp = copy_object(DICTIONARY_OBJ(styles));
UI_CALL(b, cursor_style_set, 3, b, enabledp, stylesp);
*modesp = copy_object(ARRAY_OBJ(modes));
UI_CALL(b, mode_info_set, 3, b, enabledp, modesp);
}
static void ui_bridge_cursor_style_set_event(void **argv)
static void ui_bridge_mode_info_set_event(void **argv)
{
UI *ui = UI(argv[0]);
bool *enabled = argv[1];
Object *styles = argv[2];
ui->cursor_style_set(ui, *enabled, styles->data.dictionary);
Object *modes = argv[2];
ui->mode_info_set(ui, *enabled, modes->data.array);
xfree(enabled);
api_free_object(*styles);
xfree(styles);
api_free_object(*modes);
xfree(modes);
}
static void ui_bridge_update_menu(UI *b)

View File

@ -18,155 +18,155 @@ describe('ui/cursor', function()
end)
it("'guicursor' is published as a UI event", function()
local expected_cursor_style = {
cmdline_hover = {
mode_idx = 9,
mouse_shape = 0,
short_name = 'e' },
cmdline_insert = {
mode_idx = 5,
blinkoff = 250,
blinkon = 400,
blinkwait = 700,
cell_percentage = 25,
cursor_shape = 'vertical',
hl_id = 46,
id_lm = 47,
mouse_shape = 0,
short_name = 'ci' },
cmdline_normal = {
mode_idx = 4,
local expected_mode_info = {
[1] = {
blinkoff = 250,
blinkon = 400,
blinkwait = 700,
cell_percentage = 0,
cursor_shape = 'block',
name = 'normal',
hl_id = 46,
id_lm = 47,
mouse_shape = 0,
short_name = 'c' },
cmdline_replace = {
mode_idx = 6,
short_name = 'n' },
[2] = {
blinkoff = 250,
blinkon = 400,
blinkwait = 700,
cell_percentage = 0,
cursor_shape = 'block',
name = 'visual',
hl_id = 46,
id_lm = 47,
mouse_shape = 0,
short_name = 'v' },
[3] = {
blinkoff = 250,
blinkon = 400,
blinkwait = 700,
cell_percentage = 25,
cursor_shape = 'vertical',
name = 'insert',
hl_id = 46,
id_lm = 47,
mouse_shape = 0,
short_name = 'i' },
[4] = {
blinkoff = 250,
blinkon = 400,
blinkwait = 700,
cell_percentage = 20,
cursor_shape = 'horizontal',
name = 'replace',
hl_id = 46,
id_lm = 47,
mouse_shape = 0,
short_name = 'cr' },
insert = {
mode_idx = 2,
blinkoff = 250,
blinkon = 400,
blinkwait = 700,
cell_percentage = 25,
cursor_shape = 'vertical',
hl_id = 46,
id_lm = 47,
mouse_shape = 0,
short_name = 'i' },
more = {
mode_idx = 14,
mouse_shape = 0,
short_name = 'm' },
more_lastline = {
mode_idx = 15,
mouse_shape = 0,
short_name = 'ml' },
normal = {
mode_idx = 0,
short_name = 'r' },
[5] = {
blinkoff = 250,
blinkon = 400,
blinkwait = 700,
cell_percentage = 0,
cursor_shape = 'block',
name = 'cmdline_normal',
hl_id = 46,
id_lm = 47,
mouse_shape = 0,
short_name = 'n' },
operator = {
mode_idx = 7,
short_name = 'c' },
[6] = {
blinkoff = 250,
blinkon = 400,
blinkwait = 700,
cell_percentage = 25,
cursor_shape = 'vertical',
name = 'cmdline_insert',
hl_id = 46,
id_lm = 47,
mouse_shape = 0,
short_name = 'ci' },
[7] = {
blinkoff = 250,
blinkon = 400,
blinkwait = 700,
cell_percentage = 20,
cursor_shape = 'horizontal',
name = 'cmdline_replace',
hl_id = 46,
id_lm = 47,
mouse_shape = 0,
short_name = 'cr' },
[8] = {
blinkoff = 250,
blinkon = 400,
blinkwait = 700,
cell_percentage = 50,
cursor_shape = 'horizontal',
name = 'operator',
hl_id = 46,
id_lm = 46,
mouse_shape = 0,
short_name = 'o' },
replace = {
mode_idx = 3,
blinkoff = 250,
blinkon = 400,
blinkwait = 700,
cell_percentage = 20,
cursor_shape = 'horizontal',
hl_id = 46,
id_lm = 47,
mouse_shape = 0,
short_name = 'r' },
showmatch = {
mode_idx = 16,
blinkoff = 150,
blinkon = 175,
blinkwait = 175,
cell_percentage = 0,
cursor_shape = 'block',
hl_id = 46,
id_lm = 46,
short_name = 'sm' },
statusline_drag = {
mode_idx = 11,
mouse_shape = 0,
short_name = 'sd' },
statusline_hover = {
mode_idx = 10,
mouse_shape = 0,
short_name = 's' },
visual = {
mode_idx = 1,
blinkoff = 250,
blinkon = 400,
blinkwait = 700,
cell_percentage = 0,
cursor_shape = 'block',
hl_id = 46,
id_lm = 47,
mouse_shape = 0,
short_name = 'v' },
visual_select = {
mode_idx = 8,
[9] = {
blinkoff = 250,
blinkon = 400,
blinkwait = 700,
cell_percentage = 35,
cursor_shape = 'vertical',
name = 'visual_select',
hl_id = 46,
id_lm = 46,
mouse_shape = 0,
short_name = 've' },
vsep_drag = {
mode_idx = 13,
[10] = {
name = 'cmdline_hover',
mouse_shape = 0,
short_name = 'e' },
[11] = {
name = 'statusline_hover',
mouse_shape = 0,
short_name = 's' },
[12] = {
name = 'statusline_drag',
mouse_shape = 0,
short_name = 'sd' },
[13] = {
name = 'vsep_hover',
mouse_shape = 0,
short_name = 'vs' },
[14] = {
name = 'vsep_drag',
mouse_shape = 0,
short_name = 'vd' },
vsep_hover = {
mode_idx = 12,
[15] = {
name = 'more',
mouse_shape = 0,
short_name = 'vs' }
short_name = 'm' },
[16] = {
name = 'more_lastline',
mouse_shape = 0,
short_name = 'ml' },
[17] = {
blinkoff = 150,
blinkon = 175,
blinkwait = 175,
cell_percentage = 0,
cursor_shape = 'block',
name = 'showmatch',
hl_id = 46,
id_lm = 46,
short_name = 'sm' },
}
screen:expect(function()
-- Default 'guicursor' published on startup.
eq(expected_cursor_style, screen._cursor_style)
eq(expected_mode_info, screen._mode_info)
eq(true, screen._cursor_style_enabled)
eq('normal', screen.mode)
end)
-- Event is published ONLY if the cursor style changed.
screen._cursor_style = nil
screen._mode_info = nil
command("echo 'test'")
screen:expect([[
^ |
@ -175,20 +175,24 @@ describe('ui/cursor', function()
~ |
test |
]], nil, nil, function()
eq(nil, screen._cursor_style)
eq(nil, screen._mode_info)
end)
-- Change the cursor style.
meths.set_option('guicursor', 'n-v-c:ver35-blinkwait171-blinkoff172-blinkon173,ve:hor35,o:ver50,i-ci:block,r-cr:hor90,sm:ver42')
screen:expect(function()
eq('vertical', screen._cursor_style.normal.cursor_shape)
eq('horizontal', screen._cursor_style.visual_select.cursor_shape)
eq('vertical', screen._cursor_style.operator.cursor_shape)
eq('block', screen._cursor_style.insert.cursor_shape)
eq('vertical', screen._cursor_style.showmatch.cursor_shape)
eq(171, screen._cursor_style.normal.blinkwait)
eq(172, screen._cursor_style.normal.blinkoff)
eq(173, screen._cursor_style.normal.blinkon)
local named = {}
for _, m in ipairs(screen._mode_info) do
named[m.name] = m
end
eq('vertical', named.normal.cursor_shape)
eq('horizontal', named.visual_select.cursor_shape)
eq('vertical', named.operator.cursor_shape)
eq('block', named.insert.cursor_shape)
eq('vertical', named.showmatch.cursor_shape)
eq(171, named.normal.blinkwait)
eq(172, named.normal.blinkoff)
eq(173, named.normal.blinkon)
end)
end)
@ -197,11 +201,11 @@ describe('ui/cursor', function()
screen:expect(function()
-- Empty 'guicursor' sets enabled=false.
eq(false, screen._cursor_style_enabled)
for _, m in ipairs({ 'cmdline_insert', 'cmdline_normal', 'cmdline_replace', 'insert',
'showmatch', 'normal', 'replace', 'visual',
'visual_select', }) do
eq('block', screen._cursor_style[m].cursor_shape)
eq(0, screen._cursor_style[m].blinkon)
for _, m in ipairs(screen._mode_info) do
if m['cursor_shape'] ~= nil then
eq('block', m.cursor_shape)
eq(0, m.blinkon)
end
end
end)
end)

View File

@ -348,9 +348,9 @@ function Screen:_handle_resize(width, height)
}
end
function Screen:_handle_cursor_style_set(enabled, style)
self._cursor_style_enabled = enabled
self._cursor_style = style
function Screen:_handle_mode_info_set(cursor_style_enabled, mode_info)
self._cursor_style_enabled = cursor_style_enabled
self._mode_info = mode_info
end
function Screen:_handle_clear()
@ -385,7 +385,7 @@ function Screen:_handle_mouse_off()
end
function Screen:_handle_mode_change(mode, idx)
assert(idx == self._cursor_style[mode].mode_idx)
assert(mode == self._mode_info[idx+1].name)
self.mode = mode
end