Sublime JSFormat:配置为不自动格式化JSON

时间:2023-01-14 10:32:10

I have been looking around the web for a good Sublime (3) package to use to auto-format my source code in various languages, namely JavaScript. I came across this SOF post (Sublime Text 2: Auto fix indentation for javascript?) and thus decided to give JSFormat a try. So far, it seems to work pretty good...except for when it handles JSON objects in the JS code. For example, let's say that I have a function like this:

我一直在网上寻找一个好的Sublime(3)软件包,用于自动格式化各种语言的源代码,即JavaScript。我遇到了这篇SOF帖子(Sublime Text 2:自动修复javascript的缩进?)因此决定尝试JSFormat。到目前为止,它似乎工作得很好......除了它处理JS代码中的JSON对象。例如,假设我有这样的函数:

function foo() {
  return {name: 'Dave', score: 1000};
}

It returns a JavaScript object in JSON format, prettu much a hash object. I like writing such objects in one line because it is simple and easy to read, especially since it is just a small, ad-hoc object. But, if I were to format this with JSFormat, my function would now look like this:

它返回一个JSON格式的JavaScript对象,它是一个哈希对象。我喜欢在一行中编写这样的对象,因为它简单易读,特别是因为它只是一个小的特殊对象。但是,如果我用JSFormat格式化它,我的函数现在看起来像这样:

function foo() {
  return {
    name: 'Dave',
    score: 1000
  };
}

Maybe this is just me, but I really don't like representing such simple JSON objects in multiple lines. Yes, normally JavaScript code that requires braces should have its contents on a separate lines from the braces, such as functions, if statements and loops. Maybe if the JSON object was a long object that contained functions inside of it, such as a jQuery Ajax class, then it makes sense to separate the attributes onto multiple lines.

也许这只是我,但我真的不喜欢在多行中表示这样简单的JSON对象。是的,通常需要大括号的JavaScript代码应该将其内容放在与大括号不同的行上,例如函数,if语句和循环。也许如果JSON对象是一个包含其中的函数的长对象,例如jQuery Ajax类,那么将属性分成多行是有意义的。

Nonetheless, regardless whether my points about the braces makes sense, I know that JSFormat supports configuration and perhaps there is a way to configure JSFormat to not separate the attributes of a JSON object into multiple lines, if it is not desired. Any ideas?

尽管如此,无论我对括号的看法是否有意义,我都知道JSFormat支持配置,并且可能有一种方法可以配置JSFormat,以便在不需要的情况下不将JSON对象的属性分成多行。有任何想法吗?

1 个解决方案

#1


6  

Sorry for the bad news, but...

JSFormat uses js-beautify, which does not support single-line function definitions. Everything is broken into "beautified" lines, making it "more readable."

JSFormat使用js-beautify,它不支持单行函数定义。一切都被打破成“美化”的线条,使其“更具可读性”。

Look at the example given for js-beautify... the example itself is of a single-line function definition. There is no way to distinguish single-line function definitions you do want preserved from those you don't.

看看为js-beautify给出的例子......示例本身是单行函数定义。没有办法区分您想要保留的单行函数定义与您不希望保留的单行函数定义。

If you think about it, the ideal situation to use a beautifier is if you want to take minified code and make it readable... That's just one long line of code too.

如果你考虑一下,使用美化器的理想情况是你想要采用缩小的代码并使其可读......这只是一长串代码。

I feel your pain, believe me.

相信我,我感受到你的痛苦。

#1


6  

Sorry for the bad news, but...

JSFormat uses js-beautify, which does not support single-line function definitions. Everything is broken into "beautified" lines, making it "more readable."

JSFormat使用js-beautify,它不支持单行函数定义。一切都被打破成“美化”的线条,使其“更具可读性”。

Look at the example given for js-beautify... the example itself is of a single-line function definition. There is no way to distinguish single-line function definitions you do want preserved from those you don't.

看看为js-beautify给出的例子......示例本身是单行函数定义。没有办法区分您想要保留的单行函数定义与您不希望保留的单行函数定义。

If you think about it, the ideal situation to use a beautifier is if you want to take minified code and make it readable... That's just one long line of code too.

如果你考虑一下,使用美化器的理想情况是你想要采用缩小的代码并使其可读......这只是一长串代码。

I feel your pain, believe me.

相信我,我感受到你的痛苦。