修改PLSQL.vim

Wayne posted @ Wed, 27 Jul 2011 07:23:56 +0000 in Experience , 4683 readers

 

手头一大批PLSQL DEVELOPER生成的“.sql”结尾的文件。直接用VIM打开的话,会采用某种SQL的代码高亮,我不知道是哪种,反正不是PLSQL,不能识别 replace 等关键字。每次都手动输入 :set ft=plsql的话,太麻烦了。所以我想修改为默认以 PLSQL 的模式来高亮 .sql 结尾的文件。
要实现这一点,倒也挺简单。查看“filetype.vim”文件,可以看到这一段:
 
" SQL
au BufNewFile,BufRead *.sql call s:SQL()

func! s:SQL()
  if exists("g:filetype_sql")
    exe "setf " . g:filetype_sql
  else
    setf sql
  endif
endfunc
 
可以发现,关键在于 g:filetype_sql 这个变量里。于是直接在 VIMRC 中写入 let g:filetype_sql="plsql", 然后再打开 .sql 的文件,果然地,已经按 PLSQL 模式来高亮了。
 
但是还有一些问题存在着。我的文件并非我手写而成,大部分是程序生成的,里面有不少 “PROMPT xxxx” 之类的东西,以及各种排版后的语句分行。“PROMPT xxx”这句最后的分号被原有的PLSQL高亮模式识别为错误符号而标红,并连累它底下的大片代码中的分号都被标红。至于排版导致的问题则让它把一个分成几行写的PLSQL String Literal给当成错误了,不该高亮的时候高亮,该高亮的时候不高亮,还标红,很明显,它只认一行之内的。
要解决这个问题,不得不修改 PLSQL.VIM了,这个文件在Syntax文件夹内。对于“PROMPT xxx”的解决思路,我是打算把它当成注释来处理。原有的注释基本上是这样写的:
" Various types of comments.
if exists("c_comment_strings")
  syntax match plsqlCommentSkip contained "^\s*\*\($\|\s\+\)"
  syntax region plsqlCommentString contained start=+L\="+ skip=+\\\\\|\\"+ end=+"+ end=+\*/+me=s-1 contains=plsqlCommentSkip
  syntax region plsqlComment2String contained start=+L\="+ skip=+\\\\\|\\"+ end=+"+ end="$"
  syntax region plsqlCommentL start="--" skip="\\$" end="$" keepend contains=@plsqlCommentGroup,plsqlComment2String,plsqlCharLiteral,plsqlBooleanLiteral,plsqlNumbersCom,plsqlSpaceError
  syntax region plsqlComment start="/\*" end="\*/" contains=@plsqlCommentGroup,plsqlComment2String,plsqlCharLiteral,plsqlBooleanLiteral,plsqlNumbersCom,plsqlSpaceError
else
  syntax region plsqlCommentL start="--" skip="\\$" end="$" keepend contains=@plsqlCommentGroup,plsqlSpaceError
  syntax region plsqlComment start="/\*" end="\*/" contains=@plsqlCommentGroup,plsqlSpaceError
endif

syn sync ccomment plsqlComment
syn sync ccomment plsqlCommentL
 
为了区别于原有的注释,我于是果断新建了一个注释组,加入
syntax region plsqlCommentLP start="PROMPT" skip="\\$" end="$" keepend contains=@plsqlCommentGroup,plsqlSpaceError

syn sync ccoment plsqlCommentLP
 
并在渲染的时候,另行指定了一个颜色:
HiLink plsqlCommentLP Operator
 
而对于String Literal,我没有看明白为何它原本无法识别多行,它原本采用的是一个正则表达式:
syn match   plsqlStringLiteral  "'\([^']\|''\)*'"
并没有不能跨行的限制,或者不跨行是VIM正则的内嵌机制?
 
解决办法倒不难,参考注释的写法,拿来直接用就是了:
syn region plsqlStringLiteral start="'" end="'"
 
无效化原来的那句后,保存。打开一份“.sql”文件,全部正常了。
Maha Board 9th Clas said:
Mon, 25 Sep 2023 06:08:05 +0000

Maha Board High School Students Summers Holidays Don’t Waste Time 9th Standard Pursuing Students can Download the Textbooks and take the hard copy for further use, we will update the Latest Information on this page. Maharashtra e-Books 2024 Latest Version to be Upload Every Year in Online Mode.Maharashtra Board High School Textbooks 2024 are Very Important to Students on the Maha Board 9th Class Books 2024 Preparations of the Monthly and Final Examination. Here, we are Providing the Latest Edition of MH Class Book 2024 which is Published by the Balbharati. All the chapters can be Downloaded in form of PDFs.Maharashtra High School Students Follows.


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter