Rory Becker - DevExpress CodeRush Blog

April 2011 - Posts

  • CodeRush Tip – Quick Navigation – Build your own Search

    As we saw in our previous post, Quick Navigation has many filter options available to for you to fine tune your search, and it will even remember these choices between usages.

    What’s less well know about CodeRush, is that many of the commands support the passing of parameters.

    In the case of the QuickNav command, 4 parameters are accepted (TypeFilter, MemberFilter, AccessFilter and LocationFilter)
    Each of these parameters have several different values which may be passed.

    At this point I think it’s worth paying homage to AlexS and a forum post now nearly 3 years old, but no less valid for it.

    To quote some of his post…
    -------------------------------------------------------------
    TypeFilter -- defines default type filter for the Quick Navigation.
    Use the following values to setup default type filter: Classes, Interfaces, Structs, Enums, Delegates, AllTypes.
    Use "and" keyword to combine filters together like this: Classes and Structs

    MemberFilter -- defines default member filter for the Quick Navigation.
    Use the following values to setup default member filter: Methods, Properties, Events, Fields, LocalsAndParams, AllMembers.
    Use "and" keyword to combine filters together like this: Methods and Properties.

    AccessFilter -- defines default access filter for the Quick Navigation.
    Use the following values to setup default access filter: Private, Protected, Internal, ProtectedInternal, Public, AllVisibilities.
    Use "and" keyword to combine filters together like this: Private and Protected.

    LocationFilter -- defines default location filter for the Quick Navigation.
    Use the following values to setup default location filter: AllFiles, CurrentSolution, CurrentProject, CurrentNamespace, CurrentFile.
    You can not use "and" keyword here to combine filters together.
    -------------------------------------------------------------
    Note: These parameters should be comma separated, in order, leaving blanks for unset parameters.

    So by creating a new shortcut and passing the right parameters, we can start to create our very own sub features.

    For example, I like to use this facility to setup:

    • Ctrl+T –> Find Types in Solution
    • Ctrl+M –> Find Members in File

    To create the 2 features mentioned above, use the following bindings and parameters:

    Find Types in Solution
    Bind to : Suggest Ctrl+T
    Command: QuickNav
    Params: AllTypes, , AllVisibilities, CurrentSolution

    Find Members in File
    Bind to : Suggest Ctrl+M
    Command: QuickNav
    Params: , AllMembers, AllVisibilities, CurrentFile

    It should be noted that these shortcuts will set the options, open Quick Navigation and perform the search. You are entirely free to use the resultant screen as is, or tweak the options further and really narrow your search.

    • If you use these shortcuts again, they will restore the options to the assigned settings.
    • Use of the raw Quick Navigation function (Ctrl+Shift+Q) will remember whatever set of options were last configured.

    Happy searching

  • CodeRush Tip – Quick Navigation

    Quick Navigation is a kind of intelligent search.

    Basic Search:

    • Press Ctrl+Shift+Q.
    • Type the (partial) name of a Type or Member you’re looking for in the box at the top.

    QuickNavigationSearchString

    • As you type, the search results are narrowed and displayed for your perusal.
    • The phrase you typed, is highlighted in red in each of the results.
    • Select an entry (Enter or Double Click) to jump directly to it.

    Camel Case Search

    Additionally Quick Navigation will recognise the use of Capital letters to indicate word boundaries

    • If you type CHS this will be interpreted as “please find me phrases with these letters in sequence
    • Found characters are highlighted in red with blue highlights indicating additional capitals which existing between those sought.
    • Note below how the phrase CHS has located both “CopyAsHexString” and “CSharpEventHandlerService”

    QuickNavigationCopyHexString

    Intelligent Searching

    Like the References Tool window, Quick Navigation is intelligent.

    It is not simply a text search facility. Instead Quick Navigation searches for Types or Members which match your search criteria. Like the References Tool window, this means that comments and partial matches are excluded from the results, preventing you from having to filter those results out yourself.

    Options

    Quick Navigation hides it’s filter options by default. They can be toggled using the dropdown in the upper right position of this dialog.

    QuickNavigationFilters

    The buttons presented represent various restrictions that can be placed on the results brought back.

    • The first 3 groups of buttons (Types, members and Access) can have their buttons set in any arrangement.
    • The last set of buttons (scope) may have only a single button active at once.

    You can restrict

    • the type of Types searched to any combination of Classes, Interfaces, Structures, Enumerations and Delegates
    • the type of Members searched to any combination of Methods, Properties, Events, Fields and Locals and Parameters
    • the Access level of results to any combination of private, protected, internal (friend), protected internal or public

    … and of course you can restrict the Scope of the search itself to any of current File, Namespace, Project, Solution or All files

    Stay tuned for more Quick Navigation related tips

  • CodeRush Tip – Enhance Studio’s Find command.

    “How can I easily return to where I started having used Visual Studio’s find command to locate something?”

    One way of achieving this is….

    • Drop a marker (Alt+Home) before you perform your search.
    • Hit Esc to return to the marker when you’re finished looking around.

    However that’s a lot to remember to do, so why not have CodeRush automate a little more for you.

    • Create a new shortcut (Details here)
    • Set key 1 to Ctrl+F
    • 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
    • Click Ok

    CodeRush should now drop a marker whenever you bring up the Find Dialog. which means you only have to remember to hit Esc to collect them.

    Bonus marker facts

    • Markers are stored in a stack formation so that they leave a sort of trail of breadcrumbs.
    • Each successive press of Alt+Home adds a marker to the top of the stack.
    • Pressing Esc “Collects” a marker, removing it from the top of the stack and navigating you to the location it represents.
    • Collecting a marker doesn’t just restore you to the position in the file it represents, it also restores the view and selection.
    • Markers are also removed from the stack when the files they refer to are closed.
  • CodeRush Tip – The References Toolwindow

    What does it do?

    The References Toolwindow shows you every reference to a specified type or identifier (solution wide). It is triggered either using the DevExpress\Tool Windows\References menu or by using it’s default shortcut of [Shift] +[F12]

    Why is this different from “Find in Files”?

    Intelligent search

    Put simply, “Find in Files” is not very clever.

    Imagine that you inherit a large legacy application, and are informed that there is a global variable called “str”. You’d like to get some idea of how often this variable is accessed…. some sense of how tightly or loosely coupled it is to the rest of the system.

    Before the References Tool window, your best bet was Visual Studio’s “Find in Files” feature. This will find you every minor reference to str anywhere in your search range. It will blindly find you references, comments and even partials, but this isn’t always very useful.

    Can you imagine how many hits you’re going to get back searching for references to ‘str’? You’d get hits on string, instruction, tostring, stretch, strong, strange, and of course any comment with any of the previous words and more in it. That is a lot of false positives.

    The References Tool window provides an intelligent search, picking up only references to the identifier in question.

    Presentation of Results

    The results are displayed (by default) in a tool window split left and right (see below).

    • The left side is the main tree of results arranged in a tree by project, files namespaces etc.
    • The right side is used for showing a preview of the selected result in situ.

    ReferencesToolWindowDisplay

    Single clicking or otherwise selecting any single result in the list on the left, will cause the preview window on the right to be updated to show the result in question along with any surrounding code to give added contextual information.

    Command vs. Live Sync mode

    The References Toolwindow can operate in 2 different modes.

    ReferencesCommandMode  OR referencesLiveSync

    When pushed, these buttons will change the mode, displaying at all times whichever is the current mode.

    In Command mode the References Toolwindow will wait for you to explicitly click the refresh button (on the far left of it’s toolbar), before refreshing it’s contents based on the identifier at the caret..

    In Live Sync mode CodeRush will refresh the References window every time you move the caret from one identifier to another.

    Manipulating the results

    ReferencesToolbar

    The buttons after the mode toggle allow you to manipulate how the results are displayed.

    • The first 2 buttons allow you to sort references by either Reference Count or Alphabetically.
    • The next 5 buttons toggle hierarchy elements in the results. (ie Projects, Files, Namespaces, Classes and Methods in the Tree of results.
    • The next 2 buttons allow you to alter the font size of the results in the tree.
    • The next 3 buttons let you pick a preview location (right, bottom or off.)
    • The final button determines if the progress indicator shows up when references are being calculated.

    How Fast is it?

    Another good question. I decided to put that to the test. I went out and tried to find the largest open source solution I could. So that others might duplicate my test.

    After a little searching I came across SharpDevelop. This is a pretty well known open source IDE. An alternative, if you will, to Visual studio. It’s a pretty complex project with over 750, 000 lines of code.

    I loaded up the primary solution and had a look around. I was looking for something that would produce a lot of results, to really push the References Tool window.

    Then it hit me….. “Let’s search for references to string.” There should be more than a few of those in a solution this size.. right?

    So I found an initial reference to string, and hit [Shift]+[F12]

    ReferencesToolWindow

    As you can see from the results. I got back more than 16000 references in ~6 seconds including the time it took to populate the results window. In the real world, you’re not typically going to be searching for something that prolific. but even if you are, 6 seconds is a pretty quick time to get your results back in.

    For what it’s worth… if I had used “Find In Files”, I’d have got back 21,933 references in 2316 files. That’s over 5717 extra results I didn’t want or need. These would have included partial matches and comments that might have had nothing to do with the type I was actually searching for.

    Ah and did I mention that this works with C#', VB.Net, C++ and even JavaScript.

  • CodeRush Tip – Where are my CodeRush settings stored?

    I’d like to back up my CodeRush settings, where are they kept?

    Your CodeRush settings are kept within a folder called "Settings.XML" folder. This folder is stored, by default, within your windows profile folder.

    On my machine this is "C:\Users\Rory.Becker\AppData\Roaming\CodeRush for VS .NET\1.1\Settings.xml"

    There are 2 ways to locate this folder

    • Copy this phrase to your explorer address bar "%appdata%\CodeRush for VS .NET\1.1\Settings.xml\" and hit enter

      OR

    • Visit the DevExpress\About menu option and click the Settings button

    You can alter the location where CodeRush stores your settings by visiting the options screen (Ctrl+Shift+Alt+O) and locating the Core\Settings options page. The first option on this page determines where settings are stored.

    Bonus tip:

    This folder (the Settings.XML folder) can be obliterated (or simply renamed) at will, and CodeRush will recreate it's contents from a copy generated at the time of install.

    Warning: This effectively resets your settings to factory defaults and no further copy is kept. If you delete this folder, you will lose any customizations you have made including any custom templates or shortcuts.

  • CodeRush Tip – How to locate key bindings

    How do I locate a CodeRush key binding?

    CodeRush key bindings can be found in the options (Ctrl+Shift+Alt+O) under IDE\Shortcuts. However there are quite a few and searching through the hierarchy manually can be something of a pain unless you know where to look.

    Happily a manual search is not your only option.

    Above the shortcut tree, there is a toolbar containing several buttons which are useful in different circumstances.

    IDE-Shortcuts-toolbar

    In this case the following 2 options may be of some use.

    Option 1 – Search

    • The 2nd last icon on the toolbar is a search facility which can be used to search for shortcuts.
    • You can search by folder, key binding, mouse binding or by command name.

    IDE-Shortcuts-Search

    Option 2 - Flatten hierarchy and sort.

    • Click third last icon in the toolbar to collapse the hierarchy.
    • You'll now be viewing the same set of shortcuts in a flat grid rather than in a tree.
    • You should now be able to sort by either Shortcut or Command and easily locate the binding you're after.

    Bonus Tip

    We recommend placing user created bindings in a custom root folder within the hierarchy so as to easily distinguish between your own shortcuts and those shipped with CodeRush. This can also ease transferal of these shortcuts to other machines since this will cause said shortcuts to be placed in a file of their own.

  • CodeRush Tip - Tab to Next Reference

    TabToNextReference

    Tab to next Reference is one of those features that’s so simple to pick up, but once you’ve used it for even a few minutes, you’ll wonder how you ever survived without it.

    It allows you to [Tab] between the references to an identifier just, as if you were tabbing between the elements of a dialog box.

    Simply place your caret within an identifier, and hit the [Tab] key.

    Immediately CodeRush highlights all references to the identifier, and navigates you to the next one in sequence.

    Each additional press of the [Tab] key will navigate to the next occurrence of the identifier in question.

    When you reach the last reference, an additional press of [Tab] will navigate back to the first instance.

    Like tabbing between controls on a form, [Shift] + [Tab] will navigate through your references in reverse order.

    Finally, hit the [Esc] key and your caret will jump back to where you started.

    Oh and did I mention, this feature works solution wide, skipping from file to file and project to project, to locate each and every reference.

    I should also point out that whilst my example on the right is in VB.Net, this feature works just as well in C#. It’s even supported in JavaScript.

    Go on… Try it…

  • New CodeRush Plugin – DX_ReviseSelection

    Just recently during one of our Live Webinars on “Working with the TextDocument and TextView” a viewer challenged us to write a plugin.

    Plugin Requirements : To duplicate the selected text as a comment above the selection, as a reminder of what the code used to look like before edits that are about to be made.

    So we started the clock and went to work. 13 minutes later, we were done.

    The creation of the plugin was recorded, and is present within the webinar above at around 1 hour and 6 minutes into the recording.

    Usage

    Assuming that you assign the ReviseSelection command to Ctrl+R, simply highlight the code you’re looking to revise and hit the Ctrl+R combo

    ReviseSelection

    Your code will be duplicated in a comment above your selection, leaving you free to modify this code knowing you have a reference to how it used to work immediately above. Obviously once you have your new code working and passing all your tests, you can remove this comment from the file altogether.

    Note: This plugin is equally happy working on either C# or VB.Net

    DX_ReviseSelection is available here

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

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