简体   繁体   中英

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!

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 (eg github links).

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