Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 integrated 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?

like image 292
GuruM Avatar asked Oct 10 '22 16:10

GuruM


2 Answers

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
like image 180
darcyparker Avatar answered Oct 14 '22 03:10

darcyparker


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'    
like image 42
kipelovets Avatar answered Oct 14 '22 03:10

kipelovets