Visual Studio 2013 Preview: CodeLens aka Code Information Indicators

[Updated: 7/12/2013: Thanks to John Gardner for the updated info.]

 

There is only one way to describe this next feature: IT ROCKS!

 

To use this feature you will need Visual Studio 2013 Preview and Team Foundation Server 2013 Preview as some information comes from TFS.

 

 

Presenting the Problem

For me, this is the signature feature in Visual Studio 2013. It’s well thought out and solves multiple issues we face as developers. But what IS it exactly? The best way to demonstrate it is to remind you what life was like before we had it. Here is a method (from the TailSpin toys sample code) the way we have seen it in the past:

5-16-2012 6-04-11 PM

 

What if I need to find all the references to this method? I can press SHIFT+F12 and get results in the Find Symbol Results window:

5-16-2012 6-09-48 PM

 

How about the tests associated with this method? I’d either dig through the references above or dig into the Test Explorer window and see what I can find. But what if I want to know who last changed this method? How about the number of times this code has changed? Now I have to dig into other windows to locate the data. In short, I can get all the information I need but have to go to several different places to get it.

 

 

Solving the Problem

The Visual Studio team has come up with an elegant solution to the problem: show information about the code WITH the code. It’s a great addition to the development experience. Originally called Code Information Indicators, the marketing folks have now dubbed this CodeLens.

 

Turning It On

CodeLens is on by default but I want to point out the information you can choose to view. Go to Tools | Options | Text Editor | All Languages | Code Information Indicators:

5-16-2012 6-29-59 PM

(NOTE: a faster way to get here is to press CTRL+Q for QuickLaunch and type the word “information” into the search area then select the Code Information Indicators entry)

 

I’ll show these in the order you will most likely see them when you begin using this feature.

 

 

 

Show References

5-16-2012 6-38-10 PM

The first indicator you will typically notice is the references. This is just like using Find All References but instead of presenting the information in another window a summary of references is shown with the code. With any of the CodeLens indicators you can access the details in a couple of ways.

 

 

Mouse Access

You can click on any indicator to see the results:

5-16-2012 6-56-14 PM

 

Also there is a scroll bar (if needed) to allow moving through multiple entries:

5-16-2012 6-57-02 PM

 

 

Keyboard Access

If you don’t want to take your hands off the keyboard, you can simply hold down your ALT key on your keyboard and each indicator will show a number above it:

5-16-2012 7-02-04 PM

 

Pressing ALT + [indicator number] will show the information within. In this example, ALT+2 will open the references. These numbers are static so you can always depend on them. Regardless of where you are, ALT+2 will always show the references.

 

 

 

Show Tested By

With any method we often want to know what tests are associated with it. The Show Tested By option will show what tests are associated with your code and an overall status indicator:

5-16-2012 7-35-20 PM

 

As you can see in this example, the tests haven’t been run yet as shown by the icon to the left of the test summary. If I click on the Show Tested By section or press ALT+3:

5-16-2012 7-42-07 PM

 

Then I get the information window:

5-16-2012 7-49-55 PM

 

Notice we can run the currently selected test or run all tests. To access the Run All or Run links with just your keyboard just use your TAB key. If you want to examine an individual test method you can press Enter (or Double Click) any entry in the information window and the editor will show the test method details:

5-16-2012 8-02-04 PM

 

Let’s jump back to our original code. Looking at the Show Tested By indicator we can see that if all the tests have passed this is the result:

5-16-2012 8-06-05 PM

 

If any tests fail, then we will see something like this:

5-16-2012 8-10-03 PM

 

You can see any tests that failed by opening the Show Tested By area:

5-16-2012 8-11-33 PM

 

 

 

Show Test Status

Now let’s discuss the Show Test Status indicator. Normally, when we look at a test method this is what we see:

5-16-2012 8-22-36 PM

 

However, once we run our tests, it would be nice to see the status of the run with the test method itself. This is where Show Test Status comes in. By turning this on we can see the status of the last run represented as an icon (to the left of the references information):

5-16-2012 8-27-31 PM

 

If you want to see the latest test result, you can Click the icon or press ALT+1:

5-16-2012 8-29-33 PM

 

And see the test result window:

5-16-2012 8-34-24 PM

 

 

 

Show Authors and Show Changes

The Show Authors and Show Changes options actually show the same information but filter it in two ways. Both are updated when you Get Latest from source control. Show Authors will show the name of the last person to make a change plus a count of any other unique individuals who have worked with the same code:

5-16-2012 9-04-43 PM

 

Whereas Show Changes will show the total number of changes in the code’s history:

5-16-2012 9-14-16 PM

 

Selecting either of these by using your mouse or by pressing ALT+4 and ALT+5 respectively:

5-16-2012 9-17-18 PM

 

Opening either one of these indicators will reveal the following:

5-16-2012 9-19-59 PM

 

This is where things get interesting. You can Right Click any entry in this window to be presented with three options that we will examine in detail:

5-16-2012 9-24-40 PM

 

 

View Diff Of Changeset[Number]

This will show the difference between the selected changeset and the one prior for comparison:

5-16-2012 9-29-10 PM

 

 

Changeset Details

This choice will show the details of the changeset in Team Explorer:

5-16-2012 9-30-39 PM

 

 

Send Email To [Author]

Pretty self-explanatory, this option will open up your default email program and allow you to begin crafting an email to the author of the changeset with a link to remind the author of the code changed:

5-16-2012 9-34-22 PM 

 

 

 

Miscellaneous

In this section I’ll show some additional items that may be of interest to you as you discover more about CodeLens.

 

Once you have opened any information window:

5-16-2012 12-04-12 PM

 

You can use RIGHT ARROW or LEFT ARROW to move between the other information windows.

(NOTE: The team that works on this let me know that the official keyboard shortcut for moving between these windows is ALT+RIGHT ARROW and ALT+LEFT ARROW. Be aware that just plain ARROW keys may stop working in future iterations of the build and you will need to use the official keys to move between windows here.)

 

 

Skipping Lines

I began this article saying that this feature is well thought out. Nothing emphasizes my point more than the way the indicator are treated in the editor. Rather than being intrusive and even throwing off your line counts by treating the indicators as a line of code the indicators are simply decorations added in the editor and are not counted as an actual line:

5-16-2012 10-14-49 PM

 

The concept of being non-intrusive is so ingrained into this feature that even the cursor will skip over it when you are using your keyboard to move between lines in the editor.

 

 

Changing Indicator Fonts and Colors

If you want to modify the font and/or color of the indicators you can go to Tools | Options | Environment | Fonts and Colors then select Show Settings For the Code Information Indicators:

5-16-2012 9-54-27 PM

 

Experiment with these to find settings that are appealing to you.

 

 

Turning It Off

If, for some reason, you don’t like this feature then you can always turn it off by going to Tools | Options | Text Editor | All Languages | Code Information Indicators and disable the feature.

 

 

 

Finally

CodeLens is truly an epic addition to the Visual Studio world. I am a huge fan of this feature. Try it yourself and tell me what you think! :)