Kevin Beal
Kevin Beal

Reputation: 10849

vscode - Is there a way to manually trigger events?

I am running a callback on the workspace.onWillSaveTextDocument handler. It provides vscode.TextDocument and this is necessary for the work I want to do on this event.

In certain cases, I want to treat other files (that aren't currently open) as if they had just been saved, as well.

It would suffice to be able to create a new instance of vscode.TextDocument, but I was unable to figure that out.

Is there a way to do something like:

workspace.pretendThisWasSavedJustNow('/path/to/other/file.ts');

Upvotes: 2

Views: 1552

Answers (1)

Matt Bierner
Matt Bierner

Reputation: 65223

I work on VSCode and while I don't know your exact use case, I believe you are looking for workspace.openTextDocument.

To use the same function for workspace.onWillSaveTextDocument and on some other event that your extension triggers, try something like:

// Actual save
workspace.onWillSaveTextDocument(e => onDocumentSave(e.document))

// Emulated save
setInterval(() => {
    workspace.openTextDocument('path/to/other/file.ts')
        .then(document => onDocumentSave(document))
}, 5000)

// Common save handling implementation
function onDocumentSave(document: TextDocument) { ... }

Upvotes: 2

Related Questions