eval: Fix setmatches(), setqflist() and setloclist()

This commit is contained in:
ZyX 2017-12-10 22:28:18 +03:00
parent f572bd7e4e
commit 5008205a3e
2 changed files with 87 additions and 93 deletions

View File

@ -14653,8 +14653,8 @@ skip_args:
title = (wp ? "setloclist()" : "setqflist()"); title = (wp ? "setloclist()" : "setqflist()");
} }
list_T *l = list_arg->vval.v_list; list_T *const l = list_arg->vval.v_list;
if (l && set_errorlist(wp, l, action, (char_u *)title, d) == OK) { if (set_errorlist(wp, l, action, (char_u *)title, d) == OK) {
rettv->vval.v_number = 0; rettv->vval.v_number = 0;
} }
} }
@ -14687,92 +14687,89 @@ static void f_setmatches(typval_T *argvars, typval_T *rettv, FunPtr fptr)
EMSG(_(e_listreq)); EMSG(_(e_listreq));
return; return;
} }
list_T *l; list_T *const l = argvars[0].vval.v_list;
if ((l = argvars[0].vval.v_list) != NULL) { // To some extent make sure that we are dealing with a list from
// "getmatches()".
// To some extent make sure that we are dealing with a list from int i = 0;
// "getmatches()". TV_LIST_ITER_CONST(l, li, {
int i = 0; if (TV_LIST_ITEM_TV(li)->v_type != VAR_DICT
TV_LIST_ITER_CONST(l, li, { || (d = TV_LIST_ITEM_TV(li)->vval.v_dict) == NULL) {
if (TV_LIST_ITEM_TV(li)->v_type != VAR_DICT emsgf(_("E474: List item %d is either not a dictionary "
|| (d = TV_LIST_ITEM_TV(li)->vval.v_dict) == NULL) { "or an empty one"), i);
emsgf(_("E474: List item %d is either not a dictionary " return;
"or an empty one"), i);
return;
}
if (!(tv_dict_find(d, S_LEN("group")) != NULL
&& (tv_dict_find(d, S_LEN("pattern")) != NULL
|| tv_dict_find(d, S_LEN("pos1")) != NULL)
&& tv_dict_find(d, S_LEN("priority")) != NULL
&& tv_dict_find(d, S_LEN("id")) != NULL)) {
emsgf(_("E474: List item %d is missing one of the required keys"), i);
return;
}
i++;
});
clear_matches(curwin);
bool match_add_failed = false;
TV_LIST_ITER_CONST(l, li, {
int i = 0;
d = TV_LIST_ITEM_TV(li)->vval.v_dict;
dictitem_T *const di = tv_dict_find(d, S_LEN("pattern"));
if (di == NULL) {
if (s == NULL) {
s = tv_list_alloc();
}
// match from matchaddpos()
for (i = 1; i < 9; i++) {
char buf[5];
snprintf(buf, sizeof(buf), "pos%d", i);
dictitem_T *const pos_di = tv_dict_find(d, buf, -1);
if (pos_di != NULL) {
if (pos_di->di_tv.v_type != VAR_LIST) {
return;
}
tv_list_append_tv(s, &pos_di->di_tv);
tv_list_ref(s);
} else {
break;
}
}
}
// Note: there are three number buffers involved:
// - group_buf below.
// - numbuf in tv_dict_get_string().
// - mybuf in tv_get_string().
//
// If you change this code make sure that buffers will not get
// accidentally reused.
char group_buf[NUMBUFLEN];
const char *const group = tv_dict_get_string_buf(d, "group", group_buf);
const int priority = (int)tv_dict_get_number(d, "priority");
const int id = (int)tv_dict_get_number(d, "id");
dictitem_T *const conceal_di = tv_dict_find(d, S_LEN("conceal"));
const char *const conceal = (conceal_di != NULL
? tv_get_string(&conceal_di->di_tv)
: NULL);
if (i == 0) {
if (match_add(curwin, group,
tv_dict_get_string(d, "pattern", false),
priority, id, NULL, conceal) != id) {
match_add_failed = true;
}
} else {
if (match_add(curwin, group, NULL, priority, id, s, conceal) != id) {
match_add_failed = true;
}
tv_list_unref(s);
s = NULL;
}
});
if (!match_add_failed) {
rettv->vval.v_number = 0;
} }
if (!(tv_dict_find(d, S_LEN("group")) != NULL
&& (tv_dict_find(d, S_LEN("pattern")) != NULL
|| tv_dict_find(d, S_LEN("pos1")) != NULL)
&& tv_dict_find(d, S_LEN("priority")) != NULL
&& tv_dict_find(d, S_LEN("id")) != NULL)) {
emsgf(_("E474: List item %d is missing one of the required keys"), i);
return;
}
i++;
});
clear_matches(curwin);
bool match_add_failed = false;
TV_LIST_ITER_CONST(l, li, {
int i = 0;
d = TV_LIST_ITEM_TV(li)->vval.v_dict;
dictitem_T *const di = tv_dict_find(d, S_LEN("pattern"));
if (di == NULL) {
if (s == NULL) {
s = tv_list_alloc();
}
// match from matchaddpos()
for (i = 1; i < 9; i++) {
char buf[5];
snprintf(buf, sizeof(buf), "pos%d", i);
dictitem_T *const pos_di = tv_dict_find(d, buf, -1);
if (pos_di != NULL) {
if (pos_di->di_tv.v_type != VAR_LIST) {
return;
}
tv_list_append_tv(s, &pos_di->di_tv);
tv_list_ref(s);
} else {
break;
}
}
}
// Note: there are three number buffers involved:
// - group_buf below.
// - numbuf in tv_dict_get_string().
// - mybuf in tv_get_string().
//
// If you change this code make sure that buffers will not get
// accidentally reused.
char group_buf[NUMBUFLEN];
const char *const group = tv_dict_get_string_buf(d, "group", group_buf);
const int priority = (int)tv_dict_get_number(d, "priority");
const int id = (int)tv_dict_get_number(d, "id");
dictitem_T *const conceal_di = tv_dict_find(d, S_LEN("conceal"));
const char *const conceal = (conceal_di != NULL
? tv_get_string(&conceal_di->di_tv)
: NULL);
if (i == 0) {
if (match_add(curwin, group,
tv_dict_get_string(d, "pattern", false),
priority, id, NULL, conceal) != id) {
match_add_failed = true;
}
} else {
if (match_add(curwin, group, NULL, priority, id, s, conceal) != id) {
match_add_failed = true;
}
tv_list_unref(s);
s = NULL;
}
});
if (!match_add_failed) {
rettv->vval.v_number = 0;
} }
} }

View File

@ -42,12 +42,6 @@ describe('NULL', function()
describe('list', function() describe('list', function()
-- Incorrect behaviour -- Incorrect behaviour
-- FIXME should return 0
null_expr_test('is accepted by setqflist()', 'setqflist(L)', 0, -1)
-- FIXME should return 0
null_expr_test('is accepted by setloclist()', 'setloclist(1, L)', 0, -1)
-- FIXME should return 0
null_expr_test('is accepted by setmatches()', 'setmatches(L)', 0, -1)
-- FIXME should not error out -- FIXME should not error out
null_test('is accepted by :cexpr', 'cexpr L', 'Vim(cexpr):E777: String or List expected') null_test('is accepted by :cexpr', 'cexpr L', 'Vim(cexpr):E777: String or List expected')
-- FIXME should not error out -- FIXME should not error out
@ -129,6 +123,9 @@ describe('NULL', function()
'', '\n', function() '', '\n', function()
eq({''}, curbufmeths.get_lines(0, -1, false)) eq({''}, curbufmeths.get_lines(0, -1, false))
end) end)
null_expr_test('is accepted by setmatches()', 'setmatches(L)', 0, 0)
null_expr_test('is accepted by setqflist()', 'setqflist(L)', 0, 0)
null_expr_test('is accepted by setloclist()', 'setloclist(1, L)', 0, 0)
end) end)
describe('dict', function() describe('dict', function()
it('does not crash when indexing NULL dict', function() it('does not crash when indexing NULL dict', function()