Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax highlighting for Vim script comments is broken #11307

Closed
chdiza opened this issue Oct 7, 2022 · 14 comments
Closed

Syntax highlighting for Vim script comments is broken #11307

chdiza opened this issue Oct 7, 2022 · 14 comments
Assignees

Comments

@chdiza
Copy link

chdiza commented Oct 7, 2022

Steps to reproduce

Make a buffer with &ft=vim and syntax highlighting on. Make a comment line that also has an additional double-quote in it later, e.g.,:

" This is what we call " blah

Expected behaviour

That entire line should be colored as a comment. But only the part from the second quote onward is colored that way. It looks like "This is what we call has the highlight group "vimIsCommand".

This is a bad regression; Vim didn't formerly do this. It's common to have lines that have a trailing comment, and then to comment out those very lines later. In my colorschemes, this makes the "This is what we call part colored the same as the "Normal" group; it defeats the purpose of syntax highlighting if one can't tell comments from non-comments by color.

Bisection reveals that the first bad commit is the following recent runtime file update:

f269eabc6c4f5bdcef989cd5b4b95ba8ccaa4d8a is the first bad commit
commit f269eabc6c4f5bdcef989cd5b4b95ba8ccaa4d8a
Author: Bram Moolenaar <Bram@vim.org>
Date:   Mon Oct 3 18:04:35 2022 +0100

    Update runtime files

Version of Vim

9.0.688

Environment

Ubuntu 20.04, though it also happens on macOS.
Terminal is kitty, or uxterm, doesn't matter
Also happens in gVim.

Logs and stack traces

No response

@chdiza chdiza added the bug label Oct 7, 2022
@brammool
Copy link
Contributor

brammool commented Oct 7, 2022 via email

@chdiza
Copy link
Author

chdiza commented Oct 8, 2022

A proper solution would know whether the line is in legacy script or Vim9 script,

I don't understand. Surely vim is already aware that it's highlighting a legacy comment---otherwise it wouldn't make any of my example line colored like a comment, because there are no '#' characters in it.

Plus, in the commit mentioned above, the line of vim.vim that got changed and broke things has got nothing to do with vim9script, AFAICT.

Paging Dr. @cecamp.

@Shane-XB-Qian
Copy link
Contributor

i also thought for now such highlight for comment (for such case) is bad, which caused hard to find it was a comment.

@Shane-XB-Qian
Copy link
Contributor

vim9 with # looks ok vs but legacy with " behavior different now at least.
// it thought whole line is a comment when vim9 with #.

@arp242
Copy link
Contributor

arp242 commented Oct 9, 2022

A proper solution would know whether the line is in legacy script or Vim9 script,

I don't understand. Surely vim is already aware that it's highlighting a legacy comment---otherwise it wouldn't make any of my example line colored like a comment, because there are no '#' characters in it.

Plus, in the commit mentioned above, the line of vim.vim that got changed and broke things has got nothing to do with vim9script, AFAICT.

The issue is that this:

vim9script
var x = "hello"

is valid in Vim9Script, it assigns a string to the variable x, but in VimScript the " is interpreted as a comment.

Disambiguating between the two uses of " is pretty hard.

That patch changed vimLineComment from +^[ \t:]*".*$+ to +^[ \t:]*"\("[^"]*"\|[^"]\)*$+. I'm not sure on the context of that change (i.e. what it fixed), but it's probably something that got highlighted as a comment when it shouldn't have.

@chdiza
Copy link
Author

chdiza commented Oct 10, 2022

In that example, Vim knows what to do because vim9script is present in that buffer. I don't use vim9script, so no such line is in any of my buffers, so Vim should know that it's dealing with legacy script.

@arp242
Copy link
Contributor

arp242 commented Oct 10, 2022

In that example, Vim knows what to do because vim9script is present in that buffer. I don't use vim9script, so no such line is in any of my buffers, so Vim should know that it's dealing with legacy script.

You can use fun in vim9script and def in VimScript. You can mix and match the two.

Clearly things could be improved, but it's not that simple.

@brammool
Copy link
Contributor

brammool commented Oct 10, 2022 via email

@chdiza
Copy link
Author

chdiza commented Oct 10, 2022

