From MozillaWiki
< Labs‎ | Bespin‎ | DesignDocs
Jump to: navigation, search

Design Document: Snippets

Version 1 is a 40 point solution :)


Snippets enable quick access to putting data into a document and are inspired by Textmate.

The notion is that you type something into the editor (e.g. "ifloop") and then if you hit the TAB key, flush that out:

 if (EXPR) {

Version 1

When the user currently hits the tab key while the editor is in focus two things may happen:

a) If text isn't selected, insert a tab / spaces into the document (depending on tabmode) b) If text is selected, indent the text block

Add on to this, which is currently:

 listener.bindKeyString("", Key.TAB, this.actions.insertTab);
 listener.bindKeyString("SHIFT", Key.TAB, this.actions.unindent);

insertTab does the indent OR put tabs/spaces into the document.

Change "insertTab" to actually call a high level "tabPushed" action that adds a check to see that if text is not selected and there are non-space characters before the cursor, see if there is a matching snippet.


" ifloop[CURSOR HERE]" -> checks to see if there is an "ifloop" snippet registered and then call that action.

This brings up the notion of registering snippets, which should be very open and easy to do. It can piggy back on the event sub system, e.g. bespin.publish("bespin:snippet:register", { snippet object }) (which includes a name, execution logic etc).

At first, the API can simply return text to insert at the point of the snippet command name. So, "ifloop" is cut out from the document and the if block is inserted in its place.

Add a "snippets" directory inside of BespinSettings where people can add snippet files just like you can add command files inside BespinSettings/commands. Actual helper commands a la cmdlist, cmdadd, etc but snippetlist, snippetadd of course.

Version 2

Handle the notion of snippet points so once you invoke a snippet, you can tab through the items (e.g. in the ifloop example, it jumps to the EXPR area, and then to BLOCK). This means that a little mode needs to be entered for this.

Version 3

There are lots of great snippet files already build for Textmate. Add integration code that allows you to just import that work and all the features that make sense are available, and anything that isn't is ignored.