VSCode plug in

First Extension

Requirement

install generator

npm install -g yo generator-code

Generate scaffolds

yo code
# ? What's the identifier of your extension? helloworld
code ./helloworld

Run

Anatomy

  1. onCommand Activation Event: onCommand:extension.helloWorld
  2. contributes.commands Contribution Point: Hello World in Command Palette
  3. commands.registerCommand VS Code API: extension.helloWorld bind command and function

File Structure

.
β”œβ”€β”€ .vscode
β”‚   β”œβ”€β”€ launch.json     // Config for launching and debugging the extension
β”‚   └── tasks.json      // Config for build task that compiles TypeScript
β”œβ”€β”€ .gitignore          // Ignore build output and node_modules
β”œβ”€β”€ README.md           // Readable description of your extension's functionality
β”œβ”€β”€ src
β”‚   └── extension.ts    // Extension source code
β”œβ”€β”€ package.json        // Extension manifest
β”œβ”€β”€ tsconfig.json       // TypeScript configuration

Extension Manifest package.json

{
  "name": "helloworld-sample",
  "displayName": "helloworld-sample",
  "description": "HelloWorld example for VS Code",
  "version": "0.0.1",
  "publisher": "vscode-samples",
  "repository": "https://github.com/microsoft/vscode-extension-samples/helloworld-sample",
  "engines": {
    "vscode": "^1.51.0"
  },
  "categories": ["Other"],
  "activationEvents": ["onCommand:extension.helloWorld"],
  "main": "./out/extension.js",
  "contributes": {
    "commands": [
      {
        "command": "extension.helloWorld",
        "title": "Hello World"
      }
    ]
  },
  "scripts": {
    "vscode:prepublish": "npm run compile",
    "compile": "tsc -p ./",
    "watch": "tsc -watch -p ./"
  },
  "devDependencies": {
    "@types/node": "^8.10.25",
    "@types/vscode": "^1.51.0",
    "tslint": "^5.16.0",
    "typescript": "^3.4.5"
  }
}

Extension Entry File

// 'vscode' λͺ¨λ“ˆμ€ VS Code extensibility APIλ₯Ό 포함
// μ•„λž˜ μ½”λ“œμ—μ„œ λͺ¨λ“ˆμ„ κ°€μ Έμ˜€κ³  μ½”λ“œμ—μ„œ vscode 별칭을 톡해 μ°Έμ‘° κ°€λŠ₯
import * as vscode from 'vscode';

// ν™•μž₯ λͺ¨λ“ˆμ΄ ν™œμ„±ν™”λ˜λ©΄ μ‹€ν–‰λ˜λŠ” λ©”μ†Œλ“œ
// ν™œμ„±ν™”λ˜λ©΄ 제일 λ¨Όμ € μ‹€ν–‰λ˜λŠ” λΆ€λΆ„
export function activate(context: vscode.ExtensionContext) {
  // 진단 정보λ₯Ό 좜λ ₯ν•˜κΈ° μœ„ν•΄ console.log와 console.errorλ₯Ό μ‚¬μš©
  // ν™•μž₯ λͺ¨λ“ˆμ΄ ν™œμ„±ν™” 되면 ν•œ 번만 μ‹€ν–‰
  console.log('Congratulations, your extension "helloworld-sample" is now active!');

  // package.json νŒŒμΌμ— μ •μ˜λœ λͺ…λ Ή
  // registerCommand둜 λͺ…λ Ήμ˜ κ΅¬ν˜„μ²΄ 제곡
  // commandId νŒŒλΌλ―Έν„°λŠ” package.json에 μžˆλŠ” command ν•„λ“œμ™€ λ§€μΉ˜λ˜μ–΄μ•Ό 함
  let disposable = vscode.commands.registerCommand('extension.helloWorld', () => {
    // μ—¬λŸ¬λΆ„μ΄ μ •μ˜ν•œ λͺ…령이 싀행될 λ•Œλ§ˆλ‹€ μ‹€ν–‰λ˜λŠ” μ½”λ“œ

    // μ‚¬μš©μžμ—κ²Œ λ©”μ‹œμ§€ λ°•μŠ€ ν‘œμ‹œ
    vscode.window.showInformationMessage('Hello World!');
  });

  context.subscriptions.push(disposable);
}

// 이 λ©”μ†Œλ“œλŠ” μ—¬λŸ¬λΆ„μ˜ ν™•μž₯ λͺ¨λ“ˆμ΄ λΉ„ν™œμ„±ν™”λ  λ•Œ 싀행됨
export function deactivate() {}

ref

What Else?
inflearn react api server -50% 할인쿠폰: 15108-f2af1e086101 buy me a coffee