Vim C#语法高亮显示方法调用就好像它们是类型一样

时间:2015-07-13 11:45:23

标签: regex vim vim-syntax-highlighting

我在语法文件中遇到问题我在使用括号后突出显示方法调用的地方,好像它们是类型/类一样。

enter image description here

我找到了责任界:

syn match csClass contained       /\<[A-Z][a-z]\w\+/ nextgroup=csGeneric

证明如果我从此群组中删除csClass,我就不会突出显示图片中突出显示的方法:

syn region csEnclosed start="(" end=")" contains=csConstant, csType, csString, csVerbatimString, csCharacter, csNumber, csIface, csClass

这里是完整的cs.vim文件:

" Vim syntax file
" Language:             Microsoft C#
" Maintainer:           Kian Ryan (kian@orangetentacle.co.uk)
" Former Maintainer:    Anduin Withers (awithers@anduin.com)
" Filenames:            *.cs
"
" REFERENCES:
" [1] ECMA TC39: C# Language Specification (WD13Oct01.doc)

if exists("b:current_syntax")
    finish
endif

let s:cs_cpo_save = &cpo
set cpo&vim

" type
syn keyword csType          bool byte char decimal double float int long object sbyte short string uint ulong ushort void var dynamic
" storage
syn keyword csTypeDecleration           class enum struct nextgroup=csClass skipwhite
syn keyword csStorage           delegate interface namespace struct
syn keyword csInterfaceDecleration      interface nextgroup=csIface skipwhite
" repeat / condition / label
syn keyword csRepeat            break continue do for foreach goto return while
syn keyword csConditional       else if switch
syn keyword csLabel         case default
" :: is usually an error in C#, except for the special case of "global::"
syn match csOperatorError       display +::+
syn match csGlobal          display +global::+
" user labels (see [1] 8.6 Statements)
syn match   csLabel         display +^\s*\I\i*\s*:\([^:]\)\@=+
" modifier
syn keyword csModifier          abstract const extern internal override private protected public readonly sealed static virtual volatile nextgroup=CsClass,CsIface skipwhite
" constant
syn keyword csConstant          false null true
" exception
syn keyword csException         try catch finally throw

" TODO:
syn keyword csUnspecifiedStatement  as base checked event fixed in is lock operator out params ref sizeof stackalloc this unchecked unsafe using 
" TODO:
syn keyword csUnsupportedStatement  add remove value
" TODO:
syn keyword csUnspecifiedKeyword    explicit implicit

syn keyword csTypeOf                    typeof nextgroup=csEnclosed

" Linq Keywords
"syn keyword csLinq                      from where select group into orderby join let in on equals by ascending descending

" Async Keywords
syn keyword csAsync                     async await

