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

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

+sound is needed for sound_* functions.

Make swapinfo and swapname take exactly one argument.
Previously, they could erroneously take one or more.
This commit is contained in:
Sean Dewar
2021-10-31 19:04:28 +00:00
parent 2ee0bc09d9
commit 0f4510cb1a
9 changed files with 82 additions and 39 deletions

View File

@@ -9254,6 +9254,9 @@ soundfold({word})
This can be used for making spelling suggestions. Note that This can be used for making spelling suggestions. Note that
the method can be quite slow. the method can be quite slow.
Can also be used as a |method|: >
GetWord()->soundfold()
<
*spellbadword()* *spellbadword()*
spellbadword([{sentence}]) spellbadword([{sentence}])
Without argument: The result is the badly spelled word under Without argument: The result is the badly spelled word under
@@ -9279,6 +9282,9 @@ spellbadword([{sentence}])
The spelling information for the current window and the value The spelling information for the current window and the value
of 'spelllang' are used. of 'spelllang' are used.
Can also be used as a |method|: >
GetText()->spellbadword()
<
*spellsuggest()* *spellsuggest()*
spellsuggest({word} [, {max} [, {capital}]]) spellsuggest({word} [, {max} [, {capital}]])
Return a |List| with spelling suggestions to replace {word}. Return a |List| with spelling suggestions to replace {word}.
@@ -9301,6 +9307,8 @@ spellsuggest({word} [, {max} [, {capital}]])
The spelling information for the current window is used. The The spelling information for the current window is used. The
values of 'spelllang' and 'spellsuggest' are used. values of 'spelllang' and 'spellsuggest' are used.
Can also be used as a |method|: >
GetWord()->spellsuggest()
split({string} [, {pattern} [, {keepempty}]]) *split()* split({string} [, {pattern} [, {keepempty}]]) *split()*
Make a |List| out of {string}. When {pattern} is omitted or Make a |List| out of {string}. When {pattern} is omitted or
@@ -9432,6 +9440,20 @@ str2nr({string} [, {base}]) *str2nr()*
{base} is 2 a leading "0b" or "0B" is ignored. {base} is 2 a leading "0b" or "0B" is ignored.
Text after the number is silently ignored. Text after the number is silently ignored.
Can also be used as a |method|: >
GetText()->str2nr()
strcharpart({src}, {start} [, {len}]) *strcharpart()*
Like |strpart()| but using character index and length instead
of byte index and length. Composing characters are counted
separately.
When a character index is used where a character does not
exist it is assumed to be one character. For example: >
strcharpart('abc', -1, 2)
< results in 'a'.
Can also be used as a |method|: >
GetText()->strcharpart(5)
strchars({string} [, {skipcc}]) *strchars()* strchars({string} [, {skipcc}]) *strchars()*
The result is a Number, which is the number of characters The result is a Number, which is the number of characters
@@ -9457,14 +9479,8 @@ strchars({string} [, {skipcc}]) *strchars()*
endfunction endfunction
endif endif
< <
strcharpart({src}, {start} [, {len}]) *strcharpart()* Can also be used as a |method|: >
Like |strpart()| but using character index and length instead GetText()->strchars()
of byte index and length. Composing characters are counted
separately.
When a character index is used where a character does not
exist it is assumed to be one character. For example: >
strcharpart('abc', -1, 2)
< results in 'a'.
strdisplaywidth({string} [, {col}]) *strdisplaywidth()* strdisplaywidth({string} [, {col}]) *strdisplaywidth()*
The result is a Number, which is the number of display cells The result is a Number, which is the number of display cells
@@ -9479,6 +9495,9 @@ strdisplaywidth({string} [, {col}]) *strdisplaywidth()*
Ambiguous, this function's return value depends on 'ambiwidth'. Ambiguous, this function's return value depends on 'ambiwidth'.
Also see |strlen()|, |strwidth()| and |strchars()|. Also see |strlen()|, |strwidth()| and |strchars()|.
Can also be used as a |method|: >
GetText()->strdisplaywidth()
strftime({format} [, {time}]) *strftime()* strftime({format} [, {time}]) *strftime()*
The result is a String, which is a formatted date and time, as The result is a String, which is a formatted date and time, as
specified by the {format} string. The given {time} is used, specified by the {format} string. The given {time} is used,
@@ -9496,12 +9515,18 @@ strftime({format} [, {time}]) *strftime()*
:echo strftime("%c", getftime("file.c")) :echo strftime("%c", getftime("file.c"))
Show mod time of file.c. Show mod time of file.c.
< Can also be used as a |method|: >
GetFormat()->strftime()
strgetchar({str}, {index}) *strgetchar()* strgetchar({str}, {index}) *strgetchar()*
Get character {index} from {str}. This uses a character Get character {index} from {str}. This uses a character
index, not a byte index. Composing characters are considered index, not a byte index. Composing characters are considered
separate characters here. separate characters here.
Also see |strcharpart()| and |strchars()|. Also see |strcharpart()| and |strchars()|.
Can also be used as a |method|: >
GetText()->strgetchar(5)
stridx({haystack}, {needle} [, {start}]) *stridx()* stridx({haystack}, {needle} [, {start}]) *stridx()*
The result is a Number, which gives the byte index in The result is a Number, which gives the byte index in
{haystack} of the first occurrence of the String {needle}. {haystack} of the first occurrence of the String {needle}.
@@ -9521,6 +9546,9 @@ stridx({haystack}, {needle} [, {start}]) *stridx()*
stridx() works similar to the C function strstr(). When used stridx() works similar to the C function strstr(). When used
with a single character it works similar to strchr(). with a single character it works similar to strchr().
Can also be used as a |method|: >
GetHaystack()->stridx(needle)
*string()* *string()*
string({expr}) Return {expr} converted to a String. If {expr} is a Number, string({expr}) Return {expr} converted to a String. If {expr} is a Number,
Float, String, Blob or a composition of them, then the result Float, String, Blob or a composition of them, then the result
@@ -9583,6 +9611,9 @@ strpart({src}, {start} [, {len} [, {chars}]]) *strpart()*
example, to get the character under the cursor: > example, to get the character under the cursor: >
strpart(getline("."), col(".") - 1, 1, v:true) strpart(getline("."), col(".") - 1, 1, v:true)
< <
Can also be used as a |method|: >
GetText()->strpart(5)
strptime({format}, {timestring}) *strptime()* strptime({format}, {timestring}) *strptime()*
The result is a Number, which is a unix timestamp representing The result is a Number, which is a unix timestamp representing
the date and time in {timestring}, which is expected to match the date and time in {timestring}, which is expected to match
@@ -9626,6 +9657,9 @@ strridx({haystack}, {needle} [, {start}]) *strridx()*
When used with a single character it works similar to the C When used with a single character it works similar to the C
function strrchr(). function strrchr().
Can also be used as a |method|: >
GetHaystack()->strridx(needle)
strtrans({string}) *strtrans()* strtrans({string}) *strtrans()*
The result is a String, which is {string} with all unprintable The result is a String, which is {string} with all unprintable
characters translated into printable characters |'isprint'|. characters translated into printable characters |'isprint'|.
@@ -9674,6 +9708,9 @@ submatch({nr} [, {list}]) *submatch()* *E935*
< This finds the first number in the line and adds one to it. < This finds the first number in the line and adds one to it.
A line break is included as a newline character. A line break is included as a newline character.
Can also be used as a |method|: >
GetNr()->submatch()
substitute({string}, {pat}, {sub}, {flags}) *substitute()* substitute({string}, {pat}, {sub}, {flags}) *substitute()*
The result is a String, which is a copy of {string}, in which The result is a String, which is a copy of {string}, in which
the first match of {pat} is replaced with {sub}. the first match of {pat} is replaced with {sub}.
@@ -9736,6 +9773,9 @@ swapinfo({fname}) *swapinfo()*
Not a swap file: does not contain correct block ID Not a swap file: does not contain correct block ID
Magic number mismatch: Info in first block is invalid Magic number mismatch: Info in first block is invalid
Can also be used as a |method|: >
GetFilename()->swapinfo()
swapname({buf}) *swapname()* swapname({buf}) *swapname()*
The result is the swap file path of the buffer {buf}. The result is the swap file path of the buffer {buf}.
For the use of {buf}, see |bufname()| above. For the use of {buf}, see |bufname()| above.
@@ -9743,6 +9783,9 @@ swapname({buf}) *swapname()*
|:swapname| (unless there is no swap file). |:swapname| (unless there is no swap file).
If buffer {buf} has no swap file, returns an empty string. If buffer {buf} has no swap file, returns an empty string.
Can also be used as a |method|: >
GetBufname()->swapname()
synID({lnum}, {col}, {trans}) *synID()* synID({lnum}, {col}, {trans}) *synID()*
The result is a Number, which is the syntax ID at the position The result is a Number, which is the syntax ID at the position
{lnum} and {col} in the current window. {lnum} and {col} in the current window.

