From 93dbf1d86cd9aff11f6ee41f321976e1214a711b Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 10 Nov 2024 10:00:35 +0800 Subject: [PATCH] vim-patch:818c641: runtime(vim): Update base-syntax file, improve class, enum and interface highlighting - Enable folding of class, enum and interface declarations. - Highlight constructor names with the Function highlight group, like other special methods. - Mark function definitions using special method names as errors. - Highlight :type arguments. fixes: vim/vim#14393#issuecomment-2042796198. closes: vim/vim#13810 https://github.com/vim/vim/commit/818c641b6fac73b574a2b760213f515cee9a3c8e Co-authored-by: Doug Kearns --- runtime/doc/syntax.txt | 2 +- runtime/syntax/vim.vim | 177 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 158 insertions(+), 21 deletions(-) diff --git a/runtime/doc/syntax.txt b/runtime/doc/syntax.txt index 9d7dbd39c4..b9daff84f3 100644 --- a/runtime/doc/syntax.txt +++ b/runtime/doc/syntax.txt @@ -3493,7 +3493,7 @@ This option is disabled by default. Some folding is now supported with when 'foldmethod' is set to "syntax": > g:vimsyn_folding == 0 or doesn't exist: no syntax-based folding - g:vimsyn_folding =~ 'a' : augroups + g:vimsyn_folding =~ 'a' : fold augroups g:vimsyn_folding =~ 'f' : fold functions g:vimsyn_folding =~ 'h' : fold heredocs g:vimsyn_folding =~ 'l' : fold Lua script diff --git a/runtime/syntax/vim.vim b/runtime/syntax/vim.vim index b2a703eba9..602e22b258 100644 --- a/runtime/syntax/vim.vim +++ b/runtime/syntax/vim.vim @@ -74,12 +74,22 @@ syn case match com! -nargs=* Vim9 execute s:vim9script ? "" : "contained" com! -nargs=* VimL execute s:vim9script ? "contained" : "" -if exists("g:vimsyn_folding") && g:vimsyn_folding =~# '[afhHlmpPrt]' +if exists("g:vimsyn_folding") && g:vimsyn_folding =~# '[acefhiHlmpPrt]' if g:vimsyn_folding =~# 'a' com! -nargs=* VimFolda fold else com! -nargs=* VimFolda endif + if g:vimsyn_folding =~# 'c' + com! -nargs=* VimFoldc fold + else + com! -nargs=* VimFoldc + endif + if g:vimsyn_folding =~# 'e' + com! -nargs=* VimFolde fold + else + com! -nargs=* VimFolde + endif if g:vimsyn_folding =~# 'f' com! -nargs=* VimFoldf fold else @@ -95,6 +105,11 @@ if exists("g:vimsyn_folding") && g:vimsyn_folding =~# '[afhHlmpPrt]' else com! -nargs=* VimFoldH endif + if g:vimsyn_folding =~# 'i' + com! -nargs=* VimFoldi fold + else + com! -nargs=* VimFoldi + endif if g:vimsyn_folding =~# 'l' com! -nargs=* VimFoldl fold else @@ -127,7 +142,10 @@ if exists("g:vimsyn_folding") && g:vimsyn_folding =~# '[afhHlmpPrt]' endif else com! -nargs=* VimFolda + com! -nargs=* VimFoldc + com! -nargs=* VimFolde com! -nargs=* VimFoldf + com! -nargs=* VimFoldi com! -nargs=* VimFoldh com! -nargs=* VimFoldH com! -nargs=* VimFoldl @@ -177,8 +195,8 @@ syn match vimNumber '\%(^\|\A\)\zs#\x\{6}' skipwhite nextgroup=vimGlobal,vimSub syn case match " All vimCommands are contained by vimIsCommand. {{{2 -syn cluster vimCmdList contains=vimAbb,vimAddress,vimAutoCmd,vimAugroup,vimBehave,vimCall,vimCatch,vimConst,vimDef,vimDelcommand,@vimEcho,vimEnddef,vimEndfunction,vimExecute,vimIsCommand,vimExtCmd,vimFor,vimFunction,vimFuncFold,vimGlobal,vimHighlight,vimLet,vimLoadkeymap,vimMap,vimMark,vimMatch,vimNotFunc,vimNorm,vimSet,vimSleep,vimSyntax,vimThrow,vimUnlet,vimUnmap,vimUserCmd,vimMenu,vimMenutranslate,@vim9CmdList -syn cluster vim9CmdList contains=vim9Const,vim9Final,vim9For,vim9Var +syn cluster vimCmdList contains=vimAbb,vimAddress,vimAutoCmd,vimAugroup,vimBehave,vimCall,vimCatch,vimConst,vimDef,vimDefFold,vimDelcommand,@vimEcho,vimEnddef,vimEndfunction,vimExecute,vimIsCommand,vimExtCmd,vimFor,vimFunction,vimFuncFold,vimGlobal,vimHighlight,vimLet,vimLoadkeymap,vimMap,vimMark,vimMatch,vimNotFunc,vimNorm,vimSet,vimSleep,vimSyntax,vimThrow,vimUnlet,vimUnmap,vimUserCmd,vimMenu,vimMenutranslate,@vim9CmdList +syn cluster vim9CmdList contains=vim9Class,vim9Const,vim9Enum,vim9Export,vim9Final,vim9For,vim9Interface,vim9Type,vim9Var syn match vimCmdSep "[:|]\+" skipwhite nextgroup=@vimCmdList,vimSubst1 syn match vimIsCommand "\<\%(\h\w*\|[23]mat\%[ch]\)\>" contains=vimCommand syn match vimBang contained "!" @@ -220,6 +238,12 @@ syn keyword vimThrow th[row] skipwhite nextgroup=@vimExprList syn keyword vimCatch cat[ch] skipwhite nextgroup=vimCatchPattern syn region vimCatchPattern contained matchgroup=Delimiter start="\z([!#$%&'()*+,-./:;<=>?@[\]^_`{}~]\)" skip="\\\\\|\\\z1" end="\z1" contains=@vimSubstList oneline +" Export {{{2 +" ====== +if s:vim9script + syn keyword vim9Export export skipwhite nextgroup=vim9Abstract,vim9ClassBody,vim9Const,vim9Def,vim9EnumBody,vim9Final,vim9InterfaceBody,vim9Type,vim9Var +endif + " Filetypes {{{2 " ========= syn match vimFiletype "\" skipwhite nextgroup=vimCmdSep,vimCommen syn match vimDef "\" skipwhite nextgroup=vimCmdSep,vimComment,vimFuncPattern contains=vimDefKey syn match vimFunction "\!\=\s*\%(<[sS][iI][dD]>\|[sg]:\)\=\%(\i\|[#.]\|{.\{-1,}}\)\+" contains=@vimFuncList skipwhite nextgroup=vimFuncParams -syn match vimDef "\!\=\s*\%(<[sS][iI][dD]>\|[sg]:\)\=\%(\i\|[#.]\|{.\{-1,}}\)\+" contains=@vimDefList,vimMethodName nextgroup=vimDefParams +syn match vimDef "\!\=\s*\%(<[sS][iI][dD]>\|[sg]:\)\=\%(\i\|[#.]\|{.\{-1,}}\)\+" contains=@vimDefList nextgroup=vimDefParams syn match vimFuncComment contained +".*+ skipwhite skipempty nextgroup=vimFuncBody,vimEndfunction syn match vimDefComment contained "#.*" skipwhite skipempty nextgroup=vimDefBody,vimEnddef @@ -284,7 +307,6 @@ syn match vimFuncSID contained "\c" syn match vimFuncSID contained "\<[sg]:" syn keyword vimFuncKey contained fu[nction] syn keyword vimDefKey contained def -syn keyword vimMethodName contained empty len string syn region vimFuncParams contained matchgroup=Delimiter start="(" skip=+\n\s*\\\|\n\s*"\\ + end=")" skipwhite skipempty nextgroup=vimFuncBody,vimFuncComment,vimEndfunction,vimFuncMod,vim9CommentError contains=vimFuncParam,@vimContinue syn region vimDefParams contained matchgroup=Delimiter start="(" end=")" skipwhite skipempty nextgroup=vimDefBody,vimDefComment,vimEnddef,vimReturnType,vimCommentError contains=vimDefParam,vim9Comment,vimFuncParamEquals @@ -301,9 +323,8 @@ syn match vimEndfunction "\" skipwhite nextgroup=vimCmdSep,vim syn match vimEnddef "\" skipwhite nextgroup=vimCmdSep,vim9Comment,vimCommentError if exists("g:vimsyn_folding") && g:vimsyn_folding =~# 'f' - syn region vimFuncFold start="\!\=\s*\%(<[sS][iI][dD]>\|[sg]:\)\=\%(\i\|[#.]\|{.\{-1,}}\)\+\s*(" end="\" contains=vimFunction fold keepend extend transparent - syn region vimFuncFold start="\!\=\s*\%(<[sS][iI][dD]>\|[sg]:\)\=\%(\i\|[#.]\)\+(" end="\" contains=vimDef fold keepend extend transparent - syn region vimFuncFold start="\" @@ -324,6 +345,100 @@ syn match vimUserType contained "\<\u\w*\>" syn cluster vimType contains=vimType,vimCompoundType,vimUserType +" Classes, Enums And Interfaces: {{{2 +" ============================= + +if s:vim9script + " Methods {{{3 + syn match vim9MethodDef contained "\" skipwhite nextgroup=vim9MethodDefName + syn match vim9MethodDefName contained "\<\h\w*\>" nextgroup=vim9MethodDefParams contains=@vim9MethodName + syn region vim9MethodDefParams contained + \ matchgroup=Delimiter start="(" end=")" + \ skipwhite skipnl nextgroup=vim9MethodDefBody,vimDefComment,vimEnddef,vim9MethodDefReturnType,vimCommentError + \ contains=vimDefParam,vim9Comment,vimFuncParamEquals + syn region vim9MethodDefReturnType contained + \ start=":\s" end="$" matchgroup=vim9Comment end="\ze[#"]" + \ skipwhite skipnl nextgroup=vim9MethodDefBody,vimDefComment,vimCommentError + \ contains=vimTypeSep + \ transparent + syn region vim9MethodDefBody contained + \ start="^.\=" matchgroup=vimCommand end="\" + \ skipwhite nextgroup=vimCmdSep,vim9Comment,vimCommentError + \ contains=@vim9MethodDefBodyList + + syn cluster vim9MethodDefBodyList contains=@vimDefBodyList,vim9This,vim9Super + + if !exists("g:vimsyn_noerror") && !exists("g:vimsyn_novimfunctionerror") + syn match vim9MethodNameError contained "\<[a-z0-9]\i\>" + endif + syn match vim9MethodName contained "\" + syn keyword vim9MethodName contained empty len string + + syn cluster vim9MethodName contains=vim9MethodName,vim9MethodNameError + + if exists("g:vimsyn_folding") && g:vimsyn_folding =~# 'f' + syn region vim9MethodDefFold contained start="\%(^\s*\%(:\=static\s\+\)\=\)\@16<=:\=def\s\+\h\i*(" end="^\s*:\=enddef\>" contains=vim9MethodDef fold keepend extend transparent + syn region vim9MethodDefFold contained start="^\s*:\=def\s\+new\i*(" end="^\s*:\=enddef\>" contains=vim9MethodDef fold keepend extend transparent + endif + + syn cluster vim9MethodDef contains=vim9MethodDef,vim9MethodDefFold + + " Classes {{{3 + syn cluster vim9ClassBodyList contains=vim9Abstract,vim9Class,vim9Comment,vim9LineComment,@vim9Continue,@vimExprList,vim9Extends,vim9Implements,@vim9MethodDef,vim9Public,vim9Static,vim9Const,vim9Final,vim9This,vim9Super,vim9Var + + syn match vim9Class contained "\" skipwhite nextgroup=vim9ClassName + syn match vim9ClassName contained "\<\u\w*\>" skipwhite skipnl nextgroup=vim9Extends,vim9Implements + syn match vim9SuperClass contained "\<\u\w*\>" skipwhite skipnl nextgroup=vim9Implements + syn match vim9ImplementedInterface contained "\<\u\w*\>" skipwhite skipnl nextgroup=vim9InterfaceListComma,vim9Extends + syn match vim9InterfaceListComma contained "," skipwhite skipnl nextgroup=vim9ImplementedInterface + syn keyword vim9Abstract abstract skipwhite skipnl nextgroup=vim9ClassBody,vim9AbstractDef + syn keyword vim9Extends contained extends skipwhite skipnl nextgroup=vim9SuperClass + syn keyword vim9Implements contained implements skipwhite skipnl nextgroup=vim9ImplementedInterface + syn keyword vim9Public contained public + syn keyword vim9Static contained static + syn keyword vim9This contained this + syn keyword vim9Super contained super + + VimFoldc syn region vim9ClassBody start="\" matchgroup=vimCommand end="\" contains=@vim9ClassBodyList transparent + + " Enums {{{3 + syn cluster vim9EnumBodyList contains=vim9Comment,vim9LineComment,@vim9Continue,vim9Enum,vim9Implements,@vim9MethodDef,vim9Const,vim9Final,vim9Var + + syn match vim9Enum contained "\" skipwhite nextgroup=vim9EnumName + syn match vim9EnumName contained "\<\u\w*\>" skipwhite skipnl nextgroup=vim9Implements + + VimFolde syn region vim9EnumBody start="\" matchgroup=vimCommand end="\" contains=@vim9EnumBodyList transparent + + " Interfaces {{{3 + " TODO: limit to decl only - no init values + syn cluster vim9InterfaceBodyList contains=vim9Comment,vim9LineComment,@vim9Continue,vim9Extends,vim9Interface,vim9AbstractDef,vim9Var + + syn match vim9Interface contained "\" skipwhite nextgroup=vim9InterfaceName + syn match vim9InterfaceName contained "\<\u\w*\>" skipwhite skipnl nextgroup=vim9Extends + + syn keyword vim9AbstractDef contained def skipwhite nextgroup=vim9AbstractDefName + syn match vim9AbstractDefName contained "\<\h\w*\>" skipwhite nextgroup=vim9AbstractDefParams contains=@vim9MethodName + syn region vim9AbstractDefParams contained + \ matchgroup=Delimiter start="(" end=")" + \ skipwhite skipnl nextgroup=vimDefComment,vim9AbstractDefReturnType,vimCommentError + \ contains=vimDefParam,vim9Comment,vimFuncParamEquals + syn region vim9AbstractDefReturnType contained + \ start=":\s" end="$" matchgroup=vim9Comment end="\ze[#"]" + \ skipwhite skipnl nextgroup=vimDefComment,vimCommentError + \ contains=vimTypeSep + \ transparent + + VimFoldi syn region vim9InterfaceBody start="\" matchgroup=vimCommand end="\" contains=@vim9InterfaceBodyList transparent + + " type {{{3 + syn match vim9Type "\" skipwhite nextgroup=vim9TypeAlias,vim9TypeAliasError + syn match vim9TypeAlias contained "\<\u\w*\>" skipwhite nextgroup=vim9TypeEquals + syn match vim9TypeEquals contained "=" skipwhite nextgroup=@vimType + if !exists("g:vimsyn_noerror") && !exists("g:vimsyn_notypealiaserror") + syn match vim9TypeAliasError contained "\<\U\w*" + endif +endif + " Keymaps: {{{2 " ======= @@ -542,7 +657,7 @@ Vim9 syn keyword vim9Const const skipwhite nextgroup=vim9Variable,vim9VariableLi Vim9 syn keyword vim9Final final skipwhite nextgroup=vim9Variable,vim9VariableList Vim9 syn keyword vim9Var var skipwhite nextgroup=vim9Variable,vim9VariableList -syn match vim9Variable contained "\<\h\w*\>" skipwhite nextgroup=vimTypeSep,vimLetHereDoc +syn match vim9Variable contained "\<\h\w*\>" skipwhite nextgroup=vimTypeSep,vimLetHereDoc,vimOper syn region vim9VariableList contained start="\[" end="]" contains=vim9Variable,@vimContinue " For: {{{2 @@ -669,7 +784,7 @@ syn case match " (following Gautam Iyer's suggestion) " ========================== syn match vimFunc "\%(\%([sSgGbBwWtTlL]:\|<[sS][iI][dD]>\)\=\%(\w\+\.\)*\I[a-zA-Z0-9_.]*\)\ze\s*(" contains=vimFuncEcho,vimFuncName,vimUserFunc,vimExecute -syn match vimUserFunc contained "\%(\%([sSgGbBwWtTlL]:\|<[sS][iI][dD]>\)\=\%(\w\+\.\)*\I[a-zA-Z0-9_.]*\)\|\<\u[a-zA-Z0-9.]*\>\|\" contains=vimNotation,vimMethodName +syn match vimUserFunc contained "\%(\%([sSgGbBwWtTlL]:\|<[sS][iI][dD]>\)\=\%(\w\+\.\)*\I[a-zA-Z0-9_.]*\)\|\<\u[a-zA-Z0-9.]*\>\|\" contains=vimNotation,vim9MethodName syn keyword vimFuncEcho contained ec ech echo syn match vimMap "\\%([(#[]\|\s\+\%([-+*/%]\=\|\.\.\)=\)\@ " Errors And Warnings: {{{2 " ==================== if !exists("g:vimsyn_noerror") && !exists("g:vimsyn_novimfunctionerror") - " TODO: The new-prefix exception should only apply to constructor definitions. - " TODO: The |builtin-object-methods| exception should only apply to method - " definitions. - syn match vimFunctionError "\s\zs\%(empty\|len\|new\|string\)\@![a-z0-9]\i\{-}\ze\s*(" contained contains=vimFuncKey,vimFuncBlank + syn match vimFunctionError "\s\zs[a-z0-9]\i\{-}\ze\s*(" contained contains=vimFuncKey,vimFuncBlank syn match vimFunctionError "\s\zs\%(<[sS][iI][dD]>\|[sSgGbBwWtTlL]:\)\d\i\{-}\ze\s*(" contained contains=vimFuncKey,vimFuncBlank syn match vimElseIfErr "\" syn match vimBufnrWarn /\