Instructions: Replace the template text and remove irrelevant text (including this line)
Hi, Bram.
I'm a you and Vim's fan!
I want to introduce the string interpolation (the template string) into Vim script.
Now it is introduced onto
A clear and concise description of what you want to happen.
'The string interpolation' ('The template string') embeds any expressions ${x} into a string via Vim function string().
For example.
" Embed literals using back quotations (the template string) and "${" "}" call assert_equal(`I have ${10}`, 'I have 10') call assert_equal(`I'm a ${'vim'}`, "I'm a vim") " Variables let x = 10 call assert_equal(`I have ${x}`, 'I have 10') " Others let x = ch_open('localhost:25252') " dummy call assert_equal(`${x}`, string(x))
I'm refering to the JavaScript's format of ${x} for the Vim script's template string.
(${x} ${dict.a} ${10} ${function('function')})
These expressions must be surrounded by "${" and "}".
I'm implementing it now.
The test can be shown at here :D
Please see this 👇 if you can!
A clear and concise description of any alternative solutions or features you've considered.
I'm planning a template string form $"I'm ${name}" (surrounded by $" and ")
if Bram doesn't like a back quoted form I'm ${name}.
After it introduced, expression embedding is to be more easier, and I think it is very cool!
Thanks!
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.![]()
I'm implementing this at here :D
Backticks are hard to use on some keyboards, and also they are hard to distinguish from single quotes.
On top of that, Vim has two types of strings: single quoted and double quoted. Both are useful.
Therefore the $"template" and $'template' syntax seems a better solution.
Since we only used $ for environment variables, I don't think there is a conflict with existing syntax.
For $"foo ${var} bar" I assume that "var" is taken literally, thus backslashes are kept as-is.
Thank you, Bram!
That solution seems very good!
I'll implement $'' and $"" 👍
I force-pushed the newer test here :)
What about instead adding a function similar to eval() or map() to perform the interpolation? Eg interpol().
Or what about instead adding a function to create an intermediate representation (ie a Vim expression) which can then be eval()uated?
And for the syntax:
We already have statusline syntax: abc%{expr}def, how about reusing that?
—
I have some minor problem with using $"" and $'', since it results in a
lot of dollars, e.g.:
Is there any other syntax that would work better?
Opinions?
I like $"" and $'' the best, but I think it is good to add 't'' and t""' or '_'' and _""' as that synonym 👍
Should we also allow using $var instead of ${var}, like Python does?
I thought it, and took a questionnaire to
What do you like any support?
1. only ${var}
2. only $var
3. both ${var} and $var
the 1. is gotten most votes!
I think $var can be appended later if $var is needed later.
But I wish to will start off of with only ${var} now 😂
(I don't hate $var :D)
It does require escaping every literal $ with $$.
Good!
I'll implement the escaping as it!
The 3 types of string interpolation I'm used to seeing and using are:
`Value is: ${val}`f'Value is {val}' f'Literal braces: {{}}'
$"Value is {val}" $"Literal braces: {{}}"
Keeping similar to at least one of these seems sensible - since javascript is so popular these days my preference would be to use that syntax. The other nice thing about the javascript/typescript syntax is that you don't have to escape literal characters - $, { and } can all be used alone, the interpolation only captures the full ${} pattern.
I don't see the point of any of this, as opposed to just using existing concatenation. Doing let foo = "one $[bar} baz" saves only one measly character when compared with let foo = "one ".bar." baz"
@chdiza Maybe it is a question for general 😂
Please find the purpose of other languages :)
—
After all, I like $var too :D
I want to implement
$10 and $string(30) is illformed. $var and ${var} is well-formed.Can I make this? 😃
(I like a form $'' and $"" 😂 I want to this.)
Isn't $var confusing with environment variables?
I also use Javascript/Typescript regularly, which is why I know the
backtick is hard to recognize. It depends on the font, but it looks
very much like the backtick. This may lead to mistakes.
OK, that's fair enough Bram (although I don't personally find it confusing and, as noted, this is already used by javascript/typescript so clearly many people find it acceptable).
My one last statement in favour of the javascript/typescript syntax is that vim already uses the {} interpolation characters in :h curly_brace_names, where it is possible in vimscript to do this:
let my_{&filetype}_variable = 'something';
It would be nice and consistent to then be able to do this:
let my_{&filetype}_variable = `something {&filetype}-specific`;
Apologies, I'm clearly typing before thinking, the curly_brace_name syntax is similar to C# syntax, not javascript/typescript syntax, as there is no leading $ before the interpolation {} characters. So perhaps the C# syntax would be appropriate?
let my_{&filetype}_variable = $"something {&filetype}-specific (escaping with doubled characters for literal {{ and }})"
Just idea, but from Swift langauge's string literal, I wonder \(...) with extending existing double quote strings might match to Vim script.
print("6 times 7 is \(6 * 7).")
Though it does not fully maintain backward compatibility, \( simply represents( in "..." literal so it should not appear in current Vim script codebases.
Just idea, but from Swift langauge's string literal, I wonder (...) with extending existing double quote strings might match to Vim script.
print("6 times 7 is (6 * 7).")
Oh, I didn't bethink "(xxx)"..., but this is the best choice in my opinion!
I'll re-implementing all to this if Bram is OK :)
(
Does this mean that doesn't support string-interpolations in string literals?
echo "\(10)" " ==> 10 echo '\(10)' " ==> \(10)
I think this is very good.
)
Thank you for the quick and positive response.
Does this mean that doesn't support string-interpolations in string literals?
Yes, I meant that. Since single quote string is something like raw string literals in other languages, no interpolation sounds natural for me.
But it's just an idea. There may be some mismatch to Vim script. So let's carefully consider grammar. After some thoughts, I came up with possible confusion. \(...) in double quote is similar to \(...\) in single quote string (grouping of regex).
—
—
I would not want to limit this to double quoted strings.
I see 👍
I'll proceed with $'' $"" and _'' _"".
The taglist plugin (which was developed back in 2002 and widely used)
heavily uses the curly brace syntax in variables. This was implemented
before Lists/Dicts were added to Vim.
In my opinion, it has no problem 👍
Because string interpolations is available only under the $'' $"" _'' _""!
Please see above comments for back compatibilities if you want :D
—
You are receiving this because you commented.
I'm working on this about everyday, please wait! :D
My all tests passed!
This is checked on this revision!!
Remain tasks
- Make syntax highlighting
Syntax highlighting is maintained differently.
It is maintained by Dr. Chip, so you should sent a patch to him.
_"foo" breaks compatibility. Current behavior is:
:let _ = 1
:echo _"foo"
1 foo
@brammool
Hi, I opened #4634 !
That is '' omitted version. because #4491 (comment) X)
Please take this patch if you ok to agree '' unsupport 🙏
Or let's discuss again about '_' support if you really wan to it 👍
—
You are receiving this because you commented.
—
I wonder why two syntaxes of same feature are supported (_"some ${var} here", $"some ${var} here").
I don't disagree strongly that adding two syntaxes, but as my simple feeling, I think one syntax is enough.
IMHO, I recommend Python 3's f-strings syntax (because Vim script syntax is similar to Python):
https://docs.python.org/ja/3.6/reference/lexical_analysis.html#f-strings
$ python3.7
Python 3.7.3 (default, Mar 27 2019, 09:23:15)
[Clang 10.0.1 (clang-1001.0.46.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> var = 42
>>> print(f'some {var} here')
some 42 here
>>> print(f'some {{var}} here')
some {var} here
But f-strings has many many features (below example is from above link).
I think it's not necessary (inside {...} is just Vim expression).
>>> name = "Fred"
>>> f"He said his name is {name!r}."
"He said his name is 'Fred'."
>>> f"He said his name is {repr(name)}." # repr() is equivalent to !r
"He said his name is 'Fred'."
>>> width = 10
>>> precision = 4
>>> value = decimal.Decimal("12.34567")
>>> f"result: {value:{width}.{precision}}" # nested fields
'result: 12.35'
>>> today = datetime(year=2017, month=1, day=27)
>>> f"{today:%B %d, %Y}" # using date format specifier
'January 27, 2017'
>>> number = 1024
>>> f"{number:#0x}" # using integer format specifier
'0x400'
As @mattn said, f-strings syntax has also compatibility issue #4491 (comment)
But I think this is very minor edge case (specific to :echo and its familiar commands).
I want $'' $"" if only one is supported :D
@brammool Thanks for agreeing!
I'll fix PR reviewed terms momently 👍️
—
You are receiving this because you commented.
I fixed reviews!
—
#4639 finished!
I'll wait to be taken the patch :D
Thanks!
@tyru Oh, that's right ><
Thanks! lol
Shouldn't this be closed now that there is #4634 ?
Sure👍️
—
You are receiving this because you commented.
Closed #4491.