在将Xcode更新至8版本之后,非常实用的Alcatraz插件管理器被锁定,像clang-format、高亮选定的单词出现次数或使用快捷键调整字体大小等超级工具都消失了。
我该如何重新启用clang-format来在保存时使用任何源文件的父目录中的模板.clang-format格式化我的当前源代码文件?
在将Xcode更新至8版本之后,非常实用的Alcatraz插件管理器被锁定,像clang-format、高亮选定的单词出现次数或使用快捷键调整字体大小等超级工具都消失了。
我该如何重新启用clang-format来在保存时使用任何源文件的父目录中的模板.clang-format格式化我的当前源代码文件?
该脚本要求Xcode保存当前文档。然后提取它的文件路径并调用clang-format以就地格式化该文件。可以使用包管理器(例如 brew )下载Clang-format,并发布其路径以进行命令行访问。通常,由clang-format使用的样式指南必须具有名称 .clang-format ,并且必须位于源文件的任何父文件夹中。
以下是脚本:
#!/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 "$0")
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
不幸的是,您的小脚本经常无法在保存时更新Xcode中的格式化文件,因为它停止侦听文件更新。增加脚本中的睡眠持续时间并不会使其更可靠,并且会为常见的文件保存和文件格式化操作引入大量等待时间。
在您的情况下,我所做的是备份我的 Mac 并将 macOS 和 Xcode 恢复到最后一个版本,在该版本中 Alcatraz 的所有生产力插件都能正常工作。这提高了我的生产力。
看起来在 Xcode 8+ 中,当你取消签名 Alcatraz 插件时,它们似乎可以重新工作了。由于我没有试过这个方法,所以我只能指向该资源:
请查看该 Github 页面 ClangFormat-Xcode 上的标题 安装。
clang-format
按钮? - Zohar81