Auto code blocks/snippets with script

Help with writing and running scripts

Auto code blocks/snippets with script

Postby roland » Thu Dec 20, 2007 5:23 pm

Hello, this is a revised script based on the one discussed at Snippets with macro. It auto-completes a block using a template for each flow control construct. Maybe it will be useful for others.

Also, can anyone can tell me why that parseInt() appears to be necessary? (if I just pass i itself, an empty template is used)

autoblock.js
Code: Select all
// index must match template number
templates = []
templates[0] = 'if'
templates[1] = 'else'
templates[2] = 'elif'
templates[3] = 'for'
templates[4] = 'while'
templates[5] = 'do'
templates[6] = 'switch'

function autoblock()
{
    doc = UltraEdit.activeDocument;
   
    col = doc.currentColumnNum;
    if (typeof(UltraEdit.activeDocumentIdx) == "undefined") col++;
    line = doc.currentLineNum;
   
    doc.key('LEFT ARROW');
    doc.selectWord();
   
    keyword = doc.selection;
   
    for(i in templates)
    {
        if(keyword == templates[i])
        {
            doc.key('DEL');
            doc.gotoLineSelect(0, 1);
            doc.insertTemplate(parseInt(i));
            return;
        }
    }
   
    doc.gotoLine(line, col);
    UltraEdit.messageBox('No Match!');
}

autoblock()


The corresponding templates:
Code: Select all
[$replace$]if(^)
[$replace$]{
[$replace$]   
[$replace$]}

Code: Select all
[$replace$]else
[$replace$]{
[$replace$]    ^
[$replace$]}

Code: Select all
[$replace$]else if(^)
[$replace$]{
[$replace$]   
[$replace$]}

Code: Select all
[$replace$]for(^)
[$replace$]{
[$replace$]   
[$replace$]}

Code: Select all
[$replace$]while(^)
[$replace$]{
[$replace$]   
[$replace$]}

Code: Select all
[$replace$]do
[$replace$]{
[$replace$]   
[$replace$]} while(^);

Code: Select all
[$replace$]switch(^)
[$replace$]{
[$replace$]  case XXX:
[$replace$]   
[$replace$]    break;
[$replace$] 
[$replace$]  default:
[$replace$]   
[$replace$]    break;
[$replace$]}
User avatar
roland
Basic User
Basic User
 
Posts: 30
Joined: Sun Aug 15, 2004 11:00 pm

Re: Auto code blocks/snippets with script

Postby caveatrob » Fri Jun 06, 2008 2:05 pm

How do you hotkey a script to make the autoblocks work? Can you set it to control-space?
caveatrob
Basic User
Basic User
 
Posts: 20
Joined: Fri Apr 25, 2008 4:39 pm


Return to Scripts