0
0
mirror of https://github.com/vim/vim.git synced 2025-10-04 05:25:06 -04:00

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.
This commit is contained in:
Bram Moolenaar
2019-08-31 22:16:38 +02:00
parent f9f24ce7a0
commit 02b31110d3
10 changed files with 81 additions and 32 deletions

View File

@@ -1228,7 +1228,7 @@ next method: >
mylist->filter(filterexpr)->map(mapexpr)->sort()->join() mylist->filter(filterexpr)->map(mapexpr)->sort()->join()
< <
Example of using a lambda: > Example of using a lambda: >
GetPercentage->{x -> x * 100}()->printf('%d%%') GetPercentage()->{x -> x * 100}()->printf('%d%%')
< <
When using -> the |expr7| operators will be applied first, thus: > When using -> the |expr7| operators will be applied first, thus: >
-1.234->string() -1.234->string()
@@ -6206,6 +6206,9 @@ js_decode({string}) *js_decode()*
- Empty items in an array (between two commas) are allowed and - Empty items in an array (between two commas) are allowed and
result in v:none items. result in v:none items.
Can also be used as a |method|: >
ReadObject()->js_decode()
js_encode({expr}) *js_encode()* js_encode({expr}) *js_encode()*
This is similar to |json_encode()| with these differences: This is similar to |json_encode()| with these differences:
- Object key names are not in quotes. - Object key names are not in quotes.
@@ -6220,6 +6223,8 @@ js_encode({expr}) *js_encode()*
This encoding is valid for JavaScript. It is more efficient This encoding is valid for JavaScript. It is more efficient
than JSON, especially when using an array with optional items. than JSON, especially when using an array with optional items.
Can also be used as a |method|: >
GetObject()->js_encode()
json_decode({string}) *json_decode()* json_decode({string}) *json_decode()*
This parses a JSON formatted string and returns the equivalent This parses a JSON formatted string and returns the equivalent
@@ -6254,6 +6259,8 @@ json_decode({string}) *json_decode()*
accepted by json_decode() as the result must be a valid Vim accepted by json_decode() as the result must be a valid Vim
type, e.g. this fails: {"a":"b", "a":"c"} type, e.g. this fails: {"a":"b", "a":"c"}
Can also be used as a |method|: >
ReadObject()->json_decode()
json_encode({expr}) *json_encode()* json_encode({expr}) *json_encode()*
Encode {expr} as JSON and return this as a string. Encode {expr} as JSON and return this as a string.
@@ -6280,6 +6287,9 @@ json_encode({expr}) *json_encode()*
missing in the JSON standard, but several implementations do missing in the JSON standard, but several implementations do
allow it. If not then you will get an error. allow it. If not then you will get an error.
Can also be used as a |method|: >
GetObject()->json_encode()
keys({dict}) *keys()* keys({dict}) *keys()*
Return a |List| with all the keys of {dict}. The |List| is in Return a |List| with all the keys of {dict}. The |List| is in
arbitrary order. Also see |items()| and |values()|. arbitrary order. Also see |items()| and |values()|.
@@ -6346,6 +6356,10 @@ libcall({libname}, {funcname}, {argument})
feature is present} feature is present}
Examples: > Examples: >
:echo libcall("libc.so", "getenv", "HOME") :echo libcall("libc.so", "getenv", "HOME")
< Can also be used as a |method|, where the base is passed as
the argument to the called function: >
GetValue()->libcall("libc.so", "getenv")
< <
*libcallnr()* *libcallnr()*
libcallnr({libname}, {funcname}, {argument}) libcallnr({libname}, {funcname}, {argument})
@@ -6357,6 +6371,10 @@ libcallnr({libname}, {funcname}, {argument})
:echo libcallnr("/usr/lib/libc.so", "getpid", "") :echo libcallnr("/usr/lib/libc.so", "getpid", "")
:call libcallnr("libc.so", "printf", "Hello World!\n") :call libcallnr("libc.so", "printf", "Hello World!\n")
:call libcallnr("libc.so", "sleep", 10) :call libcallnr("libc.so", "sleep", 10)
<
Can also be used as a |method|, where the base is passed as
the argument to the called function: >
GetValue()->libcallnr("libc.so", "printf")
< <
*line()* *line()*
line({expr}) The result is a Number, which is the line number of the file 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
To jump to the last known position when opening a file see To jump to the last known position when opening a file see
|last-position-jump|. |last-position-jump|.
Can also be used as a |method|: >
GetValue()->line()
line2byte({lnum}) *line2byte()* line2byte({lnum}) *line2byte()*
Return the byte count from the start of the buffer for line Return the byte count from the start of the buffer for line
{lnum}. This includes the end-of-line character, depending on {lnum}. This includes the end-of-line character, depending on
@@ -6399,6 +6420,9 @@ line2byte({lnum}) *line2byte()*
disabled at compile time, -1 is returned. disabled at compile time, -1 is returned.
Also see |byte2line()|, |go| and |:goto|. Also see |byte2line()|, |go| and |:goto|.
Can also be used as a |method|: >
GetLnum()->line2byte()
lispindent({lnum}) *lispindent()* lispindent({lnum}) *lispindent()*
Get the amount of indent for line {lnum} according the lisp Get the amount of indent for line {lnum} according the lisp
indenting rules, as with 'lisp'. indenting rules, as with 'lisp'.
@@ -6407,6 +6431,9 @@ lispindent({lnum}) *lispindent()*
When {lnum} is invalid or Vim was not compiled the When {lnum} is invalid or Vim was not compiled the
|+lispindent| feature, -1 is returned. |+lispindent| feature, -1 is returned.
Can also be used as a |method|: >
GetLnum()->lispindent()
list2str({list} [, {utf8}]) *list2str()* list2str({list} [, {utf8}]) *list2str()*
Convert each number in {list} to a character string can Convert each number in {list} to a character string can
concatenate them all. Examples: > concatenate them all. Examples: >
@@ -6421,6 +6448,9 @@ list2str({list} [, {utf8}]) *list2str()*
With utf-8 composing characters work as expected: > With utf-8 composing characters work as expected: >
list2str([97, 769]) returns "á" list2str([97, 769]) returns "á"
< <
Can also be used as a |method|: >
GetList()->list2str()
listener_add({callback} [, {buf}]) *listener_add()* listener_add({callback} [, {buf}]) *listener_add()*
Add a callback function that will be invoked when changes have Add a callback function that will be invoked when changes have
been made to buffer {buf}. been made to buffer {buf}.
@@ -6490,6 +6520,10 @@ listener_add({callback} [, {buf}]) *listener_add()*
The {callback} is also not invoked when the buffer is The {callback} is also not invoked when the buffer is
unloaded, use the |BufUnload| autocmd event for that. unloaded, use the |BufUnload| autocmd event for that.
Can also be used as a |method|, where the base is passed as
the second argument, the buffer: >
GetBuffer()->listener_add(callback)
listener_flush([{buf}]) *listener_flush()* listener_flush([{buf}]) *listener_flush()*
Invoke listener callbacks for buffer {buf}. If there are no Invoke listener callbacks for buffer {buf}. If there are no
pending changes then no callbacks are invoked. pending changes then no callbacks are invoked.
@@ -6498,11 +6532,17 @@ listener_flush([{buf}]) *listener_flush()*
values, see |bufname()|. When {buf} is omitted the current values, see |bufname()|. When {buf} is omitted the current
buffer is used. buffer is used.
Can also be used as a |method|: >
GetBuffer()->listener_flush()
listener_remove({id}) *listener_remove()* listener_remove({id}) *listener_remove()*
Remove a listener previously added with listener_add(). Remove a listener previously added with listener_add().
Returns zero when {id} could not be found, one when {id} was Returns zero when {id} could not be found, one when {id} was
removed. removed.
Can also be used as a |method|: >
GetListenerId()->listener_remove()
localtime() *localtime()* localtime() *localtime()*
Return the current time, measured as seconds since 1st Jan Return the current time, measured as seconds since 1st Jan
1970. See also |strftime()| and |getftime()|. 1970. See also |strftime()| and |getftime()|.
@@ -6550,7 +6590,11 @@ luaeval({expr} [, {expr}]) *luaeval()*
as-is. as-is.
Other objects are returned as zero without any errors. Other objects are returned as zero without any errors.
See |lua-luaeval| for more details. See |lua-luaeval| for more details.
{only available when compiled with the |+lua| feature}
Can also be used as a |method|: >
GetExpr()->luaeval()
< {only available when compiled with the |+lua| feature}
map({expr1}, {expr2}) *map()* map({expr1}, {expr2}) *map()*
{expr1} must be a |List| or a |Dictionary|. {expr1} must be a |List| or a |Dictionary|.

