Skip to content

Commit ccfb7c6

Browse files
yegappanbrammool
authored andcommitted
patch 8.2.3356: adding many text properties requires a lot of function calls
Problem: Adding many text properties requires a lot of function calls. Solution: Add the prop_add_list() function. (Yegappan Lakshmanan, closes #8751)
1 parent 434df7a commit ccfb7c6

File tree

9 files changed

+253
-77
lines changed

9 files changed

+253
-77
lines changed

runtime/doc/eval.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2801,7 +2801,9 @@ prompt_getprompt({buf}) String get prompt text
28012801
prompt_setcallback({buf}, {expr}) none set prompt callback function
28022802
prompt_setinterrupt({buf}, {text}) none set prompt interrupt function
28032803
prompt_setprompt({buf}, {text}) none set prompt text
2804-
prop_add({lnum}, {col}, {props}) none add a text property
2804+
prop_add({lnum}, {col}, {props}) none add one text property
2805+
prop_add_list({props}, [[{lnum}, {col}, {end-lnum}, {end-col}], ...])
2806+
none add multiple text properties
28052807
prop_clear({lnum} [, {lnum-end} [, {props}]])
28062808
none remove all text properties
28072809
prop_find({props} [, {direction}])

runtime/doc/textprop.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ prop_type_list([{props}]) get list of property types
108108
Manipulating text properties:
109109

110110
prop_add({lnum}, {col}, {props}) add a text property
111+
prop_add_list({props}, [[{lnum}, {col}, {end-lnum}, {end-col}], ...])
112+
add a text property at multiple
113+
positions.
111114
prop_clear({lnum} [, {lnum-end} [, {bufnr}]])
112115
remove all text properties
113116
prop_find({props} [, {direction}]) search for a text property
@@ -158,6 +161,35 @@ prop_add({lnum}, {col}, {props})
158161
Can also be used as a |method|: >
159162
GetLnum()->prop_add(col, props)
160163
164+
*prop_add_list()*
165+
prop_add_list({props}, [[{lnum}, {col}, {end-lnum}, {end-col}], ...])
166+
Similar to prop_add(), but attaches a text property at
167+
multiple positions in a buffer.
168+
169+
{props} is a dictionary with these fields:
170+
bufnr buffer to add the property to; when omitted
171+
the current buffer is used
172+
id user defined ID for the property; must be a
173+
number; when omitted zero is used
174+
type name of the text property type
175+
All fields except "type" are optional.
176+
177+
The second argument is a List of Lists where each list
178+
specifies the starting and ending position of the text. The
179+
first two items {lnum} and {col} specify the starting position
180+
of the text where the property will be attached and the last
181+
two items {end-lnum} and {end-col} specify the position just
182+
after the text.
183+
184+
Example:
185+
call prop_add_list(#{type: 'MyProp', id: 2},
186+
\ [[1, 4, 1, 7],
187+
\ [1, 15, 1, 20],
188+
\ [2, 30, 3, 30]]
189+
190+
Can also be used as a |method|: >
191+
GetProp()->prop_add_list([[1, 1, 1, 2], [1, 4, 1, 8]])
192+
161193
162194
prop_clear({lnum} [, {lnum-end} [, {props}]]) *prop_clear()*
163195
Remove all text properties from line {lnum}.

runtime/doc/usr_41.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,7 @@ Prompt Buffer: *promptbuffer-functions*
11611161

11621162
Text Properties: *text-property-functions*
11631163
prop_add() attach a property at a position
1164+
prop_add_list() attach a property at multiple positions
11641165
prop_clear() remove all properties from a line or lines
11651166
prop_find() search for a property
11661167
prop_list() return a list of all properties in a line

src/evalfunc.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,7 @@ static argcheck_T arg2_buffer_number[] = {arg_buffer, arg_number};
708708
static argcheck_T arg2_buffer_string[] = {arg_buffer, arg_string};
709709
static argcheck_T arg2_chan_or_job_dict[] = {arg_chan_or_job, arg_dict_any};
710710
static argcheck_T arg2_chan_or_job_string[] = {arg_chan_or_job, arg_string};
711+
static argcheck_T arg2_dict_any_list_any[] = {arg_dict_any, arg_list_any};
711712
static argcheck_T arg2_dict_any_string_or_nr[] = {arg_dict_any, arg_string_or_nr};
712713
static argcheck_T arg2_dict_string[] = {arg_dict_any, arg_string};
713714
static argcheck_T arg2_float_or_nr[] = {arg_float_or_nr, arg_float_or_nr};
@@ -1740,6 +1741,8 @@ static funcentry_T global_functions[] =
17401741
ret_void, JOB_FUNC(f_prompt_setprompt)},
17411742
{"prop_add", 3, 3, FEARG_1, arg3_number_number_dict,
17421743
ret_void, PROP_FUNC(f_prop_add)},
1744+
{"prop_add_list", 2, 2, FEARG_1, arg2_dict_any_list_any,
1745+
ret_void, PROP_FUNC(f_prop_add_list)},
17431746
{"prop_clear", 1, 3, FEARG_1, arg3_number_number_dict,
17441747
ret_void, PROP_FUNC(f_prop_clear)},
17451748
{"prop_find", 1, 2, FEARG_1, arg2_dict_string,

src/proto/textprop.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* textprop.c */
22
int find_prop_type_id(char_u *name, buf_T *buf);
33
void f_prop_add(typval_T *argvars, typval_T *rettv);
4+
void f_prop_add_list(typval_T *argvars, typval_T *rettv);
45
void prop_add_common(linenr_T start_lnum, colnr_T start_col, dict_T *dict, buf_T *default_buf, typval_T *dict_arg);
56
int get_text_props(buf_T *buf, linenr_T lnum, char_u **props, int will_change);
67
int count_props(linenr_T lnum, int only_starting);

src/testdir/test_textprop.vim

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,41 @@ func Test_prop_add()
339339
bwipe!
340340
endfunc
341341

342+
" Test for the prop_add_list() function
343+
func Test_prop_add_list()
344+
new
345+
call AddPropTypes()
346+
call setline(1, ['one one one', 'two two two', 'six six six', 'ten ten ten'])
347+
call prop_add_list(#{type: 'one', id: 2},
348+
\ [[1, 1, 1, 3], [2, 5, 2, 7], [3, 6, 4, 6]])
349+
call assert_equal([#{id: 2, col: 1, type_bufnr: 0, end: 1, type: 'one',
350+
\ length: 2, start: 1}], prop_list(1))
351+
call assert_equal([#{id: 2, col: 5, type_bufnr: 0, end: 1, type: 'one',
352+
\ length: 2, start: 1}], prop_list(2))
353+
call assert_equal([#{id: 2, col: 6, type_bufnr: 0, end: 0, type: 'one',
354+
\ length: 7, start: 1}], prop_list(3))
355+
call assert_equal([#{id: 2, col: 1, type_bufnr: 0, end: 1, type: 'one',
356+
\ length: 5, start: 0}], prop_list(4))
357+
call assert_fails('call prop_add_list([1, 2], [[1, 1, 3]])', 'E1206:')
358+
call assert_fails('call prop_add_list({}, {})', 'E1211:')
359+
call assert_fails('call prop_add_list({}, [[1, 1, 3]])', 'E965:')
360+
call assert_fails('call prop_add_list(#{type: "abc"}, [[1, 1, 1, 3]])', 'E971:')
361+
call assert_fails('call prop_add_list(#{type: "one"}, [[]])', 'E474:')
362+
call assert_fails('call prop_add_list(#{type: "one"}, [[1, 1, 1, 1], {}])', 'E714:')
363+
call assert_fails('call prop_add_list(#{type: "one"}, [[1, 1, "a"]])', 'E474:')
364+
call assert_fails('call prop_add_list(#{type: "one"}, [[2, 2]])', 'E474:')
365+
call assert_fails('call prop_add_list(#{type: "one"}, [[1, 1, 2], [2, 2]])', 'E474:')
366+
call assert_fails('call prop_add_list(#{type: "one"}, [[1, 1, 1, 2], [4, 1, 5, 2]])', 'E966:')
367+
call assert_fails('call prop_add_list(#{type: "one"}, [[3, 1, 1, 2]])', 'E966:')
368+
call assert_fails('call prop_add_list(#{type: "one"}, [[2, 2, 2, 2], [3, 20, 3, 22]])', 'E964:')
369+
call assert_fails('eval #{type: "one"}->prop_add_list([[2, 2, 2, 2], [3, 20, 3, 22]])', 'E964:')
370+
call assert_fails('call prop_add_list(test_null_dict(), [[2, 2, 2]])', 'E965:')
371+
call assert_fails('call prop_add_list(#{type: "one"}, test_null_list())', 'E714:')
372+
call assert_fails('call prop_add_list(#{type: "one"}, [test_null_list()])', 'E714:')
373+
call DeletePropTypes()
374+
bw!
375+
endfunc
376+
342377
func Test_prop_remove()
343378
new
344379
call AddPropTypes()

src/testdir/test_vim9_builtin.vim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2364,6 +2364,11 @@ def Test_prop_add()
23642364
CheckDefAndScriptFailure2(['prop_add(1, 2, [])'], 'E1013: Argument 3: type mismatch, expected dict<any> but got list<unknown>', 'E1206: Dictionary required for argument 3')
23652365
enddef
23662366

2367+
def Test_prop_add_list()
2368+
CheckDefAndScriptFailure2(['prop_add_list([], [])'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>', 'E1206: Dictionary required for argument 1')
2369+
CheckDefAndScriptFailure2(['prop_add_list({}, {})'], 'E1013: Argument 2: type mismatch, expected list<any> but got dict<unknown>', 'E1211: List required for argument 2')
2370+
enddef
2371+
23672372
def Test_prop_clear()
23682373
CheckDefAndScriptFailure2(['prop_clear("a")'], 'E1013: Argument 1: type mismatch, expected number but got string', 'E1210: Number required for argument 1')
23692374
CheckDefAndScriptFailure2(['prop_clear(1, "b")'], 'E1013: Argument 2: type mismatch, expected number but got string', 'E1210: Number required for argument 2')

0 commit comments

Comments
 (0)