这是一篇文章,您可以使用代码格式化程序的的
问题,因此,我将解释解决这些问题的设置。
出现的工具
- SwiftFormat:一种自动格式化源代码的工具
- SwiftLint:一种检查源代码的工具
试图通过利用这两个工具来有效地统一源代码的样式。
每个格式都很好的不同格式/样式的问题![:fearful: :fearful:]()
但是,由于这两个工具是由第三方而非Apple独立开发的,因此以下几点与默认设置不符。
- Xcode生成的源代码格式
- 由SwiftFormat格式化的格式
- SwiftLint发出警告的条件
关于源代码的格式和样式,很难说哪个是正确的答案或出色的答案,但为了进行该项目,应将其统一为一个。在这里,由Xcode生成的代码样式是绝对的上帝的政策,我们将以尽可能与Xcode的格式/样式匹配的方式进行调整和设置。
使用Swift格式自动格式化代码
nicklockwood / SwiftFormat
安装
建议使用
1 | pod 'SwiftFormat/CLI' |
您可以使用
HomeBrew 安装它,如果快速尝试它会更容易,但是如果您想通过稍后描述的方法"在构建/测试时自动格式化",
用x4>安装。 (因为您只需执行
如果使用
1 2 | cd プロジェクトのルートディレクトリ Pods/SwiftFormat/CommandLineTool/swiftformat --version |
基本用法(从命令行尝试使用)
在
1 | Pods/SwiftFormat/CommandLineTool/swiftformat SwiftFormatExp/AppDelegate.swift |
AppDelegate.swift(格式化之前)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? private var someVar:String?=nil func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { self.someVar="abc" return true } } |
AppDelegate.swift(格式化后)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? private var someVar: String? func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { someVar = "abc" return true } } |
在这种情况下,删除不必要的
但是,如果使用默认设置进行格式化(未指定选项),则
1 | Pods/SwiftFormat/CommandLineTool/swiftformat SwiftFormatExp/AppDelegate.swift --trimwhitespace nonblank-lines --stripunusedargs closure-only --disable strongOutlets,trailingCommas |
就我而言,我尝试指定这样的选项。
<表格>
tr>
header>
<身体>
tr>
tr>
tr>
tbody>
table>
如果指定这些选项并执行格式,将如下所示,并且方法的参数名称将正确保留。
AppDelegate.swift(可选版本)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? private var someVar: String? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { someVar = "abc" return true } } |
如果要适应整个
项目,请使用
1 | Pods/SwiftFormat/CommandLineTool/swiftformat . --exclude Carthage,Pods --trimwhitespace nonblank-lines --stripunusedargs closure-only --disable strongOutlets,trailingCommas |
在构建/测试时自动格式化
您可以每次手动运行
代码格式,但是让我们创建一种机制来防止您意外忘记格式并提交到存储库。
以下时序可以视为自动运行代码格式的时序。
<表格>
tr>
header>
<身体>
tr>
tr>
tr>
tr>
tbody>
table>
*在
每种都有优点和缺点,但是失去上面编辑器的舒适性是非常有压力的,因此,如果您是在执行测试的前提下进行开发的,建议您在格式化时进行格式化测试执行。
将
在添加的脚本阶段中编写
1 | "${PODS_ROOT}/SwiftFormat/CommandLineTool/swiftformat" . --exclude Carthage,Pods --trimwhitespace nonblank-lines --stripunusedargs closure-only --disable strongOutlets,trailingCommas |
如果安装了
CocoaPods,则可以使用
完成此操作后,让我们在适当的源代码中使用不必要的缩进和空格来运行测试。如果在该时间格式化源代码,则
检查SwiftLint问题
领域/ SwiftLint
安装
这也可以与HomeBrew或CocoaPods一起安装,但是让我们与
1 | pod 'SwiftLint' |
如果还与
1 2 | cd プロジェクトのルートディレクトリ Pods/SwiftLint/swiftlint version |
基本用法(从命令行尝试使用)
在
1 2 | cd プロジェクトのルートディレクトリ Pods/SwiftLint/swiftlint |
执行时,将输出问题部分。
1 2 3 4 5 6 7 8 9 10 11 12 13 | Linting Swift files in current working directory Linting 'AppDelegate.swift' (1/3) Linting 'ViewController.swift' (2/3) Linting 'SwiftFormatExpTests.swift' (3/3) AppDelegate.swift:6: warning: Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) AppDelegate.swift:8: warning: Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) AppDelegate.swift:11: warning: Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) AppDelegate.swift:9: warning: Line Length Violation: Line should be 120 characters or less: currently 144 characters (line_length) ViewController.swift:16: warning: Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) SwiftFormatExpTests.swift:16: warning: Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) SwiftFormatExpTests.swift:21: warning: Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) SwiftFormatExpTests.swift:26: warning: Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) Done linting! Found 8 violations, 0 serious in 3 files. |
在生成时检查
让我们在构建时在
将
确保执行与
1 | "${PODS_ROOT}/SwiftLint/swiftlint" |
如果将每次构建和每次保存都设置为执行
如果在此状态下使用Xcode进行构建,则SwiftLint的结果将显示为警告。
调整设置
但是,如果这也是默认设置,则会警告
同样,由Xcode生成的代码的样式绝对是正确的,我们将设置一些规则来减轻不必要的警告。同时,在检查中排除了
在
.swiftlint.yml
1 2 3 4 5 6 | excluded: - Carthage - Pods disabled_rules: - line_length - trailing_whitespace |
上方的设置禁用
.swiftlint.yml
1 2 3 4 5 6 | excluded: - Carthage - Pods disabled_rules: - trailing_whitespace line_length: 150 |
概括
借助