简体   繁体   English

触发OnSave [Visual Studio代码:扩展]

[英]Trigger OnSave [Visual studio code: Extension]

I'm a bit stuck on creating a visual studio code Extension. 我在创建Visual Studio代码扩展方面有些困惑。 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). 注意:请检查文档URI方案,以避免对文件(例如github链接)以外的其他资源产生影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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