惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
V
Visual Studio Blog
罗磊的独立博客
Last Week in AI
Last Week in AI
爱范儿
爱范儿
The Cloudflare Blog
有赞技术团队
有赞技术团队
博客园 - 三生石上(FineUI控件)
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 司徒正美
IT之家
IT之家
博客园 - 【当耐特】
雷峰网
雷峰网
博客园_首页
Jina AI
Jina AI
Hugging Face - Blog
Hugging Face - Blog
阮一峰的网络日志
阮一峰的网络日志
量子位
V
V2EX

Java不加糖的Blog

假设, AI 把我代替的那一刻真的到来 | szhshp 的第三边境研究所 小傻瓜都能懂的 AstrBot QQ 机器人集成 MCP 功能实战指南 | szhshp 的第三边境研究所 《智人之上: 从石器时代到 AI 时代的信息网络简史》阅读笔记 | szhshp 的第三边境研究所 iPadOS 26 无法设置空间场景图片壁纸的解决方法 | szhshp 的第三边境研究所 《一路云海》(终) | szhshp 的第三边境研究所 《一路云海》(四): 如何不按套路旅行 | szhshp 的第三边境研究所 《一路云海》(三): In Ya Mellow Tone | szhshp 的第三边境研究所 《一路云海》(二): 关西世博参观纪实 | szhshp 的第三边境研究所 《一路云海》(一): 新的征程 | szhshp 的第三边境研究所 2025 大阪世博会 [ 3 天前-先到先得 ] 阶段 场馆预约必中独家攻略 | szhshp 的第三边境研究所 Hackathon 随想 | szhshp 的第三边境研究所 一杯双皮奶 | szhshp 的第三边境研究所 Armbian + CasaOS + NAS 配置指南 | szhshp 的第三边境研究所 Docker 构建镜像报错: error getting credentials - err: exit status 1, out: `` | szhshp 的第三边境研究所 Disqus RIP! 论过高的维护成本如何治疗固执的坏习惯 | szhshp 的第三边境研究所 炸弹猫桌游变体规则 | szhshp 的第三边境研究所 《小岛经济学》阅读笔记 | szhshp 的第三边境研究所 《金钱心理学》阅读笔记 | szhshp 的第三边境研究所 为知笔记 RIP: 迁移剩余的笔记 | szhshp 的第三边境研究所 2025 博客第十年展望 - 再见我的过去 | szhshp 的第三边境研究所 我在独立游戏里面致敬的作品 | szhshp 的第三边境研究所 《How to make thing faster》阅读笔记 | szhshp 的第三边境研究所 《The Art of Clean Code》阅读笔记 | szhshp 的第三边境研究所 《Clean Architecture: A Craftsman Guide to Software Structure and Design》阅读笔记 | szhshp 的第三边境研究所 《How AI Works》阅读笔记 | szhshp 的第三边境研究所 游戏策划废案 - Project Uranus | szhshp 的第三边境研究所 游戏策划废案 - Project X | szhshp 的第三边境研究所 人生第一款独立游戏开发复盘 | szhshp 的第三边境研究所 Trap of Life | szhshp 的第三边境研究所 wireguard折腾记录
VSC Extension Development-Create A Code Formatter Extensi...
2019-10-27 · via Java不加糖的Blog

Meta

目录

Environment Setup

Nothing to say here, check Official Guide

Example 1

Here we will talk about:

  1. activate , deactivate event and the lifecycle of a extension

  2. use registerCommand to regist a normal event

  3. provideDocumentFormattingEdits regist a format event and design formatter the details

extension.js

const vscode = require('vscode');

/**
 * @param {vscode.ExtensionContext} context
 */
const activate = (context) => {

  // 
  console.log('Congratulations, your extension "nurse-lisa" is now active!');

  // The key part is the ommand identifier, which will defined in package.json

  // Here we will do:
  // 1. Regist an event which will triggered via [ctrl + alt + P]
  // 2. Push the event into the event subscription queue 

  let disposable = vscode.commands.registerCommand('extension.nurselisa.format', function() {
    // The code you place here will executed every time your command executed
    // Display a message box to the user
    vscode.window.showInformationMessage(dj('nurse-lisa test abc !!!……', {
      successiveExclamationMarks: false,
      replaceWithCornerQuotes: 'double',
      halfwidthParenthesisAroundNumbers: true
    }));
  });
  context.subscriptions.push(disposable);

  // 3. Regist an event, which will invoked when triggered with 'format' action, if we have many formatter then we need to set the exact command identifier  
  // 4. Push the event into the event subscription queue 

  /*
    registerDocumentFormattingEditProvider(formatSelector, provideDocumentFormattingEdits)
    [formatSelector]: limit the [scheme] and [language]

    here we declared the [scheme], the optional values are: 
      [file] represents the saved files, 
      [untitled] represents unsaved files
  */
  const documentFilters = [{
    scheme: 'file'
  }, {
    scheme: 'untitled'
  }]
  let disposable2 = vscode.languages.registerDocumentFormattingEditProvider(documentFilters, {
    provideDocumentFormattingEdits: (document) => {
      const firstLine = document.lineAt(0);
      console.log(firstLine)
      return [vscode.TextEdit.insert(firstLine.range.start, '123456\n')];
    }
  });

  context.subscriptions.push(disposable2)

}
exports.activate = activate;

// this method called when your extension deactivated
const deactivate = () => {}

module.exports = {
  activate,
  deactivate
}

package.json

{
  "name": "nurse-lisa",
  "displayName": "Nurse-lisa",
  "description": "",
  "version": "0.0.1",
  "engines": {
    "vscode": "^1.39.0"
  },
  "categories": [
    "Other"
  ],
  "activationEvents": [
    "onCommand:extension.nurselisa.format" 
  ],
  "main": "./extension.js",
  "contributes": {
    "commands": [
      {
        "command": "extension.nurselisa.format", // here is the command identifier 
        "title": "Nurse Lisa - Format Markdown" // this is the title of your registered command
      }
    ]
  },
  "scripts": {
    "test": "node ./test/runTest.js"
  },
  "devDependencies": {
    "@types/glob": "^7.1.1",
    "@types/mocha": "^5.2.6",
    "@types/node": "^10.12.21",
    "@types/vscode": "^1.39.0",
    "eslint": "^5.13.0",
    "glob": "^7.1.4",
    "mocha": "^6.1.4",
    "typescript": "^3.3.1",
    "vscode-test": "^1.2.0"
  },
  "dependencies": {
    "doctor-jones": "^1.0.2"
  }
}

Example 2

Here we will use registerDocumentFormattingEditProvider regist a text editor format event。

	let disposable = vscode.commands.registerTextEditorCommand('extension.nurselisa.format', (editor, edit) => {
	  vscode.window.showInformationMessage('Nurse Lisa: Markdown Formatted');

	  let textToFormat = editor.document.getText();
	  let fullRange = new vscode.Range(0, 0, editor.document.lineCount, 0);

	  let formatted = format(textToFormat);

	  edit.replace(fullRange, formatted);

	});
	context.subscriptions.push(disposable);

Troubleshooting

Command not found in VSCode extension

command ‘extension.useMyExtension.commandA’ not found

Check if ALL your events are registered here:

{
    ...
    "activationEvents": [
      /* DO NOT forget to add prefix 'onCommand:' */
        "onCommand:extension.useMyExtension.commandA",
        "onCommand:extension.useMyExtension.commandB"
    ]
    ...
}