New “Goto” TextCommand in CodeRush 11.1

You will recall from our previous discussion, that Navigational TextCommands allow a template author, to inject different pieces of themselves into different parts of the TextDocument.

The newest addition to this group of TextCommands is «GoTo». It takes a single parameter (Constructors, Fields, Methods or Properties) used to indicate the type of code to locate.

During execution, «GoTo» will move the insertion point immediately prior to the first LanguageElement of the indicated type. This allows templates to navigate to the first of any of these items within the current type, inject some text and then return to the origin. The effect of this can be to group elements of a given type together and promote more readable code.

As we will see later, the p?type? template has been upgraded under 11.1, and uses this technique to separate backing store from property where appropriate.

Note: The «GoTo» TextCommand does not move the insertion point, if the item being searched for does not exist. Therefore it is advisable to create duplicate templates to handle each of these cases. In the case where the case where the target item does exist, you can simply use «GoTo» as advised. In the case of it not existing, you can choose to locate your new item elsewhere. Of course the best way of doing this is through the use of the New Contexts also provided in CodeRush 11.1

So the next time you’re creating a template which emits a field, consider creating a parallel version of it that tests Editor\Code\HasFields  and wraps the field itself in «StoreInsertionPoint(MyField)»«GoTo(Fields)» and «GotoInsertionPoint(MyField)» to ensure your new fields is placed alongside others in the same type.

no comments
No Comments

Please login or register to post comments.