如何设置 Xcode 以获得正确的 javascript 格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4658296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:04:39  来源:igfitidea点击:

How to setup Xcode for proper javascript formatting?

javascriptxcode

提问by egorFiNE

Xcode3 (and newer ;) doesn't properly format Javascript when I type something like:

当我键入以下内容时,Xcode3(和更新的 ;)无法正确格式化 Javascript:

doSomething(somewhere, function(err, result) { 

It inserts a huge indentation. I tried to change Xcode formatting rules but no luck. One way or another it breaks indentations.

它插入了一个巨大的缩进。我试图更改 Xcode 格式规则,但没有成功。以一种或另一种方式打破缩进。

Is there a proper way to use Xcode for javascript development?

是否有使用 Xcode 进行 javascript 开发的正确方法?

采纳答案by Scurrying Ratatosk

A quick solution is to choose View -> Syntax Coloring -> Simple Coloring. If you are okay to edit Javascript without fancy syntax highlight, this is pretty useful.

一个快速的解决方案是选择 View -> Syntax Coloring -> Simple Coloring。如果您可以在没有花哨的语法突出显示的情况下编辑 Javascript,这非常有用。

This way only the numbers and strings are colored, and the indentation stops being so annoying.

这样只有数字和字符串被着色,缩进不再那么烦人。

回答by Claudiu

What worked fairly well for me is to switch the .js files to use C syntax coloring.

对我来说相当有效的是将 .js 文件切换为使用 C 语法着色。

In XCode 4: Editor -> Syntax Coloring -> C

在 XCode 4 中:编辑器 -> 语法着色 -> C

It still highlights numbers and strings and comments, and it indents braces in a sane way.

它仍然突出显示数字、字符串和注释,并以一种理智的方式缩进花括号。

回答by Wojtek Surowka

With some tweaking it is possible to modify the way Xcode indents JavaScript by supplying a custom xclangspec file. Xcode for formatting and syntax uses language definitions in xclangspec files kept in directory SharedFrameworks/DVTFoundation.framework/Versions/A/Resources. Since the formatting for C language works actually better for JavaScript than the Xcode original JavaScript formatting, it is possible to use some parts of definition for C in JS definition. More details you can find at http://www.conhar.com/xcode-and-javascript/.

通过一些调整,可以通过提供自定义 xclangspec 文件来修改 Xcode 缩进 JavaScript 的方式。用于格式化和语法的 Xcode 使用保存在目录 SharedFrameworks/DVTFoundation.framework/Versions/A/Resources 中的 xclangspec 文件中的语言定义。由于 C 语言的格式实际上比 Xcode 原始 JavaScript 格式更适用于 JavaScript,因此可以在 JS 定义中使用 C 的某些部分定义。您可以在http://www.conhar.com/xcode-and-javascript/ 上找到更多详细信息。

回答by Moszi

Unfortunatelly XCode has very limited formatting options. But you can use uncrustify which is pretty good. You can find some information here:
Xcode source automatic formatting
Objective-C Tidy

不幸的是,XCode 的格式选项非常有限。但是你可以使用 uncrustify,这是非常好的。您可以在这里找到一些信息:
Xcode 源代码自动格式化
Objective-C Tidy

These articles all talk about formatting objective-c code, however uncrustify can format the source code of various languages ;)

这些文章都在讨论格式化objective-c代码,但是uncrustify可以格式化各种语言的源代码;)

Hope this helps.

希望这可以帮助。

回答by cube

Uncrustify requires a .cfg file to setup which can be somewhat overwhelming. There's an alternative here

Uncrustify 需要一个 .cfg 文件来设置,这可能有点让人不知所措。这里有一个替代这里