ActiveState Community

CheckMate Macro

Posted by bigdiver on 2007-03-20 12:07
OS: All / Any

CheckMate - Advanced Code Snippets for Komodo

Author : Pedro Paixao
Email : BigDiver@gmail.com
Site : http://www.chatscope.com
version : 0.2

Introduction

Following the footsteps of other text editors, this macro simplifies the use of abbreviations to that are expanded to full code snippets, with some "intelligence".

It is possible to define selection blocks, or fields, within the snippet that get selected in the editor after abbreviature expansion every time the user presses the Key Sequence for this macro.
In other words the user types the abbreviature, presses the assigned Macro Key Sequence (defaults to F4), and it gets expanded to the code snippet. The user can now press the F4 key repeatedly to move the text selection to all defined selection fields within the code.

Contents

This ToolBox package includes the CheckMate and File-Language macros, as well as over 200 code snippets for Ruby, Ruby on Rails, Perl, some HTML, Python and some Common ones that can be used in all languages.

The Ruby, and Ruby on Rails, snippets use the same abbreviatures as in other editors, so your transition to Komodo will be smooth and easy. All you abtm, mcol, etc are here and ready to use as you've always used them in other editors.

Formatting Snippets

A selection field is defined inside each Snippet using the tags <Tn>,
</Tn>, where 'n' is an integer > 0. Each tag pair defines a selection
field, and the number 'n' indicates the order in which they will be selected in the editor. Example: create a snippet called 'mtab'

rename_table :<T1>table</T1>,:<T2>new_name</T2>

When the user types mtab<F4> the text gets expanded to:

rename_table :table, :new_name

and the word table is selected, If the user presses F4 again the word new_name will be selected, and so on.
The snippets can have multiple lines, and so can selection blocks. The user can also use the built in Komodo Snippet functionality of placing the cursor, or selection block, within a Snippet. If so this will define the last (end) position of the selection or cursor.

VERY IMPORTANT: the snippet's cursor end position cannot be placed inside a block tag, when creating a snippet.

Advanced Snippets

CheckMate also supports "advanced" snippets which allow more flexibility and can be used to automatically replace Snippet content before insertion. Advanced Snippets are regular snippets that use the tag <ReplaceMe> where ever you need to insert the text you type in editor.
For example, instead of creating a different snippet for each HTML tag
you could simply create an advanced snippet called 'ht' with the following content:

<<ReplaceMe>> </<ReplaceMe>>

Now open the Snippet called 'AdvancedSnippets' and add the 'ht' to a new line. Now go to the editor and type httable<F4> and the resulting expansion will be:

<table> </table>

So the word that follows the advanced snippet name (ht in this case) will
be used to replace the <ReplaceMe> tag!
Advanced Snippets can use field definitions tags (<Tn> </Tn>) as explained before.

Intelligent Comments

You can also create generic, or multi language comment blocks using the <CMT> <CMTM> and </CMTM>. These will get replaced by the appropriate language comment marker. This way you do not have to create comment snippets for all languages

  • <CMT> is used for single line comments
  • <CMTM>, </CMTM> are used for multi line comments

To see the Intelligent comments in action try the GNU snippet

gnu<F4>

To add support for a new language add the appropriate chars to the
dicts:

  • commentChars - for single line comments
  • commentOpen - for opening multi line comment
  • commentClose - for closing multi line comment

If a specific language is not included in the comment dicts then '#'
will be used by default.

Language Specific Snippets/Abbreviatures

CheckMate supports the use of language specific, as well as generic abbreviations. Generic abbreviations will get expanded no matter what programming language you are writing in the current file. Language specific abbreviations can only be used in files using the same programming language in which they were defined.

Generic abbreviations are created using snippets, and the name of the snippet is the abbreviation, as explained before. Language specific abbreviations are also created with code snippets, but their name is the language name, followed by '.' and the abbreviature's name. Examples:

  • Ruby ruby.if
  • Perl perl.if
  • C++ c++.if

To help you determine which language string should be used for a particular file, just run the provided File-Language macro.

Language specific abbreviations offer an easy way of using the same abbreviatures in different languages. As you know an 'if' is coded differently in Ruby, Perl or C++, so without language specific abbreviatures we would have to use different names for each of the different definitions of the 'if' snippet, which becomes very hard to memorize. Also if you try to expand an abbreviature that is not defined for that particular language, nor as a generic, nothing happens, and no coding errors will be introduced by expanding a Perl snippet into a C++ file.

Snippet Search Algorithm

First CheckMate searches for a language specific snippet (language.snippet_name) and if it does not find it, it will search for a generic snippet (snippet_name). If it still doesn't find a snippet it will search the AdvancedSnippet List and try to match the current abbreviature to one of the snippets on that list. If it still does not find a snippet nothing happens and the user can continue editing the document.

File language detection uses Komodo's own language detection mechanisms.

License

Licensed under the GNU General Public License. For more information visit:
http://www.gnu.org/copyleft/gpl.html

Known Issues/Limitations:

Please forgive any bad coding practices, but this is my
first Python script!

If you find any bugs drop me a line.

  • Snippet indentation may fail if using TABs for indentation. If 'Prefer TAB characters over spaces' Komodo setting is enabled you may experience some twilight zone effects...
  • Selection blocks/fields cannot be empty, ie, don't do <Tn></Tn> and expect it to be selected.
  • Only tested in Windows. Glad if you could provide some feedback
    for other platforms
  • If different selection blocks include the same text only the first will
    be selected

Enjoy

AttachmentSize
CheckMate.kpz86.23 KB

bigdiver | Wed, 2007-03-21 12:06

vkyr | Thu, 2007-11-08 06:00

One problem I found with CheckMate when running in Komodo Edit 4.2 for Windows is, that all via F4 inserted snippets always do have just LF as eol characters, which is somehow annoying on a Windows system. Since I'am no Python programmer, I can't tell you which portion of the macro might need a change to insert instead the snippets with correct CRLF eol charcters.

Any suggestion how to fix this?