问题:在HBuilder中自动格式化代码时,会自动把大括号进行换行,如图:
格式化时就会换行
分析:原来一直以为是自动格式化时,自动换行了,没有想到是遇到大括号才会换行,网上找了好久资料,网上大部分是关于vscode中格式化自动换行的回答
解决方式:
打开hbuilder中的工具 -> 设置 -> 插件设置,进入jsbeautifyrc.js,更改里面的"brace_style"选项添加"preserve-inline"值即可
options: {
"indent_size": "1",
"indent_char": "\t",
"indent_with_tabs": false, //使用tab缩进
"eol": "\r\n", //行结束符
"end_with_newline": false, //使用换行结束输出
"indent_level": 0, //起始代码缩进数
"preserve_newlines": true, //保留空行
"max_preserve_newlines": null, //最大连续保留换行符个数。比如设为2,则会将2行以上的空行删除为只保留1行
"space_in_paren": false, //括弧添加空格 示例 f( a, b )
"space_in_empty_paren": false, //函数的括弧内没有参数时插入空格 示例 f( )
"jslint_happy": false, //启用jslint-strict模式
"space_after_anon_function": false, //匿名函数的括号前加空格
"brace_style": "collapse,preserve-inline", //代码样式,可选值 [collapse|expand|end-expand|none][,preserve-inline] [collapse,preserve-inline]"折叠","扩展","最终扩展","无","折叠,保留内联","扩展,保留内联","最终扩展,保留内联",或"无,保留内联"
"unindent_chained_methods": false, //不缩进链式方法调用
"break_chained_methods": false, //在随后的行中断开链式方法调用
"keep_array_indentation": false, //保持数组缩进
"unescape_strings": false, //使用xNN符号编码解码可显示的字符
"wrap_line_length": 12000,
"e4x": false, //支持jsx
"comma_first": false, //把逗号放在新行开头,而不是结尾
"operator_position": "before-newline",
"unformatted": ["wbr"],
"html": {
"indent_handlebars": true,
"indent_inner_html": true,
"indent-scripts": "normal", //[keep|separate|normal]
"extra_liners": [] ,//配置标签列表,需要在这些标签前面额外加一空白行
"wrap_attributes": "auto",
}
顺带把vscode中格式化自动换行的问题,也记录一下
第一步:vscode 选择 文件->首选项->设置
第二步:搜索vetur.format.defaultFormatterOptions
第三步:点击settings.json
第四步:添加如下设置
"vetur.format.defaultFormatterOptions": {
"js-beautify-html": {
"wrap_attributes": "aligned-multiple"
// 对属性进行换行。
// - auto: 仅在超出行长度时才对属性进行换行。
// - force: 对除第一个属性外的其他每个属性进行换行。
// - force-aligned: 对除第一个属性外的其他每个属性进行换行,并保持对齐。
// - force-expand-multiline: 对每个属性进行换行。
// - aligned-multiple: 当超出折行长度时,将属性进行垂直对齐。
},
"prettyhtml": {
"printWidth": 500, // 这个值默认是100,可以自己设置,设置小了,也会自动换行,大了,有可能是一行
"singleQuote": false,
"wrapAttributes": false,
"sortAttributes": false
}
},
"vetur.format.defaultFormatter.html": "js-beautify-html",
"vetur.format.defaultFormatter.js": "vscode-typescript",
第五步:ctrl + s 保存 即可