【问题标题】:Write to a VS Code window写入 VS Code 窗口
【发布时间】:2022-01-01 17:32:53
【问题描述】:

我正在编写一个 VS Code 扩展程序,以便在您单击按钮时将 Hello World 输出到窗口。它记录您正在使用的语言并以此为基础做出决定。除了实际写入屏幕之外,我已经编写了所有逻辑,但我不知道该怎么做。

import * as vscode from 'vscode';
import "typescript";

let myStatusBar : vscode.StatusBarItem;

export function activate(context: vscode.ExtensionContext, { subscriptions }: vscode.ExtensionContext) {
    const codeBoilerplate = 'code-boilerplate.CodeBoilerPlate';
    subscriptions.push(vscode.commands.registerCommand(codeBoilerplate, () => {
        const documentFileType = vscode.window.activeTextEditor?.document.languageId;
        const documentFileName = vscode.window.activeTextEditor?.document.fileName;

        vscode.window.showInformationMessage("Generating your Code Boilerplate... ⌛")
        if (documentFileType === "javascript") {
            return null;
        }
        else if (documentFileType === "python") {
            return null;
        }
        else if (documentFileType === "typescript") {
            return null;
        }
        else if (documentFileType === "csharp") {
            return null;
        }
        else if (documentFileType === "c") {
            return null;
        }
        else if (documentFileType === "cpp") {
            return null;
        }
        else if (documentFileType === "java") {
            return null;
        }

        updateStatusBarItem();
    }));

    myStatusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
    myStatusBar.command = codeBoilerplate;
    subscriptions.push(myStatusBar);
}

function updateStatusBarItem(): void {
    myStatusBar.text = `$(new-file) Hello World!`;
}

// this method is called when your extension is deactivated
export function deactivate() {
    console.log("code-boilerplate is deactivated!");
}

我试过用这个方法,但是没用……

vscode.window.activeTextEditor.edit(editBuilder => {
                editBuilder.insert(new vscode.Position(0, 0), 'console.log("Hello World!");');
            });

【问题讨论】:

    标签: typescript vscode-extensions


    【解决方案1】:

    根据您的代码,您用于将字符串插入编辑器的函数甚至不会在您列出的类型(javascript 等)的文件中运行。虽然,它在我的 txt 文件中完美运行。所以我建议你删除以下代码块或重写它,这样它就不会返回并停止函数执行。

    if (documentFileType === "javascript") {
            return null;
        }
        else if (documentFileType === "python") {
            return null;
        }
        else if (documentFileType === "typescript") {
            return null;
        }
        else if (documentFileType === "csharp") {
            return null;
        }
        else if (documentFileType === "c") {
            return null;
        }
        else if (documentFileType === "cpp") {
            return null;
        }
        else if (documentFileType === "java") {
            return null;
        }
    

    【讨论】:

      猜你喜欢
      • 2022-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 2017-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多