View File

@@ -340,33 +340,33 @@ return {
sinh={args=1, base=1, func="float_op_wrapper", data="&sinh"}, sinh={args=1, base=1, func="float_op_wrapper", data="&sinh"},
sockconnect={args={2,3}}, sockconnect={args={2,3}},
sort={args={1, 3}, base=1}, sort={args={1, 3}, base=1},
soundfold={args=1}, soundfold={args=1, base=1},
stdioopen={args=1}, stdioopen={args=1},
spellbadword={args={0, 1}}, spellbadword={args={0, 1}, base=1},
spellsuggest={args={1, 3}}, spellsuggest={args={1, 3}, base=1},
split={args={1, 3}, base=1}, split={args={1, 3}, base=1},
sqrt={args=1, base=1, func="float_op_wrapper", data="&sqrt"}, sqrt={args=1, base=1, func="float_op_wrapper", data="&sqrt"},
stdpath={args=1}, stdpath={args=1},
str2float={args=1, base=1}, str2float={args=1, base=1},
str2list={args={1, 2}, base=1}, str2list={args={1, 2}, base=1},
str2nr={args={1, 3}}, str2nr={args={1, 3}, base=1},
strcharpart={args={2, 3}}, strcharpart={args={2, 3}, base=1},
strchars={args={1,2}}, strchars={args={1, 2}, base=1},
strdisplaywidth={args={1, 2}}, strdisplaywidth={args={1, 2}, base=1},
strftime={args={1, 2}}, strftime={args={1, 2}, base=1},
strgetchar={args={2, 2}}, strgetchar={args=2, base=1},
stridx={args={2, 3}}, stridx={args={2, 3}, base=1},
string={args=1, base=1}, string={args=1, base=1},
strlen={args=1, base=1}, strlen={args=1, base=1},
strpart={args={2, 4}}, strpart={args={2, 4}, base=1},
strptime={args=2}, strptime={args=2},
strridx={args={2, 3}}, strridx={args={2, 3}, base=1},
strtrans={args=1, base=1}, strtrans={args=1, base=1},
strwidth={args=1, base=1}, strwidth={args=1, base=1},
submatch={args={1, 2}}, submatch={args={1, 2}, base=1},
substitute={args=4, base=1}, substitute={args=4, base=1},
swapinfo={args={1}}, swapinfo={args=1, base=1},
swapname={args={1}}, swapname={args=1, base=1},
synID={args=3}, synID={args=3},
synIDattr={args={2, 3}, base=1}, synIDattr={args={2, 3}, base=1},
synIDtrans={args=1, base=1}, synIDtrans={args=1, base=1},

View File

@@ -432,7 +432,7 @@ func Test_breakindent11_vartabs()
call s:test_windows('setl cpo-=n sbr=>> nu nuw=4 nolist briopt= ts=4 vts=4') call s:test_windows('setl cpo-=n sbr=>> nu nuw=4 nolist briopt= ts=4 vts=4')
let text = getline(2) let text = getline(2)
let width = strlen(text[1:]) + 2->indent() + strlen(&sbr) * 3 " text wraps 3 times let width = strlen(text[1:]) + 2->indent() + strlen(&sbr) * 3 " text wraps 3 times
call assert_equal(width, strdisplaywidth(text)) call assert_equal(width, text->strdisplaywidth())
call s:close_windows('set sbr= vts&') call s:close_windows('set sbr= vts&')
endfunc endfunc

View File

@@ -56,7 +56,7 @@ endfunc
func Test_strgetchar() func Test_strgetchar()
call assert_equal(char2nr('a'), strgetchar('axb', 0)) call assert_equal(char2nr('a'), strgetchar('axb', 0))
call assert_equal(char2nr('x'), strgetchar('axb', 1)) call assert_equal(char2nr('x'), 'axb'->strgetchar(1))
call assert_equal(char2nr('b'), strgetchar('axb', 2)) call assert_equal(char2nr('b'), strgetchar('axb', 2))
call assert_equal(-1, strgetchar('axb', -1)) call assert_equal(-1, strgetchar('axb', -1))
@@ -66,7 +66,7 @@ endfunc
func Test_strcharpart() func Test_strcharpart()
call assert_equal('a', strcharpart('axb', 0, 1)) call assert_equal('a', strcharpart('axb', 0, 1))
call assert_equal('x', strcharpart('axb', 1, 1)) call assert_equal('x', 'axb'->strcharpart(1, 1))
call assert_equal('b', strcharpart('axb', 2, 1)) call assert_equal('b', strcharpart('axb', 2, 1))
call assert_equal('xb', strcharpart('axb', 1)) call assert_equal('xb', strcharpart('axb', 1))

View File

@@ -143,7 +143,7 @@ func Test_str2nr()
call assert_equal(-123456789, str2nr('-123456789')) call assert_equal(-123456789, str2nr('-123456789'))
call assert_equal(5, str2nr('101', 2)) call assert_equal(5, str2nr('101', 2))
call assert_equal(5, str2nr('0b101', 2)) call assert_equal(5, '0b101'->str2nr(2))
call assert_equal(5, str2nr('0B101', 2)) call assert_equal(5, str2nr('0B101', 2))
call assert_equal(-5, str2nr('-101', 2)) call assert_equal(-5, str2nr('-101', 2))
call assert_equal(-5, str2nr('-0b101', 2)) call assert_equal(-5, str2nr('-0b101', 2))
@@ -200,7 +200,7 @@ func Test_strftime()
" of strftime() can be 17 or 18, depending on timezone. " of strftime() can be 17 or 18, depending on timezone.
call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512)) call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
" "
call assert_match('^\d\d\d\d-\(0\d\|1[012]\)-\([012]\d\|3[01]\) \([01]\d\|2[0-3]\):[0-5]\d:\([0-5]\d\|60\)$', strftime('%Y-%m-%d %H:%M:%S')) call assert_match('^\d\d\d\d-\(0\d\|1[012]\)-\([012]\d\|3[01]\) \([01]\d\|2[0-3]\):[0-5]\d:\([0-5]\d\|60\)$', '%Y-%m-%d %H:%M:%S'->strftime())
call assert_fails('call strftime([])', 'E730:') call assert_fails('call strftime([])', 'E730:')
call assert_fails('call strftime("%Y", [])', 'E745:') call assert_fails('call strftime("%Y", [])', 'E745:')
@@ -375,7 +375,7 @@ endfunc
func Test_strpart() func Test_strpart()
call assert_equal('de', strpart('abcdefg', 3, 2)) call assert_equal('de', strpart('abcdefg', 3, 2))
call assert_equal('ab', strpart('abcdefg', -2, 4)) call assert_equal('ab', strpart('abcdefg', -2, 4))
call assert_equal('abcdefg', strpart('abcdefg', -2)) call assert_equal('abcdefg', 'abcdefg'->strpart(-2))
call assert_equal('fg', strpart('abcdefg', 5, 4)) call assert_equal('fg', strpart('abcdefg', 5, 4))
call assert_equal('defg', strpart('abcdefg', 3)) call assert_equal('defg', strpart('abcdefg', 3))
@@ -809,11 +809,11 @@ endfunc
func Test_stridx() func Test_stridx()
call assert_equal(-1, stridx('', 'l')) call assert_equal(-1, stridx('', 'l'))
call assert_equal(0, stridx('', '')) call assert_equal(0, stridx('', ''))
call assert_equal(0, stridx('hello', '')) call assert_equal(0, 'hello'->stridx(''))
call assert_equal(-1, stridx('hello', 'L')) call assert_equal(-1, stridx('hello', 'L'))
call assert_equal(2, stridx('hello', 'l', -1)) call assert_equal(2, stridx('hello', 'l', -1))
call assert_equal(2, stridx('hello', 'l', 0)) call assert_equal(2, stridx('hello', 'l', 0))
call assert_equal(2, stridx('hello', 'l', 1)) call assert_equal(2, 'hello'->stridx('l', 1))
call assert_equal(3, stridx('hello', 'l', 3)) call assert_equal(3, stridx('hello', 'l', 3))
call assert_equal(-1, stridx('hello', 'l', 4)) call assert_equal(-1, stridx('hello', 'l', 4))
call assert_equal(-1, stridx('hello', 'l', 10)) call assert_equal(-1, stridx('hello', 'l', 10))
@@ -826,7 +826,7 @@ func Test_strridx()
call assert_equal(0, strridx('', '')) call assert_equal(0, strridx('', ''))
call assert_equal(5, strridx('hello', '')) call assert_equal(5, strridx('hello', ''))
call assert_equal(-1, strridx('hello', 'L')) call assert_equal(-1, strridx('hello', 'L'))
call assert_equal(3, strridx('hello', 'l')) call assert_equal(3, 'hello'->strridx('l'))
call assert_equal(3, strridx('hello', 'l', 10)) call assert_equal(3, strridx('hello', 'l', 10))
call assert_equal(3, strridx('hello', 'l', 3)) call assert_equal(3, strridx('hello', 'l', 3))
call assert_equal(2, strridx('hello', 'l', 2)) call assert_equal(2, strridx('hello', 'l', 2))

