Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Have you ever developed a VS Code Extension?
Giuliano1993
Giuliano1993

Posted on

     

Have you ever developed a VS Code Extension?

Hello everyone and welcome back to MonDEV!

Ready for a new week full of good code? This week, let's focus on project ideas, and I ask you: have you ever developed an extension for VSCode?

I've often thought about it, but it hadn't happened to me until last week, even though I knew sooner or later I would delve into it, partly out of curiosity and the desire to experiment, and partly because I love developing tools that can be useful to other developers. So a few days ago, quite randomly, I created my test-extension, which we'll find out later is something quite familiar actually.

Probably to have fun with a project like this, the extension doesn't need to be huge, but something small can still be very satisfying. Alternatively, if you have a project that you've already developed and that is developed for CLI, it probably can be adapted to a project of this kind quite well.

First of all, to get started, you start by creating a project; you can do this by running this command:

npx--package yo--package generator-code-- yo code
Enter fullscreen modeExit fullscreen mode

The script will ask you some questions to create the project, after which it will create the folder and install all the necessary dependencies.

Once the project is created, there are two main files you'll start working with:package.json andsrc/extension.ts.

In thepackage.json file, in addition to the usual information about the installed packages, runnable scripts, etc., there are some keys that allow you to configure your extension. Specifically, within a newly created project, you'll find theactivationEvent array, which will currently be empty, where you can insert all the events that can activate your extension; you'll then find thecontribute.commands array, which will contain the commandyour-extension-name.helloworld. This array of commands is what allows you to add commands to the VSCode command palette, where we launch who knows how many commands every day. I leavehere the documentation for the contributions section, which extends well beyond commands. A couple of suggestions to delve into in this case could be themenus,keybindings, andsnippets sections.

Thesrc\extension.ts file is instead the real heart of our extension, where the code resides that is executed when it runs. You'll immediately see theactivate() function. This function is called when the extension is first activated, and indeed just below you can see the registration of the command we mentioned earlier, in the package.json. Inside the callback of thevscode.commands.registerCommand function, you'll find all the code executed when the registered command is called from the palette. If you want to test the extension, simply pressF5 from within this file.

In extensions, everything starts from thevscode library; some examples to get started orienting yourself can be these:

  • vscode.commands: contains all the functions related to commands, allowing their registration, listing, or even direct execution
  • vscode.window: a very comprehensive class that essentially contains every element that can appear in the window: the methodsshowQuickPick orshowInputBox allow you to receive additional input from the command palette,showTextDocument allows you to open the editor of a specific file,showInformationMessage simply opens the message at the bottom left.
  • vscode.workspace: provides access to the current workspace. An example that can be useful is the constantvscode.workspace.rootPath which contains the root of your project if you want to work with the file system

One further small note: the extension is written and built with TypeScript. If you need to add files that are not handled by the TypeScript Compiler, you'll have to implement copying these files yourself.

Ops I did it again...

I told you earlier that I started from an existing codebase to build my extension. This is because I'm preparing the VSCode extension ofMake Js Component! This is so I can always have it at hand for those who want to use it without needing to remember the command or necessarily go through the terminal and its wizard. The repo is already public and you can find ithere but as you can see it is not yet published on the VSCode store nor is it properly organized to receive contributions. If you want to encourage the evolution of the project, you can still leave a star, I'll definitely appreciate it a lot, or contribute to the starting library, since I hope to bring most of its functionality to the extension soon, as long as it maintains its immediacy. I'll definitely let you know when the actual extension is published.

In the meantime, I hope this little Monday spark was interesting for you and that maybe you'll want to try creating your own extension, starting from here. If you do, let me know! I'm always on the lookout for new ideas and projects, as you can well imagine! ;) And I'm still happy if I can offer a good suggestion!

Now I'll leave you, wishing you a good start to the week!
Happy Coding! 0_1

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
_ndeyefatoudiop profile image
Ndeye Fatou Diop
Hi 👋🏽, I am a self-taught Senior Front-End Engineer. I share tips to help overwhelmed junior frontend developers here => frontendjoy.com/ 😻🥳.
  • Email
  • Location
    Asnieres-Sur-Seine, France
  • Work
    Senior Frontend Engineer
  • Joined

Very nice ! Looking forward to seeing the final result !

CollapseExpand
 
giuliano1993 profile image
Giuliano1993
Hi, I'm Giuliano and I'm a full stack web developer based in Rome. I love playing around with code trying out new things. I love to share knowledge and get into inspiring conversations.
  • Location
    Rome, Italy
  • Pronouns
    he/him
  • Work
    Full-stack web developer and dev mentor
  • Joined

Of course! Going out with it really soon :D

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hi, I'm Giuliano and I'm a full stack web developer based in Rome. I love playing around with code trying out new things. I love to share knowledge and get into inspiring conversations.
  • Location
    Rome, Italy
  • Pronouns
    he/him
  • Work
    Full-stack web developer and dev mentor
  • Joined

More fromGiuliano1993

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp