Skip to content

Commit

Permalink
Allow attaching custom data to setqflist() via 'user_data' field
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-anders committed Jan 14, 2023
1 parent 24a8d06 commit 1c242b0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions runtime/doc/builtin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3952,6 +3952,9 @@ getqflist([{what}]) *getqflist()*
text description of the error
type type of the error, 'E', '1', etc.
valid |TRUE|: recognized error message
user_data
custom data associated with the item, can be
any type.

When there is no error list or it's empty, an empty list is
returned. Quickfix list entries with a non-existing buffer
Expand Down Expand Up @@ -8238,6 +8241,8 @@ setqflist({list} [, {action} [, {what}]]) *setqflist()*
text description of the error
type single-character error type, 'E', 'W', etc.
valid recognized error message
user_data custom data associated with the item, can be
any type.

The "col", "vcol", "nr", "type" and "text" entries are
optional. Either "lnum" or "pattern" entry can be used to
Expand Down
21 changes: 20 additions & 1 deletion src/quickfix.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct qfline_S
char_u qf_cleared; // set to TRUE if line has been deleted
char_u qf_type; // type of the error (mostly 'E'); 1 for
// :helpgrep
typval_T qf_user_data; // custom user data associated with this item
char_u qf_valid; // valid error message detected
};

Expand Down Expand Up @@ -168,7 +169,7 @@ static efm_T *fmt_start = NULL; // cached across qf_parse_line() calls
static callback_T qftf_cb;

static void qf_new_list(qf_info_T *qi, char_u *qf_title);
static int qf_add_entry(qf_list_T *qfl, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, long end_lnum, int col, int end_col, int vis_col, char_u *pattern, int nr, int type, int valid);
static int qf_add_entry(qf_list_T *qfl, char_u *dir, char_u *fname, char_u *module, int bufnum, char_u *mesg, long lnum, long end_lnum, int col, int end_col, int vis_col, char_u *pattern, int nr, int type, typval_T *user_data, int valid);
static void qf_free(qf_list_T *qfl);
static char_u *qf_types(int, int);
static int qf_get_fnum(qf_list_T *qfl, char_u *, char_u *);
Expand Down Expand Up @@ -951,6 +952,7 @@ typedef struct {
char_u *pattern;
int enr;
int type;
typval_T *user_data;
int valid;
} qffields_T;

Expand Down Expand Up @@ -1720,6 +1722,7 @@ qf_init_process_nextline(
fields->pattern,
fields->enr,
fields->type,
fields->user_data,
fields->valid);
}

Expand Down Expand Up @@ -2145,6 +2148,7 @@ qf_add_entry(
char_u *pattern, // search pattern
int nr, // error number
int type, // type character
typval_T *user_data, // custom user data or NULL
int valid) // valid entry
{
qfline_T *qfp;
Expand Down Expand Up @@ -2173,6 +2177,10 @@ qf_add_entry(
qfp->qf_col = col;
qfp->qf_end_col = end_col;
qfp->qf_viscol = vis_col;
if (user_data == NULL || user_data->v_type == VAR_UNKNOWN)
qfp->qf_user_data.v_type = VAR_UNKNOWN;
else
copy_tv(user_data, &qfp->qf_user_data);
if (pattern == NULL || *pattern == NUL)
qfp->qf_pattern = NULL;
else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
Expand Down Expand Up @@ -2335,6 +2343,7 @@ copy_loclist_entries(qf_list_T *from_qfl, qf_list_T *to_qfl)
from_qfp->qf_pattern,
from_qfp->qf_nr,
0,
NULL,
from_qfp->qf_valid) == QF_FAIL)
return FAIL;

Expand Down Expand Up @@ -6082,6 +6091,7 @@ vgr_match_buflines(
NULL, // search pattern
0, // nr
0, // type
NULL, // user_data
TRUE // valid
) == QF_FAIL)
{
Expand Down Expand Up @@ -6127,6 +6137,7 @@ vgr_match_buflines(
NULL, // search pattern
0, // nr
0, // type
NULL, // user_data
TRUE // valid
) == QF_FAIL)
{
Expand Down Expand Up @@ -6767,6 +6778,8 @@ get_qfline_items(qfline_T *qfp, list_T *list)
|| dict_add_string(dict, "pattern", qfp->qf_pattern) == FAIL
|| dict_add_string(dict, "text", qfp->qf_text) == FAIL
|| dict_add_string(dict, "type", buf) == FAIL
|| (qfp->qf_user_data.v_type != VAR_UNKNOWN
&& dict_add_tv(dict, "user_data", &qfp->qf_user_data) == FAIL )
|| dict_add_number(dict, "valid", (long)qfp->qf_valid) == FAIL)
return FAIL;

Expand Down Expand Up @@ -7301,6 +7314,9 @@ qf_add_entry_from_dict(
text = dict_get_string(d, "text", TRUE);
if (text == NULL)
text = vim_strsave((char_u *)"");
typval_T user_data;
user_data.v_type = VAR_UNKNOWN;
dict_get_tv(d, "user_data", &user_data);

valid = TRUE;
if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL))
Expand Down Expand Up @@ -7337,13 +7353,15 @@ qf_add_entry_from_dict(
pattern, // search pattern
nr,
type == NULL ? NUL : *type,
&user_data,
valid);

vim_free(filename);
vim_free(module);
vim_free(pattern);
vim_free(text);
vim_free(type);
clear_tv(&user_data);

if (valid)
*valid_entry = TRUE;
Expand Down Expand Up @@ -8221,6 +8239,7 @@ hgr_search_file(
NULL, // search pattern
0, // nr
1, // type
NULL, // user_data
TRUE // valid
) == QF_FAIL)
{
Expand Down
3 changes: 2 additions & 1 deletion src/testdir/test_quickfix.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1806,13 +1806,14 @@ func SetXlistTests(cchar, bnum)
call s:setup_commands(a:cchar)

call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 1},
\ {'bufnr': a:bnum, 'lnum': 2, 'end_lnum': 3, 'col': 4, 'end_col': 5}])
\ {'bufnr': a:bnum, 'lnum': 2, 'end_lnum': 3, 'col': 4, 'end_col': 5, 'user_data': {'6': [7, 8]}}])
let l = g:Xgetlist()
call assert_equal(2, len(l))
call assert_equal(2, l[1].lnum)
call assert_equal(3, l[1].end_lnum)
call assert_equal(4, l[1].col)
call assert_equal(5, l[1].end_col)
call assert_equal({'6': [7, 8]}, l[1].user_data)

Xnext
call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3}], 'a')
Expand Down

0 comments on commit 1c242b0

Please sign in to comment.