VS代码-扩展名命令调用

时间:2019-02-14 18:34:23

标签: typescript visual-studio-code vscode-extensions

我正在使用VS Code扩展程序,该扩展程序可以自动配置许多项目。

阅读此链接后我不清楚的内容: Is it possible to call command between extensions in VSCode?

是在TypeScript代码的同一扩展名内调用另一个命令的正确方法。

例如按CTRL + Shift + P激活并运行我的主要面向用户的命令,并通过适当的逻辑等方式运行。

// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import fs = require('fs');
import path = require('path');
const ini = require('ini');

export function activate(context: vscode.ExtensionContext) {

    // install check name to match package.json
    const myCommand = 'extension.myCommandName';

    // command handler
    const myCommandHandler = async (name?: 'MyCommand: Do Things') => {
        // Check if a local path exists
        function checkDirectorySync(directory: fs.PathLike) {
            try {
                fs.statSync(directory);
                return true;
            } catch (e) {
                return false;
            }
        }

        let isDirectoryValid = checkDirectorySync('C:\\Users\myusername\someFolder');

        // now call another command to launch an input box
        if (isDirectoryValid === false) {
            var secondCommand = vscode.extensions.getExtension('extension.MyOtherCommand');
            // is the ext loaded and ready?
            if (secondCommand.isActive == false) {
                secondCommand.activate().then(
                    function () {
                        console.log("Extension activated");
                        vscode.extensions.findCommand();
                        vscode.commands.executeCommand("extension.MyOtherCommand");
                    },
                    function () {
                        console.log("Extension activation failed");
                    }
                );
            } else {
                vscode.commands.executeCommand("extension.MyOtherCommand");
            }
        }
    };

    // push directory check command to command registry
    context.subscriptions.push(vscode.commands.registerCommand(myCommand, myCommandHandler));
}

// this method is called when your extension is deactivated
export function deactivate() { }

只想通过这个简单的例子来理解,如果这是正确的方法。其他帖子似乎有点令人困惑。

任何快速建议都将受到赞赏

0 个答案:

没有答案