Vis*_*orZ 5 xcode plugins clang-format
Xcode 更新到版本 8 后。非常有用的 Alcatraz PlugIn Manager 被锁定,并且像 clang-format、突出显示所选单词出现或使用快捷方式调整字体大小等出色的实用程序都消失了。
如何重新启用 clang-format 以在源文件的任何父目录中使用模板 .clang-format 保存时格式化当前源代码文件?
您可以创建一个作为行为添加到 Xcode 8 的 shell 脚本:Xcode > Behaviours > +(创建新的)> 运行脚本:(在此处选择文件),添加快捷键,例如 Cmd+Shift+S。
该脚本要求 Xcode 保存当前文档。然后它提取其文件路径并调用 clang-format 就地格式化该文件。Clang 格式必须可用,例如通过使用brew作为包管理器来下载它并发布其路径以供命令行访问。与往常一样,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
Run Code Online (Sandbox Code Playgroud)
请将路径 /usr/local/bin 替换为 clang 格式可执行文件所在的路径。
快乐编码!
mapbox /XcodeClangFormat扩展看起来是一种让 clang 格式与 Xcode8 一起工作的有前途的方法。
不幸的是,由于源代码编辑器扩展的限制,您只能为所有项目指定一个 .clang-format 文件。“保存时格式化”也不可用。
| 归档时间: |
|
| 查看次数: |
8498 次 |
| 最近记录: |