Home Download Buy Blog Forum Support

Regex key binding contexts

Regex key binding contexts

Postby jps on Wed Apr 16, 2008 4:29 pm

Key bindings defined within a *.sublime-keymap file may have contexts: The binding is only active if all listed contexts evaluate to true.

20080416 introduces some handy contexts that match portions of the current line(s) against regexes, named allPreceedingText, noPreceedingText, allContainedText, noContainedText, allFollowingText and noFollowingText (Yes, there is an extra e in preceding... this will be fixed in the next version, such that the current and correct spelling will both work).

The 'Preceeding' variations will match against the text from the beginning of the line to the selection, the 'Contained' ones will match against the contents of the selection, and the 'Following' ones will match against the text from the end of the selection to the end of the line.

As an example, consider JavaDoc style comments:

Code: Select all
   * Hello!

It'd be nice to alter the behavior of the enter key when the cursor is within a JavaDoc style comment, to automatically insert another line starting with a '*', so you don't have to type it yourself. Below are a couple of key bindings that will do this: The first will trigger on the start of a JavaDoc style comment (Indicated by /**), and the second binding will trigger within a JavaDoc comment (considered to be any line starting with whitespace and then an asterisk).

Code: Select all
   <binding key="enter" command="insertAndDecodeCharacters '\n* '">
       <context name="allPreceedingText" value="^[\t ]*\*($|[^/])"/>
   <binding key="enter" command="insertAndDecodeCharacters '\n * '">
       <context name="allPreceedingText" value="^[\t ]*/\*\*($|[^/])"/>
Site Admin
Posts: 3067
Joined: Wed Mar 19, 2008 12:33 pm

Re: Regex key binding contexts

Postby kib2 on Wed Apr 16, 2008 5:26 pm

Very good idea, thanks for implementing it.
Posts: 38
Joined: Sun Mar 23, 2008 8:30 am
Location: France

Re: Regex key binding contexts

Postby myel on Mon Apr 21, 2008 11:46 am

Posts: 88
Joined: Sat Mar 22, 2008 11:26 pm

Return to Plugin Announcements

Who is online

Users browsing this forum: No registered users and 17 guests