Reducing Repetitive Typing with the EditorScripts Class

One thing I really hate is repetitive typing, especially when I am trying to test something via a Job or debugging. One easy way to debug your code is by displaying the information you want on the screen via an infolog.

So imagine this scenario: you’ve just finished writing that nasty SQL statement, with 2-3 joints thrown in there (you know, for good measure), and all you want to do is look at the values it returns from the database.

If you are anything like me, you will write something like this

 info(StrFmt(‘%1, ‘%2, %3’, variable1, variable2, variable3));

It’s not that bad, writing that statement out every time, but if you use this method EVERY DAY (like I do), you will get sick of it.

So I wrote this small method to add to the EditorScripts Class. In case you don’t know what the EditorSpripts does and how it works here is a previous post explaining the Class.

Here is what it does: just write the variables one after another, highlight the text, right click, select the function and voila! ready to debug!


Here is the code

void WorkFlow_infoStrFmt(Editor e)
    str             beginingText, selectedText, endingText, finalText;
    int             i, j;
    container       cont;
    //the beginning and ending of each info statement
    beginingText = 'info(strFmt(\'';
    endingText ='));';
    j = 1;
    //getting the highlighted text (as a string)
    //and saving it in a container
    selectedText = EditorScripts::getSelectedText(e);
    cont = str2con(selectedText, ' ');
    //for each value in the container
    for(i = 1; i <= conLen(cont); i++)
        if(conPeek(cont,i) !='')
            //we add a %1 or %2 or %3 etc.
            beginingText +=  strFmt('%%1 ', j);
            //variable j keeps track of how many variables we have in the container
    //here we add the comma between the string and the values in the StrFmt method
    beginingText += '\', ';
    j = 1;
    //for each value in the container
    for(i = 1; i <= conLen(cont); i++)
        if(conPeek(cont,i) !='')
            //we add all the variables in the StrFmt method
            beginingText +=  strFmt('%1, ', conPeek(cont, i));
    //deleting the last 3 characters from the string (a comma, a space and a return)
    beginingText = strDel(beginingText, strLen(beginingText)-3, strLen(beginingText));
    //putting the 2 strings together
    finalText = beginingText + endingText;
    //positioning the cursor in the editor
    //adding the final string in the editor
    //and deselecting the text
    e.gotoCol(strlen(e.currentLine()) + 2);
    e.insertString(strFmt('%1', finalText));

Hope you enjoyed my post and in case you have a question don’t hesitate to leave a comment.

Posted in Tools and tagged , , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *