找回密码
 立即注册
首页 业界区 业界 给Xcode使用完整版本的swiftformat

给Xcode使用完整版本的swiftformat

晚能 4 小时前

  • 给Xcode使用完整版本的swiftformat

    • 脚本配置
    • 导入到Xcode
    • 快捷键设置

脚本配置

现在最新的Xcode有集成到swiftformat,但是因为集成的不好实时更新、执行外部config,所以还是做个自动脚本来执行终端的swiftformat
  1. -- ===== 个人配置,使用前请修改 =====
  2. set swiftformatPath to "/opt/homebrew/bin/swiftformat"
  3. set configPath to "~/Desktop/id.swiftformat"
  4. -- ==================================
  5. tell application "Xcode"
  6.     set projPath to path of document of window 1
  7.     set projDir to do shell script "dirname " & quoted form of projPath
  8. end tell
  9. tell application "System Events"
  10.     tell process "Xcode"
  11.         set windowTitle to name of window 1
  12.     end tell
  13. end tell
  14. if windowTitle does not contain " — " then
  15.     display dialog "无法识别当前窗口,请确保 Xcode 中有文件打开。" buttons {"OK"} default button "OK" with icon stop
  16.     return
  17. end if
  18. set AppleScript's text item delimiters to " — "
  19. set titleParts to text items of windowTitle
  20. set AppleScript's text item delimiters to ""
  21. set fileName to text item 2 of titleParts
  22. if fileName does not end with ".swift" then
  23.     display dialog "当前文件不是 .swift 文件:" & fileName buttons {"OK"} default button "OK" with icon stop
  24.     return
  25. end if
  26. set foundPath to do shell script "find " & quoted form of projDir & " -name " & quoted form of fileName & " -type f | head -1"
  27. if foundPath is "" then
  28.     display dialog "在项目目录中找不到文件:" & fileName buttons {"OK"} default button "OK" with icon stop
  29.     return
  30. end if
  31. do shell script quoted form of swiftformatPath & " " & quoted form of foundPath & " --config " & configPath
复制代码
导入到Xcode

先确保顶部配置中:

  • 工作流程收到:「没有输入」
  • 位于:「Xcode.app」
保存好之后,一般就会可以集成到 Xcode → Services里面,如果不行可以自行复制到~/Library/Services/

快捷键设置

系统设置中的快捷键设置里面去找服务这一栏,一般藏在通用里面

设置成保存按键,可以在手动触发保存的时候顺便重新格式化

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册