问题
Which formatter VCode uses? I read somewhere VC uses jsbeautifier
so I tried adding a compatible ignore comment to my .ejs
template, but to no avail.
回答1:
As far as I know, there is no way to do this without an extension.
However, you have full control of the formatting if you use this extension. It also uses js-beautify, but it adds configuration.
As specified in the js-beautify docs, the "preserve" directive only works in javascript sections. I have tested this in the script tag of an ejs
file and formatting the document does NOT change the indentation of the console.log
statement. This worked without changing any settings at all, actually. I simply installed the extension, saved this file as index.ejs
and observed that vscode had the language mode set to html
.
My test page
// 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>
来源:https://stackoverflow.com/questions/46214935/how-to-inline-ignore-format-in-visual-code