【问题标题】:Is there a vscode startup complete event or folder open event?是否有 vscode 启动完成事件或文件夹打开事件?
【发布时间】:2020-02-23 16:02:35
【问题描述】:

扩展代码是否可以设置为在 vscode 启动完成时运行?或者当一个文件夹被打开时?

如何编写一个扩展程序,在新的 vscode 窗口中打开一个文件夹,然后在该文件夹中打开一个文本文件?

我已经打开文件夹部分工作。我正在使用全局状态来存储要打开的文件的名称。

// store in name of file to open in global state.
context.globalState.update('fileToOpen', './src/index.html');

// open folder in a new vscode instance.
const uri_path = `file:///c:/web/tester/parcel`;
const uri = vscode.Uri.parse(uri_path);
await vscode.commands.executeCommand('vscode.openFolder', uri, true);

然后,当我的扩展在新的 vscode 实例中激活时,我想从全局状态读取文件名,等待 vscode 打开文件夹,然后运行openTextDocument 打开文件。

【问题讨论】:

    标签: visual-studio-code vscode-extensions


    【解决方案1】:

    从 v1.46 开始,有一个 onStartupFinished 激活事件。所以你的package.json 会拥有它:

    ...
    "activationEvents": [
        "onStartupFinished"
    ]
    ...
    

    然后继续检查激活时的状态

    export function activate(context: vscode.ExtensionContext) {
      const fileToOpen = context.globalState.get('fileToOpen')
      if (fileToOpen) {
        // open file
        let document = await vscode.workspace.openTextDocument(fileToOpen);
        await vscode.window.showTextDocument(document);
       
        // reset state
        context.globalState.update('fileToOpen', undefined);
      } else {
        // store in name of file to open in global state.
        context.globalState.update('fileToOpen', './src/index.html');
    
        // open folder in a new vscode instance.
        const uri_path = `file:///c:/web/tester/parcel`;
        const uri = vscode.Uri.parse(uri_path);
        await vscode.commands.executeCommand('vscode.openFolder', uri, true); 
      }
    
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-31
      • 2019-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2010-10-13
      相关资源
      最近更新 更多