Editorscripts: create parm method from class declaration

August 27, 2009 at 13:55
filed under Dynamics AX
Tagged ,

I’ve always found it annoying that when you want to create a parm method using the editorscripts, you have to fill in two fields on a dialog (variable name and type). Most of the time, you’ve already declared the variable in the class declaration of your class, so it would be nice if you could just right click on that and create a parm method from there.

Below is a (quick and dirty) editorscript that allows you to do that.
For AX 2009:

public void template_method_parmfromCD(Editor editor)
{
    int endLine   = editor.selectionEndLine();
    str line;
    str firstCol;
    str secondCol;
    int linecount;
    str source;
    container cont;
    XppSource xppSource;
    TreeNode tn;
    TreeNode childNode;

    boolean contains(str s, str findStr)
    {
        if(strscan(s, findStr, 1, strlen(s)) > 0)
            return true;

        return false;
    }

    str replace(str s, str findStr, str replStr)
    {
        int pos = strscan(s, findStr, 1, strlen(s));

        while(pos > 0)
        {
            s = strdel(s, pos, strlen(findStr));
            s = strins(s, replStr, pos);

            pos = strscan(s, findStr, pos + strlen(replStr), strlen(s));
        }
        return s;
    }
    ;

    editor.firstSelectedLine();
    while (editor.moreLines() && endline > editor.lineNo())
    {
        line = strltrim(editor.getLine());
        while(contains(line, "  " ))
        {
            line = replace(line, "  ", " " );
        }

        cont = str2con(line, " ");

        firstCol = conpeek(cont, 1);
        secondCol = conpeek(cont, 2);
        secondCol = strrem(secondCol, ";=");

        editor.nextSelectedLine();

        tn = EditorScripts::getApplObjectNode(editor);
        if(firstCol != "0" && secondCol != "0")
        {
            if(!tn.AOTfindChild(strfmt("parm%1", strupr(substr(secondCol,1,1))+substr(secondCol,2,strlen(secondCol)))))
            {
                if(tn.AOTfindChild("Methods"))
                    tn = tn.AOTfindChild("Methods");

                childNode = tn.AOTadd(strfmt("parm%1", strupr(substr(secondCol,1,1))+substr(secondCol,2,strlen(secondCol))));

                xppSource = new XppSource();
                source = xppSource.parmMethod(firstcol,secondCol);
                childNode.AOTsetSource(source, false);
                tn.AOTsave();
            }
            else
            {
                error(strfmt("Parm method %1 already exists", strfmt("parm%1", strupr(substr(secondCol,1,1))+substr(secondCol,2,strlen(secondCol)))));
            }
        }
    }
}

For AX 2012 (thanks to Alex):

public void template_method_parmfromCD(Editor editor)
{
     int endLine   = editor.selectionEndLine();
     str line;
     str firstCol;
     str secondCol;
     int linecount;
     str source;
     container cont;
     XppSource xppSource;
     TreeNode tn;
     TreeNode childNode;

     boolean contains(str s, str findStr)
     {
         if(strscan(s, findStr, 1, strlen(s)) > 0)
             return true;

         return false;
     }

     str replace(str s, str findStr, str replStr)
     {
         int pos = strscan(s, findStr, 1, strlen(s));

         while(pos > 0)
         {
             s = strdel(s, pos, strlen(findStr));
             s = strins(s, replStr, pos);

             pos = strscan(s, findStr, pos + strlen(replStr), strlen(s));
         }
         return s;
     }
     ;

     editor.firstSelectedLine();
     while (editor.moreSelectedLines() && endline > editor.currentLineNo())
     {
         line = strltrim(editor.getLine());
         while(contains(line, "  " ))
         {
             line = replace(line, "  ", " " );
         }

         cont = str2con(line, " ");

         firstCol = conpeek(cont, 1);
         secondCol = conpeek(cont, 2);
         secondCol = strrem(secondCol, ";=");

         editor.nextSelectedLine();

         tn = EditorScripts::getApplObjectNode(editor);
         if(firstCol != "0" && secondCol != "0")
         {
             if(!tn.AOTfindChild(strfmt("parm%1", strupr(substr(secondCol,1,1))+substr(secondCol,2,strlen(secondCol)))))
             {
                 if(tn.AOTfindChild("Methods"))
                     tn = tn.AOTfindChild("Methods");

                 childNode = tn.AOTadd(strfmt("parm%1", strupr(substr(secondCol,1,1))+substr(secondCol,2,strlen(secondCol))));

                 xppSource = new XppSource();
                 source = xppSource.parmMethod(firstcol,secondCol);
                 childNode.AOTsetSource(source, false);
                 tn.AOTsave();
             }
             else
             {
                 error(strfmt("Parm method %1 already exists", strfmt("parm%1", strupr(substr(secondCol,1,1))+substr(secondCol,2,strlen(secondCol)))));
             }
         }
     }
}

Some code from Greg Pierce’s awesome String class was used for the inner methods.
I tested it on classes, form and reports, and it works very well.

To install this editorscript, copy the code above in a new method of the class EditorScripts.
To use it, in the classDeclaration, simply select one or more declarations, then right click and choose Scripts – template – method – parmfromCD.

4 comments

RSS / trackback

  1. Jonas

    Thanks, works fine. The one thing that does not work however, is when variables are declared like:

    str firstStr, secondStr;

    I am rather new to x++, so I no not dare fixing the code myself… :-)

    /Jonas

  2. Klaas Deforche

    Hi Jonas,

    I’m glad you’re putting it to use. Some colleagues and myself are finding it very useful too :).
    You’re right, when you declare multiple variables on one line, it will not work. I didn’t think about that situation because I don’t use it very often.

    If I find the time to fix it, I’ll upload it here for sure.

    Best regards.

  3. AlexOnDAX

    Very handy! For AX 2012 2 small changes in order to make this work.

    editor.moreLines() needs to be editor.moreSelectedLines()

    editor.LineNo() needs to be editor.currentLineNo()

  4. Klaas Deforche

    Awesome, thanks Alex.
    I’m sorry for the late reply, I’ve been getting a lot of spam on my blog and you message was somewhere lost in it.

respond