Skip to content

Complete vim9 support - #206

Merged
mattn merged 25 commits into
vim-jp:masterfrom
jclsn:feat/complete-vim9-support
Nov 23, 2026
Merged

mattn merged 25 commits into
vim-jp:masterfrom
jclsn:feat/complete-vim9-support

Conversation

@jclsn

@jclsn jclsn commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

I made some good use of my tokens.

This is complete vim9scipt support. Including tests.

jclsn added 20 commits October 19, 2026 21:02
Use Vim9 syntax inside :def functions even in a legacy script, and legacy
syntax inside :function bodies in a Vim9 script, instead of a single
file-wide flag.  In Vim9 syntax a double quote no longer starts a comment.

Also parse "vim9script noclear" and treat :final as :finally in legacy
script.
In Vim9 syntax:
- a colon is only part of a name after a one letter scope (g:name)
- there are no curly-braces names
- {key: val} uses literal keys, [expr] gives an expression key
- "." is a member access, never a concatenation
- an expression continues on the next line inside brackets, after an
  operator, or when the next line starts with an operator; comments and
  empty lines in between are skipped
Add the Vim9 lambda syntax "(args) => expr" with optional argument and
return types, and lambdas with a block of statements "(args) => {" ... "}".
Add tuples "(a, b)" (NODE_TUPLE) and type casts "<type>expr" (NODE_CAST).
In Vim9 syntax recognize, like find_ex_command() in Vim:
- assignments without :let (NODE_ASSIGN), including "++var" and "--var"
- function calls without :call and expressions without :eval
- generic function calls "Func<type>(args)"
- "{" ... "}" blocks (NODE_BLOCK)

A range must be preceded by a colon.  "name = expr" is an assignment when
the name was declared or is not an Ex command.
Share one parser for :var, :final and Vim9 :const that handles a missing
initializer, unpacking with optional types "var [a: number, _; rest]"
and heredocs.  Also parse typed :for loop variables and the "func: type"
type.
Parse the arguments with Vim9 syntax also in a legacy script, allow them
to span lines with comments, and accept generic type parameters
"def Func<T>(", constructor arguments "this.name" and repeated "_".
Parse the file name as an expression and store whether it is an autoload
import (.autoload) and the name after "as" (.alias).  .str keeps the text
of the arguments.
Add NODE_CLASS, NODE_INTERFACE, NODE_ENUM with NODE_ENUMVALUE for the enum
values, and NODE_TYPE for :type.  :public, :static and :abstract wrap the
following command like :export.  Methods in an interface and abstract
methods have no body.
Add interpolated strings $"text {expr}" (NODE_INTERPOLATION), the falsy
operator "??" (NODE_FALSY) and "0o" octal numbers, which are available in
legacy script too.  In Vim9 script allow quotes between digits.
:vim9cmd and :legacy are command modifiers that select the syntax for the
command.  :defer (NODE_DEFER) and :echowindow (NODE_ECHOWINDOW) take
expressions.
"command Name {" and "autocmd Event pat {" start a block of commands in
Vim9 syntax up to "}".  The block is stored in .block of the EXCMD node.
Like Vim, accept a double quote comment after a command also in Vim9
script, e.g. "endif \"}}}", and skip the colon before a range after a
command modifier: "silent! :%delete _".
Add :clipreset, :iput, :pbuffer, :redrawtabpanel, :this, :uniq and
:wlrestore.
Like Vim, ignore lines starting with "\ (legacy) or #\ (Vim9) in between
lines continued with a backslash.
Like Vim, accept "call Func()->Method()": the expression must start with a
function call, which may be followed by method calls.
"^\+\+" in a JavaScript string is "^++", which is not a valid regular
expression, so parsing an invalid ++opt argument like "x ++" threw a
SyntaxError instead of E474.
Like Vim, :enddef at the start of a line ends the function and is not a
continuation line, so "var x =" before it is an incomplete line instead
of taking "enddef" as the value.
a:pos and a local "pos" are different variables in Vim script, but both
become "pos" in Python and JavaScript.  Rename the arguments that have
the name of a local variable: is_vim9_command() checked the command at the
wrong position, so "foo = 1" was an Ex command in Python and JavaScript,
and tuples got the position of the wrong token.
A variable alone on a line has no effect, unless an operator on the next
line continues the expression.  So "new" is the :new command, also when a
variable "new" was declared somewhere in the script.
@jclsn

jclsn commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

@mattn Care to have a look?

Like Vim, give an error for :append, :change, :insert, :k, :mode, :open,
:t, :xit and :Print in Vim9 script, instead of parsing them as commands.
@MindTooth

Copy link
Copy Markdown

I tested this PR (da15310) as a downstream user, by dropping its py/vimlparser.py into vint, in place of the current master (075a4fa).

Vim runtime corpus. I parsed all 2217 *.vim files in vim/vim runtime/ (Vim 9.2.1302) with both parsers:

master this PR
parse OK 2018 2127
fixed by this PR 111
newly failing 2
crashes (non-VimLParserException) 0

The 2 newly failing files are syntax-highlighting test inputs that contain invalid code on purpose, so the new errors look correct:

  • syntax/testdir/input/vim9_ex_function_def_tail_comment_errors.vim: # comment after a legacy :function header gives E488.
  • syntax/testdir/input/vim9_function_calls.vim: call (42) gives "Not an function call".

The 88 files that fail with both parsers are existing limitations, mostly E492 (several are non-UTF-8 lang/menu_*.vim files) and E10 on line continuations in syntax/*.vim.

Targeted samples. These parse correctly:

  • 0o17
  • x ?? y
  • $"hello {s:n}", where the expression is exposed as a node, which is nice for linters
  • indented =<< trim end markers
  • bare Vim9 assignment (count += 1)
  • class with var members and def new(this.x, this.y)
  • enum
  • tuples
  • defer F()

vint's test suite passes against this parser, with nothing different from master. vint will need to register the new node types (108–127) before it can lint files that use them, but that's on vint's side.

Not verified:

  • that py/vimlparser.py and js/vimlparser.js are in sync with autoload/vimlparser.vim (CI hasn't run on this PR)
  • the JS build
  • the upstream test suite

From a downstream point of view, this would be very welcome. Thanks for working on it!

@jclsn

jclsn commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

@MindTooth Thanks for looking into it. Would love to see this merged. If there is anything that needs improvement, let me know!

@mattn

mattn commented Nov 19, 2026

Copy link
Copy Markdown
Member

Thanks, this is great work. I checked it locally:

  • Regenerated py/ and js/ match the committed files, and the JS/Python tests pass.
  • Parsing autoload/vimlparser.vim gives the same output as master, with no slowdown.
  • The CI failures look environmental (covimerage "file is not a database", and the v7.4/v8.0 builds), not caused by this PR.

One issue: self.declared is shared by the whole file, so names leak across scopes. For example:

vim9script
var F = (edit) => edit
edit = 3

This parses as (assign = edit 3), but Vim runs it as :edit = 3. The same thing happens with a var file in one def and file = "x" in another. Could you scope declared per def/lambda/function context?

Minor nits:

  • The first let vim9 = self.isvim9() in parse_one_cmd is unused.
  • Skipping #\ between continuation lines should only apply to Vim9 script.

Also note that the AST/output changes (IMPORT.left, EXCMD.block, new node types) will affect downstream tools like vint.

@jclsn

jclsn commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

@mattn I pushed fixup commits to address your issues. Please have another look!

@MindTooth

Copy link
Copy Markdown

In autoload/vimlparser.vim, parse_vim9_statement() uses has_key(self.declared, name) to distinguish an assignment from an Ex command. When the name also matches a command, such as put, the result depends on which declarations the parser has already seen. Assignment check, line 812

vim9script
put = 42
def F()
  put = 1
enddef
var put = 7
F()

parse_cmd_def() pushes a function context before reading the body. That copies the names known at the def line, so put is missing when the parser reaches put = 1. All three parser implementations emit Ex command nodes for both put lines. Function entry, line 2287 · Scope copy, line 587

Could we add this as a regression test and account for deferred compilation when resolving names inside def bodies? In Vim 9.2, the first put inserts 42 into the buffer. F() is first compiled after var put = 7, so the second put assigns 1 to the variable. The expected nodes are an excmd at the top level and an assign inside F().

I used OpenAI Codex to reproduce this behavior and draft this comment.

Vim compiles a :def function when it is first called, usually after the
whole script was sourced, so a script variable declared after the
function is visible in it.  "put = 1" in a function was parsed as :put
when "var put" followed the function.

When a name in a :def body was not declared and the script declares it
later, parse the script again with the script variables known.  This
only happens for such names, other scripts are parsed once.
@jclsn

jclsn commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor Author

@MindTooth Addressed your request in 08d97c8

@mattn
mattn merged commit c359b1b into vim-jp:master Nov 23, 2026
@mattn

mattn commented Nov 23, 2026

Copy link
Copy Markdown
Member

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants