首页 > 解决方案 > VSCode 扩展在调试时工作,但一旦打包到 VSIX 中就不行

问题描述

我试图学习一些关于为 vscode 构建扩展的知识,但我遇到了障碍。我的扩展只应该根据文件类型从剪贴板粘贴一些额外的文本。(用于快速调试)问题是我在调试时有这个工作但是一旦它被打包我得到一个rejected promise not handled within 1 second: Error: TextEditor#edit not possible on closed editors

这对我来说很有意义,但我不知道如何保持 activeTextEditor 的更新,也不知道为什么打包后它的行为会有所不同。我肯定错过了一些简单的东西。

function activate(context) {
    const dbugText = vscode.commands.registerCommand('extension.dbug', () => {
        vscode.env.clipboard.readText().then((text) => {
            let editor = vscode.window.activeTextEditor;
            if(!editor){
                return;
            }

            var document = editor.document;
            var selection = editor.selection;
            var fileType = document.fileName.split('.')[1];

            console.log(text);

            if (fileType == 'php') {
                text = "dbug(" + text + ");";
            } else if (fileType == 'js') {
                text = "console.log(" + text + ");";
            } else {
                console.log('Unknown file type');
                return;
            }

            vscode.window.showInformationMessage(text);

            editor.edit((editBuilder) => {
                return editBuilder.insert(new vscode.Position(selection.start.line, selection.start.character), text);
            })
            .catch(err => console.error(err.message));
        })
        .catch(err => console.error(err.message));
    });
    context.subscriptions.push(dbugText);
}

标签: javascriptvisual-studio-codevsix

解决方案


推荐阅读