Skip to content

AutoComplete format

Kaamil Jasani edited this page Aug 2, 2017 · 2 revisions

The file used to modify auto-completes can be found in your processing app folder, in a folder called CodeHelper. It will be called autocompletes.txt

File Structure

Each pair of two lines represents an auto-complete. The first line specifies the regex to match with the line. If this regex matches, the autocomplete will be applied. The second line specifies what text should be inserted in place of the regex matched text.

Line matching regex

The first part of each pair is the regex. This regex will be used to try and match the line. If the line matches this regex when tab is pressed, the auto complete will run. The regex may also contain capture groups. These can then be used to substitute values into the replacement text.

Usually, you will have to deal with white-space before what has been typed (indentation) and also after, since spaces will be added when the tab key is pressed. \s* can be used before and after the regex to ignore any white-space.

Replacement text

The replacement text is formatted using Java's MessageFormat. The javadoc covers the format in detail but here are the basics:

Literal strings are represented just by themselves. The arguments passed in can be substituted in using {n}, where 'n' is the argument number. These can be formatted too, but I will leave the explanation for that to the javadoc.
This means that an open curly brace ({) can not be used in the string without being escaped. To escape a curly brace, enclose it in single quotes ('{'). This also means that single quotes need to be escaped too. They can be escaped just by having double single quotes (''). Again, this is explained in the javadoc.

In the case of this tool, argument number 0 is always a new line character, followed by the correct number of spaces to match the initial indentation. This should be used to go to a new line.
Every argument that follows is the respective capture group from the regex. So argument number 1 will be the first capture group from the regex, argument number 2 will be the second and so on.

The caret placement by default will be at the end of the replacement text. However, this can be re-positioned. The first occurrence of \| will be where the caret is placed after replacement. Any further occurrences will be ignored and treated as literal text.

Examples

Here is an example:

\s*for (\w+)\s*
for(int {1} = 0; {1} < \|; {1}++)'{'{0}  {0}}

In this example, the regex is matching any line that has a format for <someName>. The name is being captured, so argument number 1 will be the name that was typed.
The second line is the replacement text. The text represents a for loop. Where ever the counter variable was to show up, argument 1 ({1}) is used so that the name specified is used. The curly brace used to open the for loop block is escaped by enclosing it in single quotes. To start a new line, {0} is used. This allows the code to maintain indentation. Then two more spaces are added to indent the inner block, followed by another new line. The closing curly brace does not need to be escaped.
The caret placement is specified using \|. This is where the caret will be placed when the auto complete is run.

Clone this wiki locally