如何在编辑器操作工具栏中添加自定义图标

发布于 2025-01-22 19:41:37 字数 165 浏览 2 评论 0 原文

我想在编辑器操作工具栏上添加一个自定义图标,这将是指命令的链接。当在VSCODE上打开任何文件时,应与文件旁边可见图标。如何添加自定义图标?任何线索都会有用

“

I want to add a custom icon on the editor action toolbar in vs code, which would be a link to a command. The icon should be visible alongside the file when any file is open on vscode. How to add the custom icon? Any leads would be helpful

image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

邮友 2025-01-29 19:41:37

在package.json中添加您的自定义命令。有关更多详细信息,请查看以下链接:

  1. href =“ https://code.visualstudio.com/api/references/extension-guidelines#editor-actions-
  2. < a href =“ https://code.visualstudio.com/api/references/contribution-points#contributes.commands” rel =“ nofollow noreferrer”>贡献点

Add your custom command inside package.json under "editor/title" and "command". For more details check the following links:

  1. Editor Action
  2. Contribution point
染年凉城似染瑾 2025-01-29 19:41:37

要在此处添加图标,您需要编辑 package.json 文件。只需添加一个命令和菜单贡献点,该点可以说明VSCODE在哪里放置菜单项,

这里有一个答案,该答案具有链接到VS Code API文档,但是该文档已被抽出很大您将放入 package.json 文件中。因此,我将举一个简单的现实世界代码

软件包。JSON

{
    ...
    "contributes": {
        "commands": [
            {
                "command": "My.Custom.Command",
                "title": "Do Something",
                "icon": "$(run)"
            }
        ],
        "menus": {
            "editor/title": [
                {
                    "command": "My.Custom.Command",
                    "group": "navigation",
                    "when": "editorLangId == javascript"
                }
            ]
        }
    }
}

需要两个项目。

  • 下的命令贡献.commands 。该命令应具有图标(请参阅产品图标参考
  • 以及何时何地显示命令。要将命令添加到编辑器操作工具栏中,请在 convortes.menus.editor/title 菜单下添加命令。如果您希望它显示为顶级项目,请将设置为 navigation 。您还可以(并且可能应该)设置子句时设置,以便图标仅在满足某些条件时显示。 (请参阅wher strage reference
    在示例中,图标将仅在JavaScript文件上显示。如果您在
    子句时省略,它将始终显示

,当用户单击图标上时,除非您在扩展激活功能中为命令设置处理程序功能,否则它将无能为力。这也意味着您还必须在 package.json.json 文件中设置正确的 activationEvents ,否则将无法设置处理程序。

这是一个简单的扩展程序的完整示例,它可以执行您的要求:

package.json(完整示例),

{
    "name": "my-extension",
    "displayName": "My Custom Extension",
    "description": "Adds a run icon to the top of JavaScript files",
    "version": "1.0.0",
    "main": "extension.js",
    "license": "ISC",
    "engines": {
        "vscode": "^1.82.0"
    },
    "activationEvents": [
        "onLanguage:javascript"
    ],
    "contributes": {
        "commands": [
            {
                "command": "Run.JavaScript.File",
                "title": "Run this file",
                "icon": "$(run)"
            }
        ],
        "menus": {
            "editor/title": [
                {
                    "command": "Run.JavaScript.File",
                    "when": "resourceLangId == javascript",
                    "group": "navigation"
                }
            ]
        }
    }
}

并且由于我在我的 main 设置为 extension.js > package.json 文件,您还需要一个名为 extension.js 的文件。这是一个简单的示例,可以让您启动

Extension.js


// The main extension file
let vscode = require("vscode")
let childProcess = require("child_process")

module.exports = {
    activate: (context) => {

        // Register the command
        context.subscriptions.push(
            vscode.commands.registerCommand(
                // Name of the command as in the package.json file
                "Run.JavaScript.File",

                // A handler function that gets executed when the command is run
                () => {

                    // You can put anything you want in here.
                    // What do you want to happen when the user clicks the button?

                    // Here, I simply run the javascript file in node and
                    // show an information message when it's complete
                    let runner = childProcess.spawn(
                        "node",

                        // This is the path of the current active file
                        // I'm just passing that directly to node
                        [vscode.window.activeTextEditor.document.uri.path]
                    )

                    let out = ""
                    runner.stdout.on("data", (data) => {
                        out += data.toString("utf-8")
                    })
                    runner.on("close", () => {
                        vscode.window.showInformationMessage("Run completed: " + out)
                    })
                }
            )
        )
    }
}

To add an icon there, you'll need to edit your package.json file. Simply add a command and a menu contribution point that tells VSCode where to place the menu item

There is an answer here that has links to the VS code API documentation, but that documentation is very drawn out and doesn't give any good examples on exactly what you would put into your package.json file. So, I'll give a simple example of some real world code

package.json

{
    ...
    "contributes": {
        "commands": [
            {
                "command": "My.Custom.Command",
                "title": "Do Something",
                "icon": "$(run)"
            }
        ],
        "menus": {
            "editor/title": [
                {
                    "command": "My.Custom.Command",
                    "group": "navigation",
                    "when": "editorLangId == javascript"
                }
            ]
        }
    }
}

There are two items that are needed.

  • The command under contributes.commands. That command should have an icon (see the product icons reference)
  • And where and when to show the command. To add the command to the editor actions toolbar, add the command under the contributes.menus.editor/title menu. If you want it to display as a top level item, set the group to navigation. You can also (and probably should) set a when clause so that the icon only shows when certain conditions are met. (See the when clause reference)
    In the example, the icon will only show on JavaScript files. If you omit the when clause, it will always show

Note that when the user clicks on the icon, it will do nothing unless you set a handler function for the command in your extension activation function. Which also means that you have to set the correct activationEvents in your package.json file as well or the handler won't be set.

Here is a full example of a simple extension that does what you are asking:

package.json (full example)

{
    "name": "my-extension",
    "displayName": "My Custom Extension",
    "description": "Adds a run icon to the top of JavaScript files",
    "version": "1.0.0",
    "main": "extension.js",
    "license": "ISC",
    "engines": {
        "vscode": "^1.82.0"
    },
    "activationEvents": [
        "onLanguage:javascript"
    ],
    "contributes": {
        "commands": [
            {
                "command": "Run.JavaScript.File",
                "title": "Run this file",
                "icon": "$(run)"
            }
        ],
        "menus": {
            "editor/title": [
                {
                    "command": "Run.JavaScript.File",
                    "when": "resourceLangId == javascript",
                    "group": "navigation"
                }
            ]
        }
    }
}

And since I set main to extension.js in my package.json file, you'll need a file named extension.js as well. Here is a simple example that will get you started

extension.js


// The main extension file
let vscode = require("vscode")
let childProcess = require("child_process")

module.exports = {
    activate: (context) => {

        // Register the command
        context.subscriptions.push(
            vscode.commands.registerCommand(
                // Name of the command as in the package.json file
                "Run.JavaScript.File",

                // A handler function that gets executed when the command is run
                () => {

                    // You can put anything you want in here.
                    // What do you want to happen when the user clicks the button?

                    // Here, I simply run the javascript file in node and
                    // show an information message when it's complete
                    let runner = childProcess.spawn(
                        "node",

                        // This is the path of the current active file
                        // I'm just passing that directly to node
                        [vscode.window.activeTextEditor.document.uri.path]
                    )

                    let out = ""
                    runner.stdout.on("data", (data) => {
                        out += data.toString("utf-8")
                    })
                    runner.on("close", () => {
                        vscode.window.showInformationMessage("Run completed: " + out)
                    })
                }
            )
        )
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文