View File

@@ -77,7 +77,7 @@ func Test_spellbadword()
set spell set spell
call assert_equal(['bycycle', 'bad'], spellbadword('My bycycle.')) call assert_equal(['bycycle', 'bad'], spellbadword('My bycycle.'))
call assert_equal(['another', 'caps'], spellbadword('A sentence. another sentence')) call assert_equal(['another', 'caps'], 'A sentence. another sentence'->spellbadword())
call assert_equal(['TheCamelWord', 'bad'], spellbadword('TheCamelWord asdf')) call assert_equal(['TheCamelWord', 'bad'], spellbadword('TheCamelWord asdf'))
set spelloptions=camel set spelloptions=camel
@@ -407,7 +407,7 @@ func Test_zz_basic()
\ ) \ )
call assert_equal("gebletegek", soundfold('goobledygoook')) call assert_equal("gebletegek", soundfold('goobledygoook'))
call assert_equal("kepereneven", soundfold('kóopërÿnôven')) call assert_equal("kepereneven", 'kóopërÿnôven'->soundfold())
call assert_equal("everles gesvets etele", soundfold('oeverloos gezwets edale')) call assert_equal("everles gesvets etele", soundfold('oeverloos gezwets edale'))
endfunc endfunc
@@ -711,7 +711,7 @@ func TestGoodBadBase()
break break
endif endif
let prevbad = bad let prevbad = bad
let lst = spellsuggest(bad, 3) let lst = bad->spellsuggest(3)
normal mm normal mm
call add(result, [bad, lst]) call add(result, [bad, lst])

