Menu

Snippets - Part 4

Trevor Williams

Snippets (Part 4)

Let’s wrap up custom user snippet support in TKE by talking about snippet transforms. Transforms allow the snippet writer to make adjustments to text added at tabstops in mirrored tabstops or adjustments to snippet variables. Transformations have the following syntax in snippet text:

${tabstop/regexp/format/opts}  OR
${variable/regexp/format/opts}

Tabstop

The tabstop is a mirrored tabstop numerical identifier. When the user enters the original value at the matching tabstop, its value will be parsed and transformed at this location.

Variable

The variable is any snippet variable (i.e., CLIPBOARD, DIRECTORY, FILENAME, etc.). The transform will parse the value of the variable and insert the new value in its place.

Regexp

The regexp value is any legal Tcl regular expression. If parenthesis are used in the regular expression, their matching content will be stored in variables 1, 2 and so on which can be used in the format portion of the transformation.

Format

The format contains information about how to transform the text that matches in the regular expression. With the format value, you can perform the following replacements on the matched text:

Captures
  • Refer to captured text within parenthesis in the regular expression by specifying a dollar sign followed by the associated parenthesis number ($0 refers to all matched text)
  • Example:
    ${1/<img src="(.*?)">/<img src="$1" alt="$1">/}
  • If the value in the mirrored tabstop $1 contains the string <img src="some value"> it will be replaced with <img src="some value" alt="some value">
Case Foldings
  • Performs case conversions on matching text.
  • The following case conversion operators are available:
  • \u – Converts the first character to uppercase
  • \l – Converts the first character to lowercase
  • \U..\E – Converts all characters between the \U and \E to uppercase
  • \L..\E – Converts all characters between the \L and \E to lowercase
Conditional Insertions
  • Inserts text (or alternatively does not insert text) based on whether a match occurred or not.
  • First example: ${1/(-)?(\d+)/(?1:minus:plus) $2/}
  • The above example checks the mirrored tabstop value $1 for a number with an optional negative value indicator. The transformation will transform the string from something like -2 to minus 2 or 3 to plus 3.
  • Second example: ${1/(-)?(\d+)/(?1:minus )$2/}
  • The above example checks the mirrored tabstop value $1 for a number with an optional negative value indicator. However, in this case, the transform will transform the string -20 to minus 20 but a value of 30 will stay the same.
Escape Codes
  • You can insert newlines (\n), tabs (\t) and dollar signs (\$) in the transformed text.

Opts

The opts value is an option list of characters that can control the transformation behavior. The only opts value that is supported by TKE is g which will match all occurrences of the regular expression in the value, causing transformations to occur on all matching text.

Now that you are up-to-snuff on how to transform snippet text, consider yourself a snippet power-user and go make the world a better place!


Related

Wiki: Tips & Tricks