You can use fun in vim9script and def in VimScript. You can mix and match the two.

But you can't mix and match using the double-quote for comments. So what's wrong with making the vim syntax file work like this: if there is already a line that says "vim9script", then handle the quotes one way; otherwise, handle them the normal legacy way?

@dkearns
Copy link
Contributor

dkearns commented Oct 10, 2022

Legacy comments are required in legacy functions in a Vim9 script file.

Ignoring the wider fix, there's already a shared FuncBody syntax region for function blocks. It probably just needs to be split into two and each limited to including the appropriate comment type.

@dkearns
Copy link
Contributor

dkearns commented Oct 13, 2022

Here's a very quick attempt at fixing this. The general approach is that only the correct comments for the script version are allowed at TOP level and the rest are contained. This means that some inappropriate comments will still be highlighted but they would generally be invalid syntax.

This certainly needs more work and the comment highlighting could be simplified generally. E.g., vim9Comment appears to be unnecessarily contained in several syntax groups where vimComment was only included to prevent strings from being erroneously highlighted.

diff --git a/runtime/syntax/vim.vim b/runtime/syntax/vim.vim
index 839ae163f..3f4a351b6 100644
--- a/runtime/syntax/vim.vim
+++ b/runtime/syntax/vim.vim
@@ -243,14 +243,16 @@ endif
 " Functions : Tag is provided for those who wish to highlight tagged functions {{{2
 " =========
 syn cluster    vimFuncList     contains=vimCommand,vimFunctionError,vimFuncKey,Tag,vimFuncSID
-syn cluster    vimFuncBodyList contains=vimAbb,vimAddress,vimAugroupKey,vimAutoCmd,vimCmplxRepeat,vimComment,vim9Comment,vimContinue,vimCtrlChar,vimEcho,vimEchoHL,vimEnvvar,vimExecute,vimIsCommand,vimFBVar,vimFunc,vimFunction,vimFuncVar,vimGlobal,vimHighlight,vimIsCommand,vimLet,vimLetHereDoc,vimLineComment,vimMap,vimMark,vimNorm,vimNotation,vimNotFunc,vimNumber,vimOper,vimOperParen,vimRegion,vimRegister,vimSearch,vimSet,vimSpecFile,vimString,vimSubst,vimSynLine,vimUnmap,vimUserCommand
-syn match      vimFunction     "\<\(fu\%[nction]\)!\=\s\+\%(<[sS][iI][dD]>\|[sSgGbBwWtTlL]:\)\=\%(\i\|[#.]\|{.\{-1,}}\)*\ze\s*("       contains=@vimFuncList nextgroup=vimFuncBody
-syn match      vimFunction     "\<def!\=\ze\s*(" contains=@vimFuncList nextgroup=vimFuncBody
+syn cluster    vimFuncBodyList contains=vimAbb,vimAddress,vimAugroupKey,vimAutoCmd,vimCmplxRepeat,vimContinue,vimCtrlChar,vimEcho,vimEchoHL,vimEnvvar,vimExecute,vimIsCommand,vimFBVar,vimFunc,vimFunction,vimFuncVar,vimGlobal,vimHighlight,vimIsCommand,vimLet,vimLetHereDoc,vimMap,vimMark,vimNorm,vimNotation,vimNotFunc,vimNumber,vimOper,vimOperParen,vimRegion,vimRegister,vimSearch,vimSet,vimSpecFile,vimString,vimSubst,vimSynLine,vimUnmap,vimUserCommand
+syn match      vimFunction     "\<\(fu\%[nction]\)!\=\s\+\%(<[sS][iI][dD]>\|[sg]:\)\=\%(\i\|[#.]\|{.\{-1,}}\)\+\ze\s*("        contains=@vimFuncList nextgroup=vimFuncBody
+syn match      vimFunction                  "\<def!\=\s\+\%(<[sS][iI][dD]>\|[sg]:\)\=\%(\i\|[#.]\|{.\{-1,}}\)\+\ze\s*("        contains=@vimFuncList nextgroup=vim9FuncBody

 if exists("g:vimsyn_folding") && g:vimsyn_folding =~# 'f'
