CodeLens Improvements in Visual Studio 2015

Introduction

CodeLens is a new feature of Visual Studio that is provided with Visual Studio 2013 Ultimate. Basically CodeLens provides the information about our project's code just before the methods/properties/class into editor, like reference counts of methods/properties and classes, tests (Unit Tests) associated with a method and how many times the line of code has been changed. In other words we can say that CodeLens describes the following three important things:
  • References
  • Impact
  • Test
  • Changes

CodeLensView1

Let's see a brief expiation of these three features of CodeLens as listed above.

References

References is on of the features of CodeLens that shows how many times a specific method/property/class is being used in the code files. Suppose we have a method named GetEmployeeDetails() that returns the information of an employee, like name, designation, organization, salary and so on. If we call this method inside the project code, the reference count will be increased from zero to the number of times we used this method. Let's see it through an example code.

Example: In the following example the GetEmployeeDetails() method is showing 1 reference because it is called/use only one time inside the Main method.

/References1

Program Code: (References)

  1. class EmployeeInfo   
  2.     {  
  3.         //Public Properties  
  4.         public string FullName { getset; }  
  5.   
  6.         public string Designation { getset; }  
  7.   
  8.         public string Organization { getset; }  
  9.   
  10.         public double Salary { getset; }  
  11.   
  12.         public string Email { getset; }  
  13.   
  14.   
  15.         static void Main(string[] args)  
  16.         {  
  17.             // Calling GetEmployeeDetails() method  
  18.             EmployeeInfo employee = new EmployeeInfo();  
  19.   
  20.             employee.GetEmployeeDetails("Ranjan Kumar","Software Developer","MCN Solutions Pvt. Ltd.","[email protected]",52000.00);  
  21.   
  22.         }  
  23.   
  24.         //Get Employee Details  
  25.         public void GetEmployeeDetails(string _name,string _designation,string _organization,string _email,double _salary)  
  26.         {  
  27.             FullName = _name;  
  28.             Designation = _designation;  
  29.             Organization = _organization;  
  30.             Email = _email;  
  31.             Salary = _salary;  
  32.   
  33.           string data = "Name\t:" + FullName + "\nDesignation\t: " + Designation + "\nEmail\t: " + Email + "\nSalary\t: " + Salary;  
  34.             Console.WriteLine("Employee Detrails:\n\n" + data);  
  35.             Console.ReadLine();  
  36.         }  
  37.     }  

To see the referencing code double-click on References (2 References), an indicator popup will be shown just above CodeLens. It will show where this method is being used.

File uploaded successfully!ReferencesDetails

If we want to see the graphical representation of references, it is possible. To do that double-click on references of CodeLens then a popup box will be shown. In the bottom of the popup box we will see a link named Show on Code Map. Now click on this link and a graphical view of CodeLens will be shown in the CodeMap1.dgml file.

CodeMapView

Impact

Impact is nothing, it's the view of references that indicates the changes to a method that is referenced one or more times. If any changes occur inside the method, the CodeLens starts to blink which indicates that if we change anything inside this method, it will impact these places.

Impact

Test: Test is the important part of CodeLens that indicates whether this method was unit tested or not. For example if a method having 1 is passed in CodeLens then this method is tested by Unit Testing.

Test

To do unit testing with a project, we have attached a UnitTest project with this article, download this file to see it practically. For the moment see the following example code of Unit Testing.

Step 1

Create a Console Application using Git Version Control and do the following:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace CodeLensExampleProject  
  7. {  
  8.    class EmployeeInfo   
  9.     {  
  10.         //Public Properties  
  11.         public string FullName { getset; }  
  12.   
  13.         public string Designation { getset; }  
  14.   
  15.         public string Organization { getset; }  
  16.   
  17.         public double Salary { getset; }  
  18.   
  19.         public string Email { getset; }  
  20.   
  21.   
  22.         static void Main(string[] args)  
  23.         {  
  24.             // Calling GetEmployeeDetails() method  
  25.             EmployeeInfo employee = new EmployeeInfo();  
  26.   
  27.             employee.GetEmployeeDetails("Ranjan Kumar","Software Developer","MCN Solutions Pvt. Ltd.","[email protected]",52000.00);  
  28.             ErrorHandler erh = new ErrorHandler();  
  29.                        
  30.         }  
  31.   
  32.         public void SetAndGetEmployeeDetails()  
  33.         {  
  34.             EmployeeInfo employee1 = new EmployeeInfo();  
  35.             employee1.GetEmployeeDetails("Suraj Kumar""Web Designer""MCN Solutions Pvt. Ltd.""[email protected]", 38000.00);     
  36.         }  
  37.   
  38.         //Get Employee Details  
  39.         public void GetEmployeeDetails(string _name,string _designation,string _organization,string _email,double _salary)  
  40.         {  
  41.             FullName = _name;  
  42.             Designation = _designation;  
  43.             Organization = _organization;  
  44.             Email = _email;  
  45.             Salary = _salary;  
  46.   
  47.           string empDetails = "Name\t:" + FullName + "\nDesignation\t: " + Designation + "\nEmail\t: " + Email + "\nSalary\t: " + Salary;  
  48.           Console.WriteLine("Employee Detrails:\n\n" + empDetails);  
  49.           Console.ReadLine();  
  50.                           
  51.         }  
  52.   
  53.         ErrorHandler obj1 = new ErrorHandler();  
  54.     }  
  55.       
  56.     //Error Handling Class  
  57.     public class ErrorHandler  
  58.     {  
  59.         public void HanldeError()  
  60.         {  
  61.             //changes  
  62.         }  
  63.     }  
  64. }  

Step 2

Now add a UnitTest Project to this Console Application and do the following code.

  1. using System;  
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;  
  3.   
  4. using CodeLensExampleProject;  
  5. namespace UnitTest  
  6. {  
  7.     [TestClass]  
  8.     public class UnitTest1  
  9.     {  
  10.         [TestMethod]  
  11.         public void TestMethod1()  
  12.         {  
  13.             ErrorHandler erh = new ErrorHandler();  
  14.             erh.HanldeError();  
  15.         }  
  16.     }  
  17. }  
Step 3
 
Before creating the object of the "ErrorHandler" class add the reference of the console application to this UnitTest Project.
By: Add Reference
 
Changes: Changes are also a part of CodeLens that shows how many times changes occurs to a specific method, if that method is tested by a UnitTest Project.

Changes

Summary

In this article we saw what is CodeLens does and how to use it with Git or Team Foundation Version Control (TFVC) version control as well as how to test it using unit testing. 


Similar Articles