【问题标题】:How to give an argument to command "explorer.newFile" in Visial Studio Code extension development如何在 Visual Studio Code 扩展开发中为命令“explorer.new File”提供参数
【发布时间】:2021-03-18 18:33:39
【问题描述】:

如何在 Visual Studio Code 扩展开发中为命令explorer.newFile 提供参数? 我只是想在资源管理器视图中添加创建一个具有特定名称的文件然后打开它并插入一个sn-p,这些操作将在一个函数中实现.Help ^_^

【问题讨论】:

    标签: javascript api visual-studio-code command vscode-extensions


    【解决方案1】:

    我肯定是错的,但我不确定explorer.newFile 会接受争论。

    不过,这个函数会做你想做的事:创建一个文件,打开它,然后插入一个现有的 sn-p:

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

    【讨论】:

    • 非常感谢!对了,你知道如何获取右键选择的文件夹位置吗?
    • 是的,我相信我有这个工作,但你应该问一个单独的问题。
    猜你喜欢
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 2022-01-19
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    相关资源
    最近更新 更多