Skip to content

Commit a6d4849

Browse files
committed
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, closes #2430)
1 parent fae8ed1 commit a6d4849

4 files changed

Lines changed: 162 additions & 72 deletions

File tree

runtime/doc/eval.txt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4687,7 +4687,7 @@ getqflist([{what}]) *getqflist()*
46874687
winid get the quickfix |window-ID|
46884688
all all of the above quickfix properties
46894689
Non-string items in {what} are ignored. To get the value of a
4690-
particular item, set it to one.
4690+
particular item, set it to zero.
46914691
If "nr" is not present then the current quickfix list is used.
46924692
If both "nr" and a non-zero "id" are specified, then the list
46934693
specified by "id" is used.
@@ -4697,17 +4697,21 @@ getqflist([{what}]) *getqflist()*
46974697
When "lines" is specified, all the other items except "efm"
46984698
are ignored. The returned dictionary contains the entry
46994699
"items" with the list of entries.
4700-
In case of error processing {what}, an empty dictionary is
4701-
returned.
47024700

47034701
The returned dictionary contains the following entries:
4704-
context context information stored with |setqflist()|
4705-
id quickfix list ID |quickfix-ID|
4706-
idx index of the current entry in the list
4707-
items quickfix list entries
4708-
nr quickfix list number
4709-
size number of entries in the quickfix list
4710-
title quickfix list title text
4702+
context context information stored with |setqflist()|.
4703+
If not present, set to "".
4704+
id quickfix list ID |quickfix-ID|. If not
4705+
present, set to 0.
4706+
idx index of the current entry in the list. If not
4707+
present, set to 0.
4708+
items quickfix list entries. If not present, set to
4709+
an empty list.
4710+
nr quickfix list number. If not present, set to 0
4711+
size number of entries in the quickfix list. If not
4712+
present, set to 0.
4713+
title quickfix list title text. If not present, set
4714+
to "".
47114715
winid quickfix |window-ID|. If not present, set to 0
47124716

47134717
Examples: >

src/quickfix.c

Lines changed: 77 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4863,70 +4863,24 @@ qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
48634863
if (wp != NULL)
48644864
qi = GET_LOC_LIST(wp);
48654865

