vscode:插件开发

文档: 连接

终端执行 sudo pnpm install -g yo generator-code


yo code

// 这里建议选择 JavaScript 很少出错
# ? What type of extension do you want to create? New Extension (JavaScript)
# ? What's the name of your extension? HelloWorld
### Press <Enter> to choose default for all options below ###

# ? What's the identifier of your extension? helloworld
# ? What's the description of your extension? LEAVE BLANK
# ? Enable stricter TypeScript checking in 'tsconfig.json'? Yes
# ? Setup linting using 'tslint'? Yes
# ? Initialize a git repository? Yes
# ? Which package manager to use? npm

code ./helloworld

完成后进入 VS Code,按下F5,你会立即看到一个插件发开主机窗口,其中就运行着插件。

在命令面板(Ctrl+Shift+P)中输入Hello World命令。
如果你看到了Hello World提示弹窗,恭喜你成功了


正确的本地打包

pnpm i -D @vscode/vsce
然后,你需要 package.json 中写入
"scripts": {
  "package": "pnpm vsce package --no-dependencies"
}
然后终端输入
pnpm run package
即可完成打包

然后就可以在vscode本地导入 vsix 文件给其他团队使用了

示例: vscode插件开发,在选择的资源管理器的文件夹下,右键时来创建文件

首先,在您的插件项目中创建一个新的文件,例如 createFileCommand.ts,并添加以下代码:
typescript
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';

function createFile(uri: vscode.Uri) {
    if (uri && uri.scheme === 'file') {
        vscode.window.showInputBox({ prompt: 'Enter file name' }).then((fileName) => {
            if (fileName) {
                const filePath = path.join(uri.fsPath, fileName);
                fs.writeFileSync(filePath, '', 'utf-8');
                vscode.window.showInformationMessage(`File ${fileName} created successfully.`);
                vscode.commands.executeCommand('vscode.open', vscode.Uri.file(filePath));
            }
        });
    }
}

export function activate(context: vscode.ExtensionContext) {
    let disposable = vscode.commands.registerCommand('extension.createFile', (resource: vscode.Uri) => {
        createFile(resource);
    });

    context.subscriptions.push(disposable);

    vscode.workspace.onDidOpenTextDocument((e) => {
        if (e.uri.scheme !== 'file') {
            return;
        }

        const uri = vscode.Uri.file(e.uri.fsPath);
        vscode.commands.executeCommand('extension.createFile', uri);
    });
}

export function deactivate() {}package.json 文件的 contributes/contextMenus 部分中添加以下配置以注册右键菜单:
json
 "contributes": {
    "commands": [
        {
            "command": "extension.createFile",
            "title": "Create File"
        }
    ],
    "menus": {
        "explorer/context": [
            {
                "when": "resourceScheme==file",
                "command": "extension.createFile",
                "group": "navigation"
            }
        ]
    }
}
在插件的 extension.ts 文件中引入 createFileCommand.ts 并在激活插件时调用 activate 方法。
这样,当用户在资源管理器中右键点击文件夹时,将会显示一个"Create File"的选项,点击后会弹出输入框,允许用户输入新文件名并在该文件夹中创建新文件。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值