Snippets


Introduction

Snippets are small text fragments, which can be inserted into text. They can be binded to lexers. (Note: similar feature named "Code templates" is removed.) To insert snippet text, first activate needed lexer (snippets already exist for C, C++, Pascal, Java etc), call dialog "Tools -- Snippets", and choose needed item with Enter.

To create a snippet, call dialog "Tools -- New snippet". It will prompt for all properties (caption, alias, lexers list, text), and will suggest to save file to needed folder.

If snippet has an alias, then you can type it in the source code, and press Tab - this will insert snippet instantly. (You need to type the entire alias.) Several snippets may have same caption, and even same alias; when expanding "duplicate" alias with Tab, you'll see dialog with all variants.

If snippet text contains Tab-chars, they all will be replaced with spaces, if editor configured so (the global setting is considered, or the local override-setting for current lexer).


Tabstops

Snippet text can contain so called "tabstops" - positions, which will have red markers, and which will be jumped over with Tab key. Maximal 10 tabstops are allowed, they are typed so:

${1}
...
${9}
${0}

They are activated in such order: first ${1}, then ${2} ... and at last ${0}.

You can write tabstops with default text:

${1:text here}

You can miss any tabstops in list:

${1:text} text ${3:text} text ${0:end text}


Mirror tabstops

You can have so called mirror tabstops, i.e. specify same tabstop index several times:

start ${1}, carets here: ${2} and here: ${2} and here: ${2}, ended at ${0}.
In this example tabstop-2 has mirrors, on its activating - multi-carets will appear in 2 additional positions; so you can type text in 3 positions at the same time.


Tabstops mode

If snippet text contains more than one tabstop, then on snippet insertion special mode is activated - in this mode Tab key works specially, it jumps to next tabstop (and selects default tabstop text). This mode is deactivated automatically, when you jump to last tabstop. You can see when this mode is active, by seeing red triangles-markers on tabstop positions (if no such markers left - mode is deactivated).

You can distract from filling tabstops, and for example scroll to any editor position, but Tab key still works specially (mode is still on), and will jump to next tabstop.

You can turn this mode off, if you want: by removing all markers ("Bookmarks -- Markers -- Clear markers") or by collecting markers ("Bookmarks -- Markers -- Collect marker").


Macros

The following macros are allowed in snippets text:

Note: if you need in snippet text string like ${...} , you need to escape it: \${...} .


Files

Snippets are stored inside separate files. These are text files in UTF8-no-BOM encoding, with extension "synw-snippet". They can be placed in any subfolder of "Data\snippets" folder. Files and subfolders names have no meaning, but it's recommended to name new subfolders like "AuthorName.LangName", so users can easily find manually installed snippets, and delete them.

Example of file

name=do-while loop
id=do
lex=C,C++
text=
do
{
	${0:/* code */}
} while (${1:/* condition */});

Format of file

First lines have format "str=value" (no spaces around "="), where "str" is one of strings:

Then follows the line "text=" without value, and all next lines - are snippet text. Trailing blank lines are discarded.


How to make snippets distro

You can create ZIP/RAR archive with your snippets, which can be auto-installed into SynWrite - user needs to open your archive in SynWrite, and confirm installation. To create such package, compress into archive root:

Example of install.inf

[info]
title=LexerName snippets
type=template
subdir=snippets