Rory Becker - DevExpress CodeRush Blog

June 2011 - Posts

  • New Refactoring in CodeRush 11.1 – Use Named Arguments

    Another new refactoring in CodeRush 11.1 is the Use Named Arguments refactoring.

    This refactoring simply alters a given method call, so that each argument also declares the parameter which it is intended to feed.

    This can sometimes make it easier to read your code by in-lining the purpose (parameter name) alongside each argument.

    Use-Named-Arguments_thumb2_thumb1

  • New Refactorings in CodeRush 11.1 – Inline Method [and Delete]

    There are 2 Inline Method refactorings introduced in CodeRush 11.1.

    The first refactoring: “Inline Method”, will simply copy the selected method’s code into each of it’s call sites as if prior to having been extracted from there.

    Inline-Method_thumb4_thumb1

    The second refactoring “Inline Method (And Delete)” will also remove the method in question.

    InlineMethodAndDelete_thumb[1]

  • New Refactoring in CodeRush 11.1 – Convert to Point

    The new Convert To Point refactoring converts a pair of numeric values into a single point value. As you might expect, it also adjusts the call site, method signature and usages to conform with the new point value.

    Convert-To-Point_thumb2_thumb1

  • New Refactoring in CodeRush 11.1 – Convert To Tuple

    The new Convert To Tuple refactoring can be applied to 2 or more parameters of a method.

    As it’s name suggests, it replaces the parameters in question with a suitable Tuple. It further ensures that calls to the method as well, as internal references to the parameters, are updated appropriately.

    Convert-To-Tuple_thumb7_thumb1

    • CodeRush Tip – Enhance Standard VS Navigation

      How many times have you hit Ctrl+Home or Ctrl+End (to navigate to the start or end of a file), only to realize mere seconds later, that you forgot to copy something to the clipboard before you left.

      Now you have to find where you were, copy the code you were after, and then jump back.

      There are several ways to get back to where you were:

      • Remember some snippet of code in the vicinity of where you want to be, and use VS’s find window.
      • Remember the name of the method you were in, and navigate to it using Quick Navigation, then locate the code within the method.
      • Page Up or Down to the location of your code. Scanning whole pages of code as you go in case you miss it.
      • Reach for the mouse and use the vertical scrollbar. While many people don’t like the mouse, this does give finer grain control than Page Up and Down.
      • Remember the exact line you were on and use Goto line (Ctrl+G)
      • View.NavigateBackward (See alternatives section after summary)

      Wouldn’t it be great if you’d already dropped a CodeRush Marker before you left. That way you could just hit Esc and CodeRush would bring you back to where you were. It would even arrange the view the way it was before you left and restore the selection. Then you could copy the value you needed and return to whichever end of the file you wanted to.

      Configuration

      Actually setting this up is very simple. We’re going to use the exact same technique from when we Enhanced Studio's Find Command

      • Create a new shortcut (Details here)
      • Set key 1 to Ctrl+Home
      • Set command to MarkerDrop
      • Check the checkbox that says “after executing this command, pass shortcut to IDE for default handling
      • Set context to Focus\Documents\Source\Code Editor
      • Right Click this new shortcut
      • Select Duplicate Shortcut
      • Select the Duplicate
      • Change key 1 to Ctrl+End
      • Click Ok.

      …and you’re done.

      Summary

      All you really need to understand having done this is that you can now visit the top or bottom of a file (via Ctrl+Home or Ctrl+End) and then easily revisit the location you came from, by hitting Esc.

      Alternatives

      There are those that will point to the built in visual studio View.NavigateBackward (Alt+Left) function. However this command is for me, somewhat less useful. It does not restore my view to how it was before. It does not restore the selection and it does not provide any kind of locator beacon to help me focus on where I was.

      Now if only I could add CodeRush Markers to Notepad and Word Smile

    • Changes to the P?Type? Template in CodeRush 11.1

      Our previous posts on the New Contexts, the New StringProviders and the New Goto TextCommand in CodeRush 11.1 are obviously useful in their own right, but as hinted at they also come together to contribute to an upgrade to the standard p?Type? template.

      The p?Type? template now emits any backing fields together in a single region. This renders the code easier to read, making it simpler to understand and maintain.

      Consider the code expansion generated with the previous version of the p?Type? template…

      pType10_2

      As you can see, each field generated for backing store is placed next to the property that accesses it. This can be useful for grouping by function, but it can lead to code which is harder to read. Fields that are distributed in this way can be harder to locate are easily skipped over when scanning through code. We’ve long been asked for a way to group all te generated fields together in one place.

      The new implementation of the p?Type? does exactly that, navigating to the first field in the type, injecting the field in this location and then returning to finish emitting the property at the location originally picked by the user.

      pType11_1

      I think you’ll agree it’s also a lot easier on the eyes.

    • 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.

    • New StringProviders in CodeRush 11.1

      As some of you might recall, several new pieces of functionality have arisen out of our weekly webinars. We have regularly implemented functionality on the spot simply to demonstrate the ease of doing so.

      This has happened so often that we decided to make a regular webinar dedicated to new features.

      One such piece of functionality was originally described and built in our “Advanced CodeRush Plug-ins - Text Commands, String Providers and Context Providers” Webinar

      Since then it has obviously been optimized and put through quality control etc., to emerge the other side as the «?MemberHeader» and «?MemberFooter» StringProviders.

      These each take a single parameter (one of Fields, Methods, Properties, or Constructors) and use this (together with the current language) to look up a suitable string (customizable via the options) which is then returned in the usual way.

      For example the default expansion for «?MemberHeader(Fields)» under C# is:

      // Fields …

      …and in VB.Net the same value would be …

      ‘ Fields …

      These are currently used in updates to p?Type? template, but can be used in your own templates as easily as any StringProviders 

      All these strings can be altered as usual via the appropriate options page (Editor\Code Style\ Member Sections) which as mentioned is language sensitive.

      MemberHeaderFooterOptions

      Note: sensible defaults also exist for VB.Net, C++ and JavaScript

      Feel free to reference any of these from your own templates.

    • New Contexts in CodeRush 11.1

      An noted in previous posts, contexts are one of the corner stones of the DXCore, that help make your user experience as natural as possible. More contexts allow features a finer granularity with which to test if circumstances should allow them to function.

      For example: Templates intended to produce member code, should only be allowed to expand if the caret is within a type and also is not within another member. The p?Type? template is a good example of this. When activated from within a type (rather than from within a member), it produces a property of the correct type. Outside of this situation, it produces nothing.

      CodeRush 11.1 adds 4 additional contexts which will return if the current type has any of each of the 4 named items within it.

      • HasConstructors
      • HasFields
      • HasMethods
      • HasProperties

      These contexts can be found in the Editor\Code node within any context picker.

      As ever, each of these contexts can have any one of 3 states

      • Tick – I require tis context to be explicitly true in order for the specified functionality to be considered valid.
      • Cross - I require this context to be explicitly false in order for the specified functionality to be considered valid.
      • Blank – The value of this context is not important to this function. Don’t test it.

      Some of these contexts are used by new CodeRush 11.1 functionality, but all are available to be used by you if you feel the need. Consider these as 4 extra tools in your toolbox.

      Feel free to contact me if you feel you need contexts beyond those provided out of the box, and I’ll be happy to show you how they are created. You never know… I might even send you a fully functional example. Smile

    • CodeRush - Navigational TextCommands

      Navigational TextCommands are those which pertain to the movement of the current text insertion point. The insertion point is the point at which text will be emitted during the execution of a template or other TextDocument manipulation.

      Some example Navigational TextCommands are «GotoFileTop» and «GotoFileBottom» which, as you might guess, move the insertion point to the top and bottom of the current file respectively.

      But what if you want to jump to one of these locations, emit some text and the jump back to where you started?

      Well this has also been thought of.

      The «StoreInsertionPoint» and «GotoInsertionPoint» TextCommands are for exactly this purpose. The «StoreInsertionPoint» will store the current location of the insertion point, using a lookup key passed to the command as its first parameter. The «GotoInsertionPoint» will retrieve a previously stored location likewise using a lookup key passed to it as a first parameter.

      Consider the following template:
      -------------------------------------------------------------
      «StoreInsertionPoint(Origin)»«GotoFileTop»using System.Data;
      «GotoInsertionPoint(Origin)»DataSet MyDataSet;
      -------------------------------------------------------------

      It performs the following steps:

      • Store the current location of the insertion point in a variable called Origin
      • Move the insertion point to the top of the current file.
      • Emit using System.Data; followed by a newline
      • Move the insertion point back to the location previously stored.
      • Emit DataSet MyDataSet; followed by another newline

      As you can see, this places the using statement at the top of the file, whilst emitting the new DataSet variable at the point the user invoked the template.

      Navigational TextCommands are certainly a powerful concept. We’ll be providing more of these commands in the future and also showing you how you can create your own.

    LIVE CHAT

    Chat is one of the many ways you can contact members of the DevExpress Team.
    We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

    If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

    FOLLOW US

    DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

    Copyright © 1998-2017 Developer Express Inc.
    All trademarks or registered trademarks are property of their respective owners