如何在Visual Code中内联忽略格式?

时间:2017-09-14 09:09:04

标签: formatting visual-studio-code inline ignore

VCode使用哪种格式化程序?我在某处看过VC使用jsbeautifier所以我尝试在我的.ejs模板中添加兼容的忽略评论,但无济于事。

1 个答案:

答案 0 :(得分:3)

据我所知,没有扩展名就无法做到这一点。

但是,如果您使用this extension,则可以完全控制格式。它也使用js-beautify,但它增加了配置。

如js-beautify docs中所述,"保留"指令仅适用于javascript部分。我已在ejs文件的脚本标记中对此进行了测试,并且格式化文档不会更改console.log语句的缩进。实际上,这根本不需要改变任何设置。我只是安装了扩展程序,将此文件保存为index.ejs并观察到vscode的语言模式设置为html

我的测试页

// These comments DON'T work because they aren't in a javascript section of the code
/* beautify preserve:start */
<h1><%= title %></h1>
/* beautify preserve:end */
<ul>
</ul>
<script>
    function log() {
        // Without the beautify comments, format document will move console.log 
        // to align with this
        /* beautify preserve:start */
                console.log('hello');
        /* beautify preserve:end */
    }
</script>