fidel-karsto / webstorm-templates

A collection of some of my custom live templates for Webstorm / IntelliJ IDE
MIT License
1 stars 1 forks source link

Needs to convert custom variables to webstorm variables. #1

Open wawyed opened 10 years ago

wawyed commented 10 years ago

I find this project really useful but is there any way to convert sublime text custom variables to webstorm custom variables?

fidel-karsto commented 10 years ago

Good question! According to http://www.sublimetext.com/forum/viewtopic.php?f=3&t=3852 custom variables are kept in a *.tmPreferences file. It's structure is XML, so it could be transformed and used within the XSLT for converting snippets - in theory. ;) I need to think it over. It sounds quite interesting and useful to me. Thx for the hint!

wawyed commented 10 years ago

No problem, I was trying a different approach using grunt-text-replace to replace matching strings. Please share it if you find a way to do it :+1:

wawyed commented 10 years ago

Sorry when I meant variables, I meant custom parameters. For example in your CDATA:

Hello

${1:customparameter} this is a custom parameter that sets the cursor on it when you load the template in sublime text. ;customparameter' is the default value of that parameter. If you want to reause again you only need to write $1.

fidel-karsto commented 10 years ago

Ah, yes that is a good point I wanted to figure out as well some time. Text replacement should work with the built in replace() function (in XSL 2.0) or by using a substitute template like this http://stackoverflow.com/questions/3067113/xslt-string-replace (for XSL 1.0) But a pure text replacement inside of the template is only the first step. The problem is, that I don't know how to reference the inner text replacements to build separate elements for the variables.

The order of Sublimes parameters is set by the given number:

function ${1:myDefaultFunctionName} (${2:myDefaultParameter}) { 

In this case the cursor would first jump to position 1 with a default set to "myDefaultFunctionName" and after pressing ENTER it would go right to position 2.

As far as I can see, in Live Templates each variable is represented by an element describing its definition. The order (for cursor stop points) is set by the order of the variable elements in the xml definition:

 <variable name="myDefaultFunctionName" 
           expression="" 
           defaultValue="myDefaultFunctionName" 
           alwaysStopAt="true" />
 <variable name="myDefaultParameter" 
           expression="" 
           defaultValue="myDefaultParameter" 
           alwaysStopAt="true" />

So a way to go could be

While thinking about it and writing this, I found this rather old article: http://www.xml.com/pub/a/2003/06/04/tr.html Looks like a possibility to me.

I will give it at try asap - :+1:

wawyed commented 10 years ago

A possible regex could be \${([0-9]):([\w]*)}

fidel-karsto commented 10 years ago

Hey there,

I just pushed the my development branch - there I played around with the custom variable problem. First I wanted to develop a purely xsl based solution, but regular expressions is only part of xsl 2.0 which isn't widely supported. Xsltproc uses libxsl which supports only xsl 1.0. So I tried to use Saxon-CE with phantomjs, but it didn't seem to work that well. So I started to write a post processor which extracts custom variables and field points from the CDATA section and generates variable elements for live templates.

It's not thoroughly tested yet. But if you are keen enough to give it a try you are welcome to do so. I would be glad to hear from you if there are any bugs/problems. :)

wawyed commented 10 years ago

I've forked your implementation and I'm going to commit some changes in my fork if you want to have a look and tell me what do you think.

wawyed commented 10 years ago

There you go https://github.com/wawyed/webstorm-templates/tree/develop

Have a look and let me know what you think.

The only problem I have now is that if I have a CDATA with tabs or newlines when the domParser parses the template (doc = domParser.parseFromString(template, 'text/xml');) and converts it to string It also decodes newlines and tabs, so when you write the modified file you also replace the code for their actual value (a tab character is replaced with a tab).

wawyed commented 10 years ago

I've managed to fix the issue with whitespaces and tabs by replacing them with their actual utf8 code after converting the xml to string. You can have a look at the commit that fixes it.

https://github.com/wawyed/webstorm-templates/commit/3c1eec102810c8c5abca3cf94661740e296d4086

fidel-karsto commented 10 years ago

Excellent! I will take a closer look as soon as possible.