VSCode插件開發(fā)筆記 2

前言

筆者正在學習開發(fā)一款VSCode插件,文章為學習所做的筆記,供學習使用。

1. package.json

以下為插件中package.json的大致寫法

{
    // 插件的名字,應全部小寫,不能有空格
    "name": "vscode-plugin-demo",
    // 插件的友好顯示名稱,用于顯示在應用市場,支持中文
    "displayName": "VSCode插件demo",
    // 描述
    "description": "VSCode插件demo集錦",
    // 關鍵字,用于應用市場搜索
    "keywords": ["vscode", "plugin", "demo"],
    // 版本號
    "version": "1.0.0",
    // 發(fā)布者,如果要發(fā)布到應用市場的話,這個名字必須與發(fā)布者一致
    "publisher": "sxei",
    // 表示插件最低支持的vscode版本
    "engines": {
        "vscode": "^1.27.0"
    },
    // 插件應用市場分類,可選值: [Programming Languages, Snippets, Linters, Themes, Debuggers, Formatters, Keymaps, SCM Providers, Other, Extension Packs, Language Packs]
    "categories": [
        "Other"
    ],
    // 插件圖標,至少128x128像素
    "icon": "images/icon.png",
    // 擴展的激活事件數(shù)組,可以被哪些事件激活擴展,后文有詳細介紹
    "activationEvents": [
        "onCommand:extension.sayHello"
    ],
    // 插件的主入口
    "main": "./src/extension",
    // 貢獻點,整個插件最重要最多的配置項
    "contributes": {
        // 插件配置項
        "configuration": {
            "type": "object",
            // 配置項標題,會顯示在vscode的設置頁
            "title": "vscode-plugin-demo",
            "properties": {
                // 這里我隨便寫了2個設置,配置你的昵稱
                "vscodePluginDemo.yourName": {
                    "type": "string",
                    "default": "guest",
                    "description": "你的名字"
                },
                // 是否在啟動時顯示提示
                "vscodePluginDemo.showTip": {
                    "type": "boolean",
                    "default": true,
                    "description": "是否在每次啟動時顯示歡迎提示!"
                }
            }
        },
        // 命令
        "commands": [
            {
                "command": "extension.sayHello",
                "title": "Hello World"
            }
        ],
        // 快捷鍵綁定
        "keybindings": [
            {
                "command": "extension.sayHello",
                "key": "ctrl+f10",
                "mac": "cmd+f10",
                "when": "editorTextFocus"
            }
        ],
        // 菜單
        "menus": {
            // 編輯器右鍵菜單
            "editor/context": [
                {
                    // 表示只有編輯器具有焦點時才會在菜單中出現(xiàn)
                    "when": "editorFocus",
                    "command": "extension.sayHello",
                    // navigation是一個永遠置頂?shù)姆纸M,后面的@6是人工進行組內排序
                    "group": "navigation@6"
                },
                {
                    "when": "editorFocus",
                    "command": "extension.demo.getCurrentFilePath",
                    "group": "navigation@5"
                },
                {
                    // 只有編輯器具有焦點,并且打開的是JS文件才會出現(xiàn)
                    "when": "editorFocus && resourceLangId == javascript",
                    "command": "extension.demo.testMenuShow",
                    "group": "z_commands"
                },
                {
                    "command": "extension.demo.openWebview",
                    "group": "navigation"
                }
            ],
            // 編輯器右上角圖標,不配置圖片就顯示文字
            "editor/title": [
                {
                    "when": "editorFocus && resourceLangId == javascript",
                    "command": "extension.demo.testMenuShow",
                    "group": "navigation"
                }
            ],
            // 編輯器標題右鍵菜單
            "editor/title/context": [
                {
                    "when": "resourceLangId == javascript",
                    "command": "extension.demo.testMenuShow",
                    "group": "navigation"
                }
            ],
            // 資源管理器右鍵菜單
            "explorer/context": [
                {
                    "command": "extension.demo.getCurrentFilePath",
                    "group": "navigation"
                },
                {
                    "command": "extension.demo.openWebview",
                    "group": "navigation"
                }
            ]
        },
        // 代碼片段
        "snippets": [
            {
                "language": "javascript",
                "path": "./snippets/javascript.json"
            },
            {
                "language": "html",
                "path": "./snippets/html.json"
            }
        ],
        // 自定義新的activitybar圖標,也就是左側側邊欄大的圖標
        "viewsContainers": {
            "activitybar": [
                {
                    "id": "beautifulGirl",
                    "title": "美女",
                    "icon": "images/beautifulGirl.svg"
                }
            ]
        },
        // 自定義側邊欄內view的實現(xiàn)
        "views": {
            // 和 viewsContainers 的id對應
            "beautifulGirl": [
                {
                    "id": "beautifulGirl1",
                    "name": "國內美女"
                },
                {
                    "id": "beautifulGirl2",
                    "name": "國外美女"
                },
                {
                    "id": "beautifulGirl3",
                    "name": "人妖"
                }
            ]
        },
        // 圖標主題
        "iconThemes": [
            {
                "id": "testIconTheme",
                "label": "測試圖標主題",
                "path": "./theme/icon-theme.json"
            }
        ]
    },
    // 同 npm scripts
    "scripts": {
        "postinstall": "node ./node_modules/vscode/bin/install",
        "test": "node ./node_modules/vscode/bin/test"
    },
    // 開發(fā)依賴
    "devDependencies": {
        "typescript": "^2.6.1",
        "vscode": "^1.1.6",
        "eslint": "^4.11.0",
        "@types/node": "^7.0.43",
        "@types/mocha": "^2.2.42"
    },
    // 后面這幾個應該不用介紹了
    "license": "SEE LICENSE IN LICENSE.txt",
    "bugs": {
        "url": "https://github.com/sxei/vscode-plugin-demo/issues"
    },
    "repository": {
        "type": "git",
        "url": "https://github.com/sxei/vscode-plugin-demo"
    },
    // 主頁
    "homepage": "https://github.com/sxei/vscode-plugin-demo/blob/master/README.md"
}

關于package.json 寫法的更多詳情,可查看 官網extension-manifest

2. 擴展的激活事件數(shù)組 activationEvents

插件在VSCode中默認是沒有激活的,在這個數(shù)組里可以配置激活插件的方式。
目前支持以下幾種配置:

  • onLanguage:${language}
  • onCommand:${command}
  • onDebug
  • workspaceContains:${toplevelfilename}
  • onFileSystem:${scheme}
  • onView:${viewId}
  • onUri
  • *
    注意:如果配置了*,只要一啟動vscode,插件就會被激活(官方不推薦)
    其他的例子: 如配置了 onLanguage:javascript,則只要打開了JS類型的文件,插件就會被激活。

更多配置可查看 activation-events

3. contributes

更多可查看 官網貢獻點清單

參考

  1. VSCode插件開發(fā)全攻略(三)package.json詳解
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容