Skip to content

Commit 353eeea

Browse files
committed
patch 8.0.1727: qf_get_properties() function is too long
Problem: qf_get_properties() function is too long. Solution: Refactor the code. (Yegappan Lakshmanan, closes #2807)
1 parent df2c774 commit 353eeea

File tree

2 files changed

+193
-119
lines changed

2 files changed

+193
-119
lines changed

src/quickfix.c

+191-119
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,8 @@ qf_init_ext(
11831183
fields.errmsglen = CMDBUFFSIZE + 1;
11841184
fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg);
11851185
fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern);
1186-
if (fields.namebuf == NULL || fields.errmsg == NULL || fields.pattern == NULL)
1186+
if (fields.namebuf == NULL || fields.errmsg == NULL
1187+
|| fields.pattern == NULL)
11871188
goto qf_init_end;
11881189

11891190
if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL)
@@ -1817,7 +1818,6 @@ qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack)
18171818
}
18181819
}
18191820

1820-
18211821
/*
18221822
* pop dirbuf from the directory stack and return previous directory or NULL if
18231823
* stack is empty
@@ -4948,7 +4948,8 @@ enum {
49484948
};
49494949

49504950
/*
4951-
* Parse text from 'di' and return the quickfix list items
4951+
* Parse text from 'di' and return the quickfix list items.
4952+
* Existing quickfix lists are not modified.
49524953
*/
49534954
static int
49544955
qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
@@ -5017,25 +5018,13 @@ qf_winid(qf_info_T *qi)
50175018
}
50185019

50195020
/*
5020-
* Return quickfix/location list details (title) as a
5021-
* dictionary. 'what' contains the details to return. If 'list_idx' is -1,
5022-
* then current list is used. Otherwise the specified list is used.
5021+
* Convert the keys in 'what' to quickfix list property flags.
50235022
*/
5024-
int
5025-
qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
5023+
static int
5024+
qf_getprop_keys2flags(dict_T *what)
50265025
{
5027-
qf_info_T *qi = &ql_info;
5028-
int status = OK;
5029-
int qf_idx;
5030-
dictitem_T *di;
50315026
int flags = QF_GETLIST_NONE;
50325027

5033-
if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
5034-
return qf_get_list_from_lines(what, di, retdict);
5035-
5036-
if (wp != NULL)
5037-
qi = GET_LOC_LIST(wp);
5038-
50395028
if (dict_find(what, (char_u *)"all", -1) != NULL)
50405029
flags |= QF_GETLIST_ALL;
50415030

@@ -5066,140 +5055,223 @@ qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
50665055
if (dict_find(what, (char_u *)"changedtick", -1) != NULL)
50675056
flags |= QF_GETLIST_TICK;
50685057

5069-
if (qi != NULL && qi->qf_listcount != 0)
5070-
{
5071-
qf_idx = qi->qf_curlist; /* default is the current list */
5072-
if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5073-
{
5074-
/* Use the specified quickfix/location list */
5075-
if (di->di_tv.v_type == VAR_NUMBER)
5076-
{
5077-
/* for zero use the current list */
5078-
if (di->di_tv.vval.v_number != 0)
5079-
{
5080-
qf_idx = di->di_tv.vval.v_number - 1;
5081-
if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
5082-
qf_idx = -1;
5083-
}
5084-
}
5085-
else if (di->di_tv.v_type == VAR_STRING
5086-
&& di->di_tv.vval.v_string != NULL
5087-
&& STRCMP(di->di_tv.vval.v_string, "$") == 0)
5088-
/* Get the last quickfix list number */
5089-
qf_idx = qi->qf_listcount - 1;
5090-
else
5091-
qf_idx = -1;
5092-
flags |= QF_GETLIST_NR;
5093-
}
5058+
return flags;
5059+
}
5060+
5061+
/*
5062+
* Return the quickfix list index based on 'nr' or 'id' in 'what'.
5063+
* If 'nr' and 'id' are not present in 'what' then return the current
5064+
* quickfix list index.
5065+
* If 'nr' is zero then return the current quickfix list index.
5066+
* If 'nr' is '$' then return the last quickfix list index.
5067+
* If 'id' is present then return the index of the quickfix list with that id.
5068+
* If 'id' is zero then return the quickfix list index specified by 'nr'.
5069+
* Return -1, if quickfix list is not present or if the stack is empty.
5070+
*/
5071+
static int
5072+
qf_getprop_qfidx(qf_info_T *qi, dict_T *what)
5073+
{
5074+
int qf_idx;
5075+
dictitem_T *di;
50945076

5095-
if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
5077+
qf_idx = qi->qf_curlist; /* default is the current list */
5078+
if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL)
5079+
{
5080+
/* Use the specified quickfix/location list */
5081+
if (di->di_tv.v_type == VAR_NUMBER)
50965082
{
5097-
/* Look for a list with the specified id */
5098-
if (di->di_tv.v_type == VAR_NUMBER)
5083+
/* for zero use the current list */
5084+
if (di->di_tv.vval.v_number != 0)
50995085
{
5100-
/*
5101-
* For zero, use the current list or the list specifed by 'nr'
5102-
*/
5103-
if (di->di_tv.vval.v_number != 0)
5104-
qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
5105-
flags |= QF_GETLIST_ID;
5086+
qf_idx = di->di_tv.vval.v_number - 1;
5087+
if (qf_idx < 0 || qf_idx >= qi->qf_listcount)
5088+
qf_idx = -1;
51065089
}
5107-
else
5108-
qf_idx = -1;
51095090
}
5091+
else if (di->di_tv.v_type == VAR_STRING
5092+
&& di->di_tv.vval.v_string != NULL
5093+
&& STRCMP(di->di_tv.vval.v_string, "$") == 0)
5094+
/* Get the last quickfix list number */
5095+
qf_idx = qi->qf_listcount - 1;
5096+
else
5097+
qf_idx = -1;
51105098
}
51115099

5112-
/* List is not present or is empty */
5113-
if (qi == NULL || qi->qf_listcount == 0 || qf_idx == -1)
5100+
if ((di = dict_find(what, (char_u *)"id", -1)) != NULL)
51145101
{
5115-
if (flags & QF_GETLIST_TITLE)
5116-
status = dict_add_nr_str(retdict, "title", 0L, (char_u *)"");
5117-
if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5102+
/* Look for a list with the specified id */
5103+
if (di->di_tv.v_type == VAR_NUMBER)
51185104
{
5119-
list_T *l = list_alloc();
5120-
if (l != NULL)
5121-
status = dict_add_list(retdict, "items", l);
5122-
else
5123-
status = FAIL;
5105+
/*
5106+
* For zero, use the current list or the list specified by 'nr'
5107+
*/
5108+
if (di->di_tv.vval.v_number != 0)
5109+
qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number);
51245110
}
5125-
if ((status == OK) && (flags & QF_GETLIST_NR))
5126-
status = dict_add_nr_str(retdict, "nr", 0L, NULL);
5127-
if ((status == OK) && (flags & QF_GETLIST_WINID))
5128-
status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
5129-
if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5130-
status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5131-
if ((status == OK) && (flags & QF_GETLIST_ID))
5132-
status = dict_add_nr_str(retdict, "id", 0L, NULL);
5133-
if ((status == OK) && (flags & QF_GETLIST_IDX))
5134-
status = dict_add_nr_str(retdict, "idx", 0L, NULL);
5135-
if ((status == OK) && (flags & QF_GETLIST_SIZE))
5136-
status = dict_add_nr_str(retdict, "size", 0L, NULL);
5137-
if ((status == OK) && (flags & QF_GETLIST_TICK))
5138-
status = dict_add_nr_str(retdict, "changedtick", 0L, NULL);
5139-
5140-
return status;
5111+
else
5112+
qf_idx = -1;
51415113
}
51425114

5115+
return qf_idx;
5116+
}
5117+
5118+
/*
5119+
* Return default values for quickfix list properties in retdict.
5120+
*/
5121+
static int
5122+
qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict)
5123+
{
5124+
int status = OK;
5125+
51435126
if (flags & QF_GETLIST_TITLE)
5144-
{
5145-
char_u *t;
5146-
t = qi->qf_lists[qf_idx].qf_title;
5147-
if (t == NULL)
5148-
t = (char_u *)"";
5149-
status = dict_add_nr_str(retdict, "title", 0L, t);
5150-
}
5151-
if ((status == OK) && (flags & QF_GETLIST_NR))
5152-
status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
5153-
if ((status == OK) && (flags & QF_GETLIST_WINID))
5154-
status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
5127+
status = dict_add_nr_str(retdict, "title", 0L, (char_u *)"");
51555128
if ((status == OK) && (flags & QF_GETLIST_ITEMS))
51565129
{
51575130
list_T *l = list_alloc();
51585131
if (l != NULL)
5159-
{
5160-
(void)get_errorlist(qi, NULL, qf_idx, l);
5161-
dict_add_list(retdict, "items", l);
5162-
}
5132+
status = dict_add_list(retdict, "items", l);
51635133
else
51645134
status = FAIL;
51655135
}
5166-
5136+
if ((status == OK) && (flags & QF_GETLIST_NR))
5137+
status = dict_add_nr_str(retdict, "nr", 0L, NULL);
5138+
if ((status == OK) && (flags & QF_GETLIST_WINID))
5139+
status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
51675140
if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5141+
status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5142+
if ((status == OK) && (flags & QF_GETLIST_ID))
5143+
status = dict_add_nr_str(retdict, "id", 0L, NULL);
5144+
if ((status == OK) && (flags & QF_GETLIST_IDX))
5145+
status = dict_add_nr_str(retdict, "idx", 0L, NULL);
5146+
if ((status == OK) && (flags & QF_GETLIST_SIZE))
5147+
status = dict_add_nr_str(retdict, "size", 0L, NULL);
5148+
if ((status == OK) && (flags & QF_GETLIST_TICK))
5149+
status = dict_add_nr_str(retdict, "changedtick", 0L, NULL);
5150+
5151+
return status;
5152+
}
5153+
5154+
/*
5155+
* Return the quickfix list title as 'title' in retdict
5156+
*/
5157+
static int
5158+
qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict)
5159+
{
5160+
char_u *t;
5161+
5162+
t = qi->qf_lists[qf_idx].qf_title;
5163+
if (t == NULL)
5164+
t = (char_u *)"";
5165+
return dict_add_nr_str(retdict, "title", 0L, t);
5166+
}
5167+
5168+
/*
5169+
* Return the quickfix list items/entries as 'items' in retdict
5170+
*/
5171+
static int
5172+
qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict)
5173+
{
5174+
int status = OK;
5175+
list_T *l = list_alloc();
5176+
if (l != NULL)
51685177
{
5169-
if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5178+
(void)get_errorlist(qi, NULL, qf_idx, l);
5179+
dict_add_list(retdict, "items", l);
5180+
}
5181+
else
5182+
status = FAIL;
5183+
5184+
return status;
5185+
}
5186+
5187+
/*
5188+
* Return the quickfix list context (if any) as 'context' in retdict.
5189+
*/
5190+
static int
5191+
qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5192+
{
5193+
int status;
5194+
dictitem_T *di;
5195+
5196+
if (qi->qf_lists[qf_idx].qf_ctx != NULL)
5197+
{
5198+
di = dictitem_alloc((char_u *)"context");
5199+
if (di != NULL)
51705200
{
5171-
di = dictitem_alloc((char_u *)"context");
5172-
if (di != NULL)
5173-
{
5174-
copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
5175-
status = dict_add(retdict, di);
5176-
if (status == FAIL)
5177-
dictitem_free(di);
5178-
}
5179-
else
5180-
status = FAIL;
5201+
copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv);
5202+
status = dict_add(retdict, di);
5203+
if (status == FAIL)
5204+
dictitem_free(di);
51815205
}
51825206
else
5183-
status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5207+
status = FAIL;
51845208
}
5209+
else
5210+
status = dict_add_nr_str(retdict, "context", 0L, (char_u *)"");
5211+
5212+
return status;
5213+
}
5214+
5215+
/*
5216+
* Return the quickfix list index as 'idx' in retdict
5217+
*/
5218+
static int
5219+
qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict)
5220+
{
5221+
int idx = qi->qf_lists[qf_idx].qf_index;
5222+
if (qi->qf_lists[qf_idx].qf_count == 0)
5223+
/* For empty lists, qf_index is set to 1 */
5224+
idx = 0;
5225+
return dict_add_nr_str(retdict, "idx", idx, NULL);
5226+
}
51855227

