mirror of
https://github.com/vim/vim.git
synced 2025-07-24 10:45:12 -04:00
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Problem: Vim9: Not possible to use legacy and Vim9 script in one file. Solution: Vim9: allow for "if false" before :vim9script. (closes #7851)
This commit is contained in:
parent
c7dac8534e
commit
d3f8a9ee65
@ -1051,9 +1051,9 @@ that you don't do that.
|
|||||||
Namespace ~
|
Namespace ~
|
||||||
*vim9-namespace*
|
*vim9-namespace*
|
||||||
To recognize a file that can be imported the `vim9script` statement must
|
To recognize a file that can be imported the `vim9script` statement must
|
||||||
appear as the first statement in the file. It tells Vim to interpret the
|
appear as the first statement in the file (see |vim9-mix| for an exception).
|
||||||
script in its own namespace, instead of the global namespace. If a file
|
It tells Vim to interpret the script in its own namespace, instead of the
|
||||||
starts with: >
|
global namespace. If a file starts with: >
|
||||||
vim9script
|
vim9script
|
||||||
var myvar = 'yes'
|
var myvar = 'yes'
|
||||||
Then "myvar" will only exist in this file. While without `vim9script` it would
|
Then "myvar" will only exist in this file. While without `vim9script` it would
|
||||||
@ -1073,6 +1073,27 @@ Vim default value, like with: >
|
|||||||
One of the effects is that |line-continuation| is always enabled.
|
One of the effects is that |line-continuation| is always enabled.
|
||||||
The original value of 'cpoptions' is restored at the end of the script.
|
The original value of 'cpoptions' is restored at the end of the script.
|
||||||
|
|
||||||
|
*vim9-mix*
|
||||||
|
There is one way to use both legacy and Vim9 syntax in one script file: >
|
||||||
|
" comments may go here
|
||||||
|
if !has('vim9script')
|
||||||
|
" legacy script commands go here
|
||||||
|
finish
|
||||||
|
endif
|
||||||
|
vim9script
|
||||||
|
# Vim9 script commands go here
|
||||||
|
This allows for writing a script that takes advantage of the Vim9 script
|
||||||
|
syntax if possible, but will also work on an Vim version without it.
|
||||||
|
|
||||||
|
This can only work in two ways:
|
||||||
|
1. The "if" statement evaluates to false, the commands up to `endif` are
|
||||||
|
skipped and `vim9script` is then the first command actually executed.
|
||||||
|
2. The "if" statement evaluates to true, the commands up to `endif` are
|
||||||
|
executed and `finish` bails out before reaching `vim9script`.
|
||||||
|
|
||||||
|
TODO: The "vim9script" feature does not exist yet, it will only be added once
|
||||||
|
the Vim9 script syntax has been fully implemented.
|
||||||
|
|
||||||
|
|
||||||
Export ~
|
Export ~
|
||||||
*:export* *:exp*
|
*:export* *:exp*
|
||||||
|
@ -2595,8 +2595,12 @@ do_one_cmd(
|
|||||||
|
|
||||||
#ifdef FEAT_EVAL
|
#ifdef FEAT_EVAL
|
||||||
// Set flag that any command was executed, used by ex_vim9script().
|
// Set flag that any command was executed, used by ex_vim9script().
|
||||||
|
// Not if this was a command that wasn't executed or :endif.
|
||||||
if (getline_equal(ea.getline, ea.cookie, getsourceline)
|
if (getline_equal(ea.getline, ea.cookie, getsourceline)
|
||||||
&& current_sctx.sc_sid > 0)
|
&& current_sctx.sc_sid > 0
|
||||||
|
&& ea.cmdidx != CMD_endif
|
||||||
|
&& (cstack->cs_idx < 0
|
||||||
|
|| (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
|
||||||
SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
|
SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -1262,6 +1262,27 @@ def Test_use_import_in_mapping()
|
|||||||
nunmap <F3>
|
nunmap <F3>
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
|
def Test_vim9script_mix()
|
||||||
|
var lines =<< trim END
|
||||||
|
if has(g:feature)
|
||||||
|
" legacy script
|
||||||
|
let g:legacy = 1
|
||||||
|
finish
|
||||||
|
endif
|
||||||
|
vim9script
|
||||||
|
g:legacy = 0
|
||||||
|
END
|
||||||
|
g:feature = 'eval'
|
||||||
|
g:legacy = -1
|
||||||
|
CheckScriptSuccess(lines)
|
||||||
|
assert_equal(1, g:legacy)
|
||||||
|
|
||||||
|
g:feature = 'noteval'
|
||||||
|
g:legacy = -1
|
||||||
|
CheckScriptSuccess(lines)
|
||||||
|
assert_equal(0, g:legacy)
|
||||||
|
enddef
|
||||||
|
|
||||||
def Test_vim9script_fails()
|
def Test_vim9script_fails()
|
||||||
CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
|
CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
|
||||||
CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
|
CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
|
||||||
|
@ -750,6 +750,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 */
|
||||||
|
/**/
|
||||||
|
2529,
|
||||||
/**/
|
/**/
|
||||||
2528,
|
2528,
|
||||||
/**/
|
/**/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user