mirror of
				https://github.com/vim/vim.git
				synced 2025-10-29 09:37:35 -04:00 
			
		
		
		
	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.
This commit is contained in:
		| @@ -3575,9 +3575,10 @@ count({comp}, {expr} [, {ic} [, {start}]])			*count()* | |||||||
| 		When {comp} is a string then the number of not overlapping | 		When {comp} is a string then the number of not overlapping | ||||||
| 		occurrences of {expr} is returned. Zero is returned when | 		occurrences of {expr} is returned. Zero is returned when | ||||||
| 		{expr} is an empty string. | 		{expr} is an empty string. | ||||||
|  |  | ||||||
| 		Can also be used as a |method|: > | 		Can also be used as a |method|: > | ||||||
| 			mylist->count(val) | 			mylist->count(val) | ||||||
|  | < | ||||||
| 							*cscope_connection()* | 							*cscope_connection()* | ||||||
| cscope_connection([{num} , {dbpath} [, {prepend}]]) | cscope_connection([{num} , {dbpath} [, {prepend}]]) | ||||||
| 		Checks for the existence of a |cscope| connection.  If no | 		Checks for the existence of a |cscope| connection.  If no | ||||||
| @@ -5408,6 +5409,9 @@ has_key({dict}, {key})					*has_key()* | |||||||
| 		The result is a Number, which is 1 if |Dictionary| {dict} has | 		The result is a Number, which is 1 if |Dictionary| {dict} has | ||||||
| 		an entry with key {key}.  Zero otherwise. | 		an entry with key {key}.  Zero otherwise. | ||||||
|  |  | ||||||
|  | 		Can also be used as a |method|: > | ||||||
|  | 			mydict->has_key(key) | ||||||
|  |  | ||||||
| haslocaldir([{winnr} [, {tabnr}]])			*haslocaldir()* | haslocaldir([{winnr} [, {tabnr}]])			*haslocaldir()* | ||||||
| 		The result is a Number: | 		The result is a Number: | ||||||
| 		    1   when the window has set a local directory via |:lcd| | 		    1   when the window has set a local directory via |:lcd| | ||||||
| @@ -8294,6 +8298,8 @@ split({expr} [, {pattern} [, {keepempty}]])			*split()* | |||||||
| 			:let items = split(line, ':', 1) | 			:let items = split(line, ':', 1) | ||||||
| <		The opposite function is |join()|. | <		The opposite function is |join()|. | ||||||
|  |  | ||||||
|  | 		Can also be used as a |method|: > | ||||||
|  | 			GetString()->split() | ||||||
|  |  | ||||||
| sqrt({expr})						*sqrt()* | sqrt({expr})						*sqrt()* | ||||||
| 		Return the non-negative square root of Float {expr} as a | 		Return the non-negative square root of Float {expr} as a | ||||||
| @@ -8337,12 +8343,19 @@ str2list({expr} [, {utf8}])				*str2list()* | |||||||
| 		properly: > | 		properly: > | ||||||
| 			str2list("á")		returns [97, 769] | 			str2list("á")		returns [97, 769] | ||||||
|  |  | ||||||
|  | <		Can also be used as a |method|: > | ||||||
|  | 			GetString()->str2list() | ||||||
|  |  | ||||||
|  |  | ||||||
| str2nr({expr} [, {base}])				*str2nr()* | str2nr({expr} [, {base}])				*str2nr()* | ||||||
| 		Convert string {expr} to a number. | 		Convert string {expr} to a number. | ||||||
| 		{base} is the conversion base, it can be 2, 8, 10 or 16. | 		{base} is the conversion base, it can be 2, 8, 10 or 16. | ||||||
|  |  | ||||||
| 		When {base} is omitted base 10 is used.  This also means that | 		When {base} is omitted base 10 is used.  This also means that | ||||||
| 		a leading zero doesn't cause octal conversion to be used, as | 		a leading zero doesn't cause octal conversion to be used, as | ||||||
| 		with the default String to Number conversion. | 		with the default String to Number conversion.  Example: > | ||||||
|  | 			let nr = str2nr('123') | ||||||
|  | < | ||||||
| 		When {base} is 16 a leading "0x" or "0X" is ignored.  With a | 		When {base} is 16 a leading "0x" or "0X" is ignored.  With a | ||||||
| 		different base the result will be zero.  Similarly, when | 		different base the result will be zero.  Similarly, when | ||||||
| 		{base} is 8 a leading "0" is ignored, and when {base} is 2 a | 		{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 | |||||||
| 		|strchars()|. | 		|strchars()|. | ||||||
| 		Also see |len()|, |strdisplaywidth()| and |strwidth()|. | 		Also see |len()|, |strdisplaywidth()| and |strwidth()|. | ||||||
|  |  | ||||||
|  | 		Can also be used as a |method|: > | ||||||
|  | 			GetString()->strlen() | ||||||
|  |  | ||||||
| strpart({src}, {start} [, {len}])			*strpart()* | strpart({src}, {start} [, {len}])			*strpart()* | ||||||
| 		The result is a String, which is part of {src}, starting from | 		The result is a String, which is part of {src}, starting from | ||||||
| 		byte {start}, with the byte length {len}. | 		byte {start}, with the byte length {len}. | ||||||
| @@ -8514,6 +8530,9 @@ strtrans({expr})					*strtrans()* | |||||||
| <		This displays a newline in register a as "^@" instead of | <		This displays a newline in register a as "^@" instead of | ||||||
| 		starting a new line. | 		starting a new line. | ||||||
|  |  | ||||||
|  | 		Can also be used as a |method|: > | ||||||
|  | 			GetString()->strtrans() | ||||||
|  |  | ||||||
| strwidth({expr})					*strwidth()* | strwidth({expr})					*strwidth()* | ||||||
| 		The result is a Number, which is the number of display cells | 		The result is a Number, which is the number of display cells | ||||||
| 		String {expr} occupies.  A Tab character is counted as one | 		String {expr} occupies.  A Tab character is counted as one | ||||||
| @@ -8522,6 +8541,9 @@ strwidth({expr})					*strwidth()* | |||||||
| 		Ambiguous, this function's return value depends on 'ambiwidth'. | 		Ambiguous, this function's return value depends on 'ambiwidth'. | ||||||
| 		Also see |strlen()|, |strdisplaywidth()| and |strchars()|. | 		Also see |strlen()|, |strdisplaywidth()| and |strchars()|. | ||||||
|  |  | ||||||
|  | 		Can also be used as a |method|: > | ||||||
|  | 			GetString()->strwidth() | ||||||
|  |  | ||||||
| submatch({nr} [, {list}])			*submatch()* *E935* | submatch({nr} [, {list}])			*submatch()* *E935* | ||||||
| 		Only for an expression in a |:substitute| command or | 		Only for an expression in a |:substitute| command or | ||||||
| 		substitute() function. | 		substitute() function. | ||||||
| @@ -8589,6 +8611,9 @@ substitute({expr}, {pat}, {sub}, {flags})		*substitute()* | |||||||
| 		|submatch()| returns.  Example: > | 		|submatch()| returns.  Example: > | ||||||
| 		   :echo substitute(s, '%\(\x\x\)', {m -> '0x' . m[1]}, 'g') | 		   :echo substitute(s, '%\(\x\x\)', {m -> '0x' . m[1]}, 'g') | ||||||
|  |  | ||||||
|  | <		Can also be used as a |method|: > | ||||||
|  | 			GetString()->substitute(pat, sub, flags) | ||||||
|  |  | ||||||
| swapinfo({fname})					*swapinfo()* | swapinfo({fname})					*swapinfo()* | ||||||
| 		The result is a dictionary, which holds information about the | 		The result is a dictionary, which holds information about the | ||||||
| 		swapfile {fname}. The available fields are: | 		swapfile {fname}. The available fields are: | ||||||
| @@ -8674,12 +8699,19 @@ synIDattr({synID}, {what} [, {mode}])			*synIDattr()* | |||||||
| 		cursor): > | 		cursor): > | ||||||
| 	:echo synIDattr(synIDtrans(synID(line("."), col("."), 1)), "fg") | 	:echo synIDattr(synIDtrans(synID(line("."), col("."), 1)), "fg") | ||||||
| < | < | ||||||
|  | 		Can also be used as a |method|: > | ||||||
|  | 	:echo synID(line("."), col("."), 1)->synIDtrans()->synIDattr("fg") | ||||||
|  |  | ||||||
|  |  | ||||||
| synIDtrans({synID})					*synIDtrans()* | synIDtrans({synID})					*synIDtrans()* | ||||||
| 		The result is a Number, which is the translated syntax ID of | 		The result is a Number, which is the translated syntax ID of | ||||||
| 		{synID}.  This is the syntax group ID of what is being used to | 		{synID}.  This is the syntax group ID of what is being used to | ||||||
| 		highlight the character.  Highlight links given with | 		highlight the character.  Highlight links given with | ||||||
| 		":highlight link" are followed. | 		":highlight link" are followed. | ||||||
|  |  | ||||||
|  | 		Can also be used as a |method|: > | ||||||
|  | 	:echo synID(line("."), col("."), 1)->synIDtrans()->synIDattr("fg") | ||||||
|  |  | ||||||
| synconcealed({lnum}, {col})				*synconcealed()* | synconcealed({lnum}, {col})				*synconcealed()* | ||||||
| 		The result is a List with currently three items: | 		The result is a List with currently three items: | ||||||
| 		1. The first item in the list is 0 if the character at the | 		1. The first item in the list is 0 if the character at the | ||||||
| @@ -8784,6 +8816,9 @@ system({expr} [, {input}])				*system()* *E677* | |||||||
| 		Unlike ":!cmd" there is no automatic check for changed files. | 		Unlike ":!cmd" there is no automatic check for changed files. | ||||||
| 		Use |:checktime| to force a check. | 		Use |:checktime| to force a check. | ||||||
|  |  | ||||||
|  | 		Can also be used as a |method|: > | ||||||
|  | 			:echo GetCmd()->system() | ||||||
|  |  | ||||||
|  |  | ||||||
| systemlist({expr} [, {input}])				*systemlist()* | systemlist({expr} [, {input}])				*systemlist()* | ||||||
| 		Same as |system()|, but returns a |List| with lines (parts of | 		Same as |system()|, but returns a |List| with lines (parts of | ||||||
| @@ -8794,6 +8829,9 @@ systemlist({expr} [, {input}])				*systemlist()* | |||||||
|  |  | ||||||
| 		Returns an empty string on error. | 		Returns an empty string on error. | ||||||
|  |  | ||||||
|  | 		Can also be used as a |method|: > | ||||||
|  | 			:echo GetCmd()->systemlist() | ||||||
|  |  | ||||||
|  |  | ||||||
| tabpagebuflist([{arg}])					*tabpagebuflist()* | tabpagebuflist([{arg}])					*tabpagebuflist()* | ||||||
| 		The result is a |List|, where each item is the number of the | 		The result is a |List|, where each item is the number of the | ||||||
|   | |||||||
| @@ -995,7 +995,8 @@ static funcentry_T global_functions[] = | |||||||
| #define FEARG_2    2	    // base is the second argument | #define FEARG_2    2	    // base is the second argument | ||||||
|  |  | ||||||
| /* | /* | ||||||
|  * Methods that call the internal function with the base as the first argument. |  * Methods that call the internal function with the base as one of the | ||||||
|  |  * arguments. | ||||||
|  */ |  */ | ||||||
| static funcentry_T base_methods[] = | static funcentry_T base_methods[] = | ||||||
| { | { | ||||||
| @@ -1011,6 +1012,7 @@ static funcentry_T base_methods[] = | |||||||
|     {"extend",		1, 2, 0,	    f_extend}, |     {"extend",		1, 2, 0,	    f_extend}, | ||||||
|     {"filter",		1, 1, 0,	    f_filter}, |     {"filter",		1, 1, 0,	    f_filter}, | ||||||
|     {"get",		1, 2, 0,	    f_get}, |     {"get",		1, 2, 0,	    f_get}, | ||||||
|  |     {"has_key",		1, 1, 0,	    f_has_key}, | ||||||
|     {"index",		1, 3, 0,	    f_index}, |     {"index",		1, 3, 0,	    f_index}, | ||||||
|     {"insert",		1, 2, 0,	    f_insert}, |     {"insert",		1, 2, 0,	    f_insert}, | ||||||
|     {"items",		0, 0, 0,	    f_items}, |     {"items",		0, 0, 0,	    f_items}, | ||||||
| @@ -1024,7 +1026,17 @@ static funcentry_T base_methods[] = | |||||||
|     {"repeat",		1, 1, 0,	    f_repeat}, |     {"repeat",		1, 1, 0,	    f_repeat}, | ||||||
|     {"reverse",		0, 0, 0,	    f_reverse}, |     {"reverse",		0, 0, 0,	    f_reverse}, | ||||||
|     {"sort",		0, 2, 0,	    f_sort}, |     {"sort",		0, 2, 0,	    f_sort}, | ||||||
|  |     {"split",		0, 2, 0,	    f_split}, | ||||||
|  |     {"str2list",	0, 1, 0,	    f_str2list}, | ||||||
|     {"string",		0, 0, 0,	    f_string}, |     {"string",		0, 0, 0,	    f_string}, | ||||||
|  |     {"strlen",		0, 0, 0,	    f_strlen}, | ||||||
|  |     {"strtrans",	0, 0, 0,	    f_strtrans}, | ||||||
|  |     {"strwidth",	0, 0, 0,	    f_strwidth}, | ||||||
|  |     {"substitute",	3, 3, 0,	    f_substitute}, | ||||||
|  |     {"synIDattr",	1, 2, 0,	    f_synIDattr}, | ||||||
|  |     {"synIDtrans",	0, 0, 0,	    f_synIDtrans}, | ||||||
|  |     {"system",		0, 1, 0,	    f_system}, | ||||||
|  |     {"systemlist",	0, 2, 0,	    f_systemlist}, | ||||||
|     {"type",		0, 0, 0,	    f_type}, |     {"type",		0, 0, 0,	    f_type}, | ||||||
|     {"uniq",		0, 2, 0,	    f_uniq}, |     {"uniq",		0, 2, 0,	    f_uniq}, | ||||||
|     {"values",		0, 0, 0,	    f_values}, |     {"values",		0, 0, 0,	    f_values}, | ||||||
|   | |||||||
| @@ -668,13 +668,13 @@ func Test_diff_hlID() | |||||||
|   diffthis |   diffthis | ||||||
|   redraw |   redraw | ||||||
|  |  | ||||||
|   call assert_equal(synIDattr(diff_hlID(-1, 1), "name"), "") |   call diff_hlID(-1, 1)->synIDattr("name")->assert_equal("") | ||||||
|  |  | ||||||
|   call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange") |   call diff_hlID(1, 1)->synIDattr("name")->assert_equal("DiffChange") | ||||||
|   call assert_equal(synIDattr(diff_hlID(1, 2), "name"), "DiffText") |   call diff_hlID(1, 2)->synIDattr("name")->assert_equal("DiffText") | ||||||
|   call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "") |   call diff_hlID(2, 1)->synIDattr("name")->assert_equal("") | ||||||
|   call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "DiffAdd") |   call diff_hlID(3, 1)->synIDattr("name")->assert_equal("DiffAdd") | ||||||
|   call assert_equal(synIDattr(diff_hlID(4, 1), "name"), "") |   call diff_hlID(4, 1)->synIDattr("name")->assert_equal("") | ||||||
|  |  | ||||||
|   wincmd w |   wincmd w | ||||||
|   call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange") |   call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange") | ||||||
|   | |||||||
| @@ -46,6 +46,7 @@ func Test_dict() | |||||||
|   call assert_equal(2, d->get('two')) |   call assert_equal(2, d->get('two')) | ||||||
|   call assert_fails("let x = d->index(2)", 'E897:') |   call assert_fails("let x = d->index(2)", 'E897:') | ||||||
|   call assert_fails("let x = d->insert(0)", 'E899:') |   call assert_fails("let x = d->insert(0)", 'E899:') | ||||||
|  |   call assert_true(d->has_key('two')) | ||||||
|   call assert_equal([['one', 1], ['two', 2], ['three', 3]], d->items()) |   call assert_equal([['one', 1], ['two', 2], ['three', 3]], d->items()) | ||||||
|   call assert_fails("let x = d->join()", 'E714:') |   call assert_fails("let x = d->join()", 'E714:') | ||||||
|   call assert_equal(['one', 'two', 'three'], d->keys()) |   call assert_equal(['one', 'two', 'three'], d->keys()) | ||||||
| @@ -65,6 +66,16 @@ func Test_dict() | |||||||
|   call assert_equal([1, 2, 3], d->values()) |   call assert_equal([1, 2, 3], d->values()) | ||||||
| endfunc | endfunc | ||||||
|  |  | ||||||
|  | func Test_string() | ||||||
|  |   call assert_equal(['1', '2', '3'], '1 2 3'->split()) | ||||||
|  |   call assert_equal([1, 2, 3], '1 2 3'->split()->map({i, v -> str2nr(v)})) | ||||||
|  |   call assert_equal([65, 66, 67], 'ABC'->str2list()) | ||||||
|  |   call assert_equal(3, 'ABC'->strlen()) | ||||||
|  |   call assert_equal('a^Mb^[c', "a\rb\ec"->strtrans()) | ||||||
|  |   call assert_equal(4, "aあb"->strwidth()) | ||||||
|  |   call assert_equal('axc', 'abc'->substitute('b', 'x', '')) | ||||||
|  | endfunc | ||||||
|  |  | ||||||
| func Test_append() | func Test_append() | ||||||
|   new |   new | ||||||
|   eval ['one', 'two', 'three']->append(1) |   eval ['one', 'two', 'three']->append(1) | ||||||
|   | |||||||
| @@ -517,8 +517,8 @@ func Test_synstack_synIDtrans() | |||||||
|   call assert_equal([], synstack(1, 1)) |   call assert_equal([], synstack(1, 1)) | ||||||
|  |  | ||||||
|   norm f/ |   norm f/ | ||||||
|   call assert_equal(['cComment', 'cCommentStart'], map(synstack(line("."), col(".")), 'synIDattr(v:val, "name")')) |   eval synstack(line("."), col("."))->map('synIDattr(v:val, "name")')->assert_equal(['cComment', 'cCommentStart']) | ||||||
|   call assert_equal(['Comment', 'Comment'],	   map(synstack(line("."), col(".")), 'synIDattr(synIDtrans(v:val), "name")')) |   eval synstack(line("."), col("."))->map('synIDattr(synIDtrans(v:val), "name")')->assert_equal(['Comment', 'Comment']) | ||||||
|  |  | ||||||
|   norm fA |   norm fA | ||||||
|   call assert_equal(['cComment'], map(synstack(line("."), col(".")), 'synIDattr(v:val, "name")')) |   call assert_equal(['cComment'], map(synstack(line("."), col(".")), 'synIDattr(v:val, "name")')) | ||||||
|   | |||||||
| @@ -4,13 +4,13 @@ func Test_System() | |||||||
|   if !executable('echo') || !executable('cat') || !executable('wc') |   if !executable('echo') || !executable('cat') || !executable('wc') | ||||||
|     return |     return | ||||||
|   endif |   endif | ||||||
|   let out = system('echo 123') |   let out = 'echo 123'->system() | ||||||
|   " On Windows we may get a trailing space. |   " On Windows we may get a trailing space. | ||||||
|   if out != "123 \n" |   if out != "123 \n" | ||||||
|     call assert_equal("123\n", out) |     call assert_equal("123\n", out) | ||||||
|   endif |   endif | ||||||
|  |  | ||||||
|   let out = systemlist('echo 123') |   let out = 'echo 123'->systemlist() | ||||||
|   " On Windows we may get a trailing space and CR. |   " On Windows we may get a trailing space and CR. | ||||||
|   if out != ["123 \r"] |   if out != ["123 \r"] | ||||||
|     call assert_equal(['123'], out) |     call assert_equal(['123'], out) | ||||||
|   | |||||||
| @@ -773,6 +773,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 */ | ||||||
|  | /**/ | ||||||
|  |     1809, | ||||||
| /**/ | /**/ | ||||||
|     1808, |     1808, | ||||||
| /**/ | /**/ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user