Skip to content

Commit 02b3111

Browse files
committed
patch 8.1.1954: more functions can be used as a method
Problem: More functions can be used as a method. Solution: Allow more functions to be used as a method.
1 parent f9f24ce commit 02b3111

10 files changed

Lines changed: 81 additions & 32 deletions

File tree

runtime/doc/eval.txt

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ next method: >
12281228
mylist->filter(filterexpr)->map(mapexpr)->sort()->join()
12291229
<
12301230
Example of using a lambda: >
1231-
GetPercentage->{x -> x * 100}()->printf('%d%%')
1231+
GetPercentage()->{x -> x * 100}()->printf('%d%%')
12321232
<
12331233
When using -> the |expr7| operators will be applied first, thus: >
12341234
-1.234->string()
@@ -6206,6 +6206,9 @@ js_decode({string}) *js_decode()*
62066206
- Empty items in an array (between two commas) are allowed and
62076207
result in v:none items.
62086208

6209+
Can also be used as a |method|: >
6210+
ReadObject()->js_decode()
6211+
62096212
js_encode({expr}) *js_encode()*
62106213
This is similar to |json_encode()| with these differences:
62116214
- Object key names are not in quotes.
@@ -6220,6 +6223,8 @@ js_encode({expr}) *js_encode()*
62206223
This encoding is valid for JavaScript. It is more efficient
62216224
than JSON, especially when using an array with optional items.
62226225

6226+
Can also be used as a |method|: >
6227+
GetObject()->js_encode()
62236228
62246229
json_decode({string}) *json_decode()*
62256230
This parses a JSON formatted string and returns the equivalent
@@ -6254,6 +6259,8 @@ json_decode({string}) *json_decode()*
62546259
accepted by json_decode() as the result must be a valid Vim
62556260
type, e.g. this fails: {"a":"b", "a":"c"}
62566261

6262+
Can also be used as a |method|: >
6263+
ReadObject()->json_decode()
62576264
62586265
json_encode({expr}) *json_encode()*
62596266
Encode {expr} as JSON and return this as a string.
@@ -6280,6 +6287,9 @@ json_encode({expr}) *json_encode()*
62806287
missing in the JSON standard, but several implementations do
62816288
allow it. If not then you will get an error.
62826289

6290+
Can also be used as a |method|: >
6291+
GetObject()->json_encode()
6292+
62836293
keys({dict}) *keys()*
62846294
Return a |List| with all the keys of {dict}. The |List| is in
62856295
arbitrary order. Also see |items()| and |values()|.
@@ -6346,6 +6356,10 @@ libcall({libname}, {funcname}, {argument})
63466356
feature is present}
63476357
Examples: >
63486358
:echo libcall("libc.so", "getenv", "HOME")
6359+
6360+
< Can also be used as a |method|, where the base is passed as
6361+
the argument to the called function: >
6362+
GetValue()->libcall("libc.so", "getenv")
63496363
<
63506364
*libcallnr()*
63516365
libcallnr({libname}, {funcname}, {argument})
@@ -6357,6 +6371,10 @@ libcallnr({libname}, {funcname}, {argument})
63576371
:echo libcallnr("/usr/lib/libc.so", "getpid", "")
63586372
:call libcallnr("libc.so", "printf", "Hello World!\n")
63596373
:call libcallnr("libc.so", "sleep", 10)
6374+
<
6375+
Can also be used as a |method|, where the base is passed as
6376+
the argument to the called function: >
6377+
GetValue()->libcallnr("libc.so", "printf")
63606378
<
63616379
*line()*
63626380
line({expr}) The result is a Number, which is the line number of the file
@@ -6385,6 +6403,9 @@ line({expr}) The result is a Number, which is the line number of the file
63856403
To jump to the last known position when opening a file see
63866404
|last-position-jump|.
63876405

6406+
Can also be used as a |method|: >
6407+
GetValue()->line()
6408+
63886409
line2byte({lnum}) *line2byte()*
63896410
Return the byte count from the start of the buffer for line
63906411
{lnum}. This includes the end-of-line character, depending on
@@ -6399,6 +6420,9 @@ line2byte({lnum}) *line2byte()*
63996420
disabled at compile time, -1 is returned.
64006421
Also see |byte2line()|, |go| and |:goto|.
64016422

