Skip to content

Commit a74e494

Browse files
committed
patch 8.1.1809: more functions can be used as a method
Problem: More functions can be used as a method. Solution: Add has_key(), split(), str2list(), etc.
1 parent e4ce825 commit a74e494

File tree

7 files changed

+76
-13
lines changed

7 files changed

+76
-13
lines changed

runtime/doc/eval.txt

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3575,9 +3575,10 @@ count({comp}, {expr} [, {ic} [, {start}]]) *count()*
35753575
When {comp} is a string then the number of not overlapping
35763576
occurrences of {expr} is returned. Zero is returned when
35773577
{expr} is an empty string.
3578+
35783579
Can also be used as a |method|: >
35793580
mylist->count(val)
3580-
3581+
<
35813582
*cscope_connection()*
35823583
cscope_connection([{num} , {dbpath} [, {prepend}]])
35833584
Checks for the existence of a |cscope| connection. If no
@@ -5408,6 +5409,9 @@ has_key({dict}, {key}) *has_key()*
54085409
The result is a Number, which is 1 if |Dictionary| {dict} has
54095410
an entry with key {key}. Zero otherwise.
54105411

5412+
Can also be used as a |method|: >
5413+
mydict->has_key(key)
5414+
54115415
haslocaldir([{winnr} [, {tabnr}]]) *haslocaldir()*
54125416
The result is a Number:
54135417
1 when the window has set a local directory via |:lcd|
@@ -8294,6 +8298,8 @@ split({expr} [, {pattern} [, {keepempty}]]) *split()*
82948298
:let items = split(line, ':', 1)
82958299
< The opposite function is |join()|.
82968300

8301+
Can also be used as a |method|: >
8302+
GetString()->split()
82978303
82988304
sqrt({expr}) *sqrt()*
82998305
Return the non-negative square root of Float {expr} as a
@@ -8337,12 +8343,19 @@ str2list({expr} [, {utf8}]) *str2list()*
83378343
properly: >
83388344
str2list("á") returns [97, 769]
83398345
8346+
< Can also be used as a |method|: >
8347+
GetString()->str2list()
8348+
8349+
83408350
str2nr({expr} [, {base}]) *str2nr()*
83418351
Convert string {expr} to a number.
83428352
{base} is the conversion base, it can be 2, 8, 10 or 16.
8353+
83438354
When {base} is omitted base 10 is used. This also means that
83448355
a leading zero doesn't cause octal conversion to be used, as
8345-
with the default String to Number conversion.
8356+
with the default String to Number conversion. Example: >
8357+
let nr = str2nr('123')
8358+
<
83468359
When {base} is 16 a leading "0x" or "0X" is ignored. With a
83478360
different base the result will be zero. Similarly, when
83488361
{base} is 8 a leading "0" is ignored, and when {base} is 2 a
@@ -8470,6 +8483,9 @@ strlen({expr}) The result is a Number, which is the length of the String
84708483
|strchars()|.
84718484
Also see |len()|, |strdisplaywidth()| and |strwidth()|.
84728485

8486+
Can also be used as a |method|: >
8487+
GetString()->strlen()
8488+
84738489
strpart({src}, {start} [, {len}]) *strpart()*
84748490
The result is a String, which is part of {src}, starting from
84758491
byte {start}, with the byte length {len}.
@@ -8514,6 +8530,9 @@ strtrans({expr}) *strtrans()*
85148530
< This displays a newline in register a as "^@" instead of
85158531
starting a new line.
85168532

8533+
Can also be used as a |method|: >
8534+
GetString()->strtrans()
8535+
85178536
strwidth({expr}) *strwidth()*
85188537
The result is a Number, which is the number of display cells
85198538
String {expr} occupies. A Tab character is counted as one
@@ -8522,6 +8541,9 @@ strwidth({expr}) *strwidth()*
85228541
Ambiguous, this function's return value depends on 'ambiwidth'.
85238542
Also see |strlen()|, |strdisplaywidth()| and |strchars()|.
85248543

