VIM:简化的单语言项目脚本──对Java项目开发的支持

本插件属于ftplugin,并且依赖于 shellinsidevim.vim

本插件使得你可以直接在vim编译和运行带有包的java类,并且如果改类属于一个java项目,它会自动检测项目环境(不要求你一定位于项目文件夹下),并且生成正确的编译参数。这些参数包括:classpath、sourcepath、输出目录、参数文件。以上参数的意义请自行参考javac文档。

如果上述参数不够,你可以直接在命令模式输入:Javac ,后跟参数。注意,这个参数是对上述的补充,你不用在额外指定上述参数。并且,每次输入的参数都将自动保存,直到重启vim或者在Javac命令后输入“:”再接参数。

Java命令同上。


此脚本可以方便的修改,使得应用于其他的语言环境,比如flex。

此插件的功能比较简单,更强大的脚本还在测试中。

  1. "Vimglobalpluginforrunningjavaproject
  2. "Version:2.0
  3. "Maintainer:WarGrey<yoshua@gmail.com>
  4. "Lastchange:2008Dec22
  5. "


  6. "Thisgroupsofvariablesareusedtogeneratingtheoptionsforcompiling
  7. lets:LibraryOption="-classpathLO"
  8. lets:SourcepathOption="-sourcepathSO"
  9. lets:DistinationOption="-dDO"
  10. lets:ArgumentFileOption="@AFO"
  11. lets:ExtraCompileOptions=""
  12. lets:ExtraExecuteOptions=""


  13. "Commandsforcompilingandexecuting
  14. lets:Compiler="javac"
  15. lets:Interpretor="java"
  16. lets:Executor=""


  17. "Thisgroupsofvariablesarereferencingtothefoldsthatreallyexistsbaseonthenextgroups
  18. lets:ProjectRoot=''
  19. lets:SourcePath=''
  20. lets:DistinationPath=''
  21. lets:LibraryPath=''
  22. lets:ArgumentFile=''


  23. "Thisgroupsofvariablesareusedtodetectingtheenvironment
  24. if!exists("g:Java_Sources")
  25. letg:Java_Sources="src:java_src"
  26. endif
  27. if!exists("g:Java_Distinations")
  28. letg:Java_Distinations="classes:java_classes"
  29. endif
  30. if!exists("g:Java_Libraries")
  31. letg:Java_Libraries="lib:java_lib"
  32. endif
  33. if!exists("g:Java_ArgumentFiles")
  34. letg:Java_ArgumentFiles="classpath.javac:argument.javac:source.javac"
  35. endif


  36. "Excommandwhichtake0ormore(upto20)parameters
  37. command!-complete=file-nargs=*Javaccalls:CLCompile(<f-args>)
  38. command!-complete=file-nargs=*Javacalls:CLExecute(<f-args>)


  39. "Mapkeystofunctioncalls
  40. map<unique><buffer><F5>:call<SID>Compile()<CR>
  41. map<unique><buffer><M-F5>:Javac
  42. map<unique><buffer><F6>:call<SID>Execute()<CR>
  43. map<unique><buffer><M-F6>:Java
  44. map<unique><buffer><F9>:call<SID>ToggleExecutor()<CR>


  45. imap<unique><buffer><F5><ESC><F5>
  46. imap<unique><buffer><M-F5><ESC><M-F5>
  47. imap<unique><buffer><F6><ESC><F6>
  48. imap<unique><buffer><M-F6><ESC><M-F6>
  49. imap<unique><buffer><F9><ESC><F9>


  50. function!s:InitEnvironment()
  51. ifs:IsJavaFile()==0|return0|endif

  52. letcprt=s:ProjectRoot
  53. lets:Executor=s:GetFullClassName()
  54. lettail='/'.substitute(s:Executor,'\.','/','g')
  55. letsrc=fnamemodify(bufname("%"),":p:r")
  56. if(match(src,tail)+strlen(tail))==strlen(src)
  57. letsrc=substitute(src,tail,'','g')
  58. else
  59. callg:EchoErrorMsg("Thisclasshasanincorrectpackage!")
  60. endif
  61. lets:ProjectRoot=src

  62. letisProject=0
  63. letsrcs=split(g:Java_Sources,":")
  64. letdists=split(g:Java_Distinations,":")
  65. whilesrc!=fnamemodify(src,":h")
  66. forsdinsrcs
  67. if(strlen(sd)+match(src,sd))==strlen(src)
  68. letroot=substitute(src,"/".sd,"","g")
  69. fordidindists
  70. letdist=fnamemodify(root."/".did,":p")
  71. let csrc=fnamemodify(root."/".sd,":p")
  72. ifisdirectory(dist) && isdirectory(sd)
  73. lets:ProjectRoot=root
  74. lets:DistinationPath=dist
  75. letisProject=1
  76. break
  77. endif
  78. endfor
  79. ifisProject==1|break|endif
  80. endif
  81. endfor
  82. ifisProject==1|break|endif
  83. letsrc=fnamemodify(src,":h")
  84. endwhile

  85. ifisProject==1
  86. lets:SourcePath=s:PreparePath(g:Java_Sources,0)
  87. lets:LibraryPath=s:PreparePath(g:Java_Libraries,0)
  88. lets:ArgumentFile=s:PreparePath(g:Java_ArgumentFiles,1)
  89. else
  90. lets:DistinationPath=s:ProjectRoot
  91. lets:LibraryPath=s:ProjectRoot
  92. endif

  93. ifcprt!=s:ProjectRoot
  94. callg:EchoMoreMsg("Theprojectrootis".s:ProjectRoot)
  95. ifcprt!=''
  96. callg:EchoWarningMsg("Thisprojectisnottheoriginalone!")
  97. endif
  98. endif
  99. endfunction


  100. function!s:ToggleExecutor()
  101. ifs:IsJavaFile()==0|return0|endif
  102. letcer=s:Executor
  103. calls:InitEnvironment()
  104. ifs:Executor==cer
  105. callg:EchoWarningMsg(s:Executor.'isnolongertheexecutor.')
  106. lets:Executor=""
  107. else
  108. callg:EchoMoreMsg(s:Executor.'issetastheexecutor.')
  109. letmain="\\s*\\(\\(public\\|static\\)\\s\\+\\)\\{2}void\\s\\+main(String"
  110. letpara1=main."\\(\\(\\s*\\(\\.\\{3}\\|[]\\)\\s*\\)\\w\\+\\s*\\))"
  111. letpara2=main."\\(\\s\\+\\w\\+\\s*[]\\s*\\))"
  112. ifsearch(para1)+search(para2)==0
  113. callg:EchoWarningMsg(s:Executor.'doesnothavethemainmethod!')
  114. endif
  115. endif
  116. endfunction


  117. function!s:Compile()
  118. wall
  119. echo"Allopenfilesaresaved."
  120. execute"compiler!".s:Compiler

  121. letargfiles=s:GetArgumentFiles(s:ArgumentFile)
  122. ifstrlen(argfiles)>0
  123. callg:ExecuteCommand(&makeprg,argfiles,";")
  124. else
  125. calls:InitEnvironment()
  126. letdist=substitute(s:DistinationOption,"DO",s:DistinationPath,"")
  127. letcp=substitute(s:LibraryOption,"LO",s:GetClassPath(s:LibraryPath,s:DistinationPath),"")
  128. letsp=substitute(s:SourcepathOption,"SO",s:SourcePath,"")
  129. callg:ExecuteCommand(&makeprg,dist,cp,sp,s:ExtraCompileOptions,bufname("%"),";")
  130. endif
  131. endfunction


  132. function!s:Execute()
  133. ifs:Executor==""|calls:ToggleExecutor()|endif

  134. letcp=substitute(s:LibraryOption,"LO",s:GetClassPath(s:LibraryPath,s:DistinationPath),"g")
  135. if!filereadable(g:VIM_STD_IN_FILE)
  136. callwritefile([""],g:VIM_STD_IN_FILE)
  137. endif
  138. callg:ExecuteCommand(">",s:Interpretor,cp,s:ExtraExecuteOptions,s:Executor)
  139. endfunction


  140. function!s:CLCompile(...)
  141. letindex=1
  142. leteoption=""
  143. whileindex<=a:0
  144. execute'leteoption=eoption."".g:Trim(a:'.index.')'
  145. letindex=index+1
  146. endwhile
  147. ifmatch(eoption,"^:")==-1
  148. lets:ExtraCompileOptions=s:ExtraCompileOptions.eoption
  149. else
  150. lets:ExtraCompileOptions=strpart(eoption,2)
  151. endif
  152. calls:Compile()
  153. endfunction


  154. function!s:CLExecute(...)
  155. letindex=1
  156. leteoption=""
  157. whileindex<=a:0
  158. execute'leteoption=eoption."".g:Trim(a:'.index.')'
  159. letindex=index+1
  160. endwhile
  161. ifmatch(eoption,"^:")==-1
  162. lets:ExtraExecuteOptions=s:ExtraExecuteOptions.eoption
  163. else
  164. lets:ExtraExecuteOptions=strpart(eoption,2)
  165. endif
  166. calls:Execute()
  167. endfunction

  1. "Otherusefulfunctions
  2. function!s:PreparePath(srcs,isfile)
  3. letresult=""
  4. forsrcinsplit(a:srcs,":")
  5. letsource=s:ProjectRoot."/".src
  6. if(a:isfile==0&&isdirectory(source))||(a:isfile==1&&filereadable(source))
  7. letresult=s:AddPathTo(result,source,":","after")
  8. endif
  9. endfor
  10. returnresult
  11. endfunction

  12. function!s:AddPathTo(src,dir,sep,pos)
  13. letpath=s:CleanPath(a:dir)
  14. ifa:src==""
  15. returnpath
  16. elseifmatch(a:src,path)>=0
  17. returns:src
  18. else
  19. ifa:pos=='after'
  20. returna:src.a:sep.path
  21. else
  22. returnpath.a:sep.a:src
  23. endif
  24. endif
  25. endfunction

  26. function!s:CleanPath(path)
  27. letpaths=split(fnamemodify(a:path,":p"),"/")
  28. letresult=[]
  29. letn=len(paths)-1
  30. letskip=0
  31. whilen>=0
  32. ifpaths[n]==".."
  33. letskip=skip+1
  34. elseifskip==0
  35. callinsert(result,paths[n],0)
  36. else
  37. letskip=skip-1
  38. endif
  39. letn=n-1
  40. endwhile
  41. letpath="/".join(result,"/")
  42. ifmatch(path,"")>0
  43. letpath='"'.path.'"'
  44. endif
  45. returnpath
  46. endfunction

  47. function!s:IsJavaFile()
  48. letfilename=bufname("%")
  49. iffnamemodify(filename,":e")!="java"
  50. calls:EchoWarningMsg(filename."isnottheexpectone!")
  51. return0
  52. endif
  53. return1
  54. endfunction

  55. function!s:GetPackage()
  56. letcur=getpos(".")
  57. letpackage=""
  58. callsetpos(".",[0,1,1,0])
  59. letline=search('package','')
  60. ifline>0
  61. try
  62. letpackage=split(split(getline(line))[1],';')[0]
  63. catch/.*/
  64. callg:EchoErrorMsg("Thepackagedefinitionmayincorrect!")
  65. endtry
  66. endif
  67. callsetpos(".",cur)
  68. returnpackage
  69. endfunction

  70. function!s:GetFullClassName()
  71. ifs:IsJavaFile()==0|return""|endif
  72. letpackage=s:GetPackage()
  73. letclass=fnamemodify(bufname("%"),":t:r")
  74. ifstrlen(package)>0
  75. returnpackage.'.'.class
  76. else
  77. returnclass
  78. endif
  79. endfunction

  80. function!s:GetClassPath(libraries,distination)
  81. letclasspath=substitute($CLASSPATH,"\\.\\/:","","g")
  82. letlibffixes=['jar','JAR','zip','ZIP']
  83. forlibinsplit(a:libraries,":")
  84. forsuffixinlibffixes
  85. letfindlib=substitute(glob(lib."/*".suffix),"\n",":","g")
  86. ifstrlen(findlib)>0
  87. letclasspath=s:AddPathTo(classpath,findlib,":","before")
  88. endif
  89. endfor
  90. endfor
  91. returns:AddPathTo(classpath,a:distination,":","before")
  92. endfunction

  93. function!s:GetArgumentFiles(ArgumentFile)
  94. letargfiles=""
  95. forafinsplit(a:ArgumentFile,":")
  96. letargfiles=argfiles."".substitute(s:ArgumentFileOption,"AFO",af,"g")
  97. endfor
  98. returnargfiles
  99. endfunction
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值