View File

@@ -635,29 +635,29 @@ static funcentry_T global_functions[] =
{"job_stop", 1, 2, FEARG_1, f_job_stop}, {"job_stop", 1, 2, FEARG_1, f_job_stop},
#endif #endif
{"join", 1, 2, FEARG_1, f_join}, {"join", 1, 2, FEARG_1, f_join},
{"js_decode", 1, 1, 0, f_js_decode}, {"js_decode", 1, 1, FEARG_1, f_js_decode},
{"js_encode", 1, 1, 0, f_js_encode}, {"js_encode", 1, 1, FEARG_1, f_js_encode},
{"json_decode", 1, 1, 0, f_json_decode}, {"json_decode", 1, 1, FEARG_1, f_json_decode},
{"json_encode", 1, 1, 0, f_json_encode}, {"json_encode", 1, 1, FEARG_1, f_json_encode},
{"keys", 1, 1, FEARG_1, f_keys}, {"keys", 1, 1, FEARG_1, f_keys},
{"last_buffer_nr", 0, 0, 0, f_last_buffer_nr}, // obsolete {"last_buffer_nr", 0, 0, 0, f_last_buffer_nr}, // obsolete
{"len", 1, 1, FEARG_1, f_len}, {"len", 1, 1, FEARG_1, f_len},
{"libcall", 3, 3, 0, f_libcall}, {"libcall", 3, 3, FEARG_3, f_libcall},
{"libcallnr", 3, 3, 0, f_libcallnr}, {"libcallnr", 3, 3, FEARG_3, f_libcallnr},
{"line", 1, 1, 0, f_line}, {"line", 1, 1, FEARG_1, f_line},
{"line2byte", 1, 1, 0, f_line2byte}, {"line2byte", 1, 1, FEARG_1, f_line2byte},
{"lispindent", 1, 1, 0, f_lispindent}, {"lispindent", 1, 1, FEARG_1, f_lispindent},
{"list2str", 1, 2, 0, f_list2str}, {"list2str", 1, 2, FEARG_1, f_list2str},
{"listener_add", 1, 2, 0, f_listener_add}, {"listener_add", 1, 2, FEARG_2, f_listener_add},
{"listener_flush", 0, 1, 0, f_listener_flush}, {"listener_flush", 0, 1, FEARG_1, f_listener_flush},
{"listener_remove", 1, 1, 0, f_listener_remove}, {"listener_remove", 1, 1, FEARG_1, f_listener_remove},
{"localtime", 0, 0, 0, f_localtime}, {"localtime", 0, 0, 0, f_localtime},
#ifdef FEAT_FLOAT #ifdef FEAT_FLOAT
{"log", 1, 1, FEARG_1, f_log}, {"log", 1, 1, FEARG_1, f_log},
{"log10", 1, 1, FEARG_1, f_log10}, {"log10", 1, 1, FEARG_1, f_log10},
#endif #endif
#ifdef FEAT_LUA #ifdef FEAT_LUA
{"luaeval", 1, 2, 0, f_luaeval}, {"luaeval", 1, 2, FEARG_1, f_luaeval},
#endif #endif
{"map", 2, 2, FEARG_1, f_map}, {"map", 2, 2, FEARG_1, f_map},
{"maparg", 1, 4, 0, f_maparg}, {"maparg", 1, 4, 0, f_maparg},

View File

@@ -88,7 +88,7 @@ func Test_argadd_empty_curbuf()
argadd Xargadd argadd Xargadd
call assert_equal(curbuf, bufnr('%')) call assert_equal(curbuf, bufnr('%'))
call assert_equal('', bufname('%')) call assert_equal('', bufname('%'))
call assert_equal(1, line('$')) call assert_equal(1, '$'->line())
rew rew
call assert_notequal(curbuf, '%'->bufnr()) call assert_notequal(curbuf, '%'->bufnr())
call assert_equal('Xargadd', '%'->bufname()) call assert_equal('Xargadd', '%'->bufname())

View File

@@ -883,7 +883,7 @@ func Test_byte2line_line2byte()
call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1], call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
\ map(range(-1, 8), 'v:val->byte2line()')) \ map(range(-1, 8), 'v:val->byte2line()'))
call assert_equal([-1, -1, 1, 3, 6, 8, -1], call assert_equal([-1, -1, 1, 3, 6, 8, -1],
\ map(range(-1, 5), 'line2byte(v:val)')) \ map(range(-1, 5), 'v:val->line2byte()'))
set fileformat=dos set fileformat=dos
call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1], call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
@@ -1351,17 +1351,17 @@ func Test_libcall_libcallnr()
endif endif
if has('win32') if has('win32')
call assert_equal($USERPROFILE, libcall(libc, 'getenv', 'USERPROFILE')) call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv'))
else else
call assert_equal($HOME, libcall(libc, 'getenv', 'HOME')) call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv'))
endif endif
" If function returns NULL, libcall() should return an empty string. " If function returns NULL, libcall() should return an empty string.
call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT')) call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
" Test libcallnr() with string and integer argument. " Test libcallnr() with string and integer argument.
call assert_equal(4, libcallnr(libc, 'strlen', 'abcd')) call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen'))
call assert_equal(char2nr('A'), libcallnr(libc, 'toupper', char2nr('a'))) call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper'))
call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:') call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:') call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')

View File

@@ -70,7 +70,7 @@ let s:varvals = [v:true, v:false, v:null, v:null]
func Test_json_encode() func Test_json_encode()
call assert_equal(s:json1, json_encode(s:var1)) call assert_equal(s:json1, json_encode(s:var1))
call assert_equal(s:json2, json_encode(s:var2)) call assert_equal(s:json2, json_encode(s:var2))
call assert_equal(s:json3, json_encode(s:var3)) call assert_equal(s:json3, s:var3->json_encode())
call assert_equal(s:json4, json_encode(s:var4)) call assert_equal(s:json4, json_encode(s:var4))
call assert_equal(s:json5, json_encode(s:var5)) call assert_equal(s:json5, json_encode(s:var5))
@@ -110,7 +110,7 @@ endfunc
func Test_json_decode() func Test_json_decode()
call assert_equal(s:var1, json_decode(s:json1)) call assert_equal(s:var1, json_decode(s:json1))
call assert_equal(s:var2, json_decode(s:json2)) call assert_equal(s:var2, json_decode(s:json2))
call assert_equal(s:var3, json_decode(s:json3)) call assert_equal(s:var3, s:json3->json_decode())
call assert_equal(s:var4, json_decode(s:json4)) call assert_equal(s:var4, json_decode(s:json4))
call assert_equal(s:var5, json_decode(s:json5)) call assert_equal(s:var5, json_decode(s:json5))
@@ -188,7 +188,7 @@ let s:varl5 = [7, v:none, v:none]
func Test_js_encode() func Test_js_encode()
call assert_equal(s:json1, js_encode(s:var1)) call assert_equal(s:json1, js_encode(s:var1))
call assert_equal(s:json2, js_encode(s:var2)) call assert_equal(s:json2, js_encode(s:var2))
call assert_equal(s:json3, js_encode(s:var3)) call assert_equal(s:json3, s:var3->js_encode())
call assert_equal(s:json4, js_encode(s:var4)) call assert_equal(s:json4, js_encode(s:var4))
call assert_equal(s:json5, js_encode(s:var5)) call assert_equal(s:json5, js_encode(s:var5))
@@ -226,7 +226,7 @@ endfunc
func Test_js_decode() func Test_js_decode()
call assert_equal(s:var1, js_decode(s:json1)) call assert_equal(s:var1, js_decode(s:json1))
call assert_equal(s:var2, js_decode(s:json2)) call assert_equal(s:var2, js_decode(s:json2))
call assert_equal(s:var3, js_decode(s:json3)) call assert_equal(s:var3, s:json3->js_decode())
call assert_equal(s:var4, js_decode(s:json4)) call assert_equal(s:var4, js_decode(s:json4))
call assert_equal(s:var5, js_decode(s:json5)) call assert_equal(s:var5, js_decode(s:json5))

View File

@@ -43,6 +43,9 @@ func Test_lisp_indent()
\ ',@body', \ ',@body',
\ '(princ "</a>")))' \ '(princ "</a>")))'
\ ]) \ ])
call assert_equal(7, lispindent(2))
call assert_equal(5, 6->lispindent())
set lisp set lisp
set lispwords& set lispwords&
let save_copt = &cpoptions let save_copt = &cpoptions

View File

@@ -59,10 +59,10 @@ func Test_listening()
" a change above a previous change without a line number change is reported " a change above a previous change without a line number change is reported
" together " together
call setline(1, ['one one', 'two']) call setline(1, ['one one', 'two'])
call listener_flush() call listener_flush(bufnr())
call append(2, 'two two') call append(2, 'two two')
call setline(1, 'something') call setline(1, 'something')
call listener_flush() call bufnr()->listener_flush()
call assert_equal([{'lnum': 3, 'end': 3, 'col': 1, 'added': 1}, call assert_equal([{'lnum': 3, 'end': 3, 'col': 1, 'added': 1},
\ {'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], s:list) \ {'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], s:list)
@@ -134,7 +134,7 @@ func Test_listening()
redraw redraw
call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], s:list3) call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], s:list3)
call listener_remove(id) eval id->listener_remove()
bwipe! bwipe!
endfunc endfunc
@@ -214,7 +214,7 @@ func Test_listening_other_buf()
call setline(1, ['one', 'two']) call setline(1, ['one', 'two'])
let bufnr = bufnr('') let bufnr = bufnr('')
normal ww normal ww
let id = listener_add(function('s:StoreBufList'), bufnr) let id = bufnr->listener_add(function('s:StoreBufList'))
let s:list = [] let s:list = []
call setbufline(bufnr, 1, 'hello') call setbufline(bufnr, 1, 'hello')
redraw redraw

View File

@@ -36,7 +36,7 @@ func Test_eval()
" lua.eval with a string " lua.eval with a string
lua v = vim.eval('"abc"') lua v = vim.eval('"abc"')
call assert_equal('string', luaeval('vim.type(v)')) call assert_equal('string', 'vim.type(v)'->luaeval())
call assert_equal('abc', luaeval('v')) call assert_equal('abc', luaeval('v'))
" lua.eval with a list " lua.eval with a list

View File

@@ -77,7 +77,7 @@ func Test_list2str_str2list_utf8()
let s = "\u304b\u3099\u3044" let s = "\u304b\u3099\u3044"
let l = [0x304b, 0x3099, 0x3044] let l = [0x304b, 0x3099, 0x3044]
call assert_equal(l, str2list(s, 1)) call assert_equal(l, str2list(s, 1))
call assert_equal(s, list2str(l, 1)) call assert_equal(s, l->list2str(1))
if &enc ==# 'utf-8' if &enc ==# 'utf-8'
call assert_equal(str2list(s), str2list(s, 1)) call assert_equal(str2list(s), str2list(s, 1))
call assert_equal(list2str(l), list2str(l, 1)) call assert_equal(list2str(l), list2str(l, 1))

View File

@@ -761,6 +761,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
1954,
/**/ /**/
1953, 1953,
/**/ /**/