How do I create a file for a Visual Studio Code Extension?

泪湿孤枕 提交于 2021-01-28 02:50:48

问题


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 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?


回答1:


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');


来源:https://stackoverflow.com/questions/53073926/how-do-i-create-a-file-for-a-visual-studio-code-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!