[MUSIC]
>> Hello everybody. My name is Mads Kristensen.
I'm from the Visual Studio Team and I'm super excited to show
you a new way of writing Visual Studio Extensions. Let's begin.
The first thing we need to get started is
to make sure we have a certain extension installed.
Open the Extension Manager and
search for Extensibility Essentials.
This is a community developed extension pack that gives us
a bunch of features that makes it easier for
us to write extensions for Visual Studio.
I already have this installed
and it gives me some new project template.
Let's go ahead and create a new project.
We're going to search for the ''Word'' V6,
it stands for Visual Studio IDE extension.
Let's scroll down to see those new templates.
They all have the ''Word'' community in parenthesis here.
We're going to have the V6 project
with command is the one we're taking here.
We're going to give our extension a name.
Now, the extension I want to write is one that inserted good into
the text editor because that's actually a feature that
I use all the time where I
would like to have so I could use all the time.
Let's just go ahead and create this.
Really quickly, we see that we get our extension project.
This looks like any other dotnet project because it is.
The first thing we want to do is open the V6 manifest file.
This is where we keep some metadata about our extension.
For instance, its name,
and we want to give it a pretty name, insert guid.
The product id will leave as it is,
and then we may want to have a nice description.
In our case, I think we should go with
something like the easiest way
to Insert guid into the editor, and that's about it.
We can see we also have some icons and that's located right here.
It just a default icon,
you can replace this with any other icon you like.
Now we have our metadata setup.
Let's save this file, close it down.
Now let's take a look at what we got here.
We've got the V6 manifests which we just saw.
We also have a BS CT file or Visual Studio command table.
This is where Visual Studio,
where you define all the different commands and
menus that you want to show up in Visual Studio.
It's all defined in XML.
We can just open it, and you can see that I
have an XML file here that contains a group and some buttons,
well one button and some symbols.
That's all provided for by our template that contains a command.
Now, a command and a button is the same thing.
That might be a little confusing, but it's the same thing.
By default it's called My command.
We're going to update that.
We're going to call it "Insert Guid".
I want this command to exist up in the Edit menu.
Somewhere in here.
I will have a command called Insert Guid.
I want to make sure that the canonical name is edit.insert guid.
That makes it easy for people to find if they search
for the command to
assign a different keyboard shortcut or something like that.
I can also specify an icon.
Out of the gate we just get a default one.
But I already found one that I think I like,
and it's called haste.
If I want to know more about the different icon,
I can go into the known Monitor Explorer.
I get this tool window from that extension part,
the Extensibility Essentials that
I started by saying that you need to install.
Here I can search for things. I can search.
I actually like this icon a lot.
If I double-click it, I can maybe see it more clearly,
it's called paste append.
Let's use that as the icon.
I actually get almost 4,000 icons I
can use for free with Visual Studio.
These are all the icons that Visual Studio ships with.
It's cool that I have all this available for my extension.
Now our button here
has a parent which is our group defined up here.
I can have multiple buttons inside these groups.
In this case, I just have one and it's placed in
the tools main menu, that's this menu.
But I want it in the Edit Menu.
I'm going to go ahead and specify that. So far, so good.
We now have specified the name and
the icon and the location of our command or a button.
Let's actually handle what happens when people click that.
We go to the, My command right here.
This is being executed
when the command is being invoked by the user.
First of all, conceptually what I want to do is I want to
get the document that currently loaded into the editor,
and then I want to grab the selection,
and replace whatever is selected with a new good.
Now, selection in Visual Studio,
there's always a selection even though you
haven't selected like multiple characters,
even though in any document that is open,
there's always a selection and it's where the character is,
unless there is an actual selection
that spans multiple characters.
That's how we're going to do this.
We're going to start by grabbing a dock view.
Notice here I get an object here called VS.
I can say VS dot.
This is my shortcut to all the different APIs in Visual Studio.
It makes it super convenient for me to find what I'm
looking for and to write the code that I need.
I'm going to go to documents and I'm going to get
active document view like this,
and then I'm going to grab a selection.
Remember, in Visual Studio,
we have the ability to have multiple selections.
But for the simplicity of this,
I just want to get the first election.
You can have multiple. Let's see
here if I can remember how that's done.
You can have multiple selections like this.
We just want to grab the first one.
Let's get the dock view,
and let's put a question mark there because
it could be null if there's
no document actually open in Visual Studio.
Then we can get the text view and the selection,
and then the selected spans.
In this case we just want to say,
give me the first or Default.
Here we go. We need to import namespace for that one.
I would simply say, give me
the first selection there is or null if there's no selection.
But there is always one as I said.
We're just cautious here.
Then we can say if selection has value,
and let's just create a new Guid.
We say Guid.NewGuid to string.
When always need to strings here.
Now very simply, I can just go to that doc view and go straight to
the tech buffer and replace the selection with the Guid.
As simple as that. Let's just recap what we're doing here.
We've created a button that exist in
the Edit View or the Edit Menu
that when you click it will insert a new Guid
into the Text Editor using this very simple code.
Now, let's run this.
You run this like you do any other dotnet app.
You hit F5 If you want to attach the debugger or Control F5,
If you don't want to attach the debugger but it will just run
anyway. That's what I'm doing here.
What happens is that Visual Studio starts
up a new instance of Visual Studio.
This instance is called the Experimental Instance.
You can see here I've created
using a different theme just so we can see the difference.
Let's go ahead and just open a file here,
and let's test if this works.
Let's make a selection. Go to edit.
We can see we have our Insert Guid command
shows up right here, just like we expected.
I'm going to click it, and the Guid was inserted.
Fantastic. Let's try it without any selection.
I don't have any selection,
but you can see the character is right here blinking,
and let's execute that command again,
and it inserts it right there.
Super simple. There you have it, folks.
It's as easy as that to create
Visual Studio Extension using
the Extensibility Essentials Toolkit.
Go ahead, try it out, download it, play around with it.
I can't wait to see all the cool extensions
that you are going to be writing.
Make sure to catch the other Visual Studio 2022 videos and enjoy.