开发者

Is there a Ctags like tool for browsing/navigating XSLT code in Vim?

Need a way to navigate/browse XSLT files easily with Vim. Similar to the way ctags can be used with C/C++ source code. A good solution would allow installing scripts etc only for self or running them directly from inside Vim. Hate having to step out of vim just to search for a function definition or "text" search.

Searched for a few hours and came up with some good leads like 'ack'. Apparently this can be integ开发者_StackOverflowrated with vim using ack.vim. Yet to test this out properly.

Essentially I should be able to go through a maze of .xsl files which include other .xsl files and use functions, templates. Any suggestions?


I found the kipelovets' and GuruM's answers to be useful and worked out this solution that combines their advice.:

.ctags file:

--langdef=xslt
--langmap=xslt:.xsl
--regex-xslt=/<xsl:template[^>]+name=\"([-a-zA-Z0-9_:]+)\"( +mode="([^"]+)")?/\1 \2/n,namedtemplate/i
--regex-xslt=/<xsl:template[^>]+match=\"([^"]+)\"( +mode="([^"]+)")?/\1     \2/m,matchedtemplate/i
--regex-xslt=/<xsl:apply-templates +select="([^"]{2,})"( +mode="([^"]+)")?/\1 \2/a,applytemplate/i
--regex-xslt=/<xsl:call-template +select="([^"]+)"( +mode="([^"]+)")?/\1 \2/c,calltemplate/i
--regex-xslt=/<xsl:variable[^>]+name=\"([-a-zA-Z0-9_]+)\"/\1/v,variable/i
--regex-xslt=/<xsl:function[^>]+name=\"([-a-zA-Z0-9_:]+)\"/\1/f,function/i
--regex-xslt=/<xsl:param[^>]+name=\"([-a-zA-Z0-9_:]+)\"/\1/p,parameter/i

vim tagbar settings (which some people may find useful):

let g:tagbar_type_xslt = {
      \ 'ctagstype' : 'xslt',
      \ 'kinds'     : [
      \ 'n:templates (named)',
      \ 'm:templates (matched)',
      \ 'a:applied templates',
      \ 'c:called templates',
      \ 'f:functions',
      \ 'p:parameters',
      \ 'v:variables'
      \ ]
      \ }

It's better, but some issues I am still having include:

  • grouping templates by their modes
  • white space such as new lines in between attributes for a template/variable etc...
  • scoping variables and parameters and apply templates and call templates
    • So if you have multiple variables with the same name but in different templates, the scope of which template they are in is not be captured.
  • commented templates, functions, variables and parameters

A better approach to try in future:

  • The regex's for parsing the information from the xslt get complex to do this properly
  • The best solution may be to write a custom xslt tag generator using xslt itself.
    • This is what jsctags does for javascript
    • The format of the tag file looks straightforward... so I will try this next.
      • See Tag File Format


thanks for your question and answer @GuruM

though your version of ~/.ctags didn't work for me, i used following:

--langdef=xslt
--langmap=xslt:.xsl
--regex-xslt=/<xsl:template[^>]+name=\"([-a-zA-Z0-9_:]+)\"/\1/f,function/i

it works perfectly with AutoTag plugin and requires only one following line for TagList plugin:

let s:tlist_def_xslt_settings = 'xslt;f:function'    


Thanks @Izap. Sorry apparently I'm not allowed to upvote my own question. Couldn't select your answer even though it's closest to what I wanted (example code would've helped).
Creating a tags file can be done very easily:
1) Test out the regular-expressions for your custom language: egrep 'pattern' *
2) Copy-paste custom language with the above regular-expressions into ~/.ctags.

--langdef=EXSLT
--langmap=EXSLT:.xsl
--regex-EXSLT=/<xsl:variable[ \t]+name=\"([-a-zA-Z0-9_]+)\"/\1/v,variable/i
--regex-EXSLT=/<func:function[ \t]+name=\"([-a-zA-Z0-9_:]+)\"/\1/f,function/i
--regex-EXSLT=/<xsl:template[ \t]+match=\"([/-a-zA-Z0-9_:]+)\"/\1/t,template/i

Note: The ( and ) are used to create a tag-entry in tags file where \1 is the matching keyword. Here I'm adding xslt with exslt support above.
You can extend the language mapping by appending regular-expression patterns below.
3) Run cd srcdir && ctags -R *
4) Check the tags file to see if ctags has found the patterns
5) Open vim and test out the tags file using Ctrl-] to jump from function usage to definition etc.
Note: "Extending" ctags requires C coding and is not discussed here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