As you know from some of my previous post, I'm a bit fan of developer productivity. Over the years I've delved into creating new Editor Scripts in Axapta, some of the more complex ones shown below:
http://kashperuk.blogspot.dk/2008/08/another-useful-editor-script-for.html
http://kashperuk.blogspot.dk/2010/01/editor-script-for-simplifying-search-in.html
http://kashperuk.blogspot.dk/2008/05/editorscriptsaddinsopeninaot-version-2.html
That's why I was very happy to read a blog post by Martin Drab aka goshoom the other day. Martin describes the use and the simple steps to create new code snippets for Visual Studio that apply to X++ language. Here it is: http://dev.goshoom.net/en/2016/06/custom-code-snippets/
Now, as you can see from the blog post, it's not exactly the same in terms of capabilities as the Editor Scripts in AX 2012 and prior. The code snippets in VS are specifically targeted at inserting some new code into the editor or refactoring existing code in the editor, but not interacting with AOT and AX metadata.
They are however still quite handy, especially if you get used to the hotkey combination Ctrl+K, Ctrl+X (or S for surrounding your code with whatever the snipped inserts).
That's why I decided to create a public code snippet library through which we could potentially share some useful code snippets with each other.
To start it off I have ported some of the more commonly used template editor scripts from AX 2012.
Here is the link to the Visual Studio X++ code snippet library.
Share your cool code snippets here in the comments, and I will upload them to the share (just to have some moderation process in there)
If you have some comments or suggestions as to a better place to store these, let me know through the comments below.
Thanks!
Tools and tutorials for Microsoft Sustainability Manager, Microsoft Cloud for Sustainability, Microsoft Emission Impact Dashboard, as well as Microsoft Dynamics AX aka Microsoft Dynamics Supply Chain Management (F&O)
Code highlighting
Showing posts with label EditorScripts. Show all posts
Showing posts with label EditorScripts. Show all posts
Tuesday, September 27, 2016
Tuesday, January 05, 2010
Editor script for simplifying the search in AOT
When doing code refactoring, or when investigating a particular class or form to understand the functionality behind it, it is often handy to search for variables used in its methods, looking at where and how each variable is used.
It is tedious to manually go through the steps of copying the name of the variable into the clipboard, opening up the parent class/form, opening up the AOT Find dialog for it, pasting the variable name into the Selected Text field and pressing Find now.
I have automated these steps, putting the code into an editor script.
You can download the xpo, which contains the EditorScripts class with the additional method, by following this link.
Now you should be able to very easily and quickly find the required code.
The code of the editor script:
It is tedious to manually go through the steps of copying the name of the variable into the clipboard, opening up the parent class/form, opening up the AOT Find dialog for it, pasting the variable name into the Selected Text field and pressing Find now.
I have automated these steps, putting the code into an editor script.
You can download the xpo, which contains the EditorScripts class with the additional method, by following this link.
Now you should be able to very easily and quickly find the required code.
The code of the editor script:
///
/// Editor script for finding all occurrences of the selected text in the parent node of the selected method
///
///
/// Reference to the AX editor
///
///
/// 2010-01-05, ivanv
///
void addIns_FindAllOccurrencesInNode(Editor e)
{
#AOT
TreeNode treeNode;
FreeText selectedText;
FreeText GetSelectedTextFromEditor(Editor _e)
{
str 1 curSymbol;
int iCopyFrom;
int iCopyTo;
FreeText _selectedLine;
;
if (_e.markMode() != MarkMode::NoMark && _e.selectionStartCol() != _e.selectionEndCol())
{
_selectedLine = strLRTrim(subStr(_e.currentLine(), _e.selectionStartCol(), _e.selectionEndCol() - _e.selectionStartCol()));
}
else
{
_selectedLine = _e.currentLine();
for (iCopyFrom = _e.columnNo()+1; iCopyFrom >= 0; iCopyFrom--)
{
curSymbol = subStr(_selectedLine, iCopyFrom, 1);
if (!strAlpha(curSymbol) && curSymbol != '_')
break;
}
for (iCopyTo = _e.columnNo()+1; iCopyTo <= strLen(_selectedLine); iCopyTo++)
{
curSymbol = subStr(_selectedLine, iCopyTo, 1);
if (!strAlpha(curSymbol) && curSymbol != '_')
break;
}
_selectedLine = (iCopyFrom < iCopyTo) ? subStr(_selectedLine, iCopyFrom + 1, iCopyTo - iCopyFrom - 1) : '';
}
return _selectedLine;
}
void FindLinesContainingSelectedText(FreeText _selectedLine)
{
Args args = new Args(formstr(SysAOTFind));
FormRun sysAOTFindFormRun;
FormStringControl containingTextCtrl;
FormButtonControl findNowBtnCtrl;
;
sysAOTFindFormRun = classFactory.formRunClass(args);
sysAOTFindFormRun.init();
sysAOTFindFormRun.run();
// Set the text to find
containingTextCtrl = sysAOTFindFormRun.design().controlName(identifierStr(ContainingText));
containingTextCtrl.setFocus();
containingTextCtrl.pasteText(_selectedLine);
sysAOTFindFormRun.detach();
// Launch the search process
findNowBtnCtrl = sysAOTFindFormRun.design().controlName(identifierStr(FindNow));
findNowBtnCtrl.clicked();
}
;
selectedText = GetSelectedTextFromEditor(e);
if (selectedText)
{
// Find the currently open method node
treeNode = TreeNode::findNode(e.path());
// Find the parrent node of the method
treeNode = TreeNode::findNode(xUtilElements::getNodePathRough(xUtilElements::parentElement(xUtilElements::findTreeNode(treeNode))));
if (treeNode)
{
treeNode.AOTnewWindow();
FindLinesContainingSelectedText(selectedText);
}
}
}
Thursday, October 22, 2009
Running a class from AOT or "How to assign a class to an action menu item?"
OK, so this might seem as a very simple question for many of you.
But, in reality, I still now and then meet people who have worked with AX for quite some time, but don't know this very basic concept.
Therefore, I just want to briefly describe it here to refresh everyone's memory
Apart from allowing to run the class from AOT, this will allow to assign it to a menu item (according to Best Practices, it should be a menu item of type Action), and run the class using this menu item.
Below is the correct syntax of the main method:
Please also note, that there is an editor script that contains the template for the main method. You can invoke it from Scripts->template->method->main
But, in reality, I still now and then meet people who have worked with AX for quite some time, but don't know this very basic concept.
Therefore, I just want to briefly describe it here to refresh everyone's memory
Apart from allowing to run the class from AOT, this will allow to assign it to a menu item (according to Best Practices, it should be a menu item of type Action), and run the class using this menu item.
Below is the correct syntax of the main method:
public static void main(Args _args)
{
//Your code here
}
Please also note, that there is an editor script that contains the template for the main method. You can invoke it from Scripts->template->method->main
Tuesday, August 12, 2008
Another useful Editor Script for developers
In AX the principle of upcasting is used quite a lot. Some examples coule be the RunBase hierarchy or the InventMovement class hierarchy. Basically, the base (or super) classes contain the main part of the hierarchy logic, while subclasses only override a couple of methods performing actions specific to this class.
When browsing the AOT, trying to find a specific line of code (debugging without the debugger, basically), you often find yourself in a situation when you lookup a definition of a method from a derived class, and end up in this method of the base class instead (because upcasting was used in the code in question). And every time you need to see the same implementation in the child class, you need to go back to the AOT and open it from there manually.
Well, not any more.
Here is a small editor script, that you can use to navigate to an overridden method of a derived class.
I created 2 versions of the same script, one using xReferences, the second using Dictionary class. The xRef one, generally, works faster once the xRefs for the class hierarchy is updated (happens the first time you use the script). So I am not posting the second script version. If anyone is interested in that version specifically, let me know and I will send you the code.
I made a couple of small changes to the script since my previous post (which I deleted), making it possible to import the script without compilation errors into AX prior to verion 2009. Sorry for that :)
You can copy-paste the code for the script from axaptapedia.com (There is a problem with copying over the code from blogger site directly)
You can read more about Editor Scripts and how to use them here
Enjoy, and get back to me with any comments or suggestions, posting them as comments or e-mailing me directly
When browsing the AOT, trying to find a specific line of code (debugging without the debugger, basically), you often find yourself in a situation when you lookup a definition of a method from a derived class, and end up in this method of the base class instead (because upcasting was used in the code in question). And every time you need to see the same implementation in the child class, you need to go back to the AOT and open it from there manually.
Well, not any more.
Here is a small editor script, that you can use to navigate to an overridden method of a derived class.
I created 2 versions of the same script, one using xReferences, the second using Dictionary class. The xRef one, generally, works faster once the xRefs for the class hierarchy is updated (happens the first time you use the script). So I am not posting the second script version. If anyone is interested in that version specifically, let me know and I will send you the code.
I made a couple of small changes to the script since my previous post (which I deleted), making it possible to import the script without compilation errors into AX prior to verion 2009. Sorry for that :)
You can copy-paste the code for the script from axaptapedia.com (There is a problem with copying over the code from blogger site directly)
You can read more about Editor Scripts and how to use them here
Enjoy, and get back to me with any comments or suggestions, posting them as comments or e-mailing me directly
Thursday, May 29, 2008
EditorScripts.addIns_OpenInAOT - version 2
A long time ago, I posted an editor script for opening objects selected in the editor in AOT (link to that post)
Yesterday, miklenew from AxForum posted an editor script for AX 3.0 that opens a new AOT window with the object currently selected in the Editor, that works a little differently and also allows to open objects based on variable type. You can view the original post here.
Today I modified this code, extending it a bit and adding support for Dynamics AX 4.0 and Dynamics AX 2009 (For version 3.0 use the code posted on AxForum).
As you can see, the code is very simple and utilizes the xReferences.
But the great thing about it is that updating the cross references is not required for this code to work, as it updates them on the fly for this specific AOT node.
I think this is a must have method for each application developer out there. :)
Yesterday, miklenew from AxForum posted an editor script for AX 3.0 that opens a new AOT window with the object currently selected in the Editor, that works a little differently and also allows to open objects based on variable type. You can view the original post here.
Today I modified this code, extending it a bit and adding support for Dynamics AX 4.0 and Dynamics AX 2009 (For version 3.0 use the code posted on AxForum).
public void addIns_OpenInAOT(Editor e)
{
#AOT
TreeNode treeNode = TreeNode::findNode(e.path());
xRefTmpReferences xRefTmpReferences;
Column nCol = e.columnNo() + 1;
Line nLine = e.currentLineNo() + 1;
;
treeNode.AOTmakeXref(1);
xRefTmpReferences = xRefCreate::makeTmp(infolog.lastxRef());
select firstonly xRefTmpReferences
order by Column desc
where xRefTmpReferences.line == nLine &&
xRefTmpReferences.Column <= nCol;
if (!xRefTmpReferences)
return;
treeNode = TreeNode::findNode(xRefTmpReferences.path());
if (treeNode)
treeNode.AOTnewWindow();
}
As you can see, the code is very simple and utilizes the xReferences.
But the great thing about it is that updating the cross references is not required for this code to work, as it updates them on the fly for this specific AOT node.
I think this is a must have method for each application developer out there. :)
Subscribe to:
Posts (Atom)