views:

57

answers:

2

hi all, the following code is a textmate javascript snippet, can anyone explain it please? cuz i want to use this feature in my own snippets. greate thanks.

document.getElement${1/(T)|.*/(?1:s)/}By${1:T}${1/(T)|(I)|.*/(?1:agName)(?2:d)/}("$2")
A: 

I know nothing about textmate, but looks like it is to generate the followings:

document.getElementById(val);
document.getElementsByTagName(val);

Not this though:

document.getElementsByName(val);
o.k.w
+2  A: 

${1:T} is the first tab placeholder, with a default value of "T". This is the text that's highlighted when you type "get" and hit tab.

$2 is the second tab placeholder. Once you've pressed either "T" or "I" to complete the function name in the first tab placeholder, you can press tab to get here.

Those parts you probably knew already, but the other two are slightly trickier.

${1/(T)|.*/(?1:s)/} is a kind of insertion switch. It looks at the value you type into the first tab placeholder and picks a corresponding value to insert. If you type "T", it will insert an "s" (to make the word "Elements"); otherwise, it doesn't insert anything.

${1/(T)|(I)|.*/(?1:agName)(?2:d)/} is another insertion switch, which again looks at the value you type into the first tab placeholder (that's what the "1" at the beginning means). Here, there are two possible insertions: if you type "T", it will complete it to "Tagname", and if you type "I", it will complete it to "Id".

The overall result is that if you invoke the snippet and type "T", it will complete to 'getElementsByTagName("")'. If you invoke it and type "I", it will complete it to 'getElementById("")'.

No Surprises
great thanks!!!
Yousui