如何为 Visual Studio Code 扩展创建文件? [英] How do I create a file for a Visual Studio Code Extension?

查看:24
本文介绍了如何为 Visual Studio Code 扩展创建文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a file as a part of one of the commands in my extension and can't seem to get it right.

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 是用户输入的字符串.代码执行,但据我所知,没有创建文件.如何正确创建文件?

value is a string input from the user. The code executes, but from what I can tell no file is being created. How do I properly create the file?

推荐答案

看起来像 vscode.Uri 不支持相对路径(此处 是相应的问题).话虽如此,您必须使用绝对路径.以下代码段应该可以工作(在使用 vscode v1.30.0 的 Windows 上测试)

It seems like the vscode.Uri does not support relative paths (here is the corresponding issue). With that said you have to use an absolute path. The following snippet should work (tested on windows with 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');

这篇关于如何为 Visual Studio Code 扩展创建文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