5228+
/*
5229+
* Return quickfix/location list details (title) as a
5230+
* dictionary. 'what' contains the details to return. If 'list_idx' is -1,
5231+
* then current list is used. Otherwise the specified list is used.
5232+
*/
5233+
int
5234+
qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict)
5235+
{
5236+
qf_info_T *qi = &ql_info;
5237+
int status = OK;
5238+
int qf_idx;
5239+
dictitem_T *di;
5240+
int flags = QF_GETLIST_NONE;
5241+
5242+
if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL)
5243+
return qf_get_list_from_lines(what, di, retdict);
5244+
5245+
if (wp != NULL)
5246+
qi = GET_LOC_LIST(wp);
5247+
5248+
flags = qf_getprop_keys2flags(what);
5249+
5250+
if (qi != NULL && qi->qf_listcount != 0)
5251+
qf_idx = qf_getprop_qfidx(qi, what);
5252+
5253+
/* List is not present or is empty */
5254+
if (qi == NULL || qi->qf_listcount == 0 || qf_idx == -1)
5255+
return qf_getprop_defaults(qi, flags, retdict);
5256+
5257+
if (flags & QF_GETLIST_TITLE)
5258+
status = qf_getprop_title(qi, qf_idx, retdict);
5259+
if ((status == OK) && (flags & QF_GETLIST_NR))
5260+
status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL);
5261+
if ((status == OK) && (flags & QF_GETLIST_WINID))
5262+
status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL);
5263+
if ((status == OK) && (flags & QF_GETLIST_ITEMS))
5264+
status = qf_getprop_items(qi, qf_idx, retdict);
5265+
if ((status == OK) && (flags & QF_GETLIST_CONTEXT))
5266+
status = qf_getprop_ctx(qi, qf_idx, retdict);
51865267
if ((status == OK) && (flags & QF_GETLIST_ID))
51875268
status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id,
51885269
NULL);
5189-
51905270
if ((status == OK) && (flags & QF_GETLIST_IDX))
5191-
{
5192-
int idx = qi->qf_lists[qf_idx].qf_index;
5193-
if (qi->qf_lists[qf_idx].qf_count == 0)
5194-
/* For empty lists, qf_index is set to 1 */
5195-
idx = 0;
5196-
status = dict_add_nr_str(retdict, "idx", idx, NULL);
5197-
}
5198-
5271+
status = qf_getprop_idx(qi, qf_idx, retdict);
51995272
if ((status == OK) && (flags & QF_GETLIST_SIZE))
52005273
status = dict_add_nr_str(retdict, "size",
52015274
qi->qf_lists[qf_idx].qf_count, NULL);
5202-
52035275
if ((status == OK) && (flags & QF_GETLIST_TICK))
52045276
status = dict_add_nr_str(retdict, "changedtick",
52055277
qi->qf_lists[qf_idx].qf_changedtick, NULL);
@@ -5609,7 +5681,7 @@ mark_quickfix_ctx(qf_info_T *qi, int copyID)
56095681

56105682
/*
56115683
* Mark the context of the quickfix list and the location lists (if present) as
5612-
* "in use". So that garabage collection doesn't free the context.
5684+
* "in use". So that garbage collection doesn't free the context.
56135685
*/
56145686
int
56155687
set_ref_in_quickfix(int copyID)

src/version.c

+2
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,8 @@ static char *(features[]) =
762762

763763
static int included_patches[] =
764764
{ /* Add new patch number below this line */
765+
/**/
766+
1727,
765767
/**/
766768
1726,
767769
/**/

0 commit comments

Comments
 (0)