- syn region    vimFuncBody  contained  fold start="\ze\s*("    matchgroup=vimCommand end="\<\(endf\>\|endfu\%[nction]\>\|enddef\>\)"           contains=@vimFuncBodyList
+ syn region    vimFuncBody  contained  fold start="\ze\s*("    matchgroup=vimCommand end="\<endfu\%[nction]\>" contains=@vimFuncBodyList,vimComment,vimLineComment
+ syn region    vim9FuncBody contained  fold start="\ze\s*("    matchgroup=vimCommand end="\<enddef\>"  contains=@vimFuncBodyList,vim9Comment,vim9LineComment
 else
- syn region    vimFuncBody  contained  start="\ze\s*("         matchgroup=vimCommand end="\<\(endf\>\|endfu\%[nction]\>\|enddef\>\)"           contains=@vimFuncBodyList
+ syn region    vimFuncBody  contained  start="\ze\s*("         matchgroup=vimCommand end="\<endfu\%[nction]\>" contains=@vimFuncBodyList,vimComment,vimLineComment
+ syn region    vim9FuncBody contained  start="\ze\s*("         matchgroup=vimCommand end="\<enddef\>"  contains=@vimFuncBodyList,vim9Comment,vim9LineComment
 endif
 syn match      vimFuncVar   contained  "a:\(\K\k*\|\d\+\)"
 syn match      vimFuncSID   contained  "\c<sid>\|\<s:"
@@ -301,18 +303,34 @@ syn match vimUserAttrbCmplt contained     "custom,\u\w*"

 " Lower Priority Comments: after some vim commands... {{{2
 " =======================
-syn match      vimComment      excludenl +\s"[^\-:.%#=*].*$+lc=1       contains=@vimCommentGroup,vimCommentString
-syn match      vimComment      +\<endif\s\+".*$+lc=5   contains=@vimCommentGroup,vimCommentString
-syn match      vimComment      +\<else\s\+".*$+lc=4    contains=@vimCommentGroup,vimCommentString
-syn region     vimCommentString        contained oneline start='\S\s\+"'ms=e   end='"'
-" Vim9 comments - TODO: might be highlighted while they don't work
-syn match      vim9Comment     excludenl +\s#[^{].*$+lc=1      contains=@vimCommentGroup,vimCommentString
-syn match      vim9Comment     +\<endif\s\+#[^{].*$+lc=5       contains=@vimCommentGroup,vimCommentString
-syn match      vim9Comment     +\<else\s\+#[^{].*$+lc=4        contains=@vimCommentGroup,vimCommentString
-" Vim9 comment inside expression
-syn match      vim9Comment     +\s\zs#[^{].*$+ms=s+1   contains=@vimCommentGroup,vimCommentString
-syn match      vim9Comment     +^\s*#[^{].*$+  contains=@vimCommentGroup,vimCommentString
-syn match      vim9Comment     +^\s*#$+        contains=@vimCommentGroup,vimCommentString
+let s:isVim9Script = "\n" .. getline(1, 10)->join("\n") =~# '\n\s*vim9\%[script]\>'
+if s:isVim9Script
+  syn match    vimComment      excludenl +\s"[^\-:.%#=*].*$+lc=1       contains=@vimCommentGroup,vimCommentString contained
+  syn match    vimComment      +\<endif\s\+".*$+lc=5   contains=@vimCommentGroup,vimCommentString contained
+  syn match    vimComment      +\<else\s\+".*$+lc=4    contains=@vimCommentGroup,vimCommentString contained
+  syn region   vimCommentString        contained oneline start='\S\s\+"'ms=e   end='"' contained
+  " Vim9 comments - TODO: might be highlighted while they don't work
+  syn match    vim9Comment     excludenl +\s#[^{].*$+lc=1      contains=@vimCommentGroup,vimCommentString
+  syn match    vim9Comment     +\<endif\s\+#[^{].*$+lc=5       contains=@vimCommentGroup,vimCommentString
+  syn match    vim9Comment     +\<else\s\+#[^{].*$+lc=4        contains=@vimCommentGroup,vimCommentString
+  " Vim9 comment inside expression
+  syn match    vim9Comment     +\s\zs#[^{].*$+ms=s+1   contains=@vimCommentGroup,vimCommentString
+  syn match    vim9Comment     +^\s*#[^{].*$+          contains=@vimCommentGroup,vimCommentString
+  syn match    vim9Comment     +^\s*#$+                contains=@vimCommentGroup,vimCommentString
+else
+  syn match    vimComment      excludenl +\s"[^\-:.%#=*].*$+lc=1       contains=@vimCommentGroup,vimCommentString
+  syn match    vimComment      +\<endif\s\+".*$+lc=5   contains=@vimCommentGroup,vimCommentString
+  syn match    vimComment      +\<else\s\+".*$+lc=4    contains=@vimCommentGroup,vimCommentString
+  syn region   vimCommentString        contained oneline start='\S\s\+"'ms=e   end='"'
+  " Vim9 comments - TODO: might be highlighted while they don't work
+  syn match    vim9Comment     excludenl +\s#[^{].*$+lc=1      contains=@vimCommentGroup,vimCommentString contained
+  syn match    vim9Comment     +\<endif\s\+#[^{].*$+lc=5       contains=@vimCommentGroup,vimCommentString contained
+  syn match    vim9Comment     +\<else\s\+#[^{].*$+lc=4        contains=@vimCommentGroup,vimCommentString contained
+  " Vim9 comment inside expression
+  syn match    vim9Comment     +\s\zs#[^{].*$+ms=s+1   contains=@vimCommentGroup,vimCommentString contained
+  syn match    vim9Comment     +^\s*#[^{].*$+          contains=@vimCommentGroup,vimCommentString contained
+  syn match    vim9Comment     +^\s*#$+                contains=@vimCommentGroup,vimCommentString contained
+endif

 " Environment Variables: {{{2
 " =====================
