如何获取 Xcode 8 的 clang 格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39747415/
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
How to get clang format for Xcode 8?
提问by VisorZ
After Xcode update to version 8. The very useful Alcatraz PlugIn Manager is locked out and superb utilities like clang-format, or highlight selected word occurrences, or resize the font by use of a shortcut are gone.
在 Xcode 更新到版本 8 之后。非常有用的 Alcatraz PlugIn Manager 被锁定,并且像 clang-format、突出显示选定的单词出现或通过使用快捷方式调整字体大小等出色的实用程序都消失了。
How can I reenable clang-format to format my current source code file on save with a template .clang-format in any parent directory of the source file?
如何在源文件的任何父目录中使用模板 .clang-format 重新启用 clang-format 以格式化我当前的源代码文件?
采纳答案by Phlibbo
The mapbox/XcodeClangFormatextension looks like a promising way to get clang format working with Xcode8.
该mapbox / XcodeClangFormat扩展看起来像一个有前途的方式来获得铛格式的工作与Xcode8。
Due to the limitations of source editor extensions, unfortunately you can only specify one .clang-format file for all your projects. "Format on save" also is not available.
由于源代码编辑器扩展的限制,很遗憾,您只能为所有项目指定一个 .clang 格式的文件。“保存时格式化”也不可用。
回答by VisorZ
You could create a shell script that is added to Xcode 8 as a behavior: Xcode > Behaviors > +(to create new one) > Run script: (select file here), add shortcut like Cmd+Shift+S.
您可以创建一个作为行为添加到 Xcode 8 的 shell 脚本:Xcode > Behaviors > +(to create new one) > Run script: (select file here),添加像 Cmd+Shift+S 这样的快捷方式。
The script asks Xcode to save the current document. Then it extracts its filepath and calls clang-format to format that file in-place. Clang-format has to be available e.g. by using brewas the package manager to download it and having its path published for command line access. As usual the style guide used by clang-format must have the name .clang-formatand must be in any parent folder of the source file.
该脚本要求 Xcode 保存当前文档。然后它提取其文件路径并调用 clang-format 就地格式化该文件。Clang 格式必须可用,例如通过使用brew作为包管理器来下载它并发布其路径以供命令行访问。像往常一样,clang-format 使用的样式指南必须具有名称.clang-format并且必须位于源文件的任何父文件夹中。
Here is the script:
这是脚本:
#!/bin/bash
CDP=$(osascript -e '
tell application "Xcode"
activate
tell application "System Events" to keystroke "s" using {command down}
--wait for Xcode to remove edited flag from filename
delay 0.3
set last_word_in_main_window to (word -1 of (get name of window 1))
set current_document to document 1 whose name ends with last_word_in_main_window
set current_document_path to path of current_document
--CDP is assigned last set value: current_document_path
end tell ')
LOGPATH=$(dirname "##代码##")
LOGNAME=formatWithClangLog.txt
echo "Filepath: ${CDP}" > ${LOGPATH}/${LOGNAME}
sleep 0.6 ### during save Xcode stops listening for file changes
/usr/local/bin/clang-format -style=file -i -sort-includes ${CDP} >> ${LOGPATH}/${LOGNAME} 2>&1
# EOF
Please exchange the path /usr/local/bin to the one where your clang-format executable resides.
请将路径 /usr/local/bin 交换为您的 clang 格式可执行文件所在的路径。
Happy coding!
快乐编码!
回答by Gautham
Found a viable solution in this blog - code-beautifier-in-xcode
在此博客中找到了一个可行的解决方案 - code-beautifier-in-xcode
Basically, we can have clang-formatrunning as a service by automator and invoke it through Xcode whenever we need to format the code. Refer the blog for more details.
基本上,我们可以通过自动程序将clang-format作为服务运行,并在需要格式化代码时通过 Xcode 调用它。有关更多详细信息,请参阅博客。
回答by VisorZ
It looks like Alcatraz plug-ins get be back to work in Xcode 8+ when unsigning them. Because I am not in the situation to try that, I can only point you to that resource:
看起来 Alcatraz 插件在取消签名后可以在 Xcode 8+ 中恢复工作。因为我无法尝试这样做,所以我只能向您指出该资源:
Examine the header Installationon that github page ClangFormat-Xcode.
检查该github 页面ClangFormat-Xcode上的标题Installation。
回答by VisorZ
Unfortunately your little script often does not update the formatted file in Xcode because it stops listening to file updates when saving. Increasing the sleep durations in the script does not make it more reliable and introduces a lot of waiting time for the common file-save & file-format action.
不幸的是,您的小脚本通常不会更新 Xcode 中的格式化文件,因为它在保存时停止侦听文件更新。增加脚本中的睡眠持续时间并不会使其更可靠,并且会为常见的文件保存和文件格式操作引入大量等待时间。
What I did in your situation was to get my mac backup and restore macOS and Xcode to the last version where all the productivity plugins from Alcatraz work fine again. This boosted my productivity.
在您的情况下,我所做的是获取我的 mac 备份并将 macOS 和 Xcode 恢复到最后一个版本,其中来自 Alcatraz 的所有生产力插件再次正常工作。这提高了我的工作效率。