[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[StrongED] Inserting snippets



Hi All,

Snippets are (usually) short strings that can be inserted into a text.
StrongED currently has 2 ways of inserting a snippet; via the Shortcuts
defined in a ModeFile, and via the Snippets dialogue box (c-O,c-S).

Another way to insert a string into a text is through commands send from
an external utility. The Complete utility in AppBasic mode is an example
of this.

Eg to insert a method for the ActionButton gadget, enter 'act' and press
c-Space. This will open a menu with the methods that are available for
the ActionButton gadget. Use cursor up/down to select the one to insert
and press c-Return. This will insert, say:

  PROCActionButton_GetText(object,component,Text)

This is very useful, saving quite a bit of typing and therefore avoids
typing mistakes. However you still need to remove each formal parameter
and replace it with the actual parameter, eg object -> mainWindow.

The Shortcuts definition has part of the solution already as you can
indicate points where the cursor should be placed using '\@', like this:

  PROCActionButton_GetText(\@object,\@component,\@Text)

After this snippet is inserted the cursor will be placed at 'object'.
Pressing Return moves it to 'component', etc. Now there's no need to
manually place the cursor. (Note: '\@' is not inserted into text)

To make this possible the code to find and insert a shortcut was split
in two. A function 'InsertSnippet' was then added that called the code
to insert a snippet. The insertion code could now be invoked from an
external utility.


It would even nicer if the 'word' at such a tabstop would be highlighted
automatically and removed when a new 'word' is typed. To allow this the
syntax for snippets had to be extended (retaining current behaviour).

Currently the following escape sequences are defined:

  \n   is replaced with a newline

  \t   is replaced with a TAB

  \i   is replaced with the indent that the first line has

  \@   tabstop, cursor placed at first, Return to move to next

  \@n  as above but text from tabstop n copied to this tabstop
       (note that n must be smaller than current tabstop number)

The next two escape sequences have been added:

  \*   Same as \@, but 'word' at tabstop is selected ready for removal

  \*n  Same as \@n, but copied 'word' is selected ready for removal


Updating the snippet to use the new escape sequences gives:

  PROCActionButton_GetText(\*object,\*component,\*Text)

When this is inserted into a text, the cursor will be placed at 'object'
as before but now it will be highlighted. If a character is entered then
'object' is deleted and replaced by the typed character. A press on
Return moves the cursor to 'component', which is highlighted ready for
editing.

End result is that it's now much faster to insert a function template
and replace the formal parameters.


In addition to the above some more changes were made to improve things
further.

Added support to the Snippets dialogue box for escape sequences.

The Tab key can now also be used to move to the next tabstop. Holding
down Shift on either Tab or Return moves to the previous tabstop.

Pressing Return when the cursor is on the last tabstop no longer inserts
a newline but moves the cursor to the end of the snippet.

To make it clearer that a snippet is active the Tabkey field on the
Infobar will show 'Snippet' in red while the cursor is inside one. This
still seems to have some issues in not always turning off correctly.
(BTW I've made 'Learning..' (s-F9) appear in red on the Infobar too)

Syntax colouring support for snippets so that the entire snippet can be
given a different (background) colour, with the mark colour being used
to highlight the selected 'word'.


Hopefully will prove to be a useful addition, all comments welcome.

I'm considering adding escape sequences to allow the insertion of
special values, eg \$DATE to insert the current date. Also \$TIME,
\$SYSVAR. If you have any further suggestions then let me know.

Seasonal greetings,
Fred.

-- 
StrongED Developer
http://www.stronged.iconbar.com/

-- 
To unsubscribe send a mail to StrongED+unsubscribe@xxxxxxxxxxx
List archives at http://diy.Torrens.org/RO/StrongED/index.html