api: add width/height to FloatConfig

This commit is contained in:
Marco Hinz 2019-03-15 18:03:12 +01:00
parent 3c88bbecb8
commit 96edbe7b1d
No known key found for this signature in database
GPG Key ID: 1C980A1B657B4A4F
6 changed files with 83 additions and 58 deletions

View File

@ -1004,8 +1004,6 @@ Buffer nvim_create_buf(Boolean listed, Boolean scratch, Error *err)
///
/// @param buffer handle of buffer to be displayed in the window
/// @param enter whether the window should be entered (made the current window)
/// @param width width of window (in character cells)
/// @param height height of window (in character cells)
/// @param options dict of options for configuring window positioning
/// accepts the following keys:
/// `relative`: If set, the window becomes a floating window. The window
@ -1023,6 +1021,8 @@ Buffer nvim_create_buf(Boolean listed, Boolean scratch, Error *err)
/// `focusable`: Whether window can be focused by wincmds and
/// mouse events. Defaults to true. Even if set to false, the window
/// can still be entered using |nvim_set_current_win()| API call.
/// `height`: window height (in character cells). Cannot be smaller than 1.
/// `width`: window width (in character cells). Cannot be smaller than 2.
/// `row`: row position. Screen cell height are used as unit. Can be
/// floating point.
/// `col`: column position. Screen cell width is used as unit. Can be
@ -1047,16 +1047,15 @@ Buffer nvim_create_buf(Boolean listed, Boolean scratch, Error *err)
///
/// @param[out] err Error details, if any
/// @return the window handle or 0 when error
Window nvim_open_win(Buffer buffer, Boolean enter,
Integer width, Integer height,
Dictionary options, Error *err)
Window nvim_open_win(Buffer buffer, Boolean enter, Dictionary options,
Error *err)
FUNC_API_SINCE(6)
{
FloatConfig config = FLOAT_CONFIG_INIT;
if (!parse_float_config(options, &config, false, err)) {
return 0;
}
win_T *wp = win_new_float(NULL, (int)width, (int)height, config, err);
win_T *wp = win_new_float(NULL, config, err);
if (!wp) {
return 0;
}

View File

@ -439,13 +439,12 @@ Boolean nvim_win_is_valid(Window window)
/// types).
///
/// See documentation at |nvim_open_win()|, for the meaning of parameters. Pass
/// in -1 for 'witdh' and 'height' to keep exiting size.
/// in 0 for 'witdh' and 'height' to keep exiting size.
///
/// When reconfiguring a floating window, absent option keys will not be
/// changed. The following restriction apply: `row`, `col` and `relative`
/// must be reconfigured together. Only changing a subset of these is an error.
void nvim_win_set_config(Window window, Integer width, Integer height,
Dictionary options, Error *err)
void nvim_win_set_config(Window window, Dictionary options, Error *err)
FUNC_API_SINCE(6)
{
win_T *win = find_window_by_handle(window, err);
@ -453,21 +452,21 @@ void nvim_win_set_config(Window window, Integer width, Integer height,
return;
}
bool new_float = !win->w_floating;
width = width > 0 ? width: win->w_width;
height = height > 0 ? height : win->w_height;
// reuse old values, if not overriden
FloatConfig config = new_float ? FLOAT_CONFIG_INIT : win->w_float_config;
if (!parse_float_config(options, &config, !new_float, err)) {
return;
}
config.height = config.height > 0 ? config.height : win->w_height;
config.width = config.width > 0 ? config.width : win->w_width;
if (new_float) {
if (!win_new_float(win, (int)width, (int)height, config, err)) {
if (!win_new_float(win, config, err)) {
return;
}
redraw_later(NOT_VALID);
} else {
win_config_float(win, (int)width, (int)height, config);
win_config_float(win, config);
win->w_pos_changed = true;
}
}
@ -490,8 +489,8 @@ Dictionary nvim_win_get_config(Window window, Error *err)
return rv;
}
PUT(rv, "height", INTEGER_OBJ(wp->w_height));
PUT(rv, "width", INTEGER_OBJ(wp->w_width));
PUT(rv, "width", INTEGER_OBJ(wp->w_float_config.width));
PUT(rv, "height", INTEGER_OBJ(wp->w_float_config.height));
PUT(rv, "focusable", BOOLEAN_OBJ(wp->w_float_config.focusable));
PUT(rv, "external", BOOLEAN_OBJ(wp->w_float_config.external));
PUT(rv, "anchor", STRING_OBJ(cstr_to_string(

View File

@ -979,6 +979,7 @@ typedef enum {
typedef struct {
Window window;
int height, width;
double row, col;
FloatAnchor anchor;
FloatRelative relative;
@ -986,7 +987,8 @@ typedef struct {
bool focusable;
} FloatConfig;
#define FLOAT_CONFIG_INIT ((FloatConfig){ .row = 0, .col = 0, .anchor = 0, \
#define FLOAT_CONFIG_INIT ((FloatConfig){ .height = 0, .width = 0, \
.row = 0, .col = 0, .anchor = 0, \
.relative = 0, .external = false, \
.focusable = true })

View File

@ -460,7 +460,9 @@ void ui_grid_resize(handle_T grid_handle, int width, int height, Error *error)
if (wp->w_floating) {
if (width != wp->w_width && height != wp->w_height) {
win_config_float(wp, (int)width, (int)height, wp->w_float_config);
wp->w_float_config.width = width;
wp->w_float_config.height = height;
win_config_float(wp, wp->w_float_config);
}
} else {
// non-positive indicates no request

View File

@ -502,10 +502,11 @@ wingotofile:
break;
}
FloatConfig config = FLOAT_CONFIG_INIT;
config.width = curwin->w_width;
config.height = curwin->w_height;
config.external = true;
Error err = ERROR_INIT;
if (!win_new_float(curwin, curwin->w_width, curwin->w_height, config,
&err)) {
if (!win_new_float(curwin, config, &err)) {
EMSG(err.msg);
api_clear_error(&err);
beep_flush();
@ -538,8 +539,7 @@ static void cmd_with_count(char *cmd, char_u *bufp, size_t bufsize,
/// float. It must then already belong to the current tabpage!
///
/// config must already have been validated!
win_T *win_new_float(win_T *wp, int width, int height, FloatConfig config,
Error *err)
win_T *win_new_float(win_T *wp, FloatConfig config, Error *err)
{
if (wp == NULL) {
wp = win_alloc(lastwin_nofloating(), false);
@ -571,17 +571,16 @@ win_T *win_new_float(win_T *wp, int width, int height, FloatConfig config,
// TODO(bfredl): use set_option_to() after merging #9110 ?
wp->w_p_nu = false;
wp->w_allbuf_opt.wo_nu = false;
win_config_float(wp, width, height, config);
win_config_float(wp, config);
wp->w_pos_changed = true;
redraw_win_later(wp, VALID);
return wp;
}
void win_config_float(win_T *wp, int width, int height,
FloatConfig config)
void win_config_float(win_T *wp, FloatConfig config)
{
wp->w_height = MAX(height, 1);
wp->w_width = MAX(width, 2);
wp->w_width = MAX(config.width, 2);
wp->w_height = MAX(config.height, 1);
if (config.relative == kFloatRelativeCursor) {
config.relative = kFloatRelativeWindow;
@ -729,6 +728,22 @@ bool parse_float_config(Dictionary config, FloatConfig *out, bool reconf,
"'col' option has to be Integer or Float");
return false;
}
} else if (strequal(key, "width")) {
if (val.type == kObjectTypeInteger && val.data.integer >= 0) {
out->width = val.data.integer;
} else {
api_set_error(err, kErrorTypeValidation,
"'width' option has to be a positive Integer");
return false;
}
} else if (strequal(key, "height")) {
if (val.type == kObjectTypeInteger && val.data.integer >= 0) {
out->height= val.data.integer;
} else {
api_set_error(err, kErrorTypeValidation,
"'height' option has to be a positive Integer");
return false;
}
} else if (!strcmp(key, "anchor")) {
if (val.type != kObjectTypeString) {
api_set_error(err, kErrorTypeValidation,
@ -3804,7 +3819,9 @@ static void tabpage_check_windows(tabpage_T *old_curtab)
for (win_T *wp = firstwin; wp; wp = wp->w_next) {
if (wp->w_floating && !wp->w_float_config.external) {
win_config_float(wp, wp->w_width, wp->w_height, wp->w_float_config);
wp->w_float_config.width = wp->w_width;
wp->w_float_config.height = wp->w_height;
win_config_float(wp, wp->w_float_config);
}
wp->w_pos_changed = true;
}
@ -4664,7 +4681,9 @@ int win_comp_pos(void)
// Too often, but when we support anchoring floats to split windows,
// this will be needed
for (win_T *wp = lastwin; wp && wp->w_floating; wp = wp->w_prev) {
win_config_float(wp, wp->w_width, wp->w_height, wp->w_float_config);
wp->w_float_config.width = wp->w_width;
wp->w_float_config.height = wp->w_height;
win_config_float(wp, wp->w_float_config);
}
return row;
@ -4737,7 +4756,9 @@ void win_setheight_win(int height, win_T *win)
if (win->w_floating) {
if (win->w_float_config.external) {
win_config_float(win, win->w_width, height, win->w_float_config);
win->w_float_config.width = win->w_width;
win->w_float_config.height = height;
win_config_float(win, win->w_float_config);
} else {
beep_flush();
return;
@ -4942,7 +4963,9 @@ void win_setwidth_win(int width, win_T *wp)
}
if (wp->w_floating) {
if (wp->w_float_config.external) {
win_config_float(wp, width, wp->w_height, wp->w_float_config);
wp->w_float_config.width = width;
wp->w_float_config.height = wp->w_height;
win_config_float(wp, wp->w_float_config);
} else {
beep_flush();
return;

View File

@ -45,7 +45,7 @@ describe('floating windows', function()
it('can be created and reconfigured', function()
local buf = meths.create_buf(false,false)
local win = meths.open_win(buf, false, 20, 2, {relative='editor', row=2, col=5})
local win = meths.open_win(buf, false, {relative='editor', width=20, height=2, row=2, col=5})
local expected_pos = {
[3]={{id=1001}, 'NW', 1, 2, 5, true},
}
@ -84,7 +84,7 @@ describe('floating windows', function()
end
meths.win_set_config(win,0,0,{relative='editor', row=0, col=10})
meths.win_set_config(win, {relative='editor', row=0, col=10})
expected_pos[3][4] = 0
expected_pos[3][5] = 10
if multigrid then
@ -156,7 +156,7 @@ describe('floating windows', function()
command('set number')
command('hi NormalFloat guibg=#333333')
feed('ix<cr>y<cr><esc>gg')
local win = meths.open_win(0, false, 20, 4, {relative='editor', row=4, col=10})
local win = meths.open_win(0, false, {relative='editor', width=20, height=4, row=4, col=10})
if multigrid then
screen:expect{grid=[[
## grid 1
@ -232,17 +232,17 @@ describe('floating windows', function()
it('API has proper error messages', function()
local buf = meths.create_buf(false,false)
eq({false, "Invalid options key 'bork'"},
meth_pcall(meths.open_win,buf, false, 20, 2, {bork=true}))
meth_pcall(meths.open_win,buf, false, {width=20,height=2,bork=true}))
eq({false, "'win' option is only valid with relative='win'"},
meth_pcall(meths.open_win,buf, false, 20, 2, {relative='editor',row=0,col=0,win=0}))
meth_pcall(meths.open_win,buf, false, {width=20,height=2,relative='editor',row=0,col=0,win=0}))
eq({false, "Only one of 'relative' and 'external' should be used"},
meth_pcall(meths.open_win,buf, false, 20, 2, {relative='editor',row=0,col=0,external=true}))
meth_pcall(meths.open_win,buf, false, {width=20,height=2,relative='editor',row=0,col=0,external=true}))
eq({false, "Invalid value of 'relative' option"},
meth_pcall(meths.open_win,buf, false, 20, 2, {relative='shell',row=0,col=0}))
meth_pcall(meths.open_win,buf, false, {width=20,height=2,relative='shell',row=0,col=0}))
eq({false, "Invalid value of 'anchor' option"},
meth_pcall(meths.open_win,buf, false, 20, 2, {relative='editor',row=0,col=0,anchor='bottom'}))
meth_pcall(meths.open_win,buf, false, {width=20,height=2,relative='editor',row=0,col=0,anchor='bottom'}))
eq({false, "All of 'relative', 'row', and 'col' has to be specified at once"},
meth_pcall(meths.open_win,buf, false, 20, 2, {relative='editor'}))
meth_pcall(meths.open_win,buf, false, {width=20,height=2,relative='editor'}))
end)
it('can be placed relative window or cursor', function()
@ -288,7 +288,7 @@ describe('floating windows', function()
local buf = meths.create_buf(false,false)
-- no 'win' arg, relative default window
local win = meths.open_win(buf, false, 20, 2, {relative='win', row=0, col=10})
local win = meths.open_win(buf, false, {relative='win', width=20, height=2, row=0, col=10})
if multigrid then
screen:expect{grid=[[
## grid 1
@ -329,7 +329,7 @@ describe('floating windows', function()
]])
end
meths.win_set_config(win, -1, -1, {relative='cursor', row=1, col=-2})
meths.win_set_config(win, {relative='cursor', row=1, col=-2})
if multigrid then
screen:expect{grid=[[
## grid 1
@ -370,7 +370,7 @@ describe('floating windows', function()
]])
end
meths.win_set_config(win, -1, -1, {relative='cursor', row=0, col=0, anchor='SW'})
meths.win_set_config(win, {relative='cursor', row=0, col=0, anchor='SW'})
if multigrid then
screen:expect{grid=[[
## grid 1
@ -412,7 +412,7 @@ describe('floating windows', function()
end
meths.win_set_config(win, -1, -1, {relative='win', win=oldwin, row=1, col=10, anchor='NW'})
meths.win_set_config(win, {relative='win', win=oldwin, row=1, col=10, anchor='NW'})
if multigrid then
screen:expect{grid=[[
## grid 1
@ -453,7 +453,7 @@ describe('floating windows', function()
]])
end
meths.win_set_config(win, -1, -1, {relative='win', win=oldwin, row=3, col=39, anchor='SE'})
meths.win_set_config(win, {relative='win', win=oldwin, row=3, col=39, anchor='SE'})
if multigrid then
screen:expect{grid=[[
## grid 1
@ -494,7 +494,7 @@ describe('floating windows', function()
]])
end
meths.win_set_config(win, -1, -1, {relative='win', win=0, row=0, col=50, anchor='NE'})
meths.win_set_config(win, {relative='win', win=0, row=0, col=50, anchor='NE'})
if multigrid then
screen:expect{grid=[[
## grid 1
@ -544,7 +544,7 @@ describe('floating windows', function()
screen2:attach(nil, session2)
screen2:set_default_attr_ids(attrs)
local buf = meths.create_buf(false,false)
meths.open_win(buf, true, 20, 2, {relative='editor', row=2, col=5})
meths.open_win(buf, true, {relative='editor', width=20, height=2, row=2, col=5})
local expected_pos = {
[2]={{id=1001}, 'NW', 1, 2, 5}
}
@ -577,7 +577,7 @@ describe('floating windows', function()
it('handles resized screen', function()
local buf = meths.create_buf(false,false)
meths.buf_set_lines(buf, 0, -1, true, {'such', 'very', 'float'})
local win = meths.open_win(buf, false, 15, 4, {relative='editor', row=2, col=10})
local win = meths.open_win(buf, false, {relative='editor', width=15, height=4, row=2, col=10})
local expected_pos = {
[4]={{id=1002}, 'NW', 1, 2, 10, true},
}
@ -756,7 +756,7 @@ describe('floating windows', function()
]])
end
meths.win_set_config(win, -1, 3, {})
meths.win_set_config(win, {width=0, height=3})
feed('gg')
if multigrid then
screen:expect{grid=[[
@ -1096,7 +1096,7 @@ describe('floating windows', function()
it('does not crash when set cmdheight #9680', function()
local buf = meths.create_buf(false,false)
meths.open_win(buf, false, 20, 2, {relative='editor', row=2, col=5})
meths.open_win(buf, false, {relative='editor', width=20, height=2, row=2, col=5})
command("set cmdheight=2")
eq(1, meths.eval('1'))
end)
@ -1104,7 +1104,7 @@ describe('floating windows', function()
describe('and completion', function()
before_each(function()
local buf = meths.create_buf(false,false)
local win = meths.open_win(buf, true, 12, 4, {relative='editor', row=2, col=5})
local win = meths.open_win(buf, true, {relative='editor', width=12, height=4, row=2, col=5})
meths.win_set_option(win , 'winhl', 'Normal:ErrorMsg')
if multigrid then
screen:expect{grid=[[
@ -1523,7 +1523,7 @@ describe('floating windows', function()
local buf = meths.create_buf(false,true)
meths.buf_set_lines(buf,0,-1,true,{"some info", "about item"})
win = meths.open_win(buf, false, 12, 2, {relative='cursor', row=1, col=10})
win = meths.open_win(buf, false, {relative='cursor', width=12, height=2, row=1, col=10})
if multigrid then
screen:expect{grid=[[
## grid 1
@ -1714,7 +1714,7 @@ describe('floating windows', function()
command("set hidden")
meths.buf_set_lines(0,0,-1,true,{"x"})
local buf = meths.create_buf(false,false)
win = meths.open_win(buf, false, 20, 2, {relative='editor', row=2, col=5})
win = meths.open_win(buf, false, {relative='editor', width=20, height=2, row=2, col=5})
meths.buf_set_lines(buf,0,-1,true,{"y"})
expected_pos = {
[3]={{id=1001}, 'NW', 1, 2, 5, true}
@ -1824,7 +1824,7 @@ describe('floating windows', function()
end)
it("w with focusable=false", function()
meths.win_set_config(win, -1, -1, {focusable=false})
meths.win_set_config(win, {focusable=false})
expected_pos[3][6] = false
feed("<c-w>wi") -- i to provoke redraw
if multigrid then
@ -2038,7 +2038,7 @@ describe('floating windows', function()
end)
it("focus by mouse (focusable=false)", function()
meths.win_set_config(win, -1, -1, {focusable=false})
meths.win_set_config(win, {focusable=false})
meths.buf_set_lines(0, -1, -1, true, {"a"})
expected_pos[3][6] = false
if multigrid then
@ -2939,7 +2939,7 @@ describe('floating windows', function()
end
if multigrid then
meths.win_set_config(0,-1,-1,{external=true})
meths.win_set_config(0, {external=true})
expected_pos = {[3]={external=true}}
screen:expect{grid=[[
## grid 1
@ -2962,7 +2962,7 @@ describe('floating windows', function()
]], float_pos=expected_pos}
else
eq({false, "UI doesn't support external windows"},
meth_pcall(meths.win_set_config, 0,-1,-1,{external=true}))
meth_pcall(meths.win_set_config, 0, {external=true}))
return
end
@ -3228,7 +3228,7 @@ describe('floating windows', function()
it(":tabnew and :tabnext (external)", function()
if multigrid then
meths.win_set_config(win,-1,-1,{external=true})
meths.win_set_config(win, {external=true})
expected_pos = {[3]={external=true}}
feed(":tabnew<cr>")
screen:expect{grid=[[
@ -3259,7 +3259,7 @@ describe('floating windows', function()
]], float_pos=expected_pos}
else
eq({false, "UI doesn't support external windows"},
meth_pcall(meths.win_set_config, 0,-1,-1,{external=true}))
meth_pcall(meths.win_set_config, 0, {external=true}))
end
feed(":tabnext<cr>")