【问题标题】:How do I create a file for a Visual Studio Code Extension?如何为 Visual Studio 代码扩展创建文件?
【发布时间】:2019-04-04 01:30:30
【问题描述】:

我正在尝试创建一个文件作为我扩展程序中命令之一的一部分,但似乎无法正确完成。

let wsedit = new vscode.WorkspaceEdit();
const file_path = vscode.Uri.file(value + '/' + value + '.md');
vscode.window.showInformationMessage(file_path.toString());
wsedit.createFile(file_path, {ignoreIfExists: true});
vscode.workspace.applyEdit(wsedit);
vscode.window.showInformationMessage('Created a new file: ' value + '/' + value + '.md);

value 是用户输入的字符串。代码执行,但据我所知,没有创建文件。如何正确创建文件?

【问题讨论】:

    标签: visual-studio-code vscode-extensions


    【解决方案1】:

    似乎vscode.Uri 不支持相对路径(here 是相应的问题)。话虽如此,您必须使用绝对路径。以下 sn-p 应该可以工作(在 windows 上使用 vscode v1.30.0 测试)

    const wsedit = new vscode.WorkspaceEdit();
    const wsPath = vscode.workspace.workspaceFolders[0].uri.fsPath; // gets the path of the first workspace folder
    const filePath = vscode.Uri.file(wsPath + '/hello/world.md');
    vscode.window.showInformationMessage(filePath.toString());
    wsedit.createFile(filePath, { ignoreIfExists: true });
    vscode.workspace.applyEdit(wsedit);
    vscode.window.showInformationMessage('Created a new file: hello/world.md');
    

    【讨论】:

    • 我收到您的第一行错误,指出“对象可能是'未定义'”。有什么想法吗?
    • const wsedit = new vscode.WorkspaceEdit();?也许您可以将我的第 2 行和第 3 行添加到您的 sn-p 中并试一试。 (顺便说一句。我用打字稿)
    • 我能够通过使用this answer 的形式来完成它。得到对象后,我检查以确保它不是未定义的。
    猜你喜欢
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多