8544+
Can also be used as a |method|: >
8545+
GetString()->strwidth()
8546+
85258547
submatch({nr} [, {list}]) *submatch()* *E935*
85268548
Only for an expression in a |:substitute| command or
85278549
substitute() function.
@@ -8589,6 +8611,9 @@ substitute({expr}, {pat}, {sub}, {flags}) *substitute()*
85898611
|submatch()| returns. Example: >
85908612
:echo substitute(s, '%\(\x\x\)', {m -> '0x' . m[1]}, 'g')
85918613
8614+
< Can also be used as a |method|: >
8615+
GetString()->substitute(pat, sub, flags)
8616+
85928617
swapinfo({fname}) *swapinfo()*
85938618
The result is a dictionary, which holds information about the
85948619
swapfile {fname}. The available fields are:
@@ -8674,12 +8699,19 @@ synIDattr({synID}, {what} [, {mode}]) *synIDattr()*
86748699
cursor): >
86758700
:echo synIDattr(synIDtrans(synID(line("."), col("."), 1)), "fg")
86768701
<
8702+
Can also be used as a |method|: >
8703+
:echo synID(line("."), col("."), 1)->synIDtrans()->synIDattr("fg")
8704+
8705+
86778706
synIDtrans({synID}) *synIDtrans()*
86788707
The result is a Number, which is the translated syntax ID of
86798708
{synID}. This is the syntax group ID of what is being used to
86808709
highlight the character. Highlight links given with
86818710
":highlight link" are followed.
86828711

8712+
Can also be used as a |method|: >
8713+
:echo synID(line("."), col("."), 1)->synIDtrans()->synIDattr("fg")
8714+
86838715
synconcealed({lnum}, {col}) *synconcealed()*
86848716
The result is a List with currently three items:
86858717
1. The first item in the list is 0 if the character at the
@@ -8784,6 +8816,9 @@ system({expr} [, {input}]) *system()* *E677*
87848816
Unlike ":!cmd" there is no automatic check for changed files.
87858817
Use |:checktime| to force a check.
87868818

8819+
Can also be used as a |method|: >
8820+
:echo GetCmd()->system()
8821+
87878822
87888823
systemlist({expr} [, {input}]) *systemlist()*
87898824
Same as |system()|, but returns a |List| with lines (parts of
@@ -8794,6 +8829,9 @@ systemlist({expr} [, {input}]) *systemlist()*
87948829

87958830
Returns an empty string on error.
87968831

8832+
Can also be used as a |method|: >
8833+
:echo GetCmd()->systemlist()
8834+
87978835
87988836
tabpagebuflist([{arg}]) *tabpagebuflist()*
87998837
The result is a |List|, where each item is the number of the

src/evalfunc.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,8 @@ static funcentry_T global_functions[] =
995995
#define FEARG_2 2 // base is the second argument
996996

997997
/*
998-
* Methods that call the internal function with the base as the first argument.
998+
* Methods that call the internal function with the base as one of the
999+
* arguments.
9991000
*/
10001001
static funcentry_T base_methods[] =
10011002
{
@@ -1011,6 +1012,7 @@ static funcentry_T base_methods[] =
10111012
{"extend", 1, 2, 0, f_extend},
10121013
{"filter", 1, 1, 0, f_filter},
10131014
{"get", 1, 2, 0, f_get},
1015+
{"has_key", 1, 1, 0, f_has_key},
10141016
{"index", 1, 3, 0, f_index},
10151017
{"insert", 1, 2, 0, f_insert},
10161018
{"items", 0, 0, 0, f_items},
@@ -1024,7 +1026,17 @@ static funcentry_T base_methods[] =
10241026
{"repeat", 1, 1, 0, f_repeat},
10251027
{"reverse", 0, 0, 0, f_reverse},
10261028
{"sort", 0, 2, 0, f_sort},
1029+
{"split", 0, 2, 0, f_split},
1030+
{"str2list", 0, 1, 0, f_str2list},
10271031
{"string", 0, 0, 0, f_string},
1032+
{"strlen", 0, 0, 0, f_strlen},
1033+
{"strtrans", 0, 0, 0, f_strtrans},
1034+
{"strwidth", 0, 0, 0, f_strwidth},
1035+
{"substitute", 3, 3, 0, f_substitute},
1036+
{"synIDattr", 1, 2, 0, f_synIDattr},
1037+
{"synIDtrans", 0, 0, 0, f_synIDtrans},
1038+
{"system", 0, 1, 0, f_system},
1039+
{"systemlist", 0, 2, 0, f_systemlist},
10281040
{"type", 0, 0, 0, f_type},
10291041
{"uniq", 0, 2, 0, f_uniq},
10301042
{"values", 0, 0, 0, f_values},

src/testdir/test_diffmode.vim

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -668,13 +668,13 @@ func Test_diff_hlID()
668668
diffthis
669669
redraw
670670

671-
call assert_equal(synIDattr(diff_hlID(-1, 1), "name"), "")
671+
call diff_hlID(-1, 1)->synIDattr("name")->assert_equal("")
672672

673-
call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
674-
call assert_equal(synIDattr(diff_hlID(1, 2), "name"), "DiffText")
675-
call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "")
676-
call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "DiffAdd")
677-
call assert_equal(synIDattr(diff_hlID(4, 1), "name"), "")
673+
call diff_hlID(1, 1)->synIDattr("name")->assert_equal("DiffChange")
674+
call diff_hlID(1, 2)->synIDattr("name")->assert_equal("DiffText")
675+
call diff_hlID(2, 1)->synIDattr("name")->assert_equal("")
676+
call diff_hlID(3, 1)->synIDattr("name")->assert_equal("DiffAdd")
677+
call diff_hlID(4, 1)->synIDattr("name")->assert_equal("")
678678

