为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 Visial Studio Code 扩展开发中为命令“explorer.newFile”

如何在 Visial Studio Code 扩展开发中为命令“explorer.newFile”

哆啦的时光机 2023-10-20 16:34:02
explorer.newFile如何在 Visual Studio Code 扩展开发中为命令提供参数?我只是想添加在资源管理器视图中创建一个具有特定名称的文件,然后打开它并插入一个片段,这些操作将在函数中实现。帮助^_^
查看完整描述

1 回答

?
九州编程

TA贡献1785条经验 获得超4个赞

我当然可能是错的,但我不确定explorer.newFile是否会争论。


尽管如此,这个函数将执行您想要的操作:创建一个文件,打开它,然后插入一个现有的代码片段:


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

    });

}


查看完整回答
反对 回复 2023-10-20
  • 1 回答
  • 0 关注
  • 72 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信