简体   繁体   中英

Is there a vscode startup complete event or folder open event?

Can extension code be set to run when startup of vscode has completed? Or when a folder has been opened?

How to write an extension that opens a folder in a new vscode window, and then opens a text file in that folder?

I have the open the folder part working. And I am using global state to store the name of the file to open.

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

Then, when my extension is activated in the new vscode instance, I want to read the file name from global state, wait for vscode to open the folder, then run openTextDocument to open the file.

Since v1.46 there's a onStartupFinished activation event. So your package.json would have it:

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

Then proceed to check the state upon activation

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

  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM