Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Create Extensions for VS Code - Part 1
Thamaraiselvam
Thamaraiselvam

Posted on • Edited on • Originally published atthamaraiselvam.com

     

Create Extensions for VS Code - Part 1

I wanted to develop a *VS Code extension * to solve one of my daily problems, so I started to look atVS Code API

They have really awesome documentation and sample extensions which are really helpful.

I could run my first Hello world extension in less than 5 minutes.

It is very easy and super simple, come on let's see how to develop one.

Installation

Install the following Node modules for Kick Starting new extensionYeoman andVS Code Extension Generator

npm install -g yo generator-code

Create New Extension

Execute this commandyo code and select New Extension -Typescript (Which is most recommended one) to create a new extension then answer questions. It will create a Hello World *boilerplate * and also Install required node modules.

yo code.PNG

Extension File Structure

This auto-generated extension has dozen of files, refer following images to know more about file structure.

atonomy.PNG

But we are going to focus on only two files in major which are

  • src/exntension.ts
  • package.json

Run Extension

Running extension is pretty simple, Just go to debug menu from the left side menu or hitCtrl+Shift+D keybinding, Then clickRun Extension.

It will open a new window where your extension will be running, To confirm that you can see "Hello World" on the right bottom.

Tada

Okay, But How Does it work?

VS code Extension is all event-based, we need to define all our commands (actions) in package.json

Package.json

you can *package.json * files in the root dir, which holds the all the events inside ofcontributes ->commands, In this case, we have only one command which isextension.helloWorld

"contributes": {        "commands": [            {                "command": "extension.helloWorld",                "title": "Hello World"            }        ]}
Enter fullscreen modeExit fullscreen mode

We enable our events by adding our commands intoactivationEvents

"activationEvents": [    "onCommand:extension.helloWorld"]
Enter fullscreen modeExit fullscreen mode

main property holds file path which will be executed at first, once the extension is activated

"main": "./out/extension.js"
Enter fullscreen modeExit fullscreen mode

Extension.ts

Good to see that, this file has very detailed comments for better understanding.

  • function activate(context: vscode.ExtensionContext) - This Function executed at first when extension gets activated and our all business logic will lie here.

  • vscode.commands.registerCommand('extension.helloWorld', () => {}) - Registeringextension.helloWorld event with callback function.

  • vscode.window.showInformationMessage('Hello World!'); - It makes message box on the right bottom

Finally, we should push our registered command intocontext.subscriptions

*Congratulations for your first vs code extension * 🎉🎉🎉🎉🎉

Made it

In the next article, I will explain how to implement the most common extension feature such as menus, settings, keybindings and status message bar.

If you have any issues or question please comment, I am glad to help you :D

Thank you !!!
Thank you

This article originally published onHashnode

Read Part two onDev Blog or inDev.to

Top comments(7)

Subscribe
pic
Create template

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

Dismiss
CollapseExpand
 
Sloan, the sloth mascot
Comment deleted
CollapseExpand
 
thamaraiselvam profile image
Thamaraiselvam
I am a passionate person, who has been working as a Full Stack Developer for 4 years. I love GNU/Linux, Open Source Software
  • Email
  • Location
    India
  • Education
    Bachelor's Degree in Computer Science Engineering
  • Work
    Senior Software Developer at ThoughtWorks
  • Joined

@Lauret Brieu part two is published,dev.to/thamaraiselvam/create-exten...

CollapseExpand
 
thamaraiselvam profile image
Thamaraiselvam
I am a passionate person, who has been working as a Full Stack Developer for 4 years. I love GNU/Linux, Open Source Software
  • Email
  • Location
    India
  • Education
    Bachelor's Degree in Computer Science Engineering
  • Work
    Senior Software Developer at ThoughtWorks
  • Joined

Thanks for the comment@laurentbrieu, it will be in a couple of days.

CollapseExpand
 
mudjaycker profile image
maryimana butom
Afrika is coding too ! code is code it's not life
  • Education
    Lake Tanganyika University (Bujumbura)
  • Work
    Fullstack dev
  • Joined
• Edited on• Edited

Hi 🖐🏿. Thank you for this publication. I feel I'm gonna enjoy the next part 🤗.

Edit: /I figured out that I'm discovering and learning from a 2019 publication, for me that proves how useful and concise your publication. Thank you again .
I hope you still out there to read my comment 😁
🙏🏿
/

CollapseExpand
 
anfossistudio profile image
AnfossiStudio
  • Location
    Morroco , Sale
  • Joined

Thank you very much ❤

CollapseExpand
 
freeviwo profile image
Freeviw Official
Freeviw.com is a streaming blog, run by the Weendy Woop.
  • Joined

i really appreciate it

CollapseExpand
 
thamaraiselvam profile image
Thamaraiselvam
I am a passionate person, who has been working as a Full Stack Developer for 4 years. I love GNU/Linux, Open Source Software
  • Email
  • Location
    India
  • Education
    Bachelor's Degree in Computer Science Engineering
  • Work
    Senior Software Developer at ThoughtWorks
  • Joined

Thank you so much for your comment. It means a lot 🙏🙏🙏

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

I am a passionate person, who has been working as a Full Stack Developer for 4 years. I love GNU/Linux, Open Source Software
  • Location
    India
  • Education
    Bachelor's Degree in Computer Science Engineering
  • Work
    Senior Software Developer at ThoughtWorks
  • Joined

More fromThamaraiselvam

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