refactor(api): remove some unnecessary HAS_KEY() (#27283)

Since keydicts are already zero-initialized, HAS_KEY() isn't needed if
the zero-initialized value can satisfy some other condition.
This commit is contained in:
zeertzjq
2024-02-01 18:05:06 +08:00
committed by GitHub
parent 0da18ae5ce
commit cc197d04fc
2 changed files with 23 additions and 24 deletions

View File

@@ -2153,7 +2153,6 @@ Dictionary nvim_eval_statusline(String str, Dict(eval_statusline) *opts, Error *
int maxwidth; int maxwidth;
schar_T fillchar = 0; schar_T fillchar = 0;
int statuscol_lnum = 0; int statuscol_lnum = 0;
Window window = 0;
if (str.size < 2 || memcmp(str.data, "%!", 2) != 0) { if (str.size < 2 || memcmp(str.data, "%!", 2) != 0) {
const char *const errmsg = check_stl_option(str.data); const char *const errmsg = check_stl_option(str.data);
@@ -2162,9 +2161,8 @@ Dictionary nvim_eval_statusline(String str, Dict(eval_statusline) *opts, Error *
}); });
} }
if (HAS_KEY(opts, eval_statusline, winid)) { Window window = opts->winid;
window = opts->winid;
}
if (HAS_KEY(opts, eval_statusline, fillchar)) { if (HAS_KEY(opts, eval_statusline, fillchar)) {
VALIDATE_EXP((*opts->fillchar.data != 0 VALIDATE_EXP((*opts->fillchar.data != 0
&& ((size_t)utfc_ptr2len(opts->fillchar.data) == opts->fillchar.size)), && ((size_t)utfc_ptr2len(opts->fillchar.data) == opts->fillchar.size)),

View File

@@ -203,6 +203,7 @@
Window nvim_open_win(Buffer buffer, Boolean enter, Dict(float_config) *config, Error *err) Window nvim_open_win(Buffer buffer, Boolean enter, Dict(float_config) *config, Error *err)
FUNC_API_SINCE(6) FUNC_API_TEXTLOCK_ALLOW_CMDWIN FUNC_API_SINCE(6) FUNC_API_TEXTLOCK_ALLOW_CMDWIN
{ {
#define HAS_KEY_X(d, key) HAS_KEY(d, float_config, key)
buf_T *buf = find_buffer_by_handle(buffer, err); buf_T *buf = find_buffer_by_handle(buffer, err);
if (!buf) { if (!buf) {
return 0; return 0;
@@ -217,13 +218,13 @@ Window nvim_open_win(Buffer buffer, Boolean enter, Dict(float_config) *config, E
return 0; return 0;
} }
bool is_split = HAS_KEY(config, float_config, split) || HAS_KEY(config, float_config, vertical); bool is_split = HAS_KEY_X(config, split) || HAS_KEY_X(config, vertical);
win_T *wp = NULL; win_T *wp = NULL;
tabpage_T *tp = curtab; tabpage_T *tp = curtab;
if (is_split) { if (is_split) {
win_T *parent = NULL; win_T *parent = NULL;
if (!HAS_KEY(config, float_config, win) || config->win != -1) { if (config->win != -1) {
parent = find_window_by_handle(fconfig.window, err); parent = find_window_by_handle(fconfig.window, err);
if (!parent) { if (!parent) {
// find_window_by_handle has already set the error // find_window_by_handle has already set the error
@@ -234,7 +235,7 @@ Window nvim_open_win(Buffer buffer, Boolean enter, Dict(float_config) *config, E
} }
} }
if (HAS_KEY(config, float_config, vertical) && !HAS_KEY(config, float_config, split)) { if (HAS_KEY_X(config, vertical) && !HAS_KEY_X(config, split)) {
if (config->vertical) { if (config->vertical) {
fconfig.split = p_spr ? kWinSplitRight : kWinSplitLeft; fconfig.split = p_spr ? kWinSplitRight : kWinSplitLeft;
} else { } else {
@@ -286,6 +287,7 @@ Window nvim_open_win(Buffer buffer, Boolean enter, Dict(float_config) *config, E
didset_window_options(wp, true); didset_window_options(wp, true);
} }
return wp->handle; return wp->handle;
#undef HAS_KEY_X
} }
static WinSplit win_split_dir(win_T *win) static WinSplit win_split_dir(win_T *win)
@@ -333,20 +335,20 @@ static int win_split_flags(WinSplit split, bool toplevel)
void nvim_win_set_config(Window window, Dict(float_config) *config, Error *err) void nvim_win_set_config(Window window, Dict(float_config) *config, Error *err)
FUNC_API_SINCE(6) FUNC_API_SINCE(6)
{ {
#define HAS_KEY_X(d, key) HAS_KEY(d, float_config, key)
win_T *win = find_window_by_handle(window, err); win_T *win = find_window_by_handle(window, err);
if (!win) { if (!win) {
return; return;
} }
tabpage_T *win_tp = win_find_tabpage(win); tabpage_T *win_tp = win_find_tabpage(win);
bool was_split = !win->w_floating; bool was_split = !win->w_floating;
bool has_split = HAS_KEY(config, float_config, split); bool has_split = HAS_KEY_X(config, split);
bool has_vertical = HAS_KEY(config, float_config, vertical); bool has_vertical = HAS_KEY_X(config, vertical);
// reuse old values, if not overridden // reuse old values, if not overridden
FloatConfig fconfig = win->w_float_config; FloatConfig fconfig = win->w_float_config;
bool to_split = (!HAS_KEY(config, float_config, relative) || striequal(config->relative.data, "")) bool to_split = config->relative.size == 0
&& ((!HAS_KEY(config, float_config, external) && !fconfig.external) && !(HAS_KEY_X(config, external) ? config->external : fconfig.external)
|| !config->external)
&& (has_split || has_vertical || was_split); && (has_split || has_vertical || was_split);
if (!parse_float_config(config, &fconfig, !was_split || to_split, false, err)) { if (!parse_float_config(config, &fconfig, !was_split || to_split, false, err)) {
@@ -359,7 +361,7 @@ void nvim_win_set_config(Window window, Dict(float_config) *config, Error *err)
redraw_later(win, UPD_NOT_VALID); redraw_later(win, UPD_NOT_VALID);
} else if (to_split) { } else if (to_split) {
win_T *parent = NULL; win_T *parent = NULL;
if (!HAS_KEY(config, float_config, win) || config->win != -1) { if (config->win != -1) {
parent = find_window_by_handle(fconfig.window, err); parent = find_window_by_handle(fconfig.window, err);
if (!parent) { if (!parent) {
return; return;
@@ -387,16 +389,14 @@ void nvim_win_set_config(Window window, Dict(float_config) *config, Error *err)
} }
win->w_float_config = fconfig; win->w_float_config = fconfig;
// If there's no vertical or split set, or if the split is the same as the old split, // If there's no "vertical" or "split" set, or if "split" is unchanged,
// then we can just change the size of the window. // then we can just change the size of the window.
if ((!has_vertical && !has_split) if ((!has_vertical && !has_split)
|| (was_split || (was_split && !HAS_KEY_X(config, win) && old_split == fconfig.split)) {
&& !HAS_KEY(config, float_config, if (HAS_KEY_X(config, width)) {
win) && ((!has_split && !has_vertical) || old_split == fconfig.split))) {
if (HAS_KEY(config, float_config, width)) {
win_setwidth_win(fconfig.width, win); win_setwidth_win(fconfig.width, win);
} }
if (HAS_KEY(config, float_config, height)) { if (HAS_KEY_X(config, height)) {
win_setheight_win(fconfig.height, win); win_setheight_win(fconfig.height, win);
} }
redraw_later(win, UPD_NOT_VALID); redraw_later(win, UPD_NOT_VALID);
@@ -518,10 +518,10 @@ void nvim_win_set_config(Window window, Dict(float_config) *config, Error *err)
return; return;
} }
} }
if (HAS_KEY(config, float_config, width)) { if (HAS_KEY_X(config, width)) {
win_setwidth_win(fconfig.width, win); win_setwidth_win(fconfig.width, win);
} }
if (HAS_KEY(config, float_config, height)) { if (HAS_KEY_X(config, height)) {
win_setheight_win(fconfig.height, win); win_setheight_win(fconfig.height, win);
} }
redraw_later(win, UPD_NOT_VALID); redraw_later(win, UPD_NOT_VALID);
@@ -530,12 +530,13 @@ void nvim_win_set_config(Window window, Dict(float_config) *config, Error *err)
win_config_float(win, fconfig); win_config_float(win, fconfig);
win->w_pos_changed = true; win->w_pos_changed = true;
} }
if (HAS_KEY(config, float_config, style)) { if (HAS_KEY_X(config, style)) {
if (fconfig.style == kWinStyleMinimal) { if (fconfig.style == kWinStyleMinimal) {
win_set_minimal_style(win); win_set_minimal_style(win);
didset_window_options(win, true); didset_window_options(win, true);
} }
} }
#undef HAS_KEY_X
} }
static Dictionary config_put_bordertext(Dictionary config, FloatConfig *fconfig, static Dictionary config_put_bordertext(Dictionary config, FloatConfig *fconfig,
@@ -943,7 +944,7 @@ static bool parse_float_config(Dict(float_config) *config, FloatConfig *fconfig,
{ {
#define HAS_KEY_X(d, key) HAS_KEY(d, float_config, key) #define HAS_KEY_X(d, key) HAS_KEY(d, float_config, key)
bool has_relative = false, relative_is_win = false, is_split = false; bool has_relative = false, relative_is_win = false, is_split = false;
if (HAS_KEY_X(config, relative) && !striequal(config->relative.data, "")) { if (config->relative.size > 0) {
if (!parse_float_relative(config->relative, &fconfig->relative)) { if (!parse_float_relative(config->relative, &fconfig->relative)) {
api_set_error(err, kErrorTypeValidation, "Invalid value of 'relative' key"); api_set_error(err, kErrorTypeValidation, "Invalid value of 'relative' key");
return false; return false;
@@ -961,7 +962,7 @@ static bool parse_float_config(Dict(float_config) *config, FloatConfig *fconfig,
relative_is_win = true; relative_is_win = true;
fconfig->bufpos.lnum = -1; fconfig->bufpos.lnum = -1;
} }
} else if (!HAS_KEY_X(config, external) || !config->external) { } else if (!config->external) {
if (HAS_KEY_X(config, vertical) || HAS_KEY_X(config, split)) { if (HAS_KEY_X(config, vertical) || HAS_KEY_X(config, split)) {
is_split = true; is_split = true;
} else if (new_win) { } else if (new_win) {