679679
wincmd w
680680
call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")

src/testdir/test_method.vim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func Test_dict()
4646
call assert_equal(2, d->get('two'))
4747
call assert_fails("let x = d->index(2)", 'E897:')
4848
call assert_fails("let x = d->insert(0)", 'E899:')
49+
call assert_true(d->has_key('two'))
4950
call assert_equal([['one', 1], ['two', 2], ['three', 3]], d->items())
5051
call assert_fails("let x = d->join()", 'E714:')
5152
call assert_equal(['one', 'two', 'three'], d->keys())
@@ -65,6 +66,16 @@ func Test_dict()
6566
call assert_equal([1, 2, 3], d->values())
6667
endfunc
6768

69+
func Test_string()
70+
call assert_equal(['1', '2', '3'], '1 2 3'->split())
71+
call assert_equal([1, 2, 3], '1 2 3'->split()->map({i, v -> str2nr(v)}))
72+
call assert_equal([65, 66, 67], 'ABC'->str2list())
73+
call assert_equal(3, 'ABC'->strlen())
74+
call assert_equal('a^Mb^[c', "a\rb\ec"->strtrans())
75+
call assert_equal(4, "aあb"->strwidth())
76+
call assert_equal('axc', 'abc'->substitute('b', 'x', ''))
77+
endfunc
78+
6879
func Test_append()
6980
new
7081
eval ['one', 'two', 'three']->append(1)

src/testdir/test_syntax.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,8 @@ func Test_synstack_synIDtrans()
517517
call assert_equal([], synstack(1, 1))
518518

519519
norm f/
520-
call assert_equal(['cComment', 'cCommentStart'], map(synstack(line("."), col(".")), 'synIDattr(v:val, "name")'))
521-
call assert_equal(['Comment', 'Comment'], map(synstack(line("."), col(".")), 'synIDattr(synIDtrans(v:val), "name")'))
520+
eval synstack(line("."), col("."))->map('synIDattr(v:val, "name")')->assert_equal(['cComment', 'cCommentStart'])
521+
eval synstack(line("."), col("."))->map('synIDattr(synIDtrans(v:val), "name")')->assert_equal(['Comment', 'Comment'])
522522

523523
norm fA
524524
call assert_equal(['cComment'], map(synstack(line("."), col(".")), 'synIDattr(v:val, "name")'))

src/testdir/test_system.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ func Test_System()
44
if !executable('echo') || !executable('cat') || !executable('wc')
55
return
66
endif
7-
let out = system('echo 123')
7+
let out = 'echo 123'->system()
88
" On Windows we may get a trailing space.
99
if out != "123 \n"
1010
call assert_equal("123\n", out)
1111
endif
1212

13-
let out = systemlist('echo 123')
13+
let out = 'echo 123'->systemlist()
1414
" On Windows we may get a trailing space and CR.
1515
if out != ["123 \r"]
1616
call assert_equal(['123'], out)

src/version.c

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

774774
static int included_patches[] =
775775
{ /* Add new patch number below this line */
776+
/**/
777+
1809,
776778
/**/
777779
1808,
778780
/**/

0 commit comments

Comments
 (0)