4866-
/* List is not present or is empty */
4867-
if (qi == NULL || qi->qf_listcount == 0)
4868-
{
4869-
/* If querying for the size of the list, return 0 */
4870-
if (((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4871-
&& (di->di_tv.v_type == VAR_STRING)
4872-
&& (STRCMP(di->di_tv.vval.v_string, "$") == 0))
4873-
return dict_add_nr_str(retdict, "nr", 0, NULL);
4874-
return FAIL;
4875-
}
4876-
4877-
qf_idx = qi->qf_curlist; /* default is the current list */
4878-
if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4879-
{
4880-
/* Use the specified quickfix/location list */
4881-
if (di->di_tv.v_type == VAR_NUMBER)
4882-
{
4883-
/* for zero use the current list */
4884-
if (di->di_tv.vval.v_number != 0)
4885-
{
4886-
qf_idx = di->di_tv.vval.v_number - 1;
4887-
if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4888-
return FAIL;
4889-
}
4890-
}
4891-
else if ((di->di_tv.v_type == VAR_STRING)
4892-
&& (STRCMP(di->di_tv.vval.v_string, "$") == 0))
4893-
/* Get the last quickfix list number */
4894-
qf_idx = qi->qf_listcount - 1;
4895-
else
4896-
return FAIL;
4897-
flags |= QF_GETLIST_NR;
4898-
}
4899-
4900-
if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
4901-
{
4902-
/* Look for a list with the specified id */
4903-
if (di->di_tv.v_type == VAR_NUMBER)
4904-
{
4905-
/* For zero, use the current list or the list specifed by 'nr' */
4906-
if (di->di_tv.vval.v_number != 0)
4907-
{
4908-
qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
4909-
if (qf_idx == -1)
4910-
return FAIL; /* List not found */
4911-
}
4912-
flags |= QF_GETLIST_ID;
4913-
}
4914-
else
4915-
return FAIL;
4916-
}
4917-
49184866
if (dict_find(what, (char_u *)"all", -1) != NULL)
49194867
flags |= QF_GETLIST_ALL;
49204868

49214869
if (dict_find(what, (char_u *)"title", -1) != NULL)
49224870
flags |= QF_GETLIST_TITLE;
49234871

4872+
if (dict_find(what, (char_u *)"nr", -1) != NULL)
4873+
flags |= QF_GETLIST_NR;
4874+
49244875
if (dict_find(what, (char_u *)"winid", -1) != NULL)
49254876
flags |= QF_GETLIST_WINID;
49264877

49274878
if (dict_find(what, (char_u *)"context", -1) != NULL)
49284879
flags |= QF_GETLIST_CONTEXT;
49294880

4881+
if (dict_find(what, (char_u *)"id", -1) != NULL)
4882+
flags |= QF_GETLIST_ID;
4883+
49304884
if (dict_find(what, (char_u *)"items", -1) != NULL)
49314885
flags |= QF_GETLIST_ITEMS;
49324886

@@ -4936,6 +4890,77 @@ qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
49364890
if (dict_find(what, (char_u *)"size", -1) != NULL)
49374891
flags |= QF_GETLIST_SIZE;
49384892

4893+
if (qi != NULL && qi->qf_listcount != 0)
4894+
{
4895+
qf_idx = qi->qf_curlist; /* default is the current list */
4896+
if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
4897+
{
4898+
/* Use the specified quickfix/location list */
4899+
if (di->di_tv.v_type == VAR_NUMBER)
4900+
{
4901+
/* for zero use the current list */
4902+
if (di->di_tv.vval.v_number != 0)
4903+
{
4904+
qf_idx = di->di_tv.vval.v_number - 1;
4905+
if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
4906+
qf_idx = -1;
4907+
}
4908+
}
4909+
else if ((di->di_tv.v_type == VAR_STRING)
4910+
&& (STRCMP(di->di_tv.vval.v_string, "$") == 0))
4911+
/* Get the last quickfix list number */
4912+
qf_idx = qi->qf_listcount - 1;
4913+
else
4914+
qf_idx = -1;
4915+
flags |= QF_GETLIST_NR;
4916+
}
4917+
4918+
if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
4919+
{
4920+
/* Look for a list with the specified id */
4921+
if (di->di_tv.v_type == VAR_NUMBER)
4922+
{
4923+
/*
4924+
* For zero, use the current list or the list specifed by 'nr'
4925+
*/
4926+
if (di->di_tv.vval.v_number != 0)
4927+
qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
4928+
flags |= QF_GETLIST_ID;
4929+
}
4930+
else
4931+
qf_idx = -1;
4932+
}
4933+
}
4934+
4935+
/* List is not present or is empty */
4936+
if (qi == NULL || qi->qf_listcount == 0 || qf_idx == -1)
4937+
{
4938+
if (flags & QF_GETLIST_TITLE)
4939+
status = dict_add_nr_str(retdict, "title", 0L, (char_u *)"");
4940+
if ((status == OK) && (flags & QF_GETLIST_ITEMS))
4941+
{
4942+
list_T *l = list_alloc();
4943+
if (l != NULL)
4944+
status = dict_add_list(retdict, "items", l);
4945+
else
4946+
status = FAIL;
4947+
}
4948+
if ((status == OK) && (flags & QF_GETLIST_NR))
4949+
status = dict_add_nr_str(retdict, "nr", 0L, NULL);
4950+
if ((status == OK) && (flags & QF_GETLIST_WINID))
4951+
status = dict_add_nr_str(retdict, "winid", 0L, NULL);
4952+
if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
4953+
status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
4954+
if ((status == OK) && (flags & QF_GETLIST_ID))
4955+
status = dict_add_nr_str(retdict, "id", 0L, NULL);
4956+
if ((status == OK) && (flags & QF_GETLIST_IDX))
4957+
status = dict_add_nr_str(retdict, "idx", 0L, NULL);
4958+
if ((status == OK) && (flags & QF_GETLIST_SIZE))
4959+
status = dict_add_nr_str(retdict, "size", 0L, NULL);
4960+
4961+
return status;
4962+
}
4963+
49394964
if (flags & QF_GETLIST_TITLE)
49404965
{
49414966
char_u *t;

src/testdir/test_quickfix.vim

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,8 +1833,8 @@ func Xproperty_tests(cchar)
18331833
call assert_equal(-1, s)
18341834

18351835
call assert_equal({}, g:Xgetlist({'abc':1}))
1836-
call assert_equal({}, g:Xgetlist({'nr':99, 'title':1}))
1837-
call assert_equal({}, g:Xgetlist({'nr':[], 'title':1}))
1836+
call assert_equal('', g:Xgetlist({'nr':99, 'title':1}).title)
1837+
call assert_equal('', g:Xgetlist({'nr':[], 'title':1}).title)
18381838

18391839
if a:cchar == 'l'
18401840
call assert_equal({}, getloclist(99, {'title': 1}))
@@ -1870,7 +1870,7 @@ func Xproperty_tests(cchar)
18701870
call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
18711871
only
18721872
call setloclist(0, [], 'f')
1873-
call assert_equal({}, getloclist(0, {'context':1}))
1873+
call assert_equal('', getloclist(0, {'context':1}).context)
18741874
endif
18751875

18761876
" Test for changing the context of previous quickfix lists
@@ -2383,8 +2383,8 @@ func XsizeTests(cchar)
23832383

23842384
call g:Xsetlist([], 'f')
23852385
call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
2386-
call assert_equal(1, len(g:Xgetlist({'nr':'$', 'all':1})))
2387-
call assert_equal(0, len(g:Xgetlist({'nr':0})))
2386+
call assert_equal('', g:Xgetlist({'nr':'$', 'all':1}).title)
2387+
call assert_equal(0, g:Xgetlist({'nr':0}).nr)
23882388

23892389
Xexpr "File1:10:Line1"
23902390
Xexpr "File2:20:Line2"
@@ -2754,18 +2754,18 @@ func Xqfid_tests(cchar)
27542754
call s:setup_commands(a:cchar)
27552755

27562756
call g:Xsetlist([], 'f')
2757-
call assert_equal({}, g:Xgetlist({'id':0}))
2757+
call assert_equal(0, g:Xgetlist({'id':0}).id)
27582758
Xexpr ''
27592759
let start_id = g:Xgetlist({'id' : 0}).id
27602760
Xexpr '' | Xexpr ''
27612761
Xolder
27622762
call assert_equal(start_id, g:Xgetlist({'id':0, 'nr':1}).id)
27632763
call assert_equal(start_id + 1, g:Xgetlist({'id':0, 'nr':0}).id)
27642764
call assert_equal(start_id + 2, g:Xgetlist({'id':0, 'nr':'$'}).id)
2765-
call assert_equal({}, g:Xgetlist({'id':0, 'nr':99}))
2765+
call assert_equal(0, g:Xgetlist({'id':0, 'nr':99}).id)
27662766
call assert_equal(2, g:Xgetlist({'id':start_id + 1, 'nr':0}).nr)
2767-
call assert_equal({}, g:Xgetlist({'id':99, 'nr':0}))
2768-
call assert_equal({}, g:Xgetlist({'id':"abc", 'nr':0}))
2767+
call assert_equal(0, g:Xgetlist({'id':99, 'nr':0}).id)
2768+
call assert_equal(0, g:Xgetlist({'id':"abc", 'nr':0}).id)
27692769

27702770
call g:Xsetlist([], 'a', {'id':start_id, 'context':[1,2]})
27712771
call assert_equal([1,2], g:Xgetlist({'nr':1, 'context':1}).context)
@@ -2776,7 +2776,7 @@ func Xqfid_tests(cchar)
27762776

27772777
let qfid = g:Xgetlist({'id':0, 'nr':0})
27782778
call g:Xsetlist([], 'f')
2779-
call assert_equal({}, g:Xgetlist({'id':qfid, 'nr':0}))
2779+
call assert_equal(0, g:Xgetlist({'id':qfid, 'nr':0}).id)
27802780
endfunc
27812781

27822782
func Test_qf_id()
@@ -2880,3 +2880,62 @@ func Test_qfjump()
28802880
call Xqfjump_tests('c')
28812881
call Xqfjump_tests('l')
28822882
endfunc
2883+
2884+
" Tests for the getqflist() and getloclist() functions when the list is not
2885+
" present or is empty
2886+
func Xgetlist_empty_tests(cchar)
2887+
call s:setup_commands(a:cchar)
2888+
2889+
" Empty quickfix stack
2890+
call g:Xsetlist([], 'f')
2891+
call assert_equal('', g:Xgetlist({'context' : 0}).context)
2892+
call assert_equal(0, g:Xgetlist({'id' : 0}).id)
2893+
call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
2894+
call assert_equal([], g:Xgetlist({'items' : 0}).items)
2895+
call assert_equal(0, g:Xgetlist({'nr' : 0}).nr)
2896+
call assert_equal(0, g:Xgetlist({'size' : 0}).size)
2897+
call assert_equal('', g:Xgetlist({'title' : 0}).title)
2898+
call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
2899+
call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0}, g:Xgetlist({'all' : 0}))
2900+
2901+
" Empty quickfix list
2902+
Xexpr ""
2903+
call assert_equal('', g:Xgetlist({'context' : 0}).context)
2904+
call assert_notequal(0, g:Xgetlist({'id' : 0}).id)
2905+
call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
2906+
call assert_equal([], g:Xgetlist({'items' : 0}).items)
2907+
call assert_notequal(0, g:Xgetlist({'nr' : 0}).nr)
2908+
call assert_equal(0, g:Xgetlist({'size' : 0}).size)
2909+
call assert_notequal('', g:Xgetlist({'title' : 0}).title)
2910+
call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
2911+
2912+
let qfid = g:Xgetlist({'id' : 0}).id
2913+
call g:Xsetlist([], 'f')
2914+
2915+
" Non-existing quickfix identifier
2916+
call assert_equal('', g:Xgetlist({'id' : qfid, 'context' : 0}).context)
2917+
call assert_equal(0, g:Xgetlist({'id' : qfid}).id)
2918+
call assert_equal(0, g:Xgetlist({'id' : qfid, 'idx' : 0}).idx)
2919+
call assert_equal([], g:Xgetlist({'id' : qfid, 'items' : 0}).items)
2920+
call assert_equal(0, g:Xgetlist({'id' : qfid, 'nr' : 0}).nr)
2921+
call assert_equal(0, g:Xgetlist({'id' : qfid, 'size' : 0}).size)
2922+
call assert_equal('', g:Xgetlist({'id' : qfid, 'title' : 0}).title)
2923+
call assert_equal(0, g:Xgetlist({'id' : qfid, 'winid' : 0}).winid)
2924+
call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0}, g:Xgetlist({'id' : qfid, 'all' : 0}))
2925+
2926+
" Non-existing quickfix list number
2927+
call assert_equal('', g:Xgetlist({'nr' : 5, 'context' : 0}).context)
2928+
call assert_equal(0, g:Xgetlist({'nr' : 5}).nr)
2929+
call assert_equal(0, g:Xgetlist({'nr' : 5, 'idx' : 0}).idx)
2930+
call assert_equal([], g:Xgetlist({'nr' : 5, 'items' : 0}).items)
2931+
call assert_equal(0, g:Xgetlist({'nr' : 5, 'id' : 0}).id)
2932+
call assert_equal(0, g:Xgetlist({'nr' : 5, 'size' : 0}).size)
2933+
call assert_equal('', g:Xgetlist({'nr' : 5, 'title' : 0}).title)
2934+
call assert_equal(0, g:Xgetlist({'nr' : 5, 'winid' : 0}).winid)
2935+
call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0}, g:Xgetlist({'nr' : 5, 'all' : 0}))
2936+
endfunc
2937+
2938+
func Test_getqflist()
2939+
call Xgetlist_empty_tests('c')
2940+
call Xgetlist_empty_tests('l')
2941+
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,8 @@ static char *(features[]) =
771771

772772
static int included_patches[] =
773773
{ /* Add new patch number below this line */
774+
/**/
775+
1389,
774776
/**/
775777
1388,
776778
/**/

0 commit comments

Comments
 (0)