在 Xcode 中修剪尾随空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1390329/
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
Trim trailing spaces in Xcode
提问by Alexander Gladysh
Is there a way to force Xcode to trim trailing whitespaces when I save file?
当我保存文件时,有没有办法强制 Xcode 修剪尾随空格?
I'm using version 3.1.3 if that matters.
如果这很重要,我正在使用版本 3.1.3。
采纳答案by Darren
You can create a script and bind it to a keyboard shortcut:
您可以创建一个脚本并将其绑定到键盘快捷键:
- Select Scripts Menu > Edit User Scripts...
- Press the + button and select New Shell Script
- Give it a name like "Strip Trailing Spaces", and give it a shortcut like ??R.
- Set Input to "Selection" and Output to "Replace Selection"
- 选择脚本菜单 > 编辑用户脚本...
- 按 + 按钮并选择New Shell Script
- 给它起一个像“Strip Trailing Spaces”这样的名字,并给它一个像??R这样的快捷方式。
- 将输入设置为“选择”并将输出设置为“替换选择”
Then enter the following script:
然后输入以下脚本:
#!/usr/bin/perl
while (<>) {
s/\s+$//;
print "$_\n";
}
回答by Martin Stolz
Starting from Xcode 4.4 whitespaces will be trimmed automatically by default, unless the line is all whitespace. You can also activate Including whitespace-only lines
to fix this, which is not active by default.
从 Xcode 4.4 开始,默认情况下将自动修剪空白,除非该行全是空白。您也可以激活Including whitespace-only lines
来解决这个问题,默认情况下它是不活动的。
Go to Xcode > Preferences > Text Editing > While editing
去 Xcode > Preferences > Text Editing > While editing
回答by Maxime
I'm using the Google Toolbox For Mac Xcode Plugin, it adds a "Correct whitespace on save" parameter that trim trailing whitespace on save. I missed that a lot from emacs.
我正在使用Google Toolbox For Mac Xcode Plugin,它添加了一个“保存时正确的空格”参数,在保存时修剪尾随空格。我从 emacs 中错过了很多。
回答by moollaza
For Xcode 8, I installed the swimatXcode plug-in, for formatting Swift code, which removed all trailing spaces and whitespace-only lines.
对于 Xcode 8,我安装了swimatXcode 插件,用于格式化 Swift 代码,它删除了所有尾随空格和纯空格行。
Installation Methods
安装方法
Install via homebrew-cask:
brew cask install swimat
Download the App directly:
https://github.com/Jintin/Swimat/releases/download/v1.3.5/Swimat.zipClone
extension
branch and archive to Mac App.
通过 homebrew-cask 安装:
brew cask install swimat
直接下载App:https:
//github.com/Jintin/Swimat/releases/download/v1.3.5/Swimat.zip克隆
extension
分支并存档到 Mac App。
Usage
用法
Once installed, you can run Swimat in Xcode via Editor -> Swimat -> Format
.
安装后,您可以通过 Xcode 在 Xcode 中运行 Swimat Editor -> Swimat -> Format
。
回答by gregschlom
See here for Xcode4: http://www.wezm.net/technical/2011/08/strip-trailing-whitespace-xcode-4/
在这里查看 Xcode4:http: //www.wezm.net/technical/2011/08/strip-trailing-whitespace-xcode-4/
Cool, Google toolbox for Mac now adds a "trim whitespace" option for Xcode4.
酷,Mac 版 Google 工具箱现在为 Xcode4 添加了“修剪空白”选项。
http://code.google.com/p/google-toolbox-for-mac/downloads/list
http://code.google.com/p/google-toolbox-for-mac/downloads/list
Thanks you, Google!
谢谢你,谷歌!
回答by Nikolai Ruhe
This is not possible in Xcode 3.2
这在 Xcode 3.2 中是不可能的
Edit:
编辑:
I answered this question so briefly because there's no way to do this properly.
我这么简短地回答了这个问题,因为没有办法正确地做到这一点。
Of course, since it's software, you can do anything: Starting with Input Manager hacks or other ways of code injection to system wide keyboard interception, you can alter your local system to do anything anytime. You could set up an Applescript folder action (arrgh) or use a launch demon and the FSEvents facility to watch your source code files.
当然,因为它是软件,你可以做任何事情:从输入管理器黑客或其他代码注入方式开始到系统范围的键盘拦截,你可以随时改变本地系统来做任何事情。您可以设置 Applescript 文件夹操作 (arrgh) 或使用启动恶魔和 FSEvents 工具来查看源代码文件。
You can also add a couple of scripts to Xcode (user scripts in the menu, script phases in targets, custom Actions in the organizer, there's even the very unknown possibility a startup script), but all these solutions are flawed, since it involves the user or custom setup on the user's machine.
您还可以向 Xcode 添加几个脚本(菜单中的用户脚本、目标中的脚本阶段、组织器中的自定义操作,甚至还有一个非常未知的启动脚本),但是所有这些解决方案都有缺陷,因为它涉及用户或用户机器上的自定义设置。
I'm not aware of a solution which simply works after checking out a project from SCM. I believe that there's need for this and similar customization scripts, so I filed a bug (radar 7203835, "Feature: more user script triggers in Xcode workflow"). I did not receive any feedback yet.
我不知道在从 SCM 签出一个项目后可以简单地工作的解决方案。我相信需要这个和类似的自定义脚本,所以我提交了一个错误(雷达 7203835,“功能:Xcode 工作流中的更多用户脚本触发器”)。我还没有收到任何反馈。
Here's the full text of the radar entry:
这是雷达条目的全文:
It would be useful to have more places to run scripts in Xcode.
Examples:
Pre build scripts
Pre build scripts could be used to build prerequisites like *.xcconfig files or config.h headers. This is not possible with a "Run Script Build phases", since dependency tracking takes place before any build phase is triggered.Post build scripts
Similar to above, but running after the build finished (including code signing etc). Useful for additional packaging, validity checking etc.Pre/Post SCM Commit scripts.
To check project integrity.Pre/Post File Save Script.
To check/alter a file before saving. E.g. run cody beautifiersCustom project actions.
I'm aware of the organizer's ability to define arbitrary actions. But this is a per user feature (not part of the project). I'd like to define actions like build or clean that show up in the build menu and that are part of a project.
在 Xcode 中有更多地方可以运行脚本会很有用。
例子:
预构建脚本
预构建脚本可用于构建先决条件,如 *.xcconfig 文件或 config.h 头文件。这对于“运行脚本构建阶段”是不可能的,因为在触发任何构建阶段之前会进行依赖项跟踪。后构建脚本
与上面类似,但在构建完成后运行(包括代码签名等)。用于附加包装、有效性检查等。前/后 SCM 提交脚本。
检查项目完整性。前/后文件保存脚本。
在保存之前检查/更改文件。例如运行 cody 美化剂自定义项目操作。
我知道组织者有能力定义任意行为。但这是每个用户的功能(不是项目的一部分)。我想定义显示在构建菜单中并且是项目一部分的构建或清理等操作。
回答by H2zee
The best and easy way without using scripts, hacks and much more. Just go to Find And Replace The best and easy way without using scripts, hacks and much more. Just go to Find And Replace and press "alt/option + space" and press "space" button in the replace bar and then click on replace all. It will replace the whitespaces with normal spaces and the warning/ error will be gone !!
不使用脚本、黑客等的最好和简单的方法。只需转到查找和替换最佳且简单的方法,无需使用脚本、黑客等。只需转到“查找和替换”并按“alt/option + space”,然后按替换栏中的“space”按钮,然后单击全部替换。它将用普通空格替换空格,警告/错误将消失!!