What is Code Lens?
Code lens tells us the code changes in a fantastic way and the best thing about 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 the item, unit tests, and their detail of what number of unit tests 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 reading 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 the Visual Studio Community edition.
So let's start in detail with 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 that do you want to see in the editor.
There are a 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 a reference, it shows 0 References. you can view the code references by pressing (Alt + 2). if your code has references, then you can view them by moving the mouse on the top of the 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 a Parent element containing the File location, followed by the definition of the 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 are associated with the number and status of tests run or not. If you have a unit test project then this can show the unit test status by giving you the value of how much passed, and failed with a ratio of 1/2 means out of 2, 1 passed and 1 failed and it gives you detailed information of unit test with methods and duration in ms (milliseconds).
You can directly run all the tests or specific tests and can review the test code by pressing (Alt + 3).
The tests are shown with indicator marks of wrong and correct icons 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 the code lens unit test indicator.
To review the test definition double click on the test item.
Show Authors
The show author option will be able to show you the last author who has changed the file or worked on the file and check in the latest changes. If there is 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 the file. To show this option you can press the (Alt + 4) key.
Show Changes
This option can give you an 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 the (Alt + 5) key, by opening the changes window you can see the details of changes with change set ID, Change set Description, Author Name, and Date of Changes.
In the changes window, right-clicking on any item will give you the following three more options.
- View Diff of Changeset with [Number]: show the difference between the selected changeset number and with earlier changeset number.
- Changeset Details: selecting this will show the details of the changeset in the team explorer window.
- Send Email to [Author Name]: this will open your default email program and fill in the specific change details in the To, Subject, and Body sections of the Email.
Show Bugs
If there are some bugs reported with your work items, it will show 2 Bugs and by opening the bugs detail window it will show you the 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 checked-in changes, and all associated work items are related to it.
Show Code Reviews
It will give you information about the associated code review with a specific method and it will show in the last of code lens indicators as 4 reviews.
After opening the preview 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 after the selection from the drop-down list it will display Items in another 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 the 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 the keyboard move the cursor to the desired method and press and hold the Alt key for 2 seconds and then it gives you a number option to select your desired option by pressing numbers.
Code Lens Options From Visual Studio Editor
You can also open the code lens option from the Visual Studio editor. To do so just right-click on any one of the code lens indicators and you can see two options.
Refresh CodeLens Team Indicators.
CodeLens Options
CodeLens options open the same window that was elaborated at the start of this article.
Hope you enjoyed this article and happy coding with Visual Studio IDE.