From cfed9a4123afe3184c3914b6123869f2a52be250 Mon Sep 17 00:00:00 2001 From: Marco Hinz Date: Fri, 15 Mar 2019 18:23:37 +0100 Subject: [PATCH 1/8] api: add nvim_win_get_config() --- src/nvim/api/window.c | 55 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index 157f73c9fa..8a03d7acef 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -472,6 +472,61 @@ void nvim_win_config(Window window, Integer width, Integer height, } } +/// Return window configuration. +/// +/// Return a dictionary containing the same options that can be given to +/// |nvim_open_win()|. +/// +/// @param window Window handle +/// @param[out] err Error details, if any +/// @return Window configuration +Dictionary nvim_win_get_config(Window window, Error *err) + FUNC_API_SINCE(6) +{ + Dictionary rv = ARRAY_DICT_INIT; + + win_T *wp = find_window_by_handle(window, err); + if (!wp) { + return rv; + } + + PUT(rv, "height", INTEGER_OBJ(wp->w_height)); + PUT(rv, "width", INTEGER_OBJ(wp->w_width)); + 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( + float_anchor_str[wp->w_float_config.anchor]))); + + if (wp->w_float_config.relative == kFloatRelativeWindow) { + PUT(rv, "win", INTEGER_OBJ(wp->w_float_config.window)); + } + + if (wp->w_float_config.external) { + return rv; + } + + PUT(rv, "row", FLOAT_OBJ(wp->w_float_config.row)); + PUT(rv, "col", FLOAT_OBJ(wp->w_float_config.col)); + + if (wp->w_floating) { + switch (wp->w_float_config.relative) { + case kFloatRelativeEditor: + PUT(rv, "relative", STRING_OBJ(cstr_to_string("editor"))); + break; + case kFloatRelativeWindow: + PUT(rv, "relative", STRING_OBJ(cstr_to_string("win"))); + break; + case kFloatRelativeCursor: + PUT(rv, "relative", STRING_OBJ(cstr_to_string("cursor"))); + break; + } + } else { + PUT(rv, "relative", STRING_OBJ(cstr_to_string(""))); + } + + return rv; +} + /// Close a window. /// /// This is equivalent to |:close| with count except that it takes a window id. From 98391cd6abb4079ffd79bbc31ed949fd6f2d1a31 Mon Sep 17 00:00:00 2001 From: Marco Hinz Date: Wed, 13 Mar 2019 20:09:00 +0100 Subject: [PATCH 2/8] api: refactor FloatAnchor usage --- src/nvim/api/window.c | 2 +- src/nvim/buffer_defs.h | 25 ++++++++++++++----------- src/nvim/window.c | 24 +++++++++--------------- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index 8a03d7acef..b3acbe90cb 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -495,7 +495,7 @@ Dictionary nvim_win_get_config(Window window, Error *err) 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( - float_anchor_str[wp->w_float_config.anchor]))); + float_anchor_str[wp->w_float_config.anchor]))); if (wp->w_float_config.relative == kFloatRelativeWindow) { PUT(rv, "win", INTEGER_OBJ(wp->w_float_config.window)); diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h index 48cef9b1e7..199399cf4d 100644 --- a/src/nvim/buffer_defs.h +++ b/src/nvim/buffer_defs.h @@ -958,20 +958,23 @@ struct matchitem { int conceal_char; ///< cchar for Conceal highlighting }; -typedef enum { - kFloatAnchorEast = 1, - kFloatAnchorSouth = 2, +typedef int FloatAnchor; - kFloatAnchorNW = 0, - kFloatAnchorNE = 1, - kFloatAnchorSW = 2, - kFloatAnchorSE = 3, -} FloatAnchor; +enum { + kFloatAnchorEast = 1, + kFloatAnchorSouth = 2, +}; + +// NW -> 0 +// NE -> kFloatAnchorEast +// SW -> kFloatAnchorSouth +// SE -> kFloatAnchorSouth | kFloatAnchorEast +EXTERN const char *const float_anchor_str[] INIT(= { "NW", "NE", "SW", "SE" }); typedef enum { - kFloatRelativeEditor = 0, - kFloatRelativeWindow = 1, - kFloatRelativeCursor = 2, + kFloatRelativeEditor = 0, + kFloatRelativeWindow = 1, + kFloatRelativeCursor = 2, } FloatRelative; typedef struct { diff --git a/src/nvim/window.c b/src/nvim/window.c index edb5b06a2e..a4d2189ac1 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -614,12 +614,6 @@ static void ui_ext_win_position(win_T *wp) wp->w_wincol, wp->w_width, wp->w_height); return; } - const char *const anchor_str[] = { - "NW", - "NE", - "SW", - "SE" - }; FloatConfig c = wp->w_float_config; if (!c.external) { @@ -635,7 +629,7 @@ static void ui_ext_win_position(win_T *wp) api_clear_error(&dummy); } if (ui_has(kUIMultigrid)) { - String anchor = cstr_to_string(anchor_str[c.anchor]); + String anchor = cstr_to_string(float_anchor_str[c.anchor]); ui_call_win_float_pos(wp->w_grid.handle, wp->handle, anchor, grid->handle, row, col, c.focusable); } else { @@ -672,14 +666,14 @@ static bool parse_float_anchor(String anchor, FloatAnchor *out) *out = (FloatAnchor)0; } char *str = anchor.data; - if (!STRICMP(str, "NW")) { - *out = kFloatAnchorNW; - } else if (!STRICMP(str, "NE")) { - *out = kFloatAnchorNE; - } else if (!STRICMP(str, "SW")) { - *out = kFloatAnchorSW; - } else if (!STRICMP(str, "SE")) { - *out = kFloatAnchorSE; + if (striequal(str, "NW")) { + *out = 0; // NW is the default + } else if (striequal(str, "NE")) { + *out = kFloatAnchorEast; + } else if (striequal(str, "SW")) { + *out = kFloatAnchorSouth; + } else if (striequal(str, "SE")) { + *out = kFloatAnchorSouth | kFloatAnchorEast; } else { return false; } From 3c88bbecb8dc2bf1fb426cce08af232640bfd44d Mon Sep 17 00:00:00 2001 From: Marco Hinz Date: Thu, 14 Mar 2019 16:37:45 +0100 Subject: [PATCH 3/8] api: nvim_win_config() -> nvim_win_set_config() --- src/nvim/api/window.c | 4 ++-- test/functional/ui/float_spec.lua | 26 +++++++++++++------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index b3acbe90cb..f8bcaddb36 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -444,8 +444,8 @@ Boolean nvim_win_is_valid(Window window) /// 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_config(Window window, Integer width, Integer height, - Dictionary options, Error *err) +void nvim_win_set_config(Window window, Integer width, Integer height, + Dictionary options, Error *err) FUNC_API_SINCE(6) { win_T *win = find_window_by_handle(window, err); diff --git a/test/functional/ui/float_spec.lua b/test/functional/ui/float_spec.lua index bb3255840e..5e1d37e250 100644 --- a/test/functional/ui/float_spec.lua +++ b/test/functional/ui/float_spec.lua @@ -84,7 +84,7 @@ describe('floating windows', function() end - meths.win_config(win,0,0,{relative='editor', row=0, col=10}) + meths.win_set_config(win,0,0,{relative='editor', row=0, col=10}) expected_pos[3][4] = 0 expected_pos[3][5] = 10 if multigrid then @@ -329,7 +329,7 @@ describe('floating windows', function() ]]) end - meths.win_config(win, -1, -1, {relative='cursor', row=1, col=-2}) + meths.win_set_config(win, -1, -1, {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_config(win, -1, -1, {relative='cursor', row=0, col=0, anchor='SW'}) + meths.win_set_config(win, -1, -1, {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_config(win, -1, -1, {relative='win', win=oldwin, row=1, col=10, anchor='NW'}) + meths.win_set_config(win, -1, -1, {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_config(win, -1, -1, {relative='win', win=oldwin, row=3, col=39, anchor='SE'}) + meths.win_set_config(win, -1, -1, {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_config(win, -1, -1, {relative='win', win=0, row=0, col=50, anchor='NE'}) + meths.win_set_config(win, -1, -1, {relative='win', win=0, row=0, col=50, anchor='NE'}) if multigrid then screen:expect{grid=[[ ## grid 1 @@ -756,7 +756,7 @@ describe('floating windows', function() ]]) end - meths.win_config(win, -1, 3, {}) + meths.win_set_config(win, -1, 3, {}) feed('gg') if multigrid then screen:expect{grid=[[ @@ -1824,7 +1824,7 @@ describe('floating windows', function() end) it("w with focusable=false", function() - meths.win_config(win, -1, -1, {focusable=false}) + meths.win_set_config(win, -1, -1, {focusable=false}) expected_pos[3][6] = false feed("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_config(win, -1, -1, {focusable=false}) + meths.win_set_config(win, -1, -1, {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_config(0,-1,-1,{external=true}) + meths.win_set_config(0,-1,-1,{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_config, 0,-1,-1,{external=true})) + meth_pcall(meths.win_set_config, 0,-1,-1,{external=true})) return end @@ -3228,7 +3228,7 @@ describe('floating windows', function() it(":tabnew and :tabnext (external)", function() if multigrid then - meths.win_config(win,-1,-1,{external=true}) + meths.win_set_config(win,-1,-1,{external=true}) expected_pos = {[3]={external=true}} feed(":tabnew") 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_config, 0,-1,-1,{external=true})) + meth_pcall(meths.win_set_config, 0,-1,-1,{external=true})) end feed(":tabnext") From 96edbe7b1d6144f3cf9b720d61182ee31858b478 Mon Sep 17 00:00:00 2001 From: Marco Hinz Date: Fri, 15 Mar 2019 18:03:12 +0100 Subject: [PATCH 4/8] api: add width/height to FloatConfig --- src/nvim/api/vim.c | 11 +++--- src/nvim/api/window.c | 17 +++++----- src/nvim/buffer_defs.h | 4 ++- src/nvim/ui.c | 4 ++- src/nvim/window.c | 49 ++++++++++++++++++++------- test/functional/ui/float_spec.lua | 56 +++++++++++++++---------------- 6 files changed, 83 insertions(+), 58 deletions(-) diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index a773234ea0..37bad4d517 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -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; } diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index f8bcaddb36..ba505af87e 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -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( diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h index 199399cf4d..f11f3e2332 100644 --- a/src/nvim/buffer_defs.h +++ b/src/nvim/buffer_defs.h @@ -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 }) diff --git a/src/nvim/ui.c b/src/nvim/ui.c index d07ea3179e..0dddfe5f59 100644 --- a/src/nvim/ui.c +++ b/src/nvim/ui.c @@ -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 diff --git a/src/nvim/window.c b/src/nvim/window.c index a4d2189ac1..dc043c3c45 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -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; diff --git a/test/functional/ui/float_spec.lua b/test/functional/ui/float_spec.lua index 5e1d37e250..ac150bcd3e 100644 --- a/test/functional/ui/float_spec.lua +++ b/test/functional/ui/float_spec.lua @@ -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('ixygg') - 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("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") 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") From 27c4b6b9bd90fbc3a41945f87ec944bd0ced8228 Mon Sep 17 00:00:00 2001 From: Marco Hinz Date: Fri, 15 Mar 2019 18:18:56 +0100 Subject: [PATCH 5/8] api: update doc --- runtime/doc/api.txt | 49 ++++++++++++++++++++++++++++++------------- src/nvim/api/window.c | 8 ++++++- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/runtime/doc/api.txt b/runtime/doc/api.txt index 7172091ceb..d057dfca96 100644 --- a/runtime/doc/api.txt +++ b/runtime/doc/api.txt @@ -245,10 +245,10 @@ functional buffer windows and support user editing. They support the standard 'statusline' is not supported currently). Floating windows are created either by |nvim_open_win()| to open a new window, -or |nvim_win_config()| to reconfigure a normal window into a float. Currently -the position can either be grid coordinates relative the top-left of some -window, or a position relative to the current window cursor. The parameters -for positioning are described in detail at |nvim_open_win()| help. +or |nvim_win_set_config()| to reconfigure a normal window into a float. +Currently the position can either be grid coordinates relative the top-left of +some window, or a position relative to the current window cursor. The +parameters for positioning are described in detail at |nvim_open_win()|. |nvim_open_win()| assumes an existing buffer to display in the window. To create a scratch buffer for the float, |nvim_create_buffer()| can be used. The text in @@ -264,8 +264,9 @@ Here is an example for creating a float with scratch buffer: > let buf = nvim_create_buf(v:false, v:true) call nvim_buf_set_lines(buf, 0, -1, v:true, ["test", "text"]) - let opts = {'relative': 'cursor', 'col':0, 'row':1, 'anchor': 'NW'} - let win = nvim_open_win(buf, 0, 10, 2, opts) + let opts = {'relative': 'cursor', 'width': 10, 'height': 2, 'col': 0, + \ 'row': 1, 'anchor': 'NW'} + let win = nvim_open_win(buf, 0, opts) " optional: change highlight, otherwise Pmenu is used call nvim_win_set_option(win, 'winhl', 'Normal:MyHighlight') > @@ -624,8 +625,7 @@ nvim_create_buf({listed}, {scratch}) *nvim_create_buf()* Return: ~ Buffer handle, or 0 on error - *nvim_open_win()* -nvim_open_win({buffer}, {enter}, {width}, {height}, {options}) +nvim_open_win({buffer}, {enter}, {options}) *nvim_open_win()* Open a new window. Currently this is used to open floating and external windows. @@ -643,8 +643,6 @@ nvim_open_win({buffer}, {enter}, {width}, {height}, {options}) {buffer} handle of buffer to be displayed in the window {enter} whether the window should be entered (made the current window) - {width} width of window (in character cells) - {height} height of window (in character cells) {options} dict of options for configuring window positioning accepts the following keys: @@ -670,6 +668,12 @@ nvim_open_win({buffer}, {enter}, {width}, {height}, {options}) 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. @@ -1561,21 +1565,38 @@ nvim_win_is_valid({window}) *nvim_win_is_valid()* Return: ~ true if the window is valid, false otherwise - *nvim_win_config()* -nvim_win_config({window}, {width}, {height}, {options}) +nvim_win_set_config({window}, {options}) *nvim_win_set_config()* Configure window position. Currently this is only used to configure floating and external windows (including changing a split window to these types). See documentation at |nvim_open_win()|, for the meaning of - parameters. Pass in -1 for 'witdh' and 'height' to keep - exiting size. + parameters. Pass in 0 for `width` and `height` to keep + existing 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. + Parameters: ~ + {window} Window handle + {options} Dictionary of options + +nvim_win_get_config({window}) *nvim_win_get_config()* + Return window configuration. + + Return a dictionary containing the same options that can be + given to |nvim_open_win()|. + + `relative` will be empty for normal windows. + + Parameters: ~ + {window} Window handle + + Return: ~ + Window configuration + nvim_win_close({window}, {force}) *nvim_win_close()* Close a window. diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index ba505af87e..dd63dbbb4a 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -439,11 +439,15 @@ Boolean nvim_win_is_valid(Window window) /// types). /// /// See documentation at |nvim_open_win()|, for the meaning of parameters. Pass -/// in 0 for 'witdh' and 'height' to keep exiting size. +/// in 0 for `width` and `height` to keep existing 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. +/// +/// @param window Window handle +/// @param options Dictionary of options +/// @param[out] err Error details, if any void nvim_win_set_config(Window window, Dictionary options, Error *err) FUNC_API_SINCE(6) { @@ -476,6 +480,8 @@ void nvim_win_set_config(Window window, Dictionary options, Error *err) /// Return a dictionary containing the same options that can be given to /// |nvim_open_win()|. /// +/// `relative` will be empty for normal windows. +/// /// @param window Window handle /// @param[out] err Error details, if any /// @return Window configuration From 86992a7bb1fbf7f4eb2632a473ae3dbe5221d50f Mon Sep 17 00:00:00 2001 From: Marco Hinz Date: Sat, 16 Mar 2019 13:57:50 +0100 Subject: [PATCH 6/8] api: numerous small fixes --- runtime/doc/api.txt | 38 ++++++------ src/nvim/api/vim.c | 43 +++++++------- src/nvim/api/window.c | 27 ++++----- src/nvim/window.c | 98 +++++++++++++++---------------- test/functional/ui/float_spec.lua | 10 ++-- 5 files changed, 103 insertions(+), 113 deletions(-) diff --git a/runtime/doc/api.txt b/runtime/doc/api.txt index d057dfca96..ed8897a2ee 100644 --- a/runtime/doc/api.txt +++ b/runtime/doc/api.txt @@ -625,7 +625,7 @@ nvim_create_buf({listed}, {scratch}) *nvim_create_buf()* Return: ~ Buffer handle, or 0 on error -nvim_open_win({buffer}, {enter}, {options}) *nvim_open_win()* +nvim_open_win({buffer}, {enter}, {config}) *nvim_open_win()* Open a new window. Currently this is used to open floating and external windows. @@ -643,19 +643,21 @@ nvim_open_win({buffer}, {enter}, {options}) *nvim_open_win()* {buffer} handle of buffer to be displayed in the window {enter} whether the window should be entered (made the current window) - {options} dict of options for configuring window - positioning accepts the following keys: + {config} dictionary for the window configuration accepts + these keys: `relative`: If set, the window becomes a floating window. The window will be placed with row,col coordinates relative one of the following: "editor" the global editor grid - "win" a window. Use 'win' option below to - specify window id, or current window will - be used by default. + "win" a window. Use `win` to specify window id, + or current window will be used by default. "cursor" the cursor position in current window. + `win`: when using `relative='win'`, window id of the window + where the position is defined. + `anchor`: the corner of the float that the row,col position defines "NW" north-west (default) @@ -668,11 +670,9 @@ nvim_open_win({buffer}, {enter}, {options}) *nvim_open_win()* the window can still be entered using |nvim_set_current_win()| API call. - `height`: window height in character cells. Cannot be - smaller than 1. + `height`: Window height in character cells. Minimum of 1. - `width`: window width in character cells. Cannot be - smaller than 2. + `width`: Window width in character cells. Minimum of 2. `row`: row position. Screen cell height are used as unit. Can be floating point. @@ -680,12 +680,9 @@ nvim_open_win({buffer}, {enter}, {options}) *nvim_open_win()* `col`: column position. Screen cell width is used as unit. Can be floating point. - `win`: when using relative='win', window id of the window - where the position is defined. - `external`: GUI should display the window as an external top-level window. Currently accepts no other - positioning options together with this. + positioning configuration together with this. With editor positioning row=0, col=0 refers to the top-left corner of the screen-grid and row=Lines-1, Columns-1 refers to @@ -1565,14 +1562,13 @@ nvim_win_is_valid({window}) *nvim_win_is_valid()* Return: ~ true if the window is valid, false otherwise -nvim_win_set_config({window}, {options}) *nvim_win_set_config()* +nvim_win_set_config({window}, {config}) *nvim_win_set_config()* Configure window position. Currently this is only used to configure floating and external windows (including changing a split window to these types). See documentation at |nvim_open_win()|, for the meaning of - parameters. Pass in 0 for `width` and `height` to keep - existing size. + parameters. When reconfiguring a floating window, absent option keys will not be changed. The following restriction apply: `row`, `col` @@ -1580,16 +1576,16 @@ nvim_win_set_config({window}, {options}) *nvim_win_set_config()* subset of these is an error. Parameters: ~ - {window} Window handle - {options} Dictionary of options + {window} Window handle + {config} Dictionary of window configuration nvim_win_get_config({window}) *nvim_win_get_config()* Return window configuration. - Return a dictionary containing the same options that can be + Return a dictionary containing the same config that can be given to |nvim_open_win()|. - `relative` will be empty for normal windows. + `relative` will be an empty string for normal windows. Parameters: ~ {window} Window handle diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 37bad4d517..223d90758b 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -1003,35 +1003,35 @@ Buffer nvim_create_buf(Boolean listed, Boolean scratch, Error *err) /// Exactly one of `external` and `relative` must be specified. /// /// @param buffer handle of buffer to be displayed in the window -/// @param enter whether the window should be entered (made the current window) -/// @param options dict of options for configuring window positioning -/// accepts the following keys: +/// @param enter whether the window should be entered (made the current window) +/// @param config dictionary for the window configuration accepts these keys: +/// /// `relative`: If set, the window becomes a floating window. The window /// will be placed with row,col coordinates relative one of the /// following: /// "editor" the global editor grid -/// "win" a window. Use 'win' option below to specify window id, -/// or current window will be used by default. +/// "win" a window. Use `win` to specify a window id, +/// or the current window will be used by default. /// "cursor" the cursor position in current window. -/// `anchor`: the corner of the float that the row,col position defines +/// `win`: When using relative='win', window id of the window where the +/// position is defined. +/// `anchor`: The corner of the float that the row,col position defines: /// "NW" north-west (default) /// "NE" north-east /// "SW" south-west /// "SE" south-east -/// `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 +/// `height`: window height (in character cells). Minimum of 1. +/// `width`: window width (in character cells). Minimum of 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 /// floating point. -/// `win`: when using relative='win', window id of the window where the -/// position is defined. -/// `external` GUI should display the window as an external -/// top-level window. Currently accepts no other positioning options -/// together with this. +/// `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. +/// `external`: GUI should display the window as an external +/// top-level window. Currently accepts no other positioning +/// configuration together with this. /// /// With editor positioning row=0, col=0 refers to the top-left corner of the /// screen-grid and row=Lines-1, Columns-1 refers to the bottom-right corner. @@ -1047,15 +1047,14 @@ 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, Dictionary options, - Error *err) +Window nvim_open_win(Buffer buffer, Boolean enter, Dictionary config, Error *err) FUNC_API_SINCE(6) { - FloatConfig config = FLOAT_CONFIG_INIT; - if (!parse_float_config(options, &config, false, err)) { + FloatConfig fconfig = FLOAT_CONFIG_INIT; + if (!parse_float_config(config, &fconfig, false, err)) { return 0; } - win_T *wp = win_new_float(NULL, config, err); + win_T *wp = win_new_float(NULL, fconfig, err); if (!wp) { return 0; } diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index dd63dbbb4a..9e53a7bf14 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -438,17 +438,16 @@ Boolean nvim_win_is_valid(Window window) /// floating and external windows (including changing a split window to these /// types). /// -/// See documentation at |nvim_open_win()|, for the meaning of parameters. Pass -/// in 0 for `width` and `height` to keep existing size. +/// See documentation at |nvim_open_win()|, for the meaning of parameters. /// /// 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. /// /// @param window Window handle -/// @param options Dictionary of options +/// @param config Dictionary of window configuration /// @param[out] err Error details, if any -void nvim_win_set_config(Window window, Dictionary options, Error *err) +void nvim_win_set_config(Window window, Dictionary config, Error *err) FUNC_API_SINCE(6) { win_T *win = find_window_by_handle(window, err); @@ -457,34 +456,34 @@ void nvim_win_set_config(Window window, Dictionary options, Error *err) } bool new_float = !win->w_floating; // reuse old values, if not overriden - FloatConfig config = new_float ? FLOAT_CONFIG_INIT : win->w_float_config; + FloatConfig fconfig = new_float ? FLOAT_CONFIG_INIT : win->w_float_config; - if (!parse_float_config(options, &config, !new_float, err)) { + if (!parse_float_config(config, &fconfig, !new_float, err)) { return; } - config.height = config.height > 0 ? config.height : win->w_height; - config.width = config.width > 0 ? config.width : win->w_width; + fconfig.height = fconfig.height > 0 ? fconfig.height : win->w_height; + fconfig.width = fconfig.width > 0 ? fconfig.width : win->w_width; if (new_float) { - if (!win_new_float(win, config, err)) { + if (!win_new_float(win, fconfig, err)) { return; } redraw_later(NOT_VALID); } else { - win_config_float(win, config); + win_config_float(win, fconfig); win->w_pos_changed = true; } } /// Return window configuration. /// -/// Return a dictionary containing the same options that can be given to +/// Return a dictionary containing the same config that can be given to /// |nvim_open_win()|. /// -/// `relative` will be empty for normal windows. +/// `relative` will be an empty string for normal windows. /// -/// @param window Window handle +/// @param window Window handle /// @param[out] err Error details, if any -/// @return Window configuration +/// @return Window configuration Dictionary nvim_win_get_config(Window window, Error *err) FUNC_API_SINCE(6) { diff --git a/src/nvim/window.c b/src/nvim/window.c index dc043c3c45..7acf2a7f7e 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -539,7 +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, FloatConfig config, Error *err) +win_T *win_new_float(win_T *wp, FloatConfig fconfig, Error *err) { if (wp == NULL) { wp = win_alloc(lastwin_nofloating(), false); @@ -571,26 +571,26 @@ win_T *win_new_float(win_T *wp, FloatConfig config, Error *err) // 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, config); + win_config_float(wp, fconfig); wp->w_pos_changed = true; redraw_win_later(wp, VALID); return wp; } -void win_config_float(win_T *wp, FloatConfig config) +void win_config_float(win_T *wp, FloatConfig fconfig) { - wp->w_width = MAX(config.width, 2); - wp->w_height = MAX(config.height, 1); + wp->w_width = MAX(fconfig.width, 2); + wp->w_height = MAX(fconfig.height, 1); - if (config.relative == kFloatRelativeCursor) { - config.relative = kFloatRelativeWindow; - config.row += curwin->w_wrow; - config.col += curwin->w_wcol; - config.window = curwin->handle; + if (fconfig.relative == kFloatRelativeCursor) { + fconfig.relative = kFloatRelativeWindow; + fconfig.row += curwin->w_wrow; + fconfig.col += curwin->w_wcol; + fconfig.window = curwin->handle; } - bool change_external = config.external != wp->w_float_config.external; - wp->w_float_config = config; + bool change_external = fconfig.external != wp->w_float_config.external; + wp->w_float_config = fconfig; if (!ui_has(kUIMultigrid)) { wp->w_height = MIN(wp->w_height, Rows-1); @@ -697,7 +697,7 @@ static bool parse_float_relative(String relative, FloatRelative *out) return true; } -bool parse_float_config(Dictionary config, FloatConfig *out, bool reconf, +bool parse_float_config(Dictionary config, FloatConfig *fconfig, bool reconf, Error *err) { bool has_row = false, has_col = false, has_relative = false; @@ -709,62 +709,62 @@ bool parse_float_config(Dictionary config, FloatConfig *out, bool reconf, if (!strcmp(key, "row")) { has_row = true; if (val.type == kObjectTypeInteger) { - out->row = val.data.integer; + fconfig->row = val.data.integer; } else if (val.type == kObjectTypeFloat) { - out->row = val.data.floating; + fconfig->row = val.data.floating; } else { api_set_error(err, kErrorTypeValidation, - "'row' option has to be Integer or Float"); + "'row' key must be Integer or Float"); return false; } } else if (!strcmp(key, "col")) { has_col = true; if (val.type == kObjectTypeInteger) { - out->col = val.data.integer; + fconfig->col = val.data.integer; } else if (val.type == kObjectTypeFloat) { - out->col = val.data.floating; + fconfig->col = val.data.floating; } else { api_set_error(err, kErrorTypeValidation, - "'col' option has to be Integer or Float"); + "'col' key must be Integer or Float"); return false; } } else if (strequal(key, "width")) { if (val.type == kObjectTypeInteger && val.data.integer >= 0) { - out->width = val.data.integer; + fconfig->width = val.data.integer; } else { api_set_error(err, kErrorTypeValidation, - "'width' option has to be a positive Integer"); + "'width' key must be a positive Integer"); return false; } } else if (strequal(key, "height")) { if (val.type == kObjectTypeInteger && val.data.integer >= 0) { - out->height= val.data.integer; + fconfig->height= val.data.integer; } else { api_set_error(err, kErrorTypeValidation, - "'height' option has to be a positive Integer"); + "'height' key must be a positive Integer"); return false; } } else if (!strcmp(key, "anchor")) { if (val.type != kObjectTypeString) { api_set_error(err, kErrorTypeValidation, - "'anchor' option has to be String"); + "'anchor' key must be String"); return false; } - if (!parse_float_anchor(val.data.string, &out->anchor)) { + if (!parse_float_anchor(val.data.string, &fconfig->anchor)) { api_set_error(err, kErrorTypeValidation, - "Invalid value of 'anchor' option"); + "Invalid value of 'anchor' key"); return false; } } else if (!strcmp(key, "relative")) { has_relative = true; if (val.type != kObjectTypeString) { api_set_error(err, kErrorTypeValidation, - "'relative' option has to be String"); + "'relative' key must be String"); return false; } - if (!parse_float_relative(val.data.string, &out->relative)) { + if (!parse_float_relative(val.data.string, &fconfig->relative)) { api_set_error(err, kErrorTypeValidation, - "Invalid value of 'relative' option"); + "Invalid value of 'relative' key"); return false; } } else if (!strcmp(key, "win")) { @@ -772,62 +772,62 @@ bool parse_float_config(Dictionary config, FloatConfig *out, bool reconf, if (val.type != kObjectTypeInteger && val.type != kObjectTypeWindow) { api_set_error(err, kErrorTypeValidation, - "'win' option has to be Integer or Window"); + "'win' key must be Integer or Window"); return false; } - out->window = val.data.integer; + fconfig->window = val.data.integer; } else if (!strcmp(key, "external")) { if (val.type == kObjectTypeInteger) { - out->external = val.data.integer; + fconfig->external = val.data.integer; } else if (val.type == kObjectTypeBoolean) { - out->external = val.data.boolean; + fconfig->external = val.data.boolean; } else { api_set_error(err, kErrorTypeValidation, - "'external' option has to be Boolean"); + "'external' key must be Boolean"); return false; } - has_external = out->external; + has_external = fconfig->external; } else if (!strcmp(key, "focusable")) { if (val.type == kObjectTypeInteger) { - out->focusable = val.data.integer; + fconfig->focusable = val.data.integer; } else if (val.type == kObjectTypeBoolean) { - out->focusable = val.data.boolean; + fconfig->focusable = val.data.boolean; } else { api_set_error(err, kErrorTypeValidation, - "'focusable' option has to be Boolean"); + "'focusable' key must be Boolean"); return false; } } else { api_set_error(err, kErrorTypeValidation, - "Invalid options key '%s'", key); + "Invalid key '%s'", key); return false; } } - if (has_window && !(has_relative && out->relative == kFloatRelativeWindow)) { + if (has_window && !(has_relative && fconfig->relative == kFloatRelativeWindow)) { api_set_error(err, kErrorTypeValidation, - "'win' option is only valid with relative='win'"); + "'win' key is only valid with relative='win'"); return false; } - if ((has_relative && out->relative == kFloatRelativeWindow) - && (!has_window || out->window == 0)) { - out->window = curwin->handle; + if ((has_relative && fconfig->relative == kFloatRelativeWindow) + && (!has_window || fconfig->window == 0)) { + fconfig->window = curwin->handle; } if (has_relative && has_external) { api_set_error(err, kErrorTypeValidation, - "Only one of 'relative' and 'external' should be used"); + "Only one of 'relative' and 'external' must be used"); return false; } else if (!reconf && !has_relative && !has_external) { api_set_error(err, kErrorTypeValidation, "One of 'relative' and 'external' must be used"); return false; } else if (has_relative) { - out->external = false; + fconfig->external = false; } - if (out->external && !ui_has(kUIMultigrid)) { + if (fconfig->external && !ui_has(kUIMultigrid)) { api_set_error(err, kErrorTypeValidation, "UI doesn't support external windows"); return false; @@ -3819,8 +3819,6 @@ 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) { - 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; @@ -4756,7 +4754,6 @@ void win_setheight_win(int height, win_T *win) if (win->w_floating) { if (win->w_float_config.external) { - win->w_float_config.width = win->w_width; win->w_float_config.height = height; win_config_float(win, win->w_float_config); } else { @@ -4964,7 +4961,6 @@ void win_setwidth_win(int width, win_T *wp) if (wp->w_floating) { if (wp->w_float_config.external) { 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(); diff --git a/test/functional/ui/float_spec.lua b/test/functional/ui/float_spec.lua index ac150bcd3e..6f68c08800 100644 --- a/test/functional/ui/float_spec.lua +++ b/test/functional/ui/float_spec.lua @@ -231,15 +231,15 @@ describe('floating windows', function() it('API has proper error messages', function() local buf = meths.create_buf(false,false) - eq({false, "Invalid options key 'bork'"}, + eq({false, "Invalid key 'bork'"}, meth_pcall(meths.open_win,buf, false, {width=20,height=2,bork=true})) - eq({false, "'win' option is only valid with relative='win'"}, + eq({false, "'win' key is only valid with relative='win'"}, 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"}, + eq({false, "Only one of 'relative' and 'external' must be used"}, 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"}, + eq({false, "Invalid value of 'relative' key"}, meth_pcall(meths.open_win,buf, false, {width=20,height=2,relative='shell',row=0,col=0})) - eq({false, "Invalid value of 'anchor' option"}, + eq({false, "Invalid value of 'anchor' key"}, 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, {width=20,height=2,relative='editor'})) From 073ab7cda83ca2f804b02e10bc6be17a02c4d3c9 Mon Sep 17 00:00:00 2001 From: Marco Hinz Date: Sat, 16 Mar 2019 15:34:16 +0100 Subject: [PATCH 7/8] api: refactor FloatRelative usage --- src/nvim/api/vim.c | 3 ++- src/nvim/api/window.c | 18 +++--------------- src/nvim/buffer_defs.h | 8 ++++++-- src/nvim/window.c | 9 +++++---- 4 files changed, 16 insertions(+), 22 deletions(-) diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 223d90758b..ae8404a530 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -1047,7 +1047,8 @@ 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, Dictionary config, Error *err) +Window nvim_open_win(Buffer buffer, Boolean enter, Dictionary config, + Error *err) FUNC_API_SINCE(6) { FloatConfig fconfig = FLOAT_CONFIG_INIT; diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index 9e53a7bf14..2204170aab 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -512,21 +512,9 @@ Dictionary nvim_win_get_config(Window window, Error *err) PUT(rv, "row", FLOAT_OBJ(wp->w_float_config.row)); PUT(rv, "col", FLOAT_OBJ(wp->w_float_config.col)); - if (wp->w_floating) { - switch (wp->w_float_config.relative) { - case kFloatRelativeEditor: - PUT(rv, "relative", STRING_OBJ(cstr_to_string("editor"))); - break; - case kFloatRelativeWindow: - PUT(rv, "relative", STRING_OBJ(cstr_to_string("win"))); - break; - case kFloatRelativeCursor: - PUT(rv, "relative", STRING_OBJ(cstr_to_string("cursor"))); - break; - } - } else { - PUT(rv, "relative", STRING_OBJ(cstr_to_string(""))); - } + const char *rel = + wp->w_floating ? float_relative_str[wp->w_float_config.relative] : ""; + PUT(rv, "relative", STRING_OBJ(cstr_to_string(rel))); return rv; } diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h index f11f3e2332..614185a463 100644 --- a/src/nvim/buffer_defs.h +++ b/src/nvim/buffer_defs.h @@ -959,6 +959,7 @@ struct matchitem { }; typedef int FloatAnchor; +typedef int FloatRelative; enum { kFloatAnchorEast = 1, @@ -971,11 +972,14 @@ enum { // SE -> kFloatAnchorSouth | kFloatAnchorEast EXTERN const char *const float_anchor_str[] INIT(= { "NW", "NE", "SW", "SE" }); -typedef enum { +enum { kFloatRelativeEditor = 0, kFloatRelativeWindow = 1, kFloatRelativeCursor = 2, -} FloatRelative; +}; + +EXTERN const char *const float_relative_str[] INIT(= { "editor", "window", + "cursor" }); typedef struct { Window window; diff --git a/src/nvim/window.c b/src/nvim/window.c index 7acf2a7f7e..609d8f1b4f 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -685,11 +685,11 @@ static bool parse_float_relative(String relative, FloatRelative *out) *out = (FloatRelative)0; } char *str = relative.data; - if (!STRICMP(str, "editor")) { + if (striequal(str, "editor")) { *out = kFloatRelativeEditor; - } else if (!STRICMP(str, "win")) { + } else if (striequal(str, "win")) { *out = kFloatRelativeWindow; - } else if (!STRICMP(str, "cursor")) { + } else if (striequal(str, "cursor")) { *out = kFloatRelativeCursor; } else { return false; @@ -804,7 +804,8 @@ bool parse_float_config(Dictionary config, FloatConfig *fconfig, bool reconf, } } - if (has_window && !(has_relative && fconfig->relative == kFloatRelativeWindow)) { + if (has_window && !(has_relative + && fconfig->relative == kFloatRelativeWindow)) { api_set_error(err, kErrorTypeValidation, "'win' key is only valid with relative='win'"); return false; From b557654f36c4c3c4352c838d0d8d1df3e5340655 Mon Sep 17 00:00:00 2001 From: Marco Hinz Date: Sat, 16 Mar 2019 16:00:38 +0100 Subject: [PATCH 8/8] api: add tests for new code paths --- test/functional/ui/float_spec.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/functional/ui/float_spec.lua b/test/functional/ui/float_spec.lua index 6f68c08800..06d3b6bdda 100644 --- a/test/functional/ui/float_spec.lua +++ b/test/functional/ui/float_spec.lua @@ -152,6 +152,13 @@ describe('floating windows', function() end end) + it('return their configuration', function() + local buf = meths.create_buf(false, false) + local win = meths.open_win(buf, false, {relative='editor', width=20, height=2, row=3, col=5}) + local expected = {anchor='NW', col=5, external=false, focusable=true, height=2, relative='editor', row=3, width=20} + eq(expected, meths.win_get_config(win)) + end) + it('defaults to nonumber and NormalFloat highlight', function() command('set number') command('hi NormalFloat guibg=#333333') @@ -243,6 +250,10 @@ describe('floating windows', function() 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, {width=20,height=2,relative='editor'})) + eq({false, "'width' key must be a positive Integer"}, + meth_pcall(meths.open_win,buf, false, {width=-1,height=2,relative='editor'})) + eq({false, "'height' key must be a positive Integer"}, + meth_pcall(meths.open_win,buf, false, {width=20,height=-1,relative='editor'})) end) it('can be placed relative window or cursor', function()