@@ -649,8 +667,13 @@ syn match  vimCtrlChar     "[^A-^H^K^O-^_]"

 " Beginners - Patterns that involve ^ {{{2
 " =========
-syn match      vimLineComment  +^[ \t:]*"\("[^"]*"\|[^"]\)*$+  contains=@vimCommentGroup,vimCommentString,vimCommentTitle
-syn match      vim9LineComment +^[ \t:]\+#.*$+ contains=@vimCommentGroup,vimCommentString,vimCommentTitle
+if s:isVim9Script
+  syn match    vimLineComment  +^[ \t:]*".*$+  contains=@vimCommentGroup,vimCommentString,vimCommentTitle contained
+  syn match    vim9LineComment +^[ \t:]\+#.*$+ contains=@vimCommentGroup,vimCommentString,vimCommentTitle
+else
+  syn match    vimLineComment  +^[ \t:]*".*$+  contains=@vimCommentGroup,vimCommentString,vimCommentTitle
+  syn match    vim9LineComment +^[ \t:]\+#.*$+ contains=@vimCommentGroup,vimCommentString,vimCommentTitle contained
+endif
 syn match      vimCommentTitle '"\s*\%([sS]:\|\h\w*#\)\=\u\w*\(\s\+\u\w*\)*:'hs=s+1    contained contains=vimCommentTitleLeader,vimTodo,@vimCommentGroup
 syn match      vimContinue     "^\s*\\"
 syn region     vimString       start="^\s*\\\z(['"]\)" skip='\\\\\|\\\z1' end="\z1" oneline keepend contains=@vimStringGroup,vimContinue

@Aster89
Copy link
Contributor

Aster89 commented Nov 30, 2022

I wonder if it is related to #11458.

@lukelbd
Copy link

lukelbd commented Mar 26, 2023

For anyone coming across this thread looking for a quick drop-in .vimrc fix (as I was), and who hasn't transitioned to vim9 script yet, the following seems to restore original syntax highlighting behavior:

function! s:syntax_overrides() abort
  syntax match customComment /^[ \t:]*".*$/  contains=vimCommentTitle,vimCommentString,@vimCommentGroup containedin=.*\(Body\|\)
  highlight link customComment vimComment
endfunction

augroup syntax_overrides
  au!
  au BufEnter .vimrc,*.vim call s:syntax_overrides()
augroup END

dkearns added a commit to dkearns/vim that referenced this issue Sep 16, 2023
This is a simple attempt to only highlight Vim9 and legacy script
comments in the appropriate syntactic contexts.

Vim9 comments are highlighted at top level in a Vim9 script file, in all
:def functions, and in all :au and :com command blocks.  Legacy script
comments are highlighted at top level in a legacy script file and in all
:func functions.

It attempts to fix vim#13047 and vim#11307 with minimal disruption to the
existing syntax definition as there is currently no maintainer
oversight.
dkearns added a commit to dkearns/vim that referenced this issue Feb 25, 2024
This is a simple attempt to only highlight Vim9 and legacy-script
comments in the appropriate syntactic contexts.

Vim9-script comments are highlighted at top level in a Vim9-script file,
in all :def functions, and in all :au and :com command blocks.
Legacy-script comments are highlighted at top level in a legacy script
file and in all :func functions.

Fixes vim#13047, vim#11307 and vim#9587.
@dkearns dkearns self-assigned this Feb 25, 2024
dkearns added a commit to dkearns/vim that referenced this issue Feb 25, 2024
This is a simple attempt to only highlight Vim9 and legacy-script
comments in the appropriate syntactic contexts.

Vim9-script comments are highlighted at top level in a Vim9-script file,
in all :def functions, and in all :au and :com command blocks.
Legacy-script comments are highlighted at top level in a legacy script
file and in all :func functions.

Fixes vim#13047, vim#11307 and vim#9587.
dkearns added a commit to dkearns/vim that referenced this issue Feb 25, 2024
This is a simple attempt to only highlight Vim9 and legacy-script
comments in the appropriate syntactic contexts.

Vim9-script comments are highlighted at top level in a Vim9-script file,
in all :def functions, and in all :au and :com command blocks.
Legacy-script comments are highlighted at top level in a legacy script
file and in all :func functions.

Fixes vim#13047, vim#11307 and vim#9587.
dkearns added a commit to dkearns/vim that referenced this issue Feb 25, 2024
This is a simple attempt to only highlight Vim9 and legacy-script
comments in the appropriate syntactic contexts.

Vim9-script comments are highlighted at top level in a Vim9-script file,
in all :def functions, and in all :au and :com command blocks.
Legacy-script comments are highlighted at top level in a legacy script
file, in all :func functions and in the Vim9-script preamble before the
:vim9script marker command.

Fixes vim#13047, vim#11307 and vim#9587.
@dkearns dkearns added the runtime label Mar 4, 2024
dkearns added a commit to dkearns/vim that referenced this issue Apr 7, 2024
This is a first-pass attempt to limit matching of Vim9 and legacy-script
comments to the appropriate syntactic contexts.

Vim9-script comments are highlighted at top level in a Vim9-script file,
in all :def functions, and in all :autocmd and :commmand command blocks.
Legacy-script comments are highlighted at top level in a legacy script
file, in all :func functions and in the Vim9-script preamble before the
:vim9script marker command.

Fixes vim#13047, vim#11307 and vim#9587.
dkearns added a commit to dkearns/vim that referenced this issue Apr 8, 2024
This is a first-pass attempt to limit matching of Vim9 and legacy-script
comments to the appropriate syntactic contexts.

Vim9-script comments are highlighted at top level in a Vim9-script file,
in all :def functions, and in all :autocmd and :commmand command blocks.
Legacy-script comments are highlighted at top level in a legacy script
file, in all :func functions and in the Vim9-script preamble before the
:vim9script marker command.

Fixes vim#13047, vim#11307 and vim#9587.
dkearns added a commit to dkearns/vim that referenced this issue Apr 8, 2024
This is a first-pass attempt to limit matching of Vim9 and legacy-script
comments to the appropriate syntactic contexts.

Vim9-script comments are highlighted at top level in a Vim9-script file,
in all :def functions, and in all :autocmd and :commmand command blocks.
Legacy-script comments are highlighted at top level in a legacy script
file, in all :func functions and in the Vim9-script preamble before the
:vim9script marker command.

Fixes vim#13047, vim#11307 and vim#9587.
dkearns added a commit to dkearns/vim that referenced this issue Apr 8, 2024
This is a first-pass attempt to limit matching of Vim9 and legacy-script
comments to the appropriate syntactic contexts.

