Bongo
Bongo

Reputation: 3153

Axapta 4.0 comment shortcut

I am working now 3 Days with axapta 4.0 and I am not able to comment text by shortcut. I am aware that there is no shortcut implemented in axapta but is there a possible solution so that I can write a snippet or something that comments my text. I can't imagine that nobody else encountered this problem before and hasn't found a solution.

Upvotes: 0

Views: 387

Answers (2)

SysC0mp
SysC0mp

Reputation: 100

In addition to the answer above:

You can open the available editor scripts menu by typing Alt+R, S

After that, you could take a look at the getApplicableScripts method in the EditorScripts class. This method is responsible to fetch the editor scripts which the menu contains by returning a container filled with them. You can modify that code for your needs.

As an example, to modify the container to only contain the comment & uncomment functionality, add following code right underneath the variable declarations:

if (curUserId() == "YourUserId")
{
    scripts += "comments_comment";
    scripts += "comments_uncomment";

    return scripts;
}

//- Some more code

In that way, you can type Alt+R, S, C, C to comment and Alt+R, S, C, U.

... it just needs a bit practice to remember at first.

Upvotes: 0

Alex Kwitny
Alex Kwitny

Reputation: 11544

This should do the trick for you. It's sort of hacked up from other methods just to be copy & paste simple...you can optimize.

Create this method alex_GeneralComment here:

Classes\EditorScripts\alex_GeneralComment

And put in this code and you should be able to select code in the editor, [right click>Scripts>alex>General Comment]

It will create a comment that looks like this, with the cursor on the // line. Experiment with it.

// (ALEX) (12/03/2015)
// 
//-->
info("If I select these two lines of code, then right click and do the ");
info("script, the comment block will look like this");
//<--
public void alex_GeneralComment(Editor _editor)
{
    #define.YourCompanyName("ALEX")
    str         selText = EditorScripts::getSelectedText(_editor, false);
    str         selFirstLine;
    int         startLine = _editor.selectionStartLine()+1;
    int         endLine   = _editor.selectionEndLine()+1;
    int         startCol  = _editor.selectionStartCol();
    str         name      = XUserInfo::find(False, curUserId()).name ? XUserInfo::find(False, curUserId()).name : XUserInfo::find(False, curUserId()).networkAlias;
    xppSource   xppSource;

    str getSelectedText(Editor e, boolean takeAllIfEmpty = true)
    {
        int i;
        str text;
        str line;
        int _startLine = e.selectionStartLine()+1;
        int _endLine   = e.selectionEndLine()+1;
        int _startCol  = e.selectionStartCol();
        int endCol    = e.selectionEndCol();

        if (_startLine == _endLine && _startCol == endCol)
        {
            // This method returns the selected text, and if the user selects
            // no text, then it returns nothing.  So we want to return the
            // entire code block if the parameter is set
            if (!takeAllIfEmpty)
                return text;

            e.firstLine();
            while (e.moreLines())
            {
                text += e.getLine()+'\r\n';
                e.nextLine();
            }
        }
        else
        {
            e.firstSelectedLine();
            for (i = _startLine; i <= _endLine; i++)
            {
                line = e.getLine();
                if (i == _startLine && i == _endLine)
                {
                    line = subStr(line, _startCol, endCol-_startCol);
                }
                else
                if (i == _endLine)
                {
                    line = subStr(line, 1, endCol-1);
                }
                else
                if (i == _startLine)
                {
                    line = strRep(' ', _startCol-1)+subStr(line, _startCol, strLen(line));
                }

                text += line + '\r\n';
                e.nextSelectedLine();
            }
        }
        return text;
    }

    boolean isSelectionNonEmpty(str s)
    {
        // delete all special symbols
        return strLen(strRem(strRem(strRem(s," "),"\n"),"\r"))>0;
    }

    if(isSelectionNonEmpty(selText))
    {
        startLine = _editor.selectionStartLine()+1;
        _editor.firstSelectedLine();
        selFirstLine = _editor.getLine();

        startCol  = strLen(selFirstLine) -strLen(strLTrim(selFirstLine));
        xppSource = new xppSource(startCol);
        _editor.insertLines(strFmt("// ("+#YourCompanyName+") (%1)", date2str(today(), 213, 2, 4, 2, 4, 4, DateFlags::None))+"\n");
        _editor.insertLines(xppSource.indent()+strFmt("// \n"));
        _editor.insertLines(xppSource.indent()+strFmt("//-->\n"));

        // If it's one line, indent it, otherwise paste as-is
        if (startLine == endLine)
            _editor.insertLines(xppSource.indent() + selText);
        else
            _editor.insertLines(selText);

        _editor.insertLines(xppSource.indent()+strFmt("//<--"));
        _editor.gotoLine(startLine);
        _editor.gotoCol(strLen(_editor.getLine())); // Go to the comments section
    }
    else
    {
        startCol = _editor.columnNo();
        xppSource = new xppSource(startCol);
        _editor.insertLines(strFmt("// ("+#YourCompanyName+") (%1)", date2str(today(), 213, 2, 4, 2, 4, 4, DateFlags::None))+"\n");
        _editor.insertLines(xppSource.indent()+strFmt("// \n"));
        _editor.insertLines(xppSource.indent()+strFmt("//-->\n"));
        _editor.insertLines(xppSource.indent()+strFmt("\n"));
        endLine = _editor.currentLineNo(); // Line we want to end on
        _editor.insertLines(xppSource.indent()+strFmt("//<--"));
        _editor.gotoLine(endLine);
        _editor.gotoCol(startCol+1);
    }
}

Upvotes: 2

Related Questions