feat(eval/method): partially port v8.1.1993

Problem:    More functions can be used as methods.
Solution:   Make various functions usable as a method.
196b466443

server2client requires +clientserver, which hasn't been ported yet.
The eval.txt docs and test_clientserver.vim tests for server2client already exist, so include those
changes.

test_bufline.vim: Test for setbufline requires v8.1.1189 (which was reverted in #10848).
This commit is contained in:
Sean Dewar
2021-10-31 16:41:39 +00:00
parent fa9076f276
commit 4efcb72bb7
13 changed files with 106 additions and 27 deletions

View File

@@ -5854,7 +5854,7 @@ histadd({history}, {item}) *histadd()*
Can also be used as a |method|, the base is used for the Can also be used as a |method|, the base is used for the
second argument: > second argument: >
GetPattern()->histadd('search') GetHistory()->histadd('search')
histdel({history} [, {item}]) *histdel()* histdel({history} [, {item}]) *histdel()*
Clear {history}, i.e. delete all its entries. See |hist-names| Clear {history}, i.e. delete all its entries. See |hist-names|
@@ -8072,6 +8072,9 @@ screenattr({row}, {col}) *screenattr()*
arbitrary number that can only be used to compare to the arbitrary number that can only be used to compare to the
attribute at other positions. attribute at other positions.
Can also be used as a |method|: >
GetRow()->screenattr(col)
screenchar({row}, {col}) *screenchar()* screenchar({row}, {col}) *screenchar()*
The result is a Number, which is the character at position The result is a Number, which is the character at position
[row, col] on the screen. This works for every possible [row, col] on the screen. This works for every possible
@@ -8082,6 +8085,9 @@ screenchar({row}, {col}) *screenchar()*
This is mainly to be used for testing. This is mainly to be used for testing.
Returns -1 when row or col is out of range. Returns -1 when row or col is out of range.
Can also be used as a |method|: >
GetRow()->screenchar(col)
screenchars({row}, {col}) *screenchars()* screenchars({row}, {col}) *screenchars()*
The result is a List of Numbers. The first number is the same The result is a List of Numbers. The first number is the same
as what |screenchar()| returns. Further numbers are as what |screenchar()| returns. Further numbers are
@@ -8089,6 +8095,9 @@ screenchars({row}, {col}) *screenchars()*
This is mainly to be used for testing. This is mainly to be used for testing.
Returns an empty List when row or col is out of range. Returns an empty List when row or col is out of range.
Can also be used as a |method|: >
GetRow()->screenchars(col)
screencol() *screencol()* screencol() *screencol()*
The result is a Number, which is the current screen column of The result is a Number, which is the current screen column of
the cursor. The leftmost column has number 1. the cursor. The leftmost column has number 1.
@@ -8124,6 +8133,9 @@ screenpos({winid}, {lnum}, {col}) *screenpos()*
right position and use |screencol()| to get the value with right position and use |screencol()| to get the value with
|conceal| taken into account. |conceal| taken into account.
Can also be used as a |method|: >
GetWinid()->screenpos(lnum, col)
screenrow() *screenrow()* screenrow() *screenrow()*
The result is a Number, which is the current screen row of the The result is a Number, which is the current screen row of the
cursor. The top line has number one. cursor. The top line has number one.
@@ -8140,6 +8152,9 @@ screenstring({row}, {col}) *screenstring()*
This is mainly to be used for testing. This is mainly to be used for testing.
Returns an empty String when row or col is out of range. Returns an empty String when row or col is out of range.
Can also be used as a |method|: >
GetRow()->screenstring(col)
search({pattern} [, {flags} [, {stopline} [, {timeout}]]]) *search()* search({pattern} [, {flags} [, {stopline} [, {timeout}]]]) *search()*
Search for regexp pattern {pattern}. The search starts at the Search for regexp pattern {pattern}. The search starts at the
cursor position (you can use |cursor()| to set it). cursor position (you can use |cursor()| to set it).
@@ -8230,6 +8245,8 @@ search({pattern} [, {flags} [, {stopline} [, {timeout}]]]) *search()*
without the 'e' flag if the cursor is on the "f" of "if". without the 'e' flag if the cursor is on the "f" of "if".
The 'n' flag tells the function not to move the cursor. The 'n' flag tells the function not to move the cursor.
Can also be used as a |method|: >
GetPattern()->search()
searchcount([{options}]) *searchcount()* searchcount([{options}]) *searchcount()*
Get or update the last search count, like what is displayed Get or update the last search count, like what is displayed
@@ -8368,6 +8385,9 @@ searchdecl({name} [, {global} [, {thisblock}]]) *searchdecl()*
if searchdecl('myvar') == 0 if searchdecl('myvar') == 0
echo getline('.') echo getline('.')
endif endif
<
Can also be used as a |method|: >
GetName()->searchdecl()
< <
*searchpair()* *searchpair()*
searchpair({start}, {middle}, {end} [, {flags} [, {skip} searchpair({start}, {middle}, {end} [, {flags} [, {skip}
@@ -8483,6 +8503,9 @@ searchpos({pattern} [, {flags} [, {stopline} [, {timeout}]]]) *searchpos()*
< In this example "submatch" is 2 when a lowercase letter is < In this example "submatch" is 2 when a lowercase letter is
found |/\l|, 3 when an uppercase letter is found |/\u|. found |/\l|, 3 when an uppercase letter is found |/\u|.
Can also be used as a |method|: >
GetPattern()->searchpos()
server2client({clientid}, {string}) *server2client()* server2client({clientid}, {string}) *server2client()*
Send a reply string to {clientid}. The most recent {clientid} Send a reply string to {clientid}. The most recent {clientid}
that sent a string can be retrieved with expand("<client>"). that sent a string can be retrieved with expand("<client>").
@@ -8494,6 +8517,9 @@ server2client({clientid}, {string}) *server2client()*
See also |clientserver|. See also |clientserver|.
Example: > Example: >
:echo server2client(expand("<client>"), "HELLO") :echo server2client(expand("<client>"), "HELLO")
< Can also be used as a |method|: >
GetClientId()->server2client(string)
< <
serverlist() *serverlist()* serverlist() *serverlist()*
Returns a list of server addresses, or empty if all servers Returns a list of server addresses, or empty if all servers
@@ -8559,6 +8585,9 @@ setbufline({buf}, {lnum}, {text}) *setbufline()*
If {buf} is not a valid buffer or {lnum} is not valid, an If {buf} is not a valid buffer or {lnum} is not valid, an
error message is given. error message is given.
Can also be used as a |method|: >
GetText()->setbufline(buf, lnum)
setbufvar({buf}, {varname}, {val}) *setbufvar()* setbufvar({buf}, {varname}, {val}) *setbufvar()*
Set option or local variable {varname} in buffer {buf} to Set option or local variable {varname} in buffer {buf} to
{val}. {val}.
@@ -8573,6 +8602,9 @@ setbufvar({buf}, {varname}, {val}) *setbufvar()*
:call setbufvar("todo", "myvar", "foobar") :call setbufvar("todo", "myvar", "foobar")
< This function is not available in the |sandbox|. < This function is not available in the |sandbox|.
Can also be used as a |method|: >
GetValue()->setbufvar(buf, varname)
setcharsearch({dict}) *setcharsearch()* setcharsearch({dict}) *setcharsearch()*
Set the current character search information to {dict}, Set the current character search information to {dict},
which contains one or more of the following entries: which contains one or more of the following entries:
@@ -8593,6 +8625,9 @@ setcharsearch({dict}) *setcharsearch()*
:call setcharsearch(prevsearch) :call setcharsearch(prevsearch)
< Also see |getcharsearch()|. < Also see |getcharsearch()|.
Can also be used as a |method|: >
SavedSearch()->setcharsearch()
setcmdpos({pos}) *setcmdpos()* setcmdpos({pos}) *setcmdpos()*
Set the cursor position in the command line to byte position Set the cursor position in the command line to byte position
{pos}. The first position is 1. {pos}. The first position is 1.
@@ -8608,6 +8643,9 @@ setcmdpos({pos}) *setcmdpos()*
Returns FALSE when successful, TRUE when not editing the Returns FALSE when successful, TRUE when not editing the
command line. command line.
Can also be used as a |method|: >
GetPos()->setcmdpos()
setenv({name}, {val}) *setenv()* setenv({name}, {val}) *setenv()*
Set environment variable {name} to {val}. Example: > Set environment variable {name} to {val}. Example: >
call setenv('HOME', '/home/myhome') call setenv('HOME', '/home/myhome')
@@ -8615,6 +8653,9 @@ setenv({name}, {val}) *setenv()*
< When {val} is |v:null| the environment variable is deleted. < When {val} is |v:null| the environment variable is deleted.
See also |expr-env|. See also |expr-env|.
Can also be used as a |method|, passing the value as the base: >
GetPath()->setenv('PATH')
setfperm({fname}, {mode}) *setfperm()* *chmod* setfperm({fname}, {mode}) *setfperm()* *chmod*
Set the file permissions for {fname} to {mode}. Set the file permissions for {fname} to {mode}.
{mode} must be a string with 9 characters. It is of the form {mode} must be a string with 9 characters. It is of the form
@@ -8660,6 +8701,9 @@ setline({lnum}, {text}) *setline()*
< Note: The '[ and '] marks are not set. < Note: The '[ and '] marks are not set.
Can also be used as a |method|, passing the text as the base: >
GetText()->setline(lnum)
setloclist({nr}, {list}[, {action}[, {what}]]) *setloclist()* setloclist({nr}, {list}[, {action}[, {what}]]) *setloclist()*
Create or replace or add to the location list for window {nr}. Create or replace or add to the location list for window {nr}.
{nr} can be the window number or the |window-ID|. {nr} can be the window number or the |window-ID|.

View File

@@ -291,29 +291,29 @@ return {
rpcstart={args={1, 2}}, rpcstart={args={1, 2}},
rpcstop={args=1}, rpcstop={args=1},
rubyeval={args=1, base=1}, rubyeval={args=1, base=1},
screenattr={args=2}, screenattr={args=2, base=1},
screenchar={args=2}, screenchar={args=2, base=1},
screenchars={args=2}, screenchars={args=2, base=1},
screencol={}, screencol={},
screenpos={args=3}, screenpos={args=3, base=1},
screenrow={}, screenrow={},
screenstring={args=2}, screenstring={args=2, base=1},
search={args={1, 4}}, search={args={1, 4}, base=1},
searchcount={args={0,1}}, searchcount={args={0,1}},
searchdecl={args={1, 3}}, searchdecl={args={1, 3}, base=1},
searchpair={args={3, 7}}, searchpair={args={3, 7}},
searchpairpos={args={3, 7}}, searchpairpos={args={3, 7}},
searchpos={args={1, 4}}, searchpos={args={1, 4}, base=1},
serverlist={}, serverlist={},
serverstart={args={0, 1}}, serverstart={args={0, 1}},
serverstop={args=1}, serverstop={args=1},
setbufline={args=3}, setbufline={args=3, base=3},
setbufvar={args=3}, setbufvar={args=3, base=3},
setcharsearch={args=1}, setcharsearch={args=1, base=1},
setcmdpos={args=1}, setcmdpos={args=1, base=1},
setenv={args=2}, setenv={args=2, base=2},
setfperm={args=2, base=1}, setfperm={args=2, base=1},
setline={args=2}, setline={args=2, base=2},
setloclist={args={2, 4}}, setloclist={args={2, 4}},
setmatches={args={1, 2}}, setmatches={args={1, 2}},
setpos={args=2}, setpos={args=2},

View File

@@ -20,7 +20,7 @@ func Test_charsearch()
" check that setcharsearch() changes the settings. " check that setcharsearch() changes the settings.
3 3
normal! ylfep normal! ylfep
call setcharsearch({'char': 'k'}) eval {'char': 'k'}->setcharsearch()
normal! ;p normal! ;p
call setcharsearch({'forward': 0}) call setcharsearch({'forward': 0})
normal! $;p normal! $;p

View File

@@ -82,7 +82,7 @@ func Test_client_server()
call remote_send(name, ":call server2client(expand('<client>'), 'got it')\<CR>", 'g:myserverid') call remote_send(name, ":call server2client(expand('<client>'), 'got it')\<CR>", 'g:myserverid')
call assert_equal('got it', g:myserverid->remote_read(2)) call assert_equal('got it', g:myserverid->remote_read(2))
call remote_send(name, ":call server2client(expand('<client>'), 'another')\<CR>", 'g:myserverid') call remote_send(name, ":eval expand('<client>')->server2client('another')\<CR>", 'g:myserverid')
let peek_result = 'nothing' let peek_result = 'nothing'
let r = g:myserverid->remote_peek('peek_result') let r = g:myserverid->remote_peek('peek_result')
" unpredictable whether the result is already available. " unpredictable whether the result is already available.

View File

@@ -905,7 +905,7 @@ func Test_setcmdpos()
call assert_equal('"12ab', @:) call assert_equal('"12ab', @:)
" setcmdpos() returns 1 when not editing the command line. " setcmdpos() returns 1 when not editing the command line.
call assert_equal(1, setcmdpos(3)) call assert_equal(1, 3->setcmdpos())
endfunc endfunc
func Test_cmdline_overstrike() func Test_cmdline_overstrike()

View File

@@ -86,7 +86,7 @@ func Test_screenpos()
call assert_equal({'row': winrow, call assert_equal({'row': winrow,
\ 'col': wincol + 13, \ 'col': wincol + 13,
\ 'curscol': wincol + 13, \ 'curscol': wincol + 13,
\ 'endcol': wincol + 13}, screenpos(winid, 1, 7)) \ 'endcol': wincol + 13}, winid->screenpos(1, 7))
call assert_equal({'row': winrow + 2, call assert_equal({'row': winrow + 2,
\ 'col': wincol + 1, \ 'col': wincol + 1,
\ 'curscol': wincol + 1, \ 'curscol': wincol + 1,

View File

@@ -387,7 +387,7 @@ func Test_diffoff()
call setline(1, ['One', '', 'Two', 'Three']) call setline(1, ['One', '', 'Two', 'Three'])
diffthis diffthis
redraw redraw
call assert_notequal(normattr, screenattr(1, 1)) call assert_notequal(normattr, 1->screenattr(1))
diffoff! diffoff!
redraw redraw
call assert_equal(normattr, screenattr(1, 1)) call assert_equal(normattr, screenattr(1, 1))

View File

@@ -22,7 +22,7 @@ endfunc
func Test_setenv() func Test_setenv()
unlet! $TESTENV unlet! $TESTENV
call setenv('TEST ENV', 'foo') eval 'foo'->setenv('TEST ENV')
call assert_equal('foo', getenv('TEST ENV')) call assert_equal('foo', getenv('TEST ENV'))
call setenv('TEST ENV', v:null) call setenv('TEST ENV', v:null)
call assert_equal(v:null, getenv('TEST ENV')) call assert_equal(v:null, getenv('TEST ENV'))

View File

@@ -346,7 +346,7 @@ func Test_setbufvar_options()
wincmd h wincmd h
let wh = winheight(0) let wh = winheight(0)
let dummy_buf = bufnr('dummy_buf2', v:true) let dummy_buf = bufnr('dummy_buf2', v:true)
call setbufvar(dummy_buf, '&buftype', 'nofile') eval 'nofile'->setbufvar(dummy_buf, '&buftype')
execute 'belowright vertical split #' . dummy_buf execute 'belowright vertical split #' . dummy_buf
call assert_equal(wh, winheight(0)) call assert_equal(wh, winheight(0))
@@ -1330,7 +1330,7 @@ func Test_func_range_with_edit()
" is invalid in that buffer. " is invalid in that buffer.
call writefile(['just one line'], 'Xfuncrange2') call writefile(['just one line'], 'Xfuncrange2')
new new
call setline(1, 10->range()) eval 10->range()->setline(1)
write Xfuncrange1 write Xfuncrange1
call assert_fails('5,8call EditAnotherFile()', 'E16:') call assert_fails('5,8call EditAnotherFile()', 'E16:')

View File

@@ -6,7 +6,7 @@ endif
function! s:screenline(lnum) abort function! s:screenline(lnum) abort
let line = [] let line = []
for c in range(1, winwidth(0)) for c in range(1, winwidth(0))
call add(line, nr2char(screenchar(a:lnum, c))) call add(line, nr2char(a:lnum->screenchar(c)))
endfor endfor
return s:trim(join(line, '')) return s:trim(join(line, ''))
endfunction endfunction

View File

@@ -1315,7 +1315,7 @@ func Test_search_match_at_curpos()
normal gg normal gg
call search('foobar', 'c') eval 'foobar'->search('c')
call assert_equal([1, 1], [line('.'), col('.')]) call assert_equal([1, 1], [line('.'), col('.')])
normal j normal j
@@ -1354,6 +1354,41 @@ func Test_search_display_pattern()
endif endif
endfunc endfunc
func Test_searchdecl()
let lines =<< trim END
int global;
func()
{
int global;
if (cond) {
int local;
}
int local;
// comment
}
END
new
call setline(1, lines)
10
call assert_equal(0, searchdecl('local', 0, 0))
call assert_equal(7, getcurpos()[1])
10
call assert_equal(0, 'local'->searchdecl(0, 1))
call assert_equal(9, getcurpos()[1])
10
call assert_equal(0, searchdecl('global'))
call assert_equal(5, getcurpos()[1])
10
call assert_equal(0, searchdecl('global', 1))
call assert_equal(1, getcurpos()[1])
bwipe!
endfunc
func Test_search_special() func Test_search_special()
" this was causing illegal memory access and an endless loop " this was causing illegal memory access and an endless loop
set t_PE= set t_PE=

View File

@@ -69,7 +69,7 @@ func Test_screenchar_utf8()
call setline(1, ["ABC\u0308"]) call setline(1, ["ABC\u0308"])
redraw redraw
call assert_equal([0x0041], screenchars(1, 1)) call assert_equal([0x0041], screenchars(1, 1))
call assert_equal([0x0042], screenchars(1, 2)) call assert_equal([0x0042], 1->screenchars(2))
call assert_equal([0x0043, 0x0308], screenchars(1, 3)) call assert_equal([0x0043, 0x0308], screenchars(1, 3))
call assert_equal("A", screenstring(1, 1)) call assert_equal("A", screenstring(1, 1))
call assert_equal("B", screenstring(1, 2)) call assert_equal("B", screenstring(1, 2))

View File

@@ -17,7 +17,7 @@ describe('searchpos', function()
call('cursor', 1, 1) call('cursor', 1, 1)
eq({1, 1, 2}, eval([[searchpos('\%(\([a-z]\)\|\_.\)\{-}xyz', 'pcW')]])) eq({1, 1, 2}, eval([[searchpos('\%(\([a-z]\)\|\_.\)\{-}xyz', 'pcW')]]))
call('cursor', 1, 2) call('cursor', 1, 2)
eq({2, 1, 1}, eval([[searchpos('\%(\([a-z]\)\|\_.\)\{-}xyz', 'pcW')]])) eq({2, 1, 1}, eval([['\%(\([a-z]\)\|\_.\)\{-}xyz'->searchpos('pcW')]]))
command('set cpo-=c') command('set cpo-=c')
call('cursor', 1, 2) call('cursor', 1, 2)