View File

@@ -547,7 +547,7 @@ func Test_sub_replace_5()
\ substitute('A123456789', \ substitute('A123456789',
\ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', \ 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
\ '\=string([submatch(0, 1), submatch(9, 1), ' . \ '\=string([submatch(0, 1), submatch(9, 1), ' .
\ 'submatch(8, 1), submatch(7, 1), submatch(6, 1), ' . \ 'submatch(8, 1), 7->submatch(1), submatch(6, 1), ' .
\ 'submatch(5, 1), submatch(4, 1), submatch(3, 1), ' . \ 'submatch(5, 1), submatch(4, 1), submatch(3, 1), ' .
\ 'submatch(2, 1), submatch(1, 1)])', \ 'submatch(2, 1), submatch(1, 1)])',
\ '')) \ ''))

View File

@@ -113,7 +113,7 @@ func Test_swapinfo()
w w
let fname = s:swapname() let fname = s:swapname()
call assert_match('Xswapinfo', fname) call assert_match('Xswapinfo', fname)
let info = swapinfo(fname) let info = fname->swapinfo()
let ver = printf('VIM %d.%d', v:version / 100, v:version % 100) let ver = printf('VIM %d.%d', v:version / 100, v:version % 100)
call assert_equal(ver, info.version) call assert_equal(ver, info.version)
@@ -155,7 +155,7 @@ func Test_swapname()
let buf = bufnr('%') let buf = bufnr('%')
let expected = s:swapname() let expected = s:swapname()
wincmd p wincmd p
call assert_equal(expected, swapname(buf)) call assert_equal(expected, buf->swapname())
new Xtest3 new Xtest3
setlocal noswapfile setlocal noswapfile

View File

@@ -17,7 +17,7 @@ func Test_strchars()
let exp = [[1, 1, 1], [3, 3, 3], [2, 2, 1], [3, 3, 1], [1, 1, 1]] let exp = [[1, 1, 1], [3, 3, 3], [2, 2, 1], [3, 3, 1], [1, 1, 1]]
for i in range(len(inp)) for i in range(len(inp))
call assert_equal(exp[i][0], strchars(inp[i])) call assert_equal(exp[i][0], strchars(inp[i]))
call assert_equal(exp[i][1], strchars(inp[i], 0)) call assert_equal(exp[i][1], inp[i]->strchars(0))
call assert_equal(exp[i][2], strchars(inp[i], 1)) call assert_equal(exp[i][2], strchars(inp[i], 1))
endfor endfor
endfunc endfunc