Skip to content

Commit 95946f1

Browse files
committed
patch 8.1.1098: quickfix code duplication
Problem: Quickfix code duplication. Solution: Refactor the qf_init_ext() function. (Yegappan Lakshmanan, closes #4193)
1 parent 4fc8e2f commit 95946f1

3 files changed

Lines changed: 74 additions & 59 deletions

File tree

src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ findfile.c | search for files in 'path'
3232
fold.c | folding
3333
getchar.c | getting characters and key mapping
3434
indent.c | C and Lisp indentation
35+
insexpand.c | Insert mode completion
3536
mark.c | marks
3637
mbyte.c | multi-byte character handling
3738
memfile.c | storing lines for buffers in a swapfile

src/quickfix.c

Lines changed: 71 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,11 @@ static qf_info_T *ll_get_or_alloc_list(win_T *);
195195
*/
196196
#define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
197197

198+
// Macro to loop through all the items in a quickfix list
199+
// Quickfix item index starts from 1, so i below starts at 1
198200
#define FOR_ALL_QFL_ITEMS(qfl, qfp, i) \
199-
for (i = 0, qfp = qfl->qf_start; \
200-
!got_int && i < qfl->qf_count && qfp != NULL; \
201+
for (i = 1, qfp = qfl->qf_start; \
202+
!got_int && i <= qfl->qf_count && qfp != NULL; \
201203
++i, qfp = qfp->qf_next)
202204

203205
/*
@@ -1584,6 +1586,47 @@ qf_cleanup_state(qfstate_T *pstate)
15841586
convert_setup(&pstate->vc, NULL, NULL);
15851587
}
15861588

1589+
/*
1590+
* Process the next line from a file/buffer/list/string and add it
1591+
* to the quickfix list 'qfl'.
1592+
*/
1593+
static int
1594+
qf_init_process_nextline(
1595+
qf_list_T *qfl,
1596+
efm_T *fmt_first,
1597+
qfstate_T *state,
1598+
qffields_T *fields)
1599+
{
1600+
int status;
1601+
1602+
// Get the next line from a file/buffer/list/string
1603+
status = qf_get_nextline(state);
1604+
if (status != QF_OK)
1605+
return status;
1606+
1607+
status = qf_parse_line(qfl, state->linebuf, state->linelen,
1608+
fmt_first, fields);
1609+
if (status != QF_OK)
1610+
return status;
1611+
1612+
return qf_add_entry(qfl,
1613+
qfl->qf_directory,
1614+
(*fields->namebuf || qfl->qf_directory != NULL)
1615+
? fields->namebuf
1616+
: ((qfl->qf_currfile != NULL && fields->valid)
1617+
? qfl->qf_currfile : (char_u *)NULL),
1618+
fields->module,
1619+
0,
1620+
fields->errmsg,
1621+
fields->lnum,
1622+
fields->col,
1623+
fields->use_viscol,
1624+
fields->pattern,
1625+
fields->enr,
1626+
fields->type,
1627+
fields->valid);
1628+
}
1629+
15871630
/*
15881631
* Read the errorfile "efile" into memory, line by line, building the error
15891632
* list.
@@ -1676,39 +1719,14 @@ qf_init_ext(
16761719
// Try to recognize one of the error formats in each line.
16771720
while (!got_int)
16781721
{
1679-
// Get the next line from a file/buffer/list/string
1680-
status = qf_get_nextline(&state);
1722+
status = qf_init_process_nextline(qfl, fmt_first, &state, &fields);
16811723
if (status == QF_NOMEM) // memory alloc failure
16821724
goto qf_init_end;
16831725
if (status == QF_END_OF_INPUT) // end of input
16841726
break;
1685-
1686-
status = qf_parse_line(qfl, state.linebuf, state.linelen,
1687-
fmt_first, &fields);
16881727
if (status == QF_FAIL)
16891728
goto error2;
1690-
if (status == QF_NOMEM)
1691-
goto qf_init_end;
1692-
if (status == QF_IGNORE_LINE)
1693-
continue;
16941729

1695-
if (qf_add_entry(qfl,
1696-
qfl->qf_directory,
1697-
(*fields.namebuf || qfl->qf_directory != NULL)
1698-
? fields.namebuf
1699-
: ((qfl->qf_currfile != NULL && fields.valid)
1700-
? qfl->qf_currfile : (char_u *)NULL),
1701-
fields.module,
1702-
0,
1703-
fields.errmsg,
1704-
fields.lnum,
1705-
fields.col,
1706-
fields.use_viscol,
1707-
fields.pattern,
1708-
fields.enr,
1709-
fields.type,
1710-
fields.valid) == FAIL)
1711-
goto error2;
17121730
line_breakcheck();
17131731
}
17141732
if (state.fd == NULL || !ferror(state.fd))
@@ -2013,7 +2031,7 @@ check_quickfix_busy(void)
20132031

20142032
/*
20152033
* Add an entry to the end of the list of errors.
2016-
* Returns OK or FAIL.
2034+
* Returns QF_OK or QF_FAIL.
20172035
*/
20182036
static int
20192037
qf_add_entry(
@@ -2035,7 +2053,7 @@ qf_add_entry(
20352053
qfline_T **lastp; // pointer to qf_last or NULL
20362054

20372055
if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
2038-
return FAIL;
2056+
return QF_FAIL;
20392057
if (bufnum != 0)
20402058
{
20412059
buf_T *buf = buflist_findnr(bufnum);
@@ -2050,7 +2068,7 @@ qf_add_entry(
20502068
if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
20512069
{
20522070
vim_free(qfp);
2053-
return FAIL;
2071+
return QF_FAIL;
20542072
}
20552073
qfp->qf_lnum = lnum;
20562074
qfp->qf_col = col;
@@ -2061,7 +2079,7 @@ qf_add_entry(
20612079
{
20622080
vim_free(qfp->qf_text);
20632081
vim_free(qfp);
2064-
return FAIL;
2082+
return QF_FAIL;
20652083
}
20662084
if (module == NULL || *module == NUL)
20672085
qfp->qf_module = NULL;
@@ -2070,7 +2088,7 @@ qf_add_entry(
20702088
vim_free(qfp->qf_text);
20712089
vim_free(qfp->qf_pattern);
20722090
vim_free(qfp);
2073-
return FAIL;
2091+
return QF_FAIL;
20742092
}
20752093
qfp->qf_nr = nr;
20762094
if (type != 1 && !vim_isprintc(type)) // only printable chars allowed
@@ -2101,7 +2119,7 @@ qf_add_entry(
21012119
qfl->qf_ptr = qfp;
21022120
}
21032121

2104-
return OK;
2122+
return QF_OK;
21052123
}
21062124

21072125
/*
@@ -2167,7 +2185,7 @@ copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
21672185
from_qfp->qf_pattern,
21682186
from_qfp->qf_nr,
21692187
0,
2170-
from_qfp->qf_valid) == FAIL)
2188+
from_qfp->qf_valid) == QF_FAIL)
21712189
return FAIL;
21722190

21732191
// qf_add_entry() will not set the qf_num field, as the
@@ -2551,7 +2569,7 @@ is_qf_entry_present(qf_list_T *qfl, qfline_T *qf_ptr)
25512569
if (qfp == qf_ptr)
25522570
break;
25532571

2554-
if (i == qfl->qf_count) // Entry is not found
2572+
if (i > qfl->qf_count) // Entry is not found
25552573
return FALSE;
25562574

25572575
return TRUE;
@@ -3554,21 +3572,11 @@ qf_list(exarg_T *eap)
35543572

35553573
if (qfl->qf_nonevalid)
35563574
all = TRUE;
3557-
qfp = qfl->qf_start;
3558-
for (i = 1; !got_int && i <= qfl->qf_count; )
3575+
FOR_ALL_QFL_ITEMS(qfl, qfp, i)
35593576
{
35603577
if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
3561-
{
3562-
if (got_int)
3563-
break;
3564-
35653578
qf_list_entry(qfp, i, i == qfl->qf_index);
3566-
}
35673579

3568-
qfp = qfp->qf_next;
3569-
if (qfp == NULL)
3570-
break;
3571-
++i;
35723580
ui_breakcheck();
35733581
}
35743582
}
@@ -4915,16 +4923,16 @@ qf_get_cur_valid_idx(exarg_T *eap)
49154923
static int
49164924
qf_get_nth_valid_entry(qf_list_T *qfl, int n, int fdo)
49174925
{
4918-
qfline_T *qfp = qfl->qf_start;
4926+
qfline_T *qfp;
49194927
int i, eidx;
49204928
int prev_fnum = 0;
49214929

49224930
// check if the list has valid errors
49234931
if (qfl->qf_count <= 0 || qfl->qf_nonevalid)
49244932
return 1;
49254933

4926-
for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL;
4927-
i++, qfp = qfp->qf_next)
4934+
eidx = 0;
4935+
FOR_ALL_QFL_ITEMS(qfl, qfp, i)
49284936
{
49294937
if (qfp->qf_valid)
49304938
{
@@ -5330,7 +5338,7 @@ vgr_match_buflines(
53305338
0, // nr
53315339
0, // type
53325340
TRUE // valid
5333-
) == FAIL)
5341+
) == QF_FAIL)
53345342
{
53355343
got_int = TRUE;
53365344
break;
@@ -6434,7 +6442,7 @@ qf_add_entries(
64346442

64356443
retval = qf_add_entry_from_dict(qfl, d, li == list->lv_first,
64366444
&valid_entry);
6437-
if (retval == FAIL)
6445+
if (retval == QF_FAIL)
64386446
break;
64396447
}
64406448

@@ -6744,14 +6752,18 @@ qf_free_stack(win_T *wp, qf_info_T *qi)
67446752
// If the location list window is open, then create a new empty
67456753
// location list
67466754
qf_info_T *new_ll = qf_alloc_stack(QFLT_LOCATION);
6747-
new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
67486755

6749-
// first free the list reference in the location list window
6750-
ll_free_all(&qfwin->w_llist_ref);
6756+
if (new_ll != NULL)
6757+
{
6758+
new_ll->qf_bufnr = qfwin->w_buffer->b_fnum;
67516759

6752-
qfwin->w_llist_ref = new_ll;
6753-
if (wp != qfwin)
6754-
win_set_loclist(wp, new_ll);
6760+
// first free the list reference in the location list window
6761+
ll_free_all(&qfwin->w_llist_ref);
6762+
6763+
qfwin->w_llist_ref = new_ll;
6764+
if (wp != qfwin)
6765+
win_set_loclist(wp, new_ll);
6766+
}
67556767
}
67566768
}
67576769

@@ -7203,7 +7215,7 @@ hgr_search_file(
72037215
0, // nr
72047216
1, // type
72057217
TRUE // valid
7206-
) == FAIL)
7218+
) == QF_FAIL)
72077219
{
72087220
got_int = TRUE;
72097221
if (line != IObuff)

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+
1098,
774776
/**/
775777
1097,
776778
/**/

0 commit comments

Comments
 (0)