Vim9-script comments are highlighted at top level in a Vim9-script file,
in all :def functions, and in all :autocmd and :commmand command blocks.
Legacy-script comments are highlighted at top level in a legacy script
file, in all :func functions and in the Vim9-script preamble before the
:vim9script marker command.

Fixes vim#13047, vim#11307 and vim#9587.
dkearns added a commit to dkearns/vim that referenced this issue Apr 9, 2024
This is a first-pass attempt to limit matching of Vim9 and legacy-script
comments to the appropriate syntactic contexts.

Vim9-script comments are highlighted at top level in a Vim9-script file,
in all :def functions, and in all :autocmd and :commmand command blocks.
Legacy-script comments are highlighted at top level in a legacy script
file, in all :func functions and in the Vim9-script preamble before the
:vim9script marker command.

Fixes vim#13047, vim#11307 and vim#9587.
dkearns added a commit to dkearns/vim that referenced this issue Apr 9, 2024
This is a first-pass attempt to limit matching of Vim9 and legacy-script
comments to the appropriate syntactic contexts.

Vim9-script comments are highlighted at top level in a Vim9-script file,
in all :def functions, and in all :autocmd and :commmand command blocks.
Legacy-script comments are highlighted at top level in a legacy script
file, in all :func functions and in the Vim9-script preamble before the
:vim9script marker command.

Fixes vim#13047, vim#11307 and vim#9587.
dkearns added a commit to dkearns/vim that referenced this issue Apr 10, 2024
This is a first-pass attempt to limit matching of Vim9 and legacy-script
comments to the appropriate syntactic contexts.

Vim9-script comments are highlighted at top level in a Vim9-script file,
in all :def functions, and in all :autocmd and :commmand command blocks.
Legacy-script comments are highlighted at top level in a legacy script
file, in all :func functions and in the Vim9-script preamble before the
:vim9script marker command.

Fixes vim#13047, vim#11307 and vim#9587.
chrisbra pushed a commit that referenced this issue Apr 10, 2024
…3104)

This is a first-pass attempt to limit matching of Vim9 and legacy-script
comments to the appropriate syntactic contexts.

Vim9-script comments are highlighted at top level in a Vim9-script file,
in all :def functions, and in all :autocmd and :commmand command blocks.
Legacy-script comments are highlighted at top level in a legacy script
file, in all :func functions and in the Vim9-script preamble before the
:vim9script marker command.

Fixes #13047, #11307 and #9587.

Signed-off-by: Doug Kearns <dougkearns@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
zeertzjq added a commit to zeertzjq/neovim that referenced this issue Apr 10, 2024
runtime(vim): Improve Vim9 and legacy-script comment highlighting (vim/vim#13104)

This is a first-pass attempt to limit matching of Vim9 and legacy-script
comments to the appropriate syntactic contexts.

Vim9-script comments are highlighted at top level in a Vim9-script file,
in all :def functions, and in all :autocmd and :commmand command blocks.
Legacy-script comments are highlighted at top level in a legacy script
file, in all :func functions and in the Vim9-script preamble before the
:vim9script marker command.

Fixes vim/vim#13047, vim/vim#11307 and vim/vim#9587.

vim/vim@04e5363

Co-authored-by: dkearns <dougkearns@gmail.com>
zeertzjq added a commit to neovim/neovim that referenced this issue Apr 10, 2024
runtime(vim): Improve Vim9 and legacy-script comment highlighting (vim/vim#13104)

This is a first-pass attempt to limit matching of Vim9 and legacy-script
comments to the appropriate syntactic contexts.

Vim9-script comments are highlighted at top level in a Vim9-script file,
in all :def functions, and in all :autocmd and :commmand command blocks.
Legacy-script comments are highlighted at top level in a legacy script
file, in all :func functions and in the Vim9-script preamble before the
:vim9script marker command.

Fixes vim/vim#13047, vim/vim#11307 and vim/vim#9587.

vim/vim@04e5363

Co-authored-by: dkearns <dougkearns@gmail.com>
@dkearns
Copy link
Contributor

dkearns commented Apr 11, 2024

Closing as I believe everything discussed in this issue is now highlighted properly.

@dkearns dkearns closed this as completed Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants