diff src/vim9expr.c @ 28718:723c7d940cba

patch 8.2.4883: string interpolation only works in heredoc Commit: https://github.com/vim/vim/commit/2eaef106e4a7fc9dc74a7e672b5f550ec1f9786e Author: LemonBoy <thatlemon@gmail.com> Date: Fri May 6 13:14:50 2022 +0100 patch 8.2.4883: string interpolation only works in heredoc Problem: String interpolation only works in heredoc. Solution: Support interpolated strings. Use syntax for heredoc consistent with strings, similar to C#. (closes #10327)
author Bram Moolenaar <Bram@vim.org>
date Fri, 06 May 2022 14:15:03 +0200
parents bfd8e25fa207
children 3626ca6a20ea
line wrap: on
line diff
--- a/src/vim9expr.c
+++ b/src/vim9expr.c
@@ -1375,6 +1375,33 @@ compile_get_env(char_u **arg, cctx_T *cc
 }
 
 /*
+ * Compile "$"string"" or "$'string'".
+ */
+    static int
+compile_interp_string(char_u **arg, cctx_T *cctx)
+{
+    typval_T	tv;
+    int		ret;
+    int		evaluate = cctx->ctx_skip != SKIP_YES;
+
+    // *arg is on the '$' character.
+    (*arg)++;
+
+    if (**arg == '"')
+	ret = eval_string(arg, &tv, evaluate);
+    else
+	ret = eval_lit_string(arg, &tv, evaluate);
+
+    if (ret == FAIL || !evaluate)
+	return ret;
+
+    ret = compile_all_expr_in_str(tv.vval.v_string, TRUE, cctx);
+    clear_tv(&tv);
+
+    return ret;
+}
+
+/*
  * Compile "@r".
  */
     static int
@@ -2226,10 +2253,14 @@ compile_expr8(
 
 	/*
 	 * Environment variable: $VAR.
+	 * Interpolated string: $"string" or $'string'.
 	 */
 	case '$':	if (generate_ppconst(cctx, ppconst) == FAIL)
 			    return FAIL;
-			ret = compile_get_env(arg, cctx);
+			if ((*arg)[1] == '"' || (*arg)[1] == '\'')
+			    ret = compile_interp_string(arg, cctx);
+			else
+			    ret = compile_get_env(arg, cctx);
 			break;
 
 	/*