xcode Xcode源码自动格式化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1573968/
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-09 03:11:30  来源:igfitidea点击:

Xcode source automatic formatting

xcodeformattingkeyboard-shortcuts

提问by jslatts

As a C# developer, I have become highly dependent on the automatic formatting in Visual Studio 2008. Specifically, I will use the CTRL+ K, Dkeyboard shortcut to force things back into shape after my sloppy implementation.

作为一名 C# 开发人员,我已经高度依赖 Visual Studio 2008 中的自动格式化。具体来说,我将使用CTRL+ KD键盘快捷键在我草率的实施后强制恢复原状。

I am now trying to learn Objective-C and am missing certain features in Xcode, but probably none are quite as painful as the formatting shortcut. My Google searches have yielded nothing built in, though it seems there are some hacks. Am I missing something or does this feature not exist natively in Xcode?

我现在正在尝试学习 Objective-C 并且在 Xcode 中缺少某些功能,但可能没有一个像格式化快捷方式那样痛苦。我的谷歌搜索没有产生任何内置的东西,尽管似乎有一些黑客。我是否遗漏了什么,或者这个功能在 Xcode 中本身不存在?

采纳答案by Marc W

Unfortunately, Xcode doesn't have anything nearly as extensive as VS or Jalopy for Eclipse available. There are SOME disparate features, such as Structure > Re-Indentas well as the auto-formatting used when you paste code into your source file. I am totally with you, though; there definitely shouldbe something in there to help with formatting issues.

不幸的是,Xcode 没有任何可用的 VS 或 Jalopy for Eclipse 那样广泛。有一些不同的功能,例如将Structure > Re-Indent代码粘贴到源文件时使用的自动格式化。不过,我完全支持你;肯定应该有一些东西可以帮助解决格式问题。

回答by Ken

That's Ctrl+ i.

那是Ctrl+ i

Or for low-tech, cut and then paste. It'll reformat on paste.

或者对于低技术,剪切然后粘贴。它将在粘贴时重新格式化。

回答by matm

I'd like to recommend two options worth considering. Both quite new and evolving.

我想推荐两个值得考虑的选项。两者都非常新且不断发展。

ClangFormat-Xcode(free) - on each cmd+s file is reformatted to specific style and saved, easy to deploy within team

ClangFormat-Xcode(免费) - 在每个 cmd+s 文件被重新格式化为特定样式并保存,易于在团队中部署

An Xcode plug-in to format your code using Clang's format tools, by @travisjeffery.

With clang-format you can use Clang to format your code to styles such as LLVM, Google, Chromium, Mozilla, WebKit, or your own configuration.

一个 Xcode 插件,用于使用 Clang 的格式工具格式化您的代码,作者为 @travisjeffery。

使用 clang-format,您可以使用 Clang 将代码格式化为 LLVM、Google、Chromium、Mozilla、WebKit 或您自己的配置等样式。

Objective-Clean(paid, didn't try it yet) - app raising build errors if predefined style rules are violated - possibly quite hard to use within the team, so I didn't try it out.

Objective-Clean(付费,尚未尝试)- 如果违反预定义的样式规则,应用程序会引发构建错误- 可能很难在团队中使用,所以我没有尝试。

With very minimal setup, you can get Xcode to use our App to enforce your rules. If you are ever caught violating one of your rules, Xcode will throw a build error and take you right to the offending line.

只需极少的设置,您就可以让 Xcode 使用我们的应用程序来执行您的规则。如果您被发现违反了其中一项规则,Xcode 将抛出构建错误并将您直接带到违规行。

回答by Nguyen Minh Binh

In xcode, you can use this shortcut to format (re-indent) your source code

在 xcode 中,您可以使用此快捷方式来格式化(重新缩进)您的源代码

  • Command+ Ato select all source codes

  • Ctrl+ Ito re-indent

  • Command+A选择所有源代码

  • Ctrl+I重新缩进

Hope this helps.

希望这可以帮助。

回答by geowar

My personal fav PrettyC wantabe is uncrustify: http://uncrustify.sourceforge.net/. It's got a few billion options however so I also suggest you download UniversalIndentGUI_macx, (also on sourceforge) a GUI someone wrote to help set the options the way you like them.

我个人最喜欢的 PrettyC Wantabe 是 uncrustify:http://uncrustify.sourceforge.net/ 。但是它有几十亿个选项,所以我还建议您下载 UniversalIndentGUI_macx,(也在 sourceforge 上)某人编写的 GUI 可以帮助您按照您喜欢的方式设置选项。

You can then add this custom user script to uncrustify the selected text:

然后,您可以添加此自定义用户脚本来取消选中所选文本:

#! /bin/sh
#
# uncrustify!
echo -n "%%%{PBXSelection}%%%"
/usr/local/bin/uncrustify -q -c /usr/local/share/uncrustify/geo_uncrustify.cfg -l oc+ <&0
echo -n "%%%{PBXSelection}%%%"

回答by Pawe? Walaszek

You can use Command+ Ato select all content and next Ctrl+ Ito format the selected content.

您可以使用Command+A选择所有内容,然后使用下一个Ctrl+I来格式化所选内容。

回答by Jintin

I also feel xcode should have this function. So I made an extension to do it: Swimat

我也觉得xcode应该有这个功能。所以我做了一个扩展来做到这一点:Swimat

Simple install by brew cask install swimatpreview

简单安装 brew cask install swimat预览

You can give it a try, see https://github.com/Jintin/Swimatfor more information.

您可以尝试一下,更多信息请参见https://github.com/Jintin/Swimat

回答by yuvi

CmdA+ CtrlI

CmdA+ CtrlI

Or CmdAAnd then Right Click. Goto Structure -> Re-Indent

CmdA然后右键单击。转到结构 -> 重新缩进

回答by jazzcat

Consider buying yourself a license for AppCode, an intelligent Objective-C IDE that helps iOS/OS X developers. AppCode is fully compatible with Xcode, but goes beyond Xcode in adding powerful features.

考虑为自己购买AppCode的许可证,是一个可以帮助 iOS/OS X 开发人员的智能 Objective-C IDE。AppCode 与 Xcode 完全兼容,但在添加强大功能方面超越了 Xcode 。

AppCode an Objective-C variant of the Intellij IDEAIDE from JetBrains. They are also authors of popular ReSharperextension to Visual Studio, which main purpose from here seems like a desperate attempt to bring a touch of IDEA experience to a Microsoft product.

AppCode来自JetBrainsIntellij IDEAIDE的 Objective-C 变体。他们也是Visual Studio流行的ReSharper扩展的作者,从这里开始,其主要目的似乎是为 Microsoft 产品带来一点 IDEA 体验的绝望尝试。

AppCode is using its own code analyser which gives close-to-perfect refactoring and code navigation support. There is an ability to re-indent and completely reformat code also (although I still keep missing a couple of formatting settings in hard cases, but mostly it works well).

AppCode 使用自己的代码分析器,它提供近乎完美的重构和代码导航支持。还可以重新缩进和完全重新格式化代码(尽管在困难的情况下我仍然缺少一些格式化设置,但大多数情况下它运行良好)。

You might try the trial version, of course.

当然,您可以尝试试用版。

回答by omartin

You can also have a look at https://github.com/octo-online/Xcode-formatterwhich is a formatter based on Uncrustify and integrated into Xcode. Works like a charm.

您还可以查看https://github.com/octo-online/Xcode-formatter,这是一个基于 Uncrustify 并集成到 Xcode 中的格式化程序。奇迹般有效。