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.
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.
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.
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 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 MUHAMMAD 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.
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:
- View Diff of Changeset with [Number] – show difference between selected changeset number with earlier changeset number.
- Changeset Details – by selecting this will show the detail of changeset in team explorer window.
- 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 Work Items
Show Code Reviews
Code Lens Formatting
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.