user7373379
user7373379

Reputation: 98

Trigger OnSave [Visual studio code: Extension]

I'm a bit stuck on creating a visual studio code Extension. It works when I manually trigger it using the command, but I like to trigger it on save. Or maybe on a custom hot-key?

I've been searching for a while now, but the documentation is a bit vague on the subject. Been trying to understand other plugins but I have not found the answer yet.

Hope you can help!

Upvotes: 7

Views: 3279

Answers (1)

Mike Lischke
Mike Lischke

Reputation: 53532

You want to trigger an action when a document is saved? Do it like this:

workspace.onDidSaveTextDocument((document: TextDocument) => {
    if (document.languageId === "yourid" && document.uri.scheme === "file") {
        // do work
    }
});

Watch out: check the document URI scheme to avoid acting on other resources beside files (e.g. github links).

Upvotes: 13

Related Questions