Add Code Templates/ Shortcuts /Comments

If you’ve got any blocks of code that you use frequently, e.g. specific comment blocks, you can very easily add code short cuts in AX 2012 to auto-insert  them in to your X++ code.

For example you can setup AX to automatically create surrounding comment such as

Coomment1

 

whenever you type “mycom” and press the tab key.

How do you accomplish this. Very easily!

Coomment

 

Step1: Locate the class EditorScripts in the AOT.
Step2: Create a new method with the format public void template_flow_[shortcut](Editor editor)
Step3: User the editor parameter to add code the code that you would like inserted. e.g. editor.insertLines(“\\test comment”);
Step4: Add your method to the case statement in the isApplicableMethod() method in the section relating to template “editor scripts that does not apply to Macros” 

Scipt_Comment

 

 

Thats it, now if you type your shortcut into any editor in AX and press tab, the “\\test comment” code will be inserted.

Here’s a full example method

Comments are very Much self Explanatory

Public void template_flow_mycom(Editor editor)

{

xppSource xppSource = new xppSource(editor.columnNo());

int currentLine  = editor.currentLineNo();

int currentCol = editor.columnNo();

//add opening comment , You can add what ever you want 

Source template = “//–>(Code added by ” +curUserId()+ ” date On ” + date2str(today(),123,2,2,2,2,2) +”).\n”;

;

//add an empty line

template += strRep(” “, currentCol)+ “\n”;

//add closing comment with the correct number of spaces, You can add what ever you want 

template += strRep(” “, currentCol)+ “//<–(Code ended by ” + curUserId()+ ” date On ” +date2str(today(),123,2,2,2,2,2) + “).”;

editor.insertLines(template);

//move cursor to the empty line between the comments

editor.gotoLine(currentLine+2);

editor.gotoCol(currentLine+4);

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s