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!

infoMethodExample

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
            j++;
        }
    }
 
    //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));
    e.unmark();
}

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

Customizing MorphX using EditorScripts Class

Most IDEs have some cool build-in tools to help you write faster and more efficient code, and MorphX is no different. In case you hate repetitive typing as much as I do, here is a quick tweak that can help.

There are a couple of ways to add functionality to the MorphX Editor, and the easiest and fastest way is by adding a new method to the Class EditorScripts in the AOT.

Every new Method in the class can be called from the Editor by Right Click > Scripts.

MSDYNAX EditorScripts Comment

So let’s write a very basic Method, just to get an idea of what we can do with this Class. This method will comment the line in which the cursor is, or the highlighted lines in the code.

In the AOT go to Classes-EditorScripts and create a new Method which will look something like this:

private void method1()
{
}

Please note the name convention: pcoNewNode is the new node under the Scripts menu and everything after the underscore is the node under pcoNewNode. The parameter _editor is just our Editor instance.

First we want to get where the line number where the code highlighting starts and where it ends. Next, we deselect the highlighted text and for each line we write in the first position the string “//”.

public void pcoNewNode_CommentScript(Editor _editor)
{
    //getting the line number of the start and end of the highlithing
    int startLine = _editor.selectionStartLine();
    int endLine   = _editor.selectionEndLine();
    int i;
 
    //unmark the highlighted text
    _editor.unmark();
 
    //for every line
    for (i = startLine; i <= endLine; i++)
    {
        //put the cursor on that line number
        _editor.gotoLine(i);
        //put it in the first position (column) of that line
        _editor.gotoCol(1);
        //insert the string
        _editor.insertString('//');
    }
}

This is a very easy and fun way of customizing MorphX, because all you have to do is write good old  X++ code.