Stef
Stef

Reputation: 23

Auto Extract Interface in AX2012 X++ Code Editor

Is there a function in X++ editor that lets you auto-extract an interface after implementation?

Likewise there is such a function in Visual Studio that is auto-extracting an implemented interface by providing method stubs for all the methods needed for implementing the interface.

Upvotes: 0

Views: 431

Answers (1)

Matej
Matej

Reputation: 7627

No, there is no such feature.

But you can write one. See sample code below. And add Action menu item to \Menus\SysContextMenu.

Alternatively you can add method to \Classes\EditorScripts

public static void main(Args _args)
{
    #macrolib.AOT

    SysContextMenuAOT menuAot;
    TreeNode tn;
    DictClass dc;
    DictMethod dm;
    int i, j;
    str dmName;
    str intSrc, intParams;

    TreeNode itnRoot, itnChild;

    if (!SysContextMenu::startedFrom(_args))
    {
        throw error(Error::wrongUseOfFunction(funcName()));
    }

    menuAot = _args.parmObject();
    tn = menuAot.getFirstNode();

    dc = new DictClass(className2Id(tn.AOTname()));
    if (!dc)
    {
        throw error(Error::wrongUseOfFunction(funcName()));
    }

    for (i = 1; i <= dc.objectMethodCnt(); i++)
    {
        dm = dc.objectMethodObject(i);
        dmName = dm.name();
        if (dm.isStatic()
            || dm.isDelegate()
            || dm.accessSpecifier() != AccessSpecifier::public
            || dm.name() == identifierStr(classDeclaration)
        )
        {
            continue;
        }

        if (!itnRoot)
        {
            itnRoot = TreeNode::findNode(#ClassesPath).AOTadd("I" + dc.name());
            itnRoot.AOTsave();

            itnChild = itnRoot.AOTfirstChild();
            itnChild.AOTsetSource(strFmt('public interface I%1\n{\n}', dc.name()));
        }

        itnChild = itnRoot.AOTadd(dm.name());
        intParams = '';
        for (j = 1; j <= dm.parameterCnt(); j++)
        {
            if (j > 1)
            {
                intParams+= ', ';
            }
            intParams+= strFmt('%1 %2', extendedTypeId2DisplayName(dm.parameterType(j), dm.parameterId(j)), dm.parameterName(j));
        }
        intSrc = strFmt('public %1 %2(%3)\n{\n}', extendedTypeId2DisplayName(dm.returnType(), dm.returnId()), dm.name(), intParams);
        itnChild.AOTsetSource(intSrc);
    }

    if (itnRoot)
    {
        itnRoot.AOTcompile();
        itnRoot.AOTrestore();
        itnRoot.AOTnewWindow();
        itnRoot.AOTedit();
    }
}

Upvotes: 2

Related Questions