Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 450
user7373379 Avatar asked Jul 21 '26 16:07

user7373379


1 Answers

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).

like image 52
Mike Lischke Avatar answered Jul 23 '26 06:07

Mike Lischke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!