6423+
Can also be used as a |method|: >
6424+
GetLnum()->line2byte()
6425+
64026426
lispindent({lnum}) *lispindent()*
64036427
Get the amount of indent for line {lnum} according the lisp
64046428
indenting rules, as with 'lisp'.
@@ -6407,6 +6431,9 @@ lispindent({lnum}) *lispindent()*
64076431
When {lnum} is invalid or Vim was not compiled the
64086432
|+lispindent| feature, -1 is returned.
64096433

6434+
Can also be used as a |method|: >
6435+
GetLnum()->lispindent()
6436+
64106437
list2str({list} [, {utf8}]) *list2str()*
64116438
Convert each number in {list} to a character string can
64126439
concatenate them all. Examples: >
@@ -6421,6 +6448,9 @@ list2str({list} [, {utf8}]) *list2str()*
64216448
With utf-8 composing characters work as expected: >
64226449
list2str([97, 769]) returns "á"
64236450
<
6451+
Can also be used as a |method|: >
6452+
GetList()->list2str()
6453+
64246454
listener_add({callback} [, {buf}]) *listener_add()*
64256455
Add a callback function that will be invoked when changes have
64266456
been made to buffer {buf}.
@@ -6490,6 +6520,10 @@ listener_add({callback} [, {buf}]) *listener_add()*
64906520
The {callback} is also not invoked when the buffer is
64916521
unloaded, use the |BufUnload| autocmd event for that.
64926522

6523+
Can also be used as a |method|, where the base is passed as
6524+
the second argument, the buffer: >
6525+
GetBuffer()->listener_add(callback)
6526+
64936527
listener_flush([{buf}]) *listener_flush()*
64946528
Invoke listener callbacks for buffer {buf}. If there are no
64956529
pending changes then no callbacks are invoked.
@@ -6498,11 +6532,17 @@ listener_flush([{buf}]) *listener_flush()*
64986532
values, see |bufname()|. When {buf} is omitted the current
64996533
buffer is used.
65006534

6535+
Can also be used as a |method|: >
6536+
GetBuffer()->listener_flush()
6537+
65016538
listener_remove({id}) *listener_remove()*
65026539
Remove a listener previously added with listener_add().
65036540
Returns zero when {id} could not be found, one when {id} was
65046541
removed.
65056542

6543+
Can also be used as a |method|: >
6544+
GetListenerId()->listener_remove()
6545+
65066546
localtime() *localtime()*
65076547
Return the current time, measured as seconds since 1st Jan
65086548
1970. See also |strftime()| and |getftime()|.
@@ -6550,7 +6590,11 @@ luaeval({expr} [, {expr}]) *luaeval()*
65506590
as-is.
65516591
Other objects are returned as zero without any errors.
65526592
See |lua-luaeval| for more details.
6553-
{only available when compiled with the |+lua| feature}
6593+
6594+
Can also be used as a |method|: >
6595+
GetExpr()->luaeval()
6596+
6597+
< {only available when compiled with the |+lua| feature}
65546598

65556599
map({expr1}, {expr2}) *map()*
65566600
{expr1} must be a |List| or a |Dictionary|.

