Here's another keyboard shortcut tip. This time, it applies to the Code Lens feature.
- [Instructor] Here's another keyboard shortcut tip. This time, it applies to the CodeLens feature. I'll start with a quick refresher of the CodeLens. The more information you can have at your fingertips while writing code, the easier it is to keep your mind focused on solving the current problems in your application. That's why Microsoft introduced the CodeLens feature in Visual Studio. It's a tiny inline info pane that gives you critical information about who's referencing the methods in a class. It can also tap into the source control system and show stats about who's committing the code, and it shows unit test results.
CodeLens is available for the Visual Studio Professional and Enterprise editions. If you have either of those editions, you'll want to use CodeLens. It provides valuable information. It's enabled by default; I usually turn it off while recording courses. Here's how you enable it. Go to Tools, and then Options, then look here in the search area, type in lens, and then choose this CodeLens. And then you'll want to enable this top check mark, and when you do that, that adds all these CodeLens features.
I'll leave 'em all checked. Now before I click the Okay button, I want you to keep your eye right here between Line 114 and 115. Now notice that an extra space appeared there? And this information here is from CodeLens. You'll also notice there is an extra space here between 105 and 106. So essentially it adds a CodeLens above a method or a property. And what I can get from this CodeLens is information about what's referencing this code, who on the team has worked on this, and information about the check-ins.
Now to get details on this, I click on the hyperlink. So this shows me the references, this shows me more information about the team activity, and this shows me that I have two authors and two changes. And you can see the dates of the changes. It also works with unit tests. In my case, I've got this CourseLib, and then I've got CourseLib.Tests. I haven't run my unit tests yet, so let's build the project. Before I do that, let's look at this section here. This is the CodeLens that has a unit test.
So keep an eye here. I'll do a Build Solution. And after I built it, I got this new CodeLens here that says I've got zero of one passing tests. And that's because I haven't run my tests yet, so I should go to Test, Windows, Test Explorer, and then Run All. And you see I've got a passing unit test. So now I can click on this one here to reference it. See, I can get a passing unit test, and so on. So that's the basics of the CodeLens.
Now we're going to talk about how to use the keyboard to access that. Because the way it works right now, if you're on your keyboard and you use the up arrow, when it gets to the method, and you press the up arrow one more time, it skips over the CodeLens, which makes sense. Most of the time you're not interacting with the CodeLens, you're moving through your code. So it skips over that. If you are a keyboard user, you probably want to access the CodeLens with your keyboard. So here's the trick. Hold down the Alt key while you're within the boundaries of this method or property.
So hold down the Alt key, and you'll see these numbers appear. Now I can tap that number on my keyboard, and that opens that CodeLens. Press Escape to dismiss it, hold down the Alt key again, now I'll tap the seven, that takes me to the last one. There's another feature, you see the arrows? I'm still holding down the Alt key, I haven't let go of the Alt key yet. When I hold down the Alt key, those arrows appear, and now I can tap the left and right arrows on my keyboard to navigate through the CodeLenses.
Now let's move up here, move my focus into this method, and hold down the Alt key. This time you see I get the numbers two, three, five, and seven. The way this works is two is always going to be the references. Go back here and see that two is for the references. Five is always for the team activity, and seven's always for the code in GitHub. Three was added here, because that's my unit test. So let's try that. I'll have to hold down the Alt key, or I'm still holding it down, I should say, and then I'll press three, and I can see the information about my passing unit test.