使用Vim编写PHP时如何修复attributes语法被识别为注释的问题
解决方法
你遇到的冲突是因为默认PHP语法规则里的phpComment匹配项没有被清除,新旧规则同时生效导致的,无需完整学习Vim语法系统,按以下步骤修改after/syntax/php.vim即可:
- 先清除原有
phpComment相关的语法定义,避免规则冲突 - 重新定义注释匹配规则,排除
#[开头的场景 - 可选:为PHP Attribute新增单独的高亮规则,保证语法显示正常
完整配置代码如下:
" 先清除原有phpComment规则 syntax clear phpComment " 重定义块注释规则,和默认逻辑保持一致 if exists("php_parent_error_open") syn region phpComment start="/\*" end="\*/" fold contained contains=phpTodo,@Spell else syn region phpComment start="/\*" end="\*/" fold contained contains=phpTodo,@Spell extend endif " 重定义单行注释规则,#后面紧跟[的情况不匹配为注释 syn match phpComment "#[^[].\{-}\(?\>\|$\)\@=" contained contains=phpTodo,@Spell syn match phpComment "//.\{-}\(?\>\|$\)\@=" contained contains=phpTodo,@Spell " 新增PHP Attribute语法规则,可自定义高亮样式 syn region phpAttribute start="#\[rs=2" end="\]" contained contains=phpConstant,phpString,phpNumber " 把Attribute高亮链接到预处理器类的样式,和注释区分开 hi def link phpAttribute PreProc
配置保存后重启Vim,或者打开PHP文件后执行:syn off | syn on重新加载语法规则即可生效。
你之前尝试的matchdelete是用来删除matchadd()添加的临时高亮规则的,不适用于删除Vim语法系统定义的匹配项,所以会报错。
内容的提问来源于stack exchange,提问作者Clément Cartier
相关产品推荐
相关产品推荐

