Rory Becker - DevExpress CodeRush Blog
  • CodeRush Tip – Quickly comment or uncomment code.

    Question: “Does CodeRush have any features for helping me to quickly comment or uncomment blocks of code?”

    Answer: Yes. CodeRush has function called SelectionComment.

    This function comments or uncomments the currently selected block of code using the current language’s single line comment syntax.
    The shortcut for this function is the same key as used for the respective language’s single line comment. (ie VB.net uses the ' key and C# or  C++ use the / key)

    Simply

    • Select the appropriate code block
    • Press the key suited to that language.* (Either ' or /)

    Additionally SelectionComment is actually a toggle command, which means that you can use the same key to uncomment code.

    SelectionComment

     

    *One small caveat

    VB.Net users based in the Uk will need to adjust the SelectionComment shortcut because of the way keyboards of different nationalities and layouts handle scan codes.

    The US scan code for ' is the same as the UK scan code for #

    To change the key binding

    • Visit the options (Ctrl+Shift+Alt+O)
    • Navigate to IDE\Shortcuts
    • Locate the Selection\Modifying folder in the Shortcuts page.
    • Highlight the # binding
    • Place your caret in the upper right of the dialog where it says Key1: #
    • Specify the key you’d like to bind to (probably ')
    • click Ok

    Everything should now be fixed.

    You can learn more about binding keys in CodeRush in this blogpost

  • CodeRush Tip– Create a Select / Switch statement with branches for each element of an enumeration

    “Is there a way in CodeRush to auto-fill a SELECT CASE statement with the available enumerations ?”

    I was just asked this question on Twitter and because it was such a reasonable question, I figured I’d share the wealth in a more permanent way by posting the answer on my blog.

    Ok so imagine you have an Enumeration with 4 elements:
    SuitEnumerationVB

    …and you declare a variable of that type…

    SuitVariableVB

    What you need to do is

    • Copy the name of that variable to the clipboard.
    • Type either select or switch (depending on your language of choice).

    Update: You don’t even have to type all of the characters in select or switch. There are even shorter aliases. Type either se<space> or sw<space> for the same effect.

    SuitEnumerationPreExpansion

    • Hit space to expand the template.

    SuitEnumerationPostExpansion

    CodeRush will generate a switch or select statement, with branches for each of the potential value of your enumeration variable.

    Additionally CodeRush will place Markers in each of these branches, so that you can sequentially hit Escape to jump to each of them in turn, and insert whatever code you feel is appropriate.

  • New CodeRush Community Plugin – DX_GenerateProxy

    DX_GenerateProxy

    Generates a new Proxy class P based on source class S

    What?

    What this amounts to is a new class P which is given properties and methods matching the public ones that are possessed by S.
    When methods or properties of S are called their implementation defers to the instance of P held internally.

    Why?

    • I’m beginning to prefer working in terms of interfaces where possible. I like to mock interfaces rather than classes where possible.
    • I don’t like to modify generated client-side web-service code. This therefore means that I don’t like to extract interfaces from said code.

    The solution to this problem is to wrap the client-side web-service code in a proxy of my own from which I can extract an interface without having to alter the client-side web-service itself..

    So given a web-service (W1), I can create a proxy (W1_Proxy) with all the same methods and properties as the original class. I can extract an interface from W1_Proxy and write my client-side code in terms of this interface. During testing I can implement this interface a second time in a test class (or more likely I can mock the interface), and pass this to the same code.

    This gives me

    • Client code that doesn’t depend on a specific implementation of a service
    • Client code Tests that don’t depend on the web-service in order to run and prove my code works.
    • A good feeling of having done things the right way.

    Limitations

    Currently this CodeProvider only acts on Class declarations for which you have source code. a Later revision will add a facility to generate a proxy for compiled types.

    Example

    Let’s start with the following code…

    GenerateProxyClass_Before

    Place the caret on the name of the class and then press your CodeRush Refactor key

    GenerateProxyClassSmartTagMenuItem

    …and the following code will be generated for you…

    GenerateProxyClass_After

    Download DX_GenerateProxy

  • New CodeRush Community Plugin – DX_InterfaceNav

    DX_InterfaceNav

    Provides a new Navigation entry (“To Implementor”) which navigates to implementations of an interface.

    The default Navigate shortcut is Ctrl+Alt+N

    You can use this plugin to navigate to Implementations of an Interface from …

    • Instance Reference -  From reference to a variable who’s type is either ISomeInterface or descends from same.
    • Class Declarations -  From the declaration of a class. Caret on class name.

    DX_InterfaceNavExample2

    You can also use this it to Implementations of Interface Members from…

    • Method Reference -  From references to a property or method on a variable who’s type is either ISomeInterface or descends from same.
    • Variable Reference - From references to a Parameter who’s type is either ISomeInterface or descends from same.

    DX_InterfaceNavExample1

    Work will continue on this plugin to provide additional navigations.

    Download DX_InterfaceNav

  • New CodeRush Community Plugin - DX_ConvertToTryParse

    DX_ConvertToTryParse

    This new plugin provides a new item for the Code menu which converts…

    ConvertToTryParseBefore_thumb[5]

    …into…

    ConvertToTryParseAfter_thumb[2]

    There is a small limitation in this version. The new variable introduced (“Success”) is currently hardcoded and may clash with an existing variable. I’ll fix that in an update. I just wanted to get this out there for now.

    Download DX_ConvertToTryParse

  • New CodeRush Community Plugin – DX_RedundantAssignment

    Over the last few weeks I’ve returned to writing plugins for a bit, and I think some of them have reached the point where they might be useful for people beyond myself.
    This post, and the next few that follow it, are intended to just put these plugins out there and see what people think.

    Feel free to post comments and feedback.

    DX_RedundantAssignment

    This first plugin is an example of how to build a simple Refactoring and CodeIssue and link them together.

    This CodeIssue will alert you if you are assigning a variable to itself. Further, the CodeIssue provides a link to a Refactoring that will remove the assignment from your code altogether.

    RemoveRedundantAssignmentCS_thumb2_t

    The refactoring is of course available in it’s own right via your CodeRush\Refactor Key

    RemoveRedundantAssignmentVBRefactori

    Download DX_RedundantAssignment

  • What is a Dynamic list?

    Question: What is a DXCore Dynamic list?

    Simple Answer: A named dictionary<string, string> with associated Name, Description and Variable.

    OptionsPageTreeDynamicListsYou can locate the dynamic lists through the options screen.

    It’s an advanced page, found at Core\Dynamic Lists

    Note: The Dynamic lists options page is one of those pages, whose content is language based. There are lists for VB.net and C'# as well as quite a few *neutral* lists.

    As stated above, each list is really a named dictionary<string, string>. 

    Consider the following  list of dictionary items…

    Key Value
    s System.String
    i System.Int32
    ds System.Data.DataSet

    Imagine that this list was called System Types an was associated with a variable called Type.

    Now imagine another list….

    Key Value
    mbc MyBusinessClass
    mobc MyOtherBusinessClass
    mnst MyVerySpecialType

    …this second list is called Custom Types and is also associated with a variable called Type.

    Now let’s a imagine a CodeRush template called v?type? whose definition is…

    -------------------------------------------------------------
    Private «?Get(Type)» «Caret»MyVariable«BlockAnchor»;
    -------------------------------------------------------------

    What we have here, is a template that can be triggered by a user typing the letter v, followed by any of the items on the left side of either of the 2 lists.

    Therefore the following are all valid triggers for the template:

    Template Trigger Expansion
    vs<space> private System.String MyVariable;
    vi<space> private System.Int32 MyVariable;
    vds<space> private System.Data.DataSet MyVariable;
    vmbc<space> private MyBusinessClass MyVariable;
    vmobc<space> private MyOtherBusinessClass MyVariable;
    vmvst<space> private MyVerySpecialType MyVariable;

    Note: A dynamic list can be flagged as containing .Net types. In which case it will remove any unnecessary name-spacing at expansion time, and return only the core type. This would mean returning String instead of System.String. This, I think you’ll agree, is much more aesthetic.

    For those that haven’t spotted it, the example lists I mentioned for this demonstration are standard dynamic lists that already ship with CodeRush. Additonally the v?type? template is also a real template (Although the real version is a mite more complicated to allow for additional scenarios).

    The first list (System Types) contains mappings for 68 of the more commonly used types in the .Net framework as well as 3 special mappings I’ll explain in a moment.
    The second list (Custom Types) is initially empty when shipped but can be added to by you the user.

    What about my types?UseTypeInTemplates

    If you come across a business class you wish to add to this list (making it accessible by any template that uses the ?type? syntax and variable) then you can right click it and use the Use Type in Templates menu item. This will ask you what abbreviation you’d like to use for the class in question and then add it to the Custom Types list.

    Special Values

    The System Types list also contains 3 additional items.

    Key Value
    \ «?Paste»
    / «?Type»
    ? «?ReturnType»
     
    This gives us a clue that the value n these lists, need not be entirely literal. indeed they can contain StringProviders as seen in the above examples.
     
    In this case…
    … v\<space> will yield a variable whose type is pasted from the clipboard.
    … v/<space> will yield a variable whose type is the same as the type whose code enclosed the caret.
    … v?<space> will yield a variable whose type is the same as the current method’s return type.
     
    These shortcuts can obviously also save a lot of typing.

    So what’s dynamic about a dynamic list

    A Dynamic list is a list of mappings typically from shorter more terse strings to larger strings. The dynamic part, is that you can choose to add items to the list without informing anything that is referencing it.

    This means that instead of constructing one template for each potential element in your list, you can construct a single template for the list itself, and have CodeRush do the hard work for you. Additionally if you ever choose to add to the list for any reason, you won’t have to amend the template.

    Finally the same list can be reused in many many templates. (for example in CodeRush we ship v?type?, m?type?, p?type?, a?type? and r?type? )

    When you add a business type to the Custom Types list, this new class is available to all those templates that make use of the Type variable with no extra work on your part.

    Now that’s a big win.

    In an upcoming blogpost, I’ll show you how to build some additional functionality for CodeRush using a new custom dynamic list which we’ll create for the purpose.

  • CodeRush Template – Creating Virtual Methods

    CodeRush provides many templates for lots of different purposes. Of course you can’t cover every eventuality out of the box. So instead we built a system which allows you to build a solution of your own when you need to.

    Recently I was asked if there was a quick way to create a public virtual method.

    I did a quick search and discovered that there did not appear to be any pre-built in template that did this.

    However, as I’ve said before, building this sort of thing is pretty easy. So I decided to prove it and do the leg work to build my own.

    There are 2 parts to this: Naming and Implementation.

    Naming

    Well it turns out the naming is the hard part.

    It’s always good to have your templates named something which suggests the purpose of the template. Failing that, it should be closely related. ie There should be some logical link.

    Additionally it’s great if you can use as few keys as possible. Short of using mind control, and therefore zero keys, ideally we’ll settle for one… But which one?

    To this end I asked myself what the appropriate name for a template that produced virtual methods would be…

    • V – Already used for Variables
    • I  - Already used for Interfaces
    • R - Already used for Readonly properties
    • T - Already used for Types
    • U - Unused
    • A - Already used for auto-Implemented properties.
    • L – Unused

    Well that didn’t leave much of a choice. U or L.

    I chose L.

    Implementation

    Next up we need to create 2 templates which will provide us with the output we need.

    The original request was to produce a template which would output a public virtual method. The example given showed a void proc, but I figured it would be better to also provide the ability to produce methods that returned various types like our other dynamic templates.

    So I decided to base my templates on the m<space> and m?Type?<space> templates. This allows us to pass in any of the usual type params, or miss that out entirely and just emit a void proc as per the original example.

    If you’re unfamiliar with creating templates, or would like a quick refresher, then it’s probably worth reviewing my previous post on Template Creation Basics

    The Templates

    I created the following templates:

    Name: l?Type?
    Context: Editor\Code\InClass
    Expansion:
    -------------------------------------------------------------
    public virtual «:#MethodSignature#»«:#MethodBody#»
    -------------------------------------------------------------


    ...then a second template...
    Name: l
    Context: Editor\Code\InClass
    Expansion:
    -------------------------------------------------------------
    «Set(Type,void)»«:l?Type?»
    -------------------------------------------------------------

    Note: The contexts indicated above are a little simplistic. In reality, we should base the context from the m?Type? template.
    This would be something like: ((InClass or InStruct) and OnEmptyLine) but not (InComment or InMethod or InPreprocessorDirective or InProperty or InString)

    How does this work?

    In this section I'm going to explain a little of the detail about how these templates work with one another. If you don’t care about this, then feel free to skip ahead. There’s nothing vital here, but for those who are interested in creating their own templates, deconstructing an existing set can be useful.

    Let’s start with the l?Type? template which you added above:

    This template emits the phrase “public virtual “ and then defers to 2 other templates (#MethodSignature# and #MethodBody#) for the rest of it’s functionality.

    Note: Templates whose names are surrounded by # (pound signs or hash marks depending on your point of view) are called system templates. This is not because the have special access to system functionality or anything. Instead it’s because that naming convention is judged to make them unlikely to be called by a user directly and therefore are assumed to be in use solely by other templates (ie the system).

    If you look, you’ll find that each of these has their own expansions.

    #MethodSignature#

    Template#MethodSignature#

    #MethodBody#

    Template#MethodBody#

    Additionally #MethodSignature# also calls a level deeper into the #MethodParameters# template...

    #MethodParameters#

    Template#MethodParameters#

    This style of aliasing, is like calling a method. It allows you to extract a piece of a template and reuse it from as many places as you like. In this case we get to reuse functionality from templates that the DevTeam already created.

    If we expand  these aliases in the same way that CodeRush would, you can see what we would have had to type without this facility.

    TemplateLTypeAliasesExpanded

    So how does this work?

    Well this template does 6 things

    1. The literal phrase “public virtual” is emitted into the editor.
    2. The «?Get(Type)» StringProvider emits a reference to the Type passed in the trigger string. (String,int32, Dataset etc)
    3. The MethodName is wrapped in a «Field» in order to suggest a suitable edit point.
    4. This field is wrapped in  «Caret» and «BlockAnchor» pair indicating that Studio should select this text.
    5. A further «Field» is created to allow the entry of any method parameters.
    6. A method body stub is emitted. This contains a «FinalTarget» TextCommand within it represents the position to move the caret to when both fields have been entered.

    In the case of the 2nd template, we use «Set(Type,void)» in order to pre-set the type to void (ie no return type) and the call the 1st template via «:l?Type?». This passes the void type to the 1st template causing it to expand a method stub which returns no value.

    Note: To add even more functionality, you could create duplicate versions of these templates that omit the “#MethodBody#” aliases and the “public” keyword, making said duplicates suitable for use inside of interfaces. Of course the contexts of these duplicates should also be altered to only work inside of interfaces, but I’m sure you can see how quickly this could be achieved.

    Usage

    Well the usage of the template is quite simple. Position the caret somewhere sensible within a class or structure…

    Type ls<space>

    … and you will be rewarded with the stub of a public virtual method that returns a string. Alter it’s name and hit enter. Your caret jumps within the parenthesis ready for you to expand templates to.

    Additionally you can use…

    l<space>

    …to omit a return type and have the system emit a void proc instead.

    Conclusion

    As you can see, there’s not much to each of the parts involved in these templates. The power instead, is in the flexibility of each part, and the combinations in which they can be used. And so we have gone from feature request to fully developed feature in no more that 15 minutes.

  • CodeRush Plugin – TabNav

    Today I was navigating some legacy VB.Net code, and found myself wishing that I could quickly navigate from the top of a method to the bottom or vice-versa. Naturally my first thought was to write a plugin to add this capability.

    Seconds later, without knowing quite how I got there (Now that’s real muscle memory) I was starring at the default code of a new CodeRush plugin in VS.

    Note: Yes I realise that C# already has this functionality (Ctrl+]), but I wanted it in VB.Net as well.

    And so now I’ll show you how easy it was to build this exact plugin.

    Let’s build it

    This plugin is another example of a Simple Action Plugin and assumes that you already know how to Create a Plugin Project and add an Action

    The specifics of this plugin require 3 steps:

    • Set the Action’s ActionName property to “TabNav”.
    • Handle the CheckAvailability event.
    • Handle Execute event.

    Since the first step is only setting a property, we’ll concentrate on the CheckAvailability and Execute events.

    The CheckAvailability Event

    The purpose of this event, is to give us a place to decide if our action should be available, given the state of the code and our position within it.

    In our case, we require have 3 requirements:

    • The caret must be positioned on an element which has a block construct.
    • The caret must not be positioned on the name of the element (if any).
    • The caret must not be positioned on the type of the element (if any).

    To explain a little….

    • We require a block construct, because otherwise our action would be allowed to fire for primitives and statements etc. To navigate from start to end has no real meaning in these cases.
    • We explicitly do not allow our action to fire if we’re on the Name or Type of an element, because we don’t want to clash with the already wonderful TabToNextReference

    If all of these requirements are met, then we set ea.Available to true.

    TabNavCheckAvailability

    The CheckAvailability event is fired whenever the DXCore wishes to determine if the Action should be available. Because of this, our Execute event can rely on the fact that these prerequisites have been met, and get on with it’s main function with no need to double check them.

    The Execute Event

    This event is raised when the user elects to trigger the action. As noted before, the CheckAvailability event will have already been raised, and by virtue of having reached the execute event, we can assume that all requirements were met.

    Our plugin basically requires that we jump from one line of code, to another.

    We determine which line we’re on and instruct the DXCore to move the caret to the other. As an added bonus we jump to the first non-space character of that line.

    TabNavExecute

    There are 3 other methods in the code for this plugin. The PosOfFirstNonSpaceChar function is hardly worth mentioning. It’s a simple iteration that determines the (1-based) index of the first non-space character on a given line.

    The other 2 functions (shown below) determine if the caret is positioned on either the Name or Type of an element.

    TabNavUtilities

    Using TabNav

    This plugin will compile and run just fine as it is, but there is one final step you must take before you can see it in action.

    The TabNav action must be bound to a key. As you might have gathered, I recommend you bind the TabNav command to the {Tab} key

    At this point you should be able to Tab from Class –> End Class or back again. Additionally the same functionality should work for Namespaces, Methods, If statements, Do While loops and many others.

    Compatibility

    You might have noticed that nothing in the code so far, specifies any sort of language compatibility. This is because we’re working with abstract language constructs. So by default, this plugin will work in any imperative language supported by the DXCore.

    As previously noted however, this functionality is already supported by the VS Team for brace-based languages. The Ctrl+] keystroke will jump you between a function’s opening and closing brace. If you wish to rebind this, the command name is “Edit.GotoBrace”.

    Personally I think my use of {Tab} is much more intuitive, especially considering the beauty that is CodeRush’s TabToNextReference

    Whilst developing this plugin I learned that there are a few edge cases for the C# language that simply don’t apply for VB.Net. Chief among these, is the fact that the block start line is not always the same line as the Element start line. There are simple ways around this but since C# doesn’t need this feature, I elected to disable it in languages other than VB.Net.  In order to do that you can use this additional code in the CheckAvailability event:

    TabNavCheckAvailabilityAdditional

    In any case the plugin and it’s source code is available from the community plugin site

    So download, install and enjoy your new found freedom Smile

  • Merging CodeRush Templates (via Export and Import)

    When a new version of CodeRush is released, it’s quite often the case that there are new templates included with it.

    So why when you’ve completed the install, are these templates not always immediately available?

    Well put simply, it’s because we don’t want to damage or break any Template customizations that you might have created yourself.

    As you are no doubt aware by now, CodeRush is one of the most configurable products on the market. In this spirit, we allow you to add your own Templates or even customise the ones we offer you out of the box. If we were to overwrite these changes each time we installed, we’d upset people pretty quickly.

    Since we don’t like to upset people, we choose instead to not overwrite your templates with our own stuff unless you ask us to.

    This post is designed to show you how to merge your own custom Templates, with any new ones we might be shipping in a new release.

    Step 0 – Always work within a ‘Custom’ folder

    Yes there’s a step zero. This isn’t so much a step, rather more of a mind-set for when you’re creating templates of your own. We don’t force you to create templates in any particular way, but we do suggest that when you create new templates, that you do so inside of a new top level folder called ‘Custom’. By placing your Templates in this folder you are creating a single parent for all of your templates which will greatly ease the management of them in situations like merging.

    Step 1 - Export your custom TemplatesTemplateContextMenu

    • Open up the Templates options page (Ctrl+Shift+Alt+OEditor\Templates)
    • Right-click the Custom folder (The one you always put your custom templates in) and choose Export Folder. Save the resultant file somewhere safe.
    • Repeat for each language where you have created Templates of your own.

    Step 2  - Force CodeRush to rebuild your templates from defaults

    • Choose DevExpress\About
    • Click Settings
    • Dig into the Editor\Templates folder.
    • Delete (or move if you’re feeling more cautious) the files in this folder.
    • Switch back to VS
    • Return to the Templates options page again
      • Note: All of the templates will have been rebuilt from (new) defaults (without your custom templates)

    Step 3 – Reimport your Custom Templates

    • Select in turn each language for which you exported a file in step 1.
      • Right-click the template tree and pick Import Templates
      • Select the previously exported file which pertains to your currently selected language.
      • Click Ok and note how the Custom folder returns to the template tree.
    • Repeat for each language

    You have now merged the new templates with your own customizations, and are done.

    You should be able to see your own custom templates, and any new ones shipped with the most recent version of CodeRush.

3 4 5 6 7 8
9
10 11 12
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