" Contextual Keywords
syn match csContextualStatement /\<yield[[:space:]\n]\+\(return\|break\)/me=s+5
syn match csContextualStatement /\<partial[[:space:]\n]\+\(class\|struct\|interface\)/me=s+7
syn match csContextualStatement /\<\(get\|set\);/me=s+3
syn match csContextualStatement /\<\(get\|set\)[[:space:]\n]*{/me=s+3
syn match csContextualStatement /\<where\>[^:]\+:/me=s+5

"New Declerations
syn keyword csNewDecleration            new nextgroup=csClass skipwhite

"Interface & Class Identifier
syn match csClass contained       /\<[A-Z][a-z]\w\+/ nextgroup=csGeneric
syn match csIface contained       /\<I[A-Z][a-z]\w\+/ nextgroup=csGeneric
" syn region csGeneric start="<" end=">" contains=csIface,csClass
syn region csEnclosed start="(" end=")" contains=csConstant,csType,csString, csVerbatimString, csCharacter, csNumber,csIface,csClass
"syn region csInherits start=":" end="{" contains=csIface,csClass

" Attributes
syn region csAttribute start="^\s*\[" end="\]\s*" contains=csString, csVerbatimString, csCharacter, csNumber, csType


" Comments
"
" PROVIDES: @csCommentHook
"
" TODO: include strings ?
"
syn keyword csTodo      contained TODO FIXME XXX NOTE
syn region  csComment       start="/\*"  end="\*/" contains=@csCommentHook,csTodo,@Spell
syn match   csComment       "//.*$" contains=@csCommentHook,csTodo,@Spell

" xml markup inside '///' comments
syn cluster xmlRegionHook   add=csXmlCommentLeader
syn cluster xmlCdataHook    add=csXmlCommentLeader
syn cluster xmlStartTagHook add=csXmlCommentLeader
syn keyword csXmlTag        contained Libraries Packages Types Excluded ExcludedTypeName ExcludedLibraryName
syn keyword csXmlTag        contained ExcludedBucketName TypeExcluded Type TypeKind TypeSignature AssemblyInfo
syn keyword csXmlTag        contained AssemblyName AssemblyPublicKey AssemblyVersion AssemblyCulture Base
syn keyword csXmlTag        contained BaseTypeName Interfaces Interface InterfaceName Attributes Attribute
syn keyword csXmlTag        contained AttributeName Members Member MemberSignature MemberType MemberValue
syn keyword csXmlTag        contained ReturnValue ReturnType Parameters Parameter MemberOfPackage
syn keyword csXmlTag        contained ThreadingSafetyStatement Docs devdoc example overload remarks returns summary
syn keyword csXmlTag        contained threadsafe value internalonly nodoc exception param permission platnote
syn keyword csXmlTag        contained seealso b c i pre sub sup block code note paramref see subscript superscript
syn keyword csXmlTag        contained list listheader item term description altcompliant altmember

syn cluster xmlTagHook add=csXmlTag

syn match   csXmlCommentLeader  +\/\/\/+    contained
syn match   csXmlComment    +\/\/\/.*$+ contains=csXmlCommentLeader,@csXml,@Spell
syntax include @csXml syntax/xml.vim
hi def link xmlRegion Comment


" [1] 9.5 Pre-processing directives
syn region  csPreCondit
    \ start="^\s*#\s*\(define\|undef\|if\|elif\|else\|endif\|line\|error\|warning\)"
    \ skip="\\$" end="$" contains=csComment keepend
syn region  csRegion matchgroup=csPreCondit start="^\s*#\s*region.*$"
    \ end="^\s*#\s*endregion" transparent fold contains=TOP

syn region csFold start="{" end="}" transparent fold

" Strings and constants
syn match   csSpecialError  contained "\\."
syn match   csSpecialCharError  contained "[^']"
" [1] 9.4.4.4 Character literals
syn match   csSpecialChar   contained +\\["\\'0abfnrtvx]+
" unicode characters
syn match   csUnicodeNumber +\\\(u\x\{4}\|U\x\{8}\)+ contained contains=csUnicodeSpecifier
syn match   csUnicodeSpecifier  +\\[uU]+ contained
syn region  csVerbatimString    start=+@"+ end=+"+ skip=+""+ contains=csVerbatimSpec,@Spell
syn match   csVerbatimSpec  +@"+he=s+1 contained
syn region  csString        start=+"+  end=+"+ end=+$+ contains=csSpecialChar,csSpecialError,csUnicodeNumber,@Spell
syn match   csCharacter     "'[^']*'" contains=csSpecialChar,csSpecialCharError
syn match   csCharacter     "'\\''" contains=csSpecialChar
syn match   csCharacter     "'[^\\]'"
syn match   csNumber        "\<\(0[0-7]*\|0[xX]\x\+\|\d\+\)[lL]\=\>"
syn match   csNumber        "\(\<\d\+\.\d*\|\.\d\+\)\([eE][-+]\=\d\+\)\=[fFdD]\="
syn match   csNumber        "\<\d\+[eE][-+]\=\d\+[fFdD]\=\>"
syn match   csNumber        "\<\d\+\([eE][-+]\=\d\+\)\=[fFdD]\>"

" The default highlighting.
hi def link csType          Type
hi def link csTypeDecleration       StorageClass
hi def link csInterfaceDecleration      StorageClass
hi def link csNewDecleration            StorageClass
hi def link csStorage           StorageClass
hi def link csRepeat            Repeat
hi def link csConditional       Conditional
hi def link csLabel         Label
hi def link csModifier          StorageClass
hi def link csConstant          Constant
hi def link csException         Exception
hi def link csUnspecifiedStatement  Statement
hi def link csUnsupportedStatement  Statement
hi def link csUnspecifiedKeyword    Keyword
"hi def link csLinq                      Keyword
hi def link csAsync                     Keyword
hi def link csContextualStatement   Statement
hi def link csOperatorError     Error
hi def link csAttribute         PreProc

hi def link csIface                     Type
hi def link csClass                     Type

hi def link csTodo          Todo
hi def link csComment           Comment

hi def link csSpecialError      Error
hi def link csSpecialCharError      Error
hi def link csString            String
hi def link csVerbatimString        String
hi def link csVerbatimSpec      SpecialChar
hi def link csPreCondit         PreCondit
hi def link csCharacter         Character
hi def link csSpecialChar       SpecialChar
hi def link csNumber            Number
hi def link csUnicodeNumber     SpecialChar
hi def link csUnicodeSpecifier      SpecialChar
hi def link csTypeOf                    Keyword

" xml markup
hi def link csXmlCommentLeader      Comment
hi def link csXmlComment        Comment
hi def link csXmlTag            Statement

let b:current_syntax = "cs"

let &cpo = s:cs_cpo_save
unlet s:cs_cpo_save

我想我是否可以参加csClass比赛并尝试告诉它不应该以点开头。&#39;它应该解决它,对吗?但我不熟悉vim的正则表达式(我使用常规正则表达式)。我试过这个:

syn match csClass contained       /^[^\.]\<[A-Z][a-z]\w\+/ nextgroup=csGeneric

意图是否定.(不应该以{{1​​}}开头),但它不起作用,它会禁用所有类突出显示。

实现这一目标的正确方法是什么?

非常感谢帮助!

1 个答案:

答案 0 :(得分:1)

您可以使用

/[^.]\<[A-Z][a-z]\w\+/

由于此字符串可以出现在一行的任何位置,因此我们不需要^字符串开头的锚点。

否定字符类[^.]匹配任何字符,但字符点(不需要在字符类中转义)。

所以,整个正则表达式意味着:

  • [^.] - 除字面点之外的任何字符
  • \< - 开始字边界(注意VIM样式正则表达式)
  • [A-Z] - 1个英文大写字母
  • [a-z] - 1个英文小写字母
  • \w\+ - 一个或多个字母数字字符(注意VIM-style转义量词:
  

\+匹配前面一个或多个字符...

<强>更新

根据@ lcd047的评论,您可以使用\zs作为后备替代品:

/[^.]\zs\<[A-Z][a-z]\w\+/

来自VIM Wiki

  

\zs在任何位置匹配,并在那里设置匹配的开始:下一个字符是整个匹配的第一个字符。   ...   
实施例
  /^\s*\zsif
      匹配行开头的“if”,忽略空格。       
可以多次使用,最后一次遇到匹配       使用分支。