Inside TFS

Deep Focus on Your Code with CodeLens

Visual Studio 2013's new CodeLens feature provides a wide variety of information to the developer, all while never leaving the code editing window.

Every once in a while, I come across a feature in Team Foundation Server or Visual Studio that almost seems like magic. I find at least one "Oh, wow!" moment with each release. The just-released Visual Studio 2013 does not look to be an exception to this rule: CodeLens is going to be that "Oh, wow!" feature.

So what is CodeLens? It's a new feature in Visual Studio 2013 Ultimate that shows you information about your code directly in the code editor. Before CodeLens, you had to dig through several different windows to retrieve information such as method references, tests associated with a method, the last time a line of code was changed or how many times the code has been changed. Researching and finding this information takes you away from the code editor, and away from writing code. CodeLens changes that by putting this information literally at your fingertips within the code editor.

The first thing to be aware of is that CodeLens is turned on by default. You can, however, control what information is displayed in the code editor. To modify this information, go to Tools> Options >Text Editor> All Languages> Code Information Indicators. This allows you to turn off CodeLens functionality completely or just specific pieces, such as Show Test Status or Show Authors. Notice that CodeLens is referred to as Code Information Indicators in the Options menu. Code Information Indicators was the original name of this feature, but for the final 2013 release, it's been rebranded CodeLens.

I already have an option for finding references in code, by pressing SHIFT+F12 on a method. However, doing that takes me to another window with the search results, and pulls me away from the code editor. With CodeLens I can see, above the method in the code, the number of references to a particular method. Clicking on this reference link expands a window over the code editor, listing the references. The window will even include a scroll bar, depending on the number of references listed. Double-clicking a reference will take me to that code in the code editor. But I can also easily move my mouse, and have this reference window disappear, without having to move from the code editor itself. Figure 1 shows an example of the expanded references window.

In addition to the reference information shown in Figure 1, I can also see author and change information. It shows that Brian Keller made the most recent change to this method, and two other authors have also worked on this method. I can also see this method has been included in four different changesets in Team Foundation Server Version Control. Clicking on either of these links displays a list of information, including changeset id, changeset description, the changeset author and when the changeset was created. From within this window, I can right-click on a row and view changeset detail information, view the diff information for the changeset, or send an e-mail to the author of the changeset.

[Click on image for larger view.] Figure 1. Using CodeLens to view reference information.

Another indicator is the Tested By indicator, which shows the tests available that test the particular method. To enable this information, I need to first open the Test Explorer window (Test> Windows> Test Explorer). Once this window opens, a new indicator will be added to the CodeLens information, as shown in Figure 2.

[Click on image for larger view.] Figure 2. Using CodeLens to view test information.

Figure 2 shows that there are two tests for testing this method, and currently neither have been executed. Clicking the link displays the tests, and allows me to run a specific test or all tests related to this method. I can click the Run All link to run all the associated tests. Figure 3 shows the results, with one test passing, and the other test failing.

[Click on image for larger view.] Figure 3. The CodeLens information is automatically updated with the results of running the tests.

If I fix the failed test, nothing will stand out visually on the CodeLens indicator line. This is by design. CodeLens indicators are supposed to provide useful information without getting in the way or becoming distracting. The ability for a developer to stay in the code editor while receiving detailed information from multiple parts of the codebase and Team Foundation Server is going to be a feature that every developer can appreciate and utilize.

Those interested in evaluating Visual Studio 2013 or Team Foundation Server 2013 can find them here.

About the Author

Mickey Gousset spends his days as a principal consultant for Infront Consulting Group. Gousset is lead author of "Professional Application Lifecycle Management with Visual Studio 2012" (Wrox, 2012) and frequents the speaker circuit singing the praises of ALM and DevOps. He also blogs at ALM Rocks!. Gousset is one of the original Team System/ALM MVPs and has held the award since 2005.

comments powered by Disqus


Subscribe on YouTube