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

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube