Home Download Buy Blog Forum Support

Snippet Loop Blocks?

Snippet Loop Blocks?

Postby brianheylin on Mon Jul 30, 2012 7:33 pm

I would like to write a snippet to make value classes like the one below:

Code: Select all
class Person
{
   private var _name: String;
   private var _age: int;

   public function get name(): String { return _name; }
   public function get age(): String { return age; }

   public function Person(name: String, age: int)
   {
      _name = name;
      _age = age;
   }
}


a snippet to make value classes with one parameter would look something like the following:

Code: Select all
public class ${1:MyClass}
{
   private var _$2: $3;
   public var get $2(): $3 { return _$2; }

   public function $1($2: $3)
   {
      _$2 = $2;
   }
}


But I would like to extend this snippet template to handle infinite fields.
I can see no system that facilitates this in ST,
as a system like this needs to repeat blocks of text.

I imagine the system would need to adapt the $ token to indicate a repeating block.
The {} brackets would define the repeating block(s)
A repeat trigger character would also need to be defined to indicate when another repetition should be started.
In the case of the value class example the repeat character would be a comma ','

Does this feature exist in ST.
If not, has it been discussed?

Thanks for your time
Brian

Code: Select all
public class ${1:MyClass}
{
   // the first repeating block, the * char defines a repeating block
   ${2*:
   private var _$2.1: $2.2;
   public var get $2.1(): $2.2 { return _$2.1; }
   }

   // constructor argument are the second repating block
   public function $1(${2*',':$2.1: $2.2}) // the repeat character ',' is defined here
   {
      // third repeating block
      ${2*:
      _$2.1 = $2.2;
      }
   }
}
brianheylin
 
Posts: 5
Joined: Sat Jul 07, 2012 6:36 pm

Re: Snippet Loop Blocks?

Postby akhilsengar on Mon Jan 14, 2013 8:13 pm

Hey Brian,

Did you find any solution or workaround? Can you share it here?
I'm looking for something similar to generate a snippet with flexible number of fields in YAML schema for Symfony 2.
akhilsengar
 
Posts: 1
Joined: Mon Jan 14, 2013 8:09 pm

Re: Snippet Loop Blocks?

Postby svenax on Wed Jan 16, 2013 5:35 pm

Not exactly what you are asking for, but ...

The way this is usually handled is to make a snippet that inserts a new instance of it's own snippet trigger as last tab target. In that way you just press tab again to continue expanding it. Something like this:

Code: Select all
private var _$1: $2;
public function get $1(): $2 { return _$1; }
${3:getter}


Now, this will not update the constructor, but it should get you most of what you need here.
svenax
 
Posts: 155
Joined: Tue Dec 20, 2011 12:47 pm

Re: Snippet Loop Blocks?

Postby digikev on Wed Jan 23, 2013 3:48 pm

It will probably be worth your while digging through the emmett and/or zencoding packages which allow you to specify how many of a certain element you want, so for instance:
Code: Select all
ul#menu>li*5

will output to:
Code: Select all
<ul id="menu">
   <li></li>
   <li></li>
   <li></li>
   <li></li>
   <li></li>
</ul>

There might be something in there which will set you along the right path to duplicate blocks of code. I am a front-end markup and UX guy so the Python and JavaScript code is too heavy going for me to steer you any further. If you do use this, and find a solution please post back. It would certainly come in useful for some of the advanced markup snippets I have written to be able to replicate code 'X' amount of times on the fly. Best of luck.
digikev
 
Posts: 3
Joined: Wed Jan 23, 2013 3:09 pm


Return to General Discussion

Who is online

Users browsing this forum: Google [Bot] and 14 guests