1 回答

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊
我當(dāng)然可能是錯(cuò)的,但我不確定explorer.newFile是否會(huì)爭論。
盡管如此,這個(gè)函數(shù)將執(zhí)行您想要的操作:創(chuàng)建一個(gè)文件,打開它,然后插入一個(gè)現(xiàn)有的代碼片段:
async function createFileOpen() {
const we = new vscode.WorkspaceEdit();
const thisWorkspace = await vscode.workspace.workspaceFolders[0].uri.toString();
// if you want it to be in some folder under the workspaceFolder: append a folder name
// const uriBase = `${thisWorkspace}/folderName`;
// let newUri1 = vscode.Uri.parse(`${uriBase}/index.js`);
// create a Uri for a file to be created
const newUri = await vscode.Uri.parse(`${ thisWorkspace }\\myTestIndex.js`);
// create an edit that will create a file
await we.createFile(newUri, { ignoreIfExists: false, overwrite: true });
await vscode.workspace.applyEdit(we); // actually apply the edit: in this case file creation
await vscode.workspace.openTextDocument(newUri).then(
async document => {
await vscode.window.showTextDocument(document);
// if you are using a predefined snippet
await vscode.commands.executeCommand('editor.action.insertSnippet', { 'name': 'My Custom Snippet Label Here'});
});
}
添加回答
舉報(bào)