脚本配置
现在最新的Xcode有集成到swiftformat,但是因为集成的不好实时更新、执行外部config,所以还是做个自动脚本来执行终端的swiftformat

-- ===== 个人配置,使用前请修改 =====
set swiftformatPath to "/opt/homebrew/bin/swiftformat"
set configPath to "~/Desktop/id.swiftformat"
-- ==================================
tell application "Xcode"
set projPath to path of document of window 1
set projDir to do shell script "dirname " & quoted form of projPath
end tell
tell application "System Events"
tell process "Xcode"
set windowTitle to name of window 1
end tell
end tell
if windowTitle does not contain " — " then
display dialog "无法识别当前窗口,请确保 Xcode 中有文件打开。" buttons {"OK"} default button "OK" with icon stop
return
end if
set AppleScript's text item delimiters to " — "
set titleParts to text items of windowTitle
set AppleScript's text item delimiters to ""
set fileName to text item 2 of titleParts
if fileName does not end with ".swift" then
display dialog "当前文件不是 .swift 文件:" & fileName buttons {"OK"} default button "OK" with icon stop
return
end if
set foundPath to do shell script "find " & quoted form of projDir & " -name " & quoted form of fileName & " -type f | head -1"
if foundPath is "" then
display dialog "在项目目录中找不到文件:" & fileName buttons {"OK"} default button "OK" with icon stop
return
end if
do shell script quoted form of swiftformatPath & " " & quoted form of foundPath & " --config " & configPath
导入到Xcode
先确保顶部配置中:
- 工作流程收到:「没有输入」
- 位于:「Xcode.app」
保存好之后,一般就会可以集成到 Xcode → Services里面,如果不行可以自行复制到~/Library/Services/
快捷键设置
系统设置中的快捷键设置里面去找服务这一栏,一般藏在通用里面
设置成保存按键,可以在手动触发保存的时候顺便重新格式化
本文来自博客园,作者:MrYu4,转载请注明原文链接:https://www.cnblogs.com/MrYU4/p/19768784/geixcode-shi-yong-wan-zheng-ban-ben-deswiftformat
来源:https://www.cnblogs.com/MrYU4/p/19768784/geixcode-shi-yong-wan-zheng-ban-ben-deswiftformat |