Introduction Of Visual Studio Code Lens

What is Code Lens

Code lens tells us the code changes in a fantastic way and the best thing of this feature is there in editor history which you get before code lens in a lot of time.

With code lens you have a deep focus on your code, since you know what last changes were made to the file with reference history and who changed it. Code lens is a combination of References, changes in your code, code reviews, bugs associated with item, unit tests and their detail of what number of unit test passed or failed in a unique fashion.

All of the above options you can check while leaving your editor and in page references make life easier, since all of these are associated directly with your code.

But wait are you too much excited after readings its indicators, just read the note.

Note: CodeLens is available only in Visual Studio Enterprise and Visual Studio Professional editions. It is not available in Visual Studio Community edition.

So let's start in detail about indicators of code lens. You can customize the indicators on / off from Tools, Options, Text Editor, then All Languages and Code Lens.

As you can see in the image above the code lens option is enabled by default in the editor. If you are connected to Team Foundation Server or any other version control system you have all the options available and you can customize other specific options.

If you want to disable all the options you can uncheck the Enable CodeLens Option and if you want to enable CodeLens, then check the Enable CodeLens and then customize the indicators what do you want to see in the editor.

There are number of options available as elaborated below:

Show Test Status

By turning this option on you can see the indicator defining the last test run status and it will show left to the references. By clicking the status icon you can take the test result information.

Show References

If your code does not have reference, it show 0 References. you can view the code references by pressing (Alt + 2). if your code has references, then you can view them by moving mouse on the top of reference and by pressing double click you can view the reference definition.

After the references are opened you can see the information displayed in the form of Parent element contains the File location, followed by definition of method with line number as the child of each page.
Show Tested By

The Show Tested By option will show the tests associated with your code and the overall status of how many tests associated with number and status of tests run or not. If you have unit test project then this can show the unit test status by giving you value of how much passed, failed with ratio of 1/2 means out of 2, 1 passed and 1 failed and it give you detailed information of unit test with methods and duration in ms (milliseconds).

You can directly run all the tests or specific test and can review the test code by pressing (Alt + 3).

The tests shown with indicator marks of wrong and correct icon which show who failed or passed. If you see the warning icon then it means you have not run any tests and that way you can run through code lens unit test indicator.

To review the test definition double click on the test item.

Show Authors

Show author option will able to show you the last author who has changed the file or work in file and check in the latest changes. If there are more than one author associated with your code it shows the last author name like MUHAMMD AQIB + 2, the count next to the author tells that there are two more authors associated with file. To show this option you can press (Alt + 4) key.

Show Changes

This option can give you overview of the changes / history that are associated with the code. The code changes indicator shown on VS Editor is like
3 Changes. To show this option you can press (Alt + 5) key, by opening the changes window you can see the detail of changes with change set ID, Change set Description, Author Name and Date of Changes.

In the changes window, by right clicking on any item it will give you the following three more options:
  1. View Diff of Changeset with [Number] - show difference between selected changeset number with earlier changeset number.

  2. Changeset Details -  by selecting this will show the detail of changeset in team explorer window.

  3. Send Email to [Author Name] -  this will open you default email program and fill the specific change details in the To, Subject and Body section of Email. 
Show Bugs

If there are some bugs reported with your work items, it will show like 2 Bugs and by opening the bugs detail window it will show you Bug ID, Type, Description, Author and date reported.
Show Work Items

It will show the related work items with the piece of code just before the code review indicators. 3 work items,  it means there are 3 work items associated with the the checked in changes and all are associated work items are related with it.
Show Code Reviews

It will give you information about the associated code review with specific method and it will show in the last of code lens indicators as 4 reviews.
After opening the review window it will show the Review ID, Type, Title, Author and Date of Review. 
Code Lens Formatting

With this option you can perform formatting with the code lens like fonts, color and other things in Tools, Options, Environment, then Font and Colors
option and select the show settings for: code lens and after the selection from drop down list it will display Items in other window with options such as indicator text, indicator text (Hovered), indicator text (Selected), indicator text (Disabled) and Indicator separator and then if you require formatting changes, hit OK button and enjoy with formatting colors and applied fonts.

Code Lens Accessibility

You can easily access code lens indicators by mouse and with the keyboard keys.

Just point the mouse to the specified indicator and click on it to show indicator options.

From keyboard move the cursor to the desired method and press and hold Alt key for 2 seconds and then it give you number option to select your desired option by pressing numbers.

Code Lens Options From Visual Studio Editor

You can also open code lens option from the Visual studio editor. To do so just right click on any one of the code lens indicator and you can see two options.

Refresh CodeLens Team Indicators.

CodeLens Options


CodeLens options opens the same window which was elaborated at the starting of this article.

Hope you enjoyed this article and happy coding with Visual Studio IDE.