Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD traversal in VIM

Tags:

vim

xsd

I use VIM as my text editor and I edit a lot of XML and WSDL files.

WSDL files have an XSD section. Is there some VIM plugin I can use to traverse the XSD types? i.e., if I have the following line and the caret is where the '|' sign is:

<xsd:element minOccurs="0" name="FooName" type="Magic|FooType"/>

and I press Ctrl+Alt+Foo (or some other magic combo) it will get me to the definition of MagicFooType, i.e.:

<xsd:complexType name="MagicFooType">

I couldn't find how to use ctags for this and all the other plugins that I could find are for imperative languages (i.e. Java). Is there some plugin/script to do the job?

like image 782
maayank Avatar asked Jun 14 '10 16:06

maayank


3 Answers

This is relatively easy to achieve using ctags. I use Vim exclusively for XSD editing and in combination with exuberant ctags (http://ctags.sourceforge.net/) this works very well for me.

Because exuberant ctags doesn't support XSD out of the box you need to generate a custom language definition.

I use the file below as .ctags in my XSD project. After running the ctags command in the project root, I can use standard VIM tag navigation (Ctrl-[ for follow and Ctrl-O for back) for jumping to definitions.

To get vim to pick up tags files in parent directories I use the following snippet in my .vimrc

set tags=./tags;

Put this in .ctags in your project root.

--langdef=XSD
--langmap=XSD:+.xsd
--langmap=XSD:+.wsdl
--regex-XSD=/element name="([a-zA-Z0-9_]+:){0,1}([a-zA-Z0 -9_]+)"/\2/d,definition/
--regex-XSD=/Type [a-zA-Z0-9="]*[ ]{0,1}name="([a-z_]+:){0,1}([a-zA-Z0-9_]+)"/\2/d,definition/
--exclude=.git
--recurse=yes
like image 174
mjanssen Avatar answered Oct 30 '22 14:10

mjanssen


I have been watching this thread for a bit and wondering this myself as I am a VIM user and work with a lot of very large XML files and associated XSD's, XSLT's and XQuery files, all of which could benefit from this sort of thing, as I am sure you can imagine.

After some research this can be done with ctags, but there is nothing out there at the moment which will create the ctags file for it. Essentially a ctag looks like this:

{tagname}<tab>{tagfile}<tab>{tagaddress}

Because the tagaddress is an ex mode command it can take you anywhere in a particular file to land right on the definition.

Basically all we would need to do is create a script which would run through the WSDL (or XSD, or whatever) and, knowing the location of the tag and file, create the appropriate lines in a ctags file to map all of the definitions. Sounds like a fun and useful little project.

Sorry that this isn't exactly a complete answer, but it is possible to do with ctags and will give you the robustness you and looking for.

like image 23
Tim C Avatar answered Oct 30 '22 15:10

Tim C


gd ("go to declaration") is not aware of the XML format but might do the job in this case, assuming the XSD section is at the top of the WSDL.

like image 42
ngn Avatar answered Oct 30 '22 15:10

ngn