How to Write Custom IntelliSense Suggestions in Visual Studio: A Dash of .NET - Ep. 2
21K views
Dec 8, 2023
In this session, we'll dive into the intricacies of enhancing your coding experience by creating personalized IntelliSense suggestions tailored to your specific needs. Whether you're a seasoned developer or just starting, mastering this feature can significantly boost your productivity. 📺 CSharp TV - Dev Streaming Destination http://csharp.tv 🌎 C# Corner - Community of Software and Data Developers https://www.c-sharpcorner.com #csharptv #csharpcorner #dotnet #adashofdotnet #csharp #visualstudio
View Video Transcript
0:00
on how you can build your custom internal sense inside the visual studio
0:07
Hi, Rishabh. Welcome to the show and we can't wait for you to get started. Hey, Simon. Pleasure is all mine. Thank you so much. I'll get started
0:17
Okay, so welcome. And so what we will be developing is this. We will be developing
0:22
a Visual Studio extension that demonstrates custom comment auto-completion. And being in
0:28
Stephen's show. What I'm going to do is I'm going to personalize it and when we type a comment which
0:35
has Stephen it will show the completion suggestions like I've shown here in the cool I've shown here
0:42
So this is going to be a simple and helpful extension that should help you understand how
0:47
IntelliSense in Visual Studio works under the hood and how it can be extended. It will also
0:53
It will also help in providing intelligent documentation. Suppose you want to do a document method, or you want to add comments to a code, this
1:01
would also help you achieve that automation. In this modern world of artificial intelligence, where you just type a comment and the code
1:08
gets generated, this might also get you kickstarted in there. This is essentially what we are going to do
1:16
Let's get started. Okay, before we go into coding, let's quickly look at the simplified high-level concepts
1:26
of how these completions work. So Visual Studio utilizes something which is called as Managed Extensibility Framework
1:35
or MEF, which I have shown here, to provide feature extensibility. Visual Studio is feature-rich
1:42
There are n number of features and most of them can be extended
1:46
And all of them can be extended using this managed extensibility framework
1:52
So the code editor, I've shown three components here, a code editor, a completion source provider, and a completion source
1:59
And I'll tie them in the next few seconds. So in the code editor, say, for example, you are editing a C-sharp file
2:06
So the code editor knows that the type of code being edited is C-sharp
2:12
as you type or press certain key combinations a completion session is initiated okay and that
2:20
looks for that whether i have a completion or a suggestion list available for what i have typed
2:25
so so that is when your completion source provider comes into picture so since the editor knows that
2:32
it's a file type of c sharp it the language service the language c sharp language service
2:37
will look out for completion source provider, which have a content type of C Sharp
2:43
And then if there are any completion source providers for C Sharp, it will provide those completion sources
2:50
Now, these complete sources are aware of the context. They know whether for the context
2:55
they have a suggestion available or not. If they have a suggestion available
2:59
they will provide a suggestion list or a completion list. If not, then they will just say
3:04
I don't have any completion for this. So that's how everything ties up
3:08
So editor will resolve our completion source provider by means of MEF
3:14
which will then provide a completion source. Completion source would know based on the context
3:18
whether it has a completion list or not. So this is essentially what we are going to develop
3:23
in the next 10 minutes or so All right So before we get started we will what we will need to do this is the visual studio sdk right to extend the visual studio and to to do so what you will need to do
3:38
this can only be done in windows because visual studio works only with windows so so on your
3:44
windows box click click your windows button or hit the bottom left windows icon and then uh you
3:53
you know, type in Visual Studio installer. It will present you this kind of a dialogue
3:58
where you can click on Visual Studio installer. Once the installer launches, you click on modify
4:04
And in that, you have to ensure that the Visual Studio extension development workload
4:10
is selected. Once you select that, it will take a while. It will, you know, it will also workloads
4:19
And then the Visual Studio SDK would be installed in your machine. right? So this is the prerequisite for it. Once you do that, we will be developing this
4:29
IntelliSense extension in exactly five steps. And after we complete those five steps
4:34
we should be able to see our custom IntelliSense in action. So the step one is that after you
4:40
have installed the VS SDK, we will get a bunch of extensibility templates, both project as well
4:46
as the item templates. So when you do that and you hit create new project, you will see our VSIX
4:52
project under the extensions part of the filter. So I have chosen C-sharp for Windows
4:59
and the project type is extensions. So you will see a VSIX project, which is a Visual Studio
5:04
installer extension. We will select that, and we will click Next. And we will provide it a meaningful name
5:09
say, Custom IntelliSense. Step two is the completion source, which we discussed a little while back
5:15
Now, to creating a completion source is quite easy. And just because it is so easy, I could fit it in one tile
5:21
and I'm going to explain what are the different things that you need to do
5:25
Once you have the SDK available, to create any completion source, all you need to do is you need to implement an interface called iAsync Completion Source
5:36
So this is an interface, and when you will press Ctrl-Dot, all the relevant using directives will already get loaded
5:42
I have not shown here for brevity, but I will share the source code towards the end of the session
5:47
which will have the entire source code, so we don't need to worry about it just yet. So once I create it, it will give me three public methods, which are shown here
5:55
I mean, the constructor, let's forget about the fields and the constructor part here
6:00
We will have a completion context, get completion context method, get description async and
6:06
initialize completion. So let's see what each of these methods does. Now, the most important method and which should be, which is run on the UI thread and which
6:14
should be very fast is this initialize completion. So whenever this completion source comes into picture, it has to be aware of whether it needs to participate in completion or not
6:25
So it gets a lot of parameters, which are like completion trigger and snapshot point
6:30
The completion trigger is the character with which the intelligence was triggered
6:35
And snapshot point is the exact point on the editor from which your intelligence is being triggered
6:41
That's a trigger location. So at this point of time, you have the context. Now you can leverage those and define whether you want whether this completion source can provide a suggestion or a completion for that scenario or not For the sake of simplicity I am just saying here I just returning that participates in completion So if you think that you cannot provide a suggestion
7:03
at that point, you will use these and you will say does not participate in completion. But for this demo, I have just said
7:09
it participates in completion. So this is one of the completion source provider
7:13
Second method is the get completion context essay. Now this is where you will get the completion context
7:18
Now here, what I am doing is I am getting the containing line from where the IntelliSense was triggered
7:25
then I get that text. And in that text, I am checking that it is not null or white space
7:31
And then I'm doing a string manipulation just for the simplicity. I'm checking if that comment has Stephen in it or not
7:38
If there is Stephen in it, then what I am doing is I am returning a context
7:43
What is this context? This context is of a type completion context defined here
7:47
And it is initialized in my constructor. So I create a list of completion items and I have provided, okay, if Steven comes in, I can show regional director
7:57
I can show a dash of .NET show or I can show a cool host. And the second parameter is that what is the completion source
8:04
So this is the completion source. And I have created just one IntelliSense keyword icon and I have passed it along here
8:10
So I have created these three suggestions, created the context, and that's what I'm returning from here
8:15
If the index of Steven isn't there, then I return an empty thing
8:19
So this is the second method. And finally, the third method is gag description async
8:24
This is like a tooltip of that completion. So when you will hover over one of these suggestions
8:30
what will be displayed is the display text as it is. So this is just the tooltip
8:35
So with this, our completion source is done. Next is about creating a completion source provider
8:41
which will actually tie up this completion source to the C-sharp file
8:45
So to implement a completion source provider, you need to implement an interface called iAsync completion source provider. So it's so easy. If
8:55
you know it's a completion source, just implement iAsync completion source for completion source
8:59
provider. Just implement iAsync completion source provider. Once you do it, it will just ask you to
9:05
implement one method called get or create. In that get or create, you get the text view, which is the
9:10
editor. I do not need it here in this case. So what I am doing is I am just using a lazy of my
9:17
completion source and passing and just returning its value. The reason I'm doing it lazily is that
9:23
suppose I'm working in an HTML file or a JS file or a CSS file. At that point of time, I do not want
9:30
my C sharp completion source provider to be loaded. So that's why I do it lazily
9:37
So content type I have specified here C sharp. I provided a name as comment completion source
9:42
and you see the most important attribute here, which is decorated is export
9:47
So this is the MEF export attribute. So this will actually get
9:52
like in Lego, we can plug and play different bits to each other. So this is the class which is telling
9:58
which is actually advertising itself that, hey, I am an I-async completion source provider
10:03
So wherever a C-sharp language service is looking for an async completion service provider
10:08
it knows that there is one which is exported by this type. So with this, we are done with the coding
10:14
Now we need to wire up. To wire up, what we need to do is we need in our
10:19
there will be a package class that is created by the template In that template we will decorate two attributes The first attribute is the provide auto load so what this does is that it will load this extension in the background so this is
10:33
the background load so this will load it in the background even when the solution is not loaded
10:38
so what this will do is this will give you performance enhancements otherwise when you
10:42
load the solution c sharp project then it will start to load so that will be a little slow
10:46
and second is a provide language service attribute so this language service attribute
10:51
it is telling that there is a comment completion source source of type c sharp for this so this
10:59
wires up our package with the source and the source provider finally to get the mef to compose
11:07
everything what we do is we actually come we add we go to our vsix manifest add a new asset
11:14
of type MEF component, the source of which is the current project
11:20
of name custom intelligence. Once we do it, we are done with our coding
11:25
All we now need to do is just run and see our extension in action
11:33
So the source code for all of this is already here in this link
11:37
which I've already shared. And I will also share the presentation with Simon in a while
11:43
And once you do it, You will see it in action. You will just type Steven and you will see the three different things that I put as the suggestion
11:55
And you see they are already automatically sorted in the alphabetical order
12:00
And when I hover over it, you also see a tooltip. So this is actually how at a high level we do it
12:07
Now, Steven, if we have time, I can go into the code. Otherwise, this is the high level overview of how we can
12:20
Yeah, I think that was absolutely great. You made things very simple from starting
12:26
I think the very first thing that people sometimes often miss is when they go to the Visual Studio
12:31
they create a new project and that's it. I never realized that from that same Visual Studio
12:35
you can actually install a development kit and start building all these extensions and
12:40
IntelliSense. So that was absolutely great, Rishabh. So we are reaching, we are absolutely on time
12:46
There's one question that I want to ask you is that if someone is willing to learn more about it
12:50
right, where can they find it? Is there any resource that you would like them to point
12:57
Right. So there is a huge resource in Microsoft LearnSite, which I have, by the way, included in
13:03
my slide towards the end. So I would be sharing that. So the Microsoft Learn site, just go and
13:09
search for Visual Studio extensibility, and we will see a bunch of resources. And I am writing a book
13:15
by the way, I'm not, I do not want to publicize it, but I am writing a book on Visual Studio
13:19
extensibility, which should be out by the end of this year, which will cover all of these things
13:24
in great details. Wow, I'm really excited. And I encourage everyone to go ahead and look out for
13:29
that book of one that is launched. I'm going to have one in my hands whenever it is out
13:33
I'm going to get an autograph from Rishabh. Thank you, Rishabh. That was an absolutely great session
13:37
packed up with a lot of knowledge. And thank you, everyone, for watching this session
13:42
We will see you in the next episode on ashaf.net. Until then, take good care of yourself and see you soon
13:47
Bye-bye. Thank you, Simon. Thank you
#Programming
#Software
#Video Game Mods & Add-Ons
#Windows & .NET