vim-patch:8.0.1389: getqflist() items are missing if not set

Problem:    getqflist() items are missing if not set, that makes it more
            difficult to handle the values.
Solution:   When a value is not available return zero or another invalid
            value. (Yegappan Lakshmanan)

a6d4849c71
This commit is contained in:
Marco Hinz
2019-04-19 18:54:56 +02:00
parent f3b88013a8
commit 6d314484d4
2 changed files with 144 additions and 58 deletions

View File

@@ -4269,69 +4269,27 @@ int qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
if (wp != NULL) {
qi = GET_LOC_LIST(wp);
}
// List is not present or is empty
if (qi == NULL || qi->qf_listcount == 0) {
// If querying for the size of the list, return 0
if (((di = tv_dict_find(what, S_LEN("nr"))) != NULL)
&& (di->di_tv.v_type == VAR_STRING)
&& (STRCMP(di->di_tv.vval.v_string, "$") == 0)) {
return tv_dict_add_nr(retdict, S_LEN("nr"), 0);
}
return FAIL;
}
int status = OK;
int flags = QF_GETLIST_NONE;
int qf_idx = qi->qf_curlist; // default is the current list
if ((di = tv_dict_find(what, S_LEN("nr"))) != NULL) {
// Use the specified quickfix/location list
if (di->di_tv.v_type == VAR_NUMBER) {
// for zero use the current list
if (di->di_tv.vval.v_number != 0) {
qf_idx = (int)di->di_tv.vval.v_number - 1;
if (qf_idx < 0 || qf_idx >= qi->qf_listcount) {
return FAIL;
}
}
} else if (di->di_tv.v_type == VAR_STRING
&& strequal((const char *)di->di_tv.vval.v_string, "$")) {
// Get the last quickfix list number
qf_idx = qi->qf_listcount - 1;
} else {
return FAIL;
}
flags |= QF_GETLIST_NR;
}
if ((di = tv_dict_find(what, S_LEN("id"))) != NULL) {
// Look for a list with the specified id
if (di->di_tv.v_type == VAR_NUMBER) {
// For zero, use the current list or the list specifed by 'nr'
if (di->di_tv.vval.v_number != 0) {
qf_idx = qf_id2nr(qi, (unsigned)di->di_tv.vval.v_number);
if (qf_idx == -1) {
return FAIL; // List not found
}
}
flags |= QF_GETLIST_ID;
} else {
return FAIL;
}
}
if (tv_dict_find(what, S_LEN("all")) != NULL) {
flags |= QF_GETLIST_ALL;
}
if (tv_dict_find(what, S_LEN("title")) != NULL) {
flags |= QF_GETLIST_TITLE;
}
if (tv_dict_find(what, S_LEN("nr")) != NULL) {
flags |= QF_GETLIST_NR;
}
if (tv_dict_find(what, S_LEN("winid")) != NULL) {
flags |= QF_GETLIST_WINID;
}
if (tv_dict_find(what, S_LEN("context")) != NULL) {
flags |= QF_GETLIST_CONTEXT;
}
if (tv_dict_find(what, S_LEN("id")) != NULL) {
flags |= QF_GETLIST_ID;
}
if (tv_dict_find(what, S_LEN("items")) != NULL) {
flags |= QF_GETLIST_ITEMS;
}
@@ -4342,6 +4300,75 @@ int qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
flags |= QF_GETLIST_SIZE;
}
int qf_idx;
if (qi != NULL && qi->qf_listcount != 0) {
qf_idx = qi->qf_curlist; // default is the current list
if ((di = tv_dict_find(what, S_LEN("nr"))) != NULL) {
// Use the specified quickfix/location list
if (di->di_tv.v_type == VAR_NUMBER) {
// for zero use the current list
if (di->di_tv.vval.v_number != 0) {
qf_idx = (int)di->di_tv.vval.v_number - 1;
if (qf_idx < 0 || qf_idx >= qi->qf_listcount) {
qf_idx = -1;
}
}
} else if (di->di_tv.v_type == VAR_STRING
&& strequal((const char *)di->di_tv.vval.v_string, "$")) {
// Get the last quickfix list number
qf_idx = qi->qf_listcount - 1;
} else {
qf_idx = -1;
}
flags |= QF_GETLIST_NR;
}
if ((di = tv_dict_find(what, S_LEN("id"))) != NULL) {
// Look for a list with the specified id
if (di->di_tv.v_type == VAR_NUMBER) {
// For zero, use the current list or the list specifed by 'nr'
if (di->di_tv.vval.v_number != 0) {
qf_idx = qf_id2nr(qi, (unsigned)di->di_tv.vval.v_number);
}
flags |= QF_GETLIST_ID;
} else {
qf_idx = -1;
}
}
}
int status = OK;
// List is not present or is empty
if (qi == NULL || qi->qf_listcount == 0 || qf_idx == -1) {
if (flags & QF_GETLIST_TITLE) {
status = tv_dict_add_str(retdict, S_LEN("title"), (const char *)"");
}
if ((status == OK) && (flags & QF_GETLIST_ITEMS)) {
list_T *l = tv_list_alloc(kListLenMayKnow);
status = tv_dict_add_list(retdict, S_LEN("items"), l);
}
if ((status == OK) && (flags & QF_GETLIST_NR)) {
status = tv_dict_add_nr(retdict, S_LEN("nr"), 0);
}
if ((status == OK) && (flags & QF_GETLIST_WINID)) {
status = tv_dict_add_nr(retdict, S_LEN("winid"), 0);
}
if ((status == OK) && (flags & QF_GETLIST_CONTEXT)) {
status = tv_dict_add_str(retdict, S_LEN("context"), (const char *)"");
}
if ((status == OK) && (flags & QF_GETLIST_ID)) {
status = tv_dict_add_nr(retdict, S_LEN("id"), 0);
}
if ((status == OK) && (flags & QF_GETLIST_IDX)) {
status = tv_dict_add_nr(retdict, S_LEN("idx"), 0);
}
if ((status == OK) && (flags & QF_GETLIST_SIZE)) {
status = tv_dict_add_nr(retdict, S_LEN("size"), 0);
}
return status;
}
if (flags & QF_GETLIST_TITLE) {
char_u *t = qi->qf_lists[qf_idx].qf_title;

View File

@@ -1784,8 +1784,8 @@ func Xproperty_tests(cchar)
call assert_equal(-1, s)
call assert_equal({}, g:Xgetlist({'abc':1}))
call assert_equal({}, g:Xgetlist({'nr':99, 'title':1}))
call assert_equal({}, g:Xgetlist({'nr':[], 'title':1}))
call assert_equal('', g:Xgetlist({'nr':99, 'title':1}).title)
call assert_equal('', g:Xgetlist({'nr':[], 'title':1}).title)
if a:cchar == 'l'
call assert_equal({}, getloclist(99, {'title': 1}))
@@ -1821,7 +1821,7 @@ func Xproperty_tests(cchar)
call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
only
call setloclist(0, [], 'f')
call assert_equal({}, getloclist(0, {'context':1}))
call assert_equal('', getloclist(0, {'context':1}).context)
endif
" Test for changing the context of previous quickfix lists
@@ -2335,8 +2335,8 @@ func XsizeTests(cchar)
call g:Xsetlist([], 'f')
call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
call assert_equal(1, len(g:Xgetlist({'nr':'$', 'all':1})))
call assert_equal(0, len(g:Xgetlist({'nr':0})))
call assert_equal('', g:Xgetlist({'nr':'$', 'all':1}).title)
call assert_equal(0, g:Xgetlist({'nr':0}).nr)
Xexpr "File1:10:Line1"
Xexpr "File2:20:Line2"
@@ -2735,7 +2735,7 @@ func Xqfid_tests(cchar)
call s:setup_commands(a:cchar)
call g:Xsetlist([], 'f')
call assert_equal({}, g:Xgetlist({'id':0}))
call assert_equal(0, g:Xgetlist({'id':0}).id)
Xexpr ''
let start_id = g:Xgetlist({'id' : 0}).id
Xexpr '' | Xexpr ''
@@ -2743,10 +2743,10 @@ func Xqfid_tests(cchar)
call assert_equal(start_id, g:Xgetlist({'id':0, 'nr':1}).id)
call assert_equal(start_id + 1, g:Xgetlist({'id':0, 'nr':0}).id)
call assert_equal(start_id + 2, g:Xgetlist({'id':0, 'nr':'$'}).id)
call assert_equal({}, g:Xgetlist({'id':0, 'nr':99}))
call assert_equal(0, g:Xgetlist({'id':0, 'nr':99}).id)
call assert_equal(2, g:Xgetlist({'id':start_id + 1, 'nr':0}).nr)
call assert_equal({}, g:Xgetlist({'id':99, 'nr':0}))
call assert_equal({}, g:Xgetlist({'id':"abc", 'nr':0}))
call assert_equal(0, g:Xgetlist({'id':99, 'nr':0}).id)
call assert_equal(0, g:Xgetlist({'id':"abc", 'nr':0}).id)
call g:Xsetlist([], 'a', {'id':start_id, 'context':[1,2]})
call assert_equal([1,2], g:Xgetlist({'nr':1, 'context':1}).context)
@@ -2757,7 +2757,7 @@ func Xqfid_tests(cchar)
let qfid = g:Xgetlist({'id':0, 'nr':0})
call g:Xsetlist([], 'f')
call assert_equal({}, g:Xgetlist({'id':qfid, 'nr':0}))
call assert_equal(0, g:Xgetlist({'id':qfid, 'nr':0}).id)
endfunc
func Test_qf_id()
@@ -2838,3 +2838,62 @@ func Test_empty_qfbuf()
enew
call delete('Xfile1')
endfunc
" Tests for the getqflist() and getloclist() functions when the list is not
" present or is empty
func Xgetlist_empty_tests(cchar)
call s:setup_commands(a:cchar)
" Empty quickfix stack
call g:Xsetlist([], 'f')
call assert_equal('', g:Xgetlist({'context' : 0}).context)
call assert_equal(0, g:Xgetlist({'id' : 0}).id)
call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
call assert_equal([], g:Xgetlist({'items' : 0}).items)
call assert_equal(0, g:Xgetlist({'nr' : 0}).nr)
call assert_equal(0, g:Xgetlist({'size' : 0}).size)
call assert_equal('', g:Xgetlist({'title' : 0}).title)
call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0}, g:Xgetlist({'all' : 0}))
" Empty quickfix list
Xexpr ""
call assert_equal('', g:Xgetlist({'context' : 0}).context)
call assert_notequal(0, g:Xgetlist({'id' : 0}).id)
call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
call assert_equal([], g:Xgetlist({'items' : 0}).items)
call assert_notequal(0, g:Xgetlist({'nr' : 0}).nr)
call assert_equal(0, g:Xgetlist({'size' : 0}).size)
call assert_notequal('', g:Xgetlist({'title' : 0}).title)
call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
let qfid = g:Xgetlist({'id' : 0}).id
call g:Xsetlist([], 'f')
" Non-existing quickfix identifier
call assert_equal('', g:Xgetlist({'id' : qfid, 'context' : 0}).context)
call assert_equal(0, g:Xgetlist({'id' : qfid}).id)
call assert_equal(0, g:Xgetlist({'id' : qfid, 'idx' : 0}).idx)
call assert_equal([], g:Xgetlist({'id' : qfid, 'items' : 0}).items)
call assert_equal(0, g:Xgetlist({'id' : qfid, 'nr' : 0}).nr)
call assert_equal(0, g:Xgetlist({'id' : qfid, 'size' : 0}).size)
call assert_equal('', g:Xgetlist({'id' : qfid, 'title' : 0}).title)
call assert_equal(0, g:Xgetlist({'id' : qfid, 'winid' : 0}).winid)
call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0}, g:Xgetlist({'id' : qfid, 'all' : 0}))
" Non-existing quickfix list number
call assert_equal('', g:Xgetlist({'nr' : 5, 'context' : 0}).context)
call assert_equal(0, g:Xgetlist({'nr' : 5}).nr)
call assert_equal(0, g:Xgetlist({'nr' : 5, 'idx' : 0}).idx)
call assert_equal([], g:Xgetlist({'nr' : 5, 'items' : 0}).items)
call assert_equal(0, g:Xgetlist({'nr' : 5, 'id' : 0}).id)
call assert_equal(0, g:Xgetlist({'nr' : 5, 'size' : 0}).size)
call assert_equal('', g:Xgetlist({'nr' : 5, 'title' : 0}).title)
call assert_equal(0, g:Xgetlist({'nr' : 5, 'winid' : 0}).winid)
call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0}, g:Xgetlist({'nr' : 5, 'all' : 0}))
endfunc
func Test_getqflist()
call Xgetlist_empty_tests('c')
call Xgetlist_empty_tests('l')
endfunc