src/evalfunc.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -635,29 +635,29 @@ static funcentry_T global_functions[] =
635635
{"job_stop", 1, 2, FEARG_1, f_job_stop},
636636
#endif
637637
{"join", 1, 2, FEARG_1, f_join},
638-
{"js_decode", 1, 1, 0, f_js_decode},
639-
{"js_encode", 1, 1, 0, f_js_encode},
640-
{"json_decode", 1, 1, 0, f_json_decode},
641-
{"json_encode", 1, 1, 0, f_json_encode},
638+
{"js_decode", 1, 1, FEARG_1, f_js_decode},
639+
{"js_encode", 1, 1, FEARG_1, f_js_encode},
640+
{"json_decode", 1, 1, FEARG_1, f_json_decode},
641+
{"json_encode", 1, 1, FEARG_1, f_json_encode},
642642
{"keys", 1, 1, FEARG_1, f_keys},
643643
{"last_buffer_nr", 0, 0, 0, f_last_buffer_nr}, // obsolete
644644
{"len", 1, 1, FEARG_1, f_len},
645-
{"libcall", 3, 3, 0, f_libcall},
646-
{"libcallnr", 3, 3, 0, f_libcallnr},
647-
{"line", 1, 1, 0, f_line},
648-
{"line2byte", 1, 1, 0, f_line2byte},
649-
{"lispindent", 1, 1, 0, f_lispindent},
650-
{"list2str", 1, 2, 0, f_list2str},
651-
{"listener_add", 1, 2, 0, f_listener_add},
652-
{"listener_flush", 0, 1, 0, f_listener_flush},
653-
{"listener_remove", 1, 1, 0, f_listener_remove},
645+
{"libcall", 3, 3, FEARG_3, f_libcall},
646+
{"libcallnr", 3, 3, FEARG_3, f_libcallnr},
647+
{"line", 1, 1, FEARG_1, f_line},
648+
{"line2byte", 1, 1, FEARG_1, f_line2byte},
649+
{"lispindent", 1, 1, FEARG_1, f_lispindent},
650+
{"list2str", 1, 2, FEARG_1, f_list2str},
651+
{"listener_add", 1, 2, FEARG_2, f_listener_add},
652+
{"listener_flush", 0, 1, FEARG_1, f_listener_flush},
653+
{"listener_remove", 1, 1, FEARG_1, f_listener_remove},
654654
{"localtime", 0, 0, 0, f_localtime},
655655
#ifdef FEAT_FLOAT
656656
{"log", 1, 1, FEARG_1, f_log},
657657
{"log10", 1, 1, FEARG_1, f_log10},
658658
#endif
659659
#ifdef FEAT_LUA
660-
{"luaeval", 1, 2, 0, f_luaeval},
660+
{"luaeval", 1, 2, FEARG_1, f_luaeval},
661661
#endif
662662
{"map", 2, 2, FEARG_1, f_map},
663663
{"maparg", 1, 4, 0, f_maparg},

src/testdir/test_arglist.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func Test_argadd_empty_curbuf()
8888
argadd Xargadd
8989
call assert_equal(curbuf, bufnr('%'))
9090
call assert_equal('', bufname('%'))
91-
call assert_equal(1, line('$'))
91+
call assert_equal(1, '$'->line())
9292
rew
9393
call assert_notequal(curbuf, '%'->bufnr())
9494
call assert_equal('Xargadd', '%'->bufname())

src/testdir/test_functions.vim

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ func Test_byte2line_line2byte()
883883
call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
884884
\ map(range(-1, 8), 'v:val->byte2line()'))
885885
call assert_equal([-1, -1, 1, 3, 6, 8, -1],
886-
\ map(range(-1, 5), 'line2byte(v:val)'))
886+
\ map(range(-1, 5), 'v:val->line2byte()'))
887887

888888
set fileformat=dos
889889
call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
@@ -1351,17 +1351,17 @@ func Test_libcall_libcallnr()
13511351
endif
13521352

13531353
if has('win32')
1354-
call assert_equal($USERPROFILE, libcall(libc, 'getenv', 'USERPROFILE'))
1354+
call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv'))
13551355
else
1356-
call assert_equal($HOME, libcall(libc, 'getenv', 'HOME'))
1356+
call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv'))
13571357
endif
13581358

13591359
" If function returns NULL, libcall() should return an empty string.
13601360
call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
13611361

13621362
" Test libcallnr() with string and integer argument.
1363-
call assert_equal(4, libcallnr(libc, 'strlen', 'abcd'))
1364-
call assert_equal(char2nr('A'), libcallnr(libc, 'toupper', char2nr('a')))
1363+
call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen'))
1364+
call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper'))
13651365

13661366
call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
13671367
call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')

src/testdir/test_json.vim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ let s:varvals = [v:true, v:false, v:null, v:null]
7070
func Test_json_encode()
7171
call assert_equal(s:json1, json_encode(s:var1))
7272
call assert_equal(s:json2, json_encode(s:var2))
73-
call assert_equal(s:json3, json_encode(s:var3))
73+
call assert_equal(s:json3, s:var3->json_encode())
7474
call assert_equal(s:json4, json_encode(s:var4))
7575
call assert_equal(s:json5, json_encode(s:var5))
7676

@@ -110,7 +110,7 @@ endfunc
110110
func Test_json_decode()
111111
call assert_equal(s:var1, json_decode(s:json1))
112112
call assert_equal(s:var2, json_decode(s:json2))
113-
call assert_equal(s:var3, json_decode(s:json3))
113+
call assert_equal(s:var3, s:json3->json_decode())
114114
call assert_equal(s:var4, json_decode(s:json4))
115115
call assert_equal(s:var5, json_decode(s:json5))
116116

@@ -188,7 +188,7 @@ let s:varl5 = [7, v:none, v:none]
188188
func Test_js_encode()
189189
call assert_equal(s:json1, js_encode(s:var1))
190190
call assert_equal(s:json2, js_encode(s:var2))
191-
call assert_equal(s:json3, js_encode(s:var3))
191+
call assert_equal(s:json3, s:var3->js_encode())
192192
call assert_equal(s:json4, js_encode(s:var4))
193193
call assert_equal(s:json5, js_encode(s:var5))
194194

@@ -226,7 +226,7 @@ endfunc
226226
func Test_js_decode()
227227
call assert_equal(s:var1, js_decode(s:json1))
228228
call assert_equal(s:var2, js_decode(s:json2))
229-
call assert_equal(s:var3, js_decode(s:json3))
229+
call assert_equal(s:var3, s:json3->js_decode())
230230
call assert_equal(s:var4, js_decode(s:json4))
231231
call assert_equal(s:var5, js_decode(s:json5))
232232

src/testdir/test_lispwords.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ func Test_lisp_indent()
4343
\ ',@body',
4444
\ '(princ "</a>")))'
4545
\ ])
46+
call assert_equal(7, lispindent(2))
47+
call assert_equal(5, 6->lispindent())
48+
4649
set lisp
4750
set lispwords&
4851
let save_copt = &cpoptions

src/testdir/test_listener.vim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ func Test_listening()
5959
" a change above a previous change without a line number change is reported
6060
" together
6161
call setline(1, ['one one', 'two'])
62-
call listener_flush()
62+
call listener_flush(bufnr())
6363
call append(2, 'two two')
6464
call setline(1, 'something')
65-
call listener_flush()
65+
call bufnr()->listener_flush()
6666
call assert_equal([{'lnum': 3, 'end': 3, 'col': 1, 'added': 1},
6767
\ {'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], s:list)
6868

@@ -134,7 +134,7 @@ func Test_listening()
134134
redraw
135135
call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], s:list3)
136136

137-
call listener_remove(id)
137+
eval id->listener_remove()
138138
bwipe!
139139
endfunc
140140

@@ -214,7 +214,7 @@ func Test_listening_other_buf()
214214
call setline(1, ['one', 'two'])
215215
let bufnr = bufnr('')
216216
normal ww
217-
let id = listener_add(function('s:StoreBufList'), bufnr)
217+
let id = bufnr->listener_add(function('s:StoreBufList'))
218218
let s:list = []
219219
call setbufline(bufnr, 1, 'hello')
220220
redraw

src/testdir/test_lua.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func Test_eval()
3636

3737
" lua.eval with a string
3838
lua v = vim.eval('"abc"')
39-
call assert_equal('string', luaeval('vim.type(v)'))
39+
call assert_equal('string', 'vim.type(v)'->luaeval())
4040
call assert_equal('abc', luaeval('v'))
4141

4242
" lua.eval with a list

src/testdir/test_utf8.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func Test_list2str_str2list_utf8()
7777
let s = "\u304b\u3099\u3044"
7878
let l = [0x304b, 0x3099, 0x3044]
7979
call assert_equal(l, str2list(s, 1))
80-
call assert_equal(s, list2str(l, 1))
80+
call assert_equal(s, l->list2str(1))
8181
if &enc ==# 'utf-8'
8282
call assert_equal(str2list(s), str2list(s, 1))
8383
call assert_equal(list2str(l), list2str(l, 1))

src/version.c

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

762762
static int included_patches[] =
763763
{ /* Add new patch number below this line */
764+
/**/
765+
1954,
764766
/**/
765767
1953,
766768
/**/

0 commit comments

Comments
 (0)