Sublime Forum

Snippet Loop Blocks?

#1

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

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:

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

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;
		}
	}
}
0 Likes

#2

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.

0 Likes

#3

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:

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.

0 Likes

#4

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:

ul#menu>li*5

will output to:

[code]






[/code] 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.
0 Likes