diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c847b27..e888e7f9 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Change Log +## [0.47.0](https://github.com/nicklockwood/SwiftFormat/releases/tag/0.47.0) (2020-10-14) + +- Indenting of wrapped `if`, `guard`, `while`, etc. now matches Xcode 12 behavior +- Added `--assetliterals` option for managing how image/color literal length is handled +- Added `enumNamespaces` rule to replace classes/structs that only have static members with an enum +- Added `extensionAccessControl` rule to hoist/unhoist access control modifiers inside extensions +- Added `markTypes` rule for automatically inserting `MARK:` comments between type declarations +- Added `sortedSwitchCases` rule to automatically sort grouped switch cases alphabetically +- Added ``--wrapreturntype` option to control how return type is wrapped in function declarations +- The `organizeDeclarations` rule is now also applied to extensions +- Updated `--xcodeindentation` behavior for Xcode 12 + ## [0.46.3](https://github.com/nicklockwood/SwiftFormat/releases/tag/0.46.3) (2020-09-20) - Fixed bug where `redundantType` rule corrupted assignments involving ternary expressions diff --git a/CommandLineTool/swiftformat b/CommandLineTool/swiftformat index be52eccd..acf4910c 100755 Binary files a/CommandLineTool/swiftformat and b/CommandLineTool/swiftformat differ diff --git a/EditorExtension/Application/Source/FreeTextTableCellView.swift b/EditorExtension/Application/Source/FreeTextTableCellView.swift index d03a263e..9d153f08 100644 --- a/EditorExtension/Application/Source/FreeTextTableCellView.swift +++ b/EditorExtension/Application/Source/FreeTextTableCellView.swift @@ -68,7 +68,7 @@ class FreeTextTableCellView: NSTableCellView { extension FreeTextTableCellView: NSTextFieldDelegate { override func controlTextDidChange(_ obj: Notification) { guard let textView: NSTextView = obj.userInfo!["NSFieldEditor"] as? NSTextView, - let freeText = objectValue as? UserSelectionFreeText + let freeText = objectValue as? UserSelectionFreeText else { return } diff --git a/EditorExtension/Shared/OptionsStore.swift b/EditorExtension/Shared/OptionsStore.swift index b5a8adb5..809e64f1 100644 --- a/EditorExtension/Shared/OptionsStore.swift +++ b/EditorExtension/Shared/OptionsStore.swift @@ -40,8 +40,8 @@ struct SavedOption { } } -extension SavedOption { - fileprivate init(_ rep: OptionsStore.OptionRepresentation) throws { +private extension SavedOption { + init(_ rep: OptionsStore.OptionRepresentation) throws { guard let descriptor = Descriptors.byName[rep.id] else { throw FormatError.options("Unknown option \(rep.id)") } @@ -53,8 +53,8 @@ extension SavedOption { } } -extension FormatOptions { - fileprivate init(_ rep: OptionsStore.OptionStoreRepresentation) throws { +private extension FormatOptions { + init(_ rep: OptionsStore.OptionStoreRepresentation) throws { var formatOptions = FormatOptions.default for descriptor in Descriptors.all.reversed() { // By loading formatting options in reverse, we ensure that diff --git a/EditorExtension/Shared/RulesStore.swift b/EditorExtension/Shared/RulesStore.swift index 797bf946..ed4db140 100644 --- a/EditorExtension/Shared/RulesStore.swift +++ b/EditorExtension/Shared/RulesStore.swift @@ -60,8 +60,8 @@ extension Rule: Comparable { } } -extension Rule { - fileprivate init(_ ruleRep: (String, Bool)) { +private extension Rule { + init(_ ruleRep: (String, Bool)) { self.init(name: ruleRep.0, isEnabled: ruleRep.1) } } diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/CodeResources b/EditorExtension/SwiftFormat for Xcode.app/Contents/CodeResources index 07dc20f2..456803e1 100644 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/CodeResources and b/EditorExtension/SwiftFormat for Xcode.app/Contents/CodeResources differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftAppKit.dylib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftAppKit.dylib index aa201ea9..6e93d9f5 100755 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftAppKit.dylib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftAppKit.dylib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCore.dylib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCore.dylib index 187b00bc..fe8e75ef 100755 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCore.dylib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCore.dylib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCoreData.dylib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCoreData.dylib index 94340774..fa759c87 100755 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCoreData.dylib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCoreData.dylib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCoreFoundation.dylib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCoreFoundation.dylib index a223a81f..f0561426 100755 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCoreFoundation.dylib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCoreFoundation.dylib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCoreGraphics.dylib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCoreGraphics.dylib index d37e4784..c95d9cb3 100755 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCoreGraphics.dylib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCoreGraphics.dylib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCoreImage.dylib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCoreImage.dylib index 7feb9079..cfeb0d39 100755 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCoreImage.dylib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftCoreImage.dylib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftDarwin.dylib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftDarwin.dylib index 904df330..f3c28aea 100755 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftDarwin.dylib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftDarwin.dylib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftDispatch.dylib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftDispatch.dylib index ac5bd09d..e37b29d1 100755 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftDispatch.dylib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftDispatch.dylib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftFoundation.dylib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftFoundation.dylib index 205ada1a..d842d4af 100755 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftFoundation.dylib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftFoundation.dylib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftIOKit.dylib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftIOKit.dylib index 76c3139d..9dce016b 100755 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftIOKit.dylib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftIOKit.dylib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftMetal.dylib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftMetal.dylib index 11cd8352..a644a54b 100755 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftMetal.dylib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftMetal.dylib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftObjectiveC.dylib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftObjectiveC.dylib index 3a7a9d1c..7bfa7867 100755 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftObjectiveC.dylib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftObjectiveC.dylib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftQuartzCore.dylib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftQuartzCore.dylib index f074d09f..9e062353 100755 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftQuartzCore.dylib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftQuartzCore.dylib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftXPC.dylib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftXPC.dylib index df126d1a..9a2eb570 100755 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftXPC.dylib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftXPC.dylib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftos.dylib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftos.dylib index 9de7ae82..0540d6d7 100755 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftos.dylib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Frameworks/libswiftos.dylib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Info.plist b/EditorExtension/SwiftFormat for Xcode.app/Contents/Info.plist index 230e5560..2d757b74 100644 --- a/EditorExtension/SwiftFormat for Xcode.app/Contents/Info.plist +++ b/EditorExtension/SwiftFormat for Xcode.app/Contents/Info.plist @@ -3,7 +3,7 @@ BuildMachineOSBuild - 19G2021 + 19H2 CFBundleDevelopmentRegion en CFBundleDocumentTypes @@ -36,7 +36,7 @@ CFBundlePackageType APPL CFBundleShortVersionString - 0.46.3 + 0.47.0 CFBundleSupportedPlatforms MacOSX @@ -62,7 +62,7 @@ LSApplicationCategoryType public.app-category.developer-tools LSMinimumSystemVersion - 10.11 + 10.14 NSHumanReadableCopyright Copyright © 2016 Nick Lockwood. All rights reserved. NSMainStoryboardFile diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/MacOS/SwiftFormat for Xcode b/EditorExtension/SwiftFormat for Xcode.app/Contents/MacOS/SwiftFormat for Xcode index 66914948..56ff3bb1 100755 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/MacOS/SwiftFormat for Xcode and b/EditorExtension/SwiftFormat for Xcode.app/Contents/MacOS/SwiftFormat for Xcode differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/PlugIns/SwiftFormat.appex/Contents/Frameworks/XcodeKit.framework/Versions/A/XcodeKit b/EditorExtension/SwiftFormat for Xcode.app/Contents/PlugIns/SwiftFormat.appex/Contents/Frameworks/XcodeKit.framework/Versions/A/XcodeKit index 6ef1baca..1b4c65f9 100755 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/PlugIns/SwiftFormat.appex/Contents/Frameworks/XcodeKit.framework/Versions/A/XcodeKit and b/EditorExtension/SwiftFormat for Xcode.app/Contents/PlugIns/SwiftFormat.appex/Contents/Frameworks/XcodeKit.framework/Versions/A/XcodeKit differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/PlugIns/SwiftFormat.appex/Contents/Info.plist b/EditorExtension/SwiftFormat for Xcode.app/Contents/PlugIns/SwiftFormat.appex/Contents/Info.plist index 018b1a85..efc501c3 100644 --- a/EditorExtension/SwiftFormat for Xcode.app/Contents/PlugIns/SwiftFormat.appex/Contents/Info.plist +++ b/EditorExtension/SwiftFormat for Xcode.app/Contents/PlugIns/SwiftFormat.appex/Contents/Info.plist @@ -3,7 +3,7 @@ BuildMachineOSBuild - 19G2021 + 19H2 CFBundleDevelopmentRegion en CFBundleExecutable @@ -41,7 +41,7 @@ DTXcodeBuild 12A7209 LSMinimumSystemVersion - 10.11 + 10.14 NSExtension NSExtensionAttributes diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/PlugIns/SwiftFormat.appex/Contents/MacOS/SwiftFormat b/EditorExtension/SwiftFormat for Xcode.app/Contents/PlugIns/SwiftFormat.appex/Contents/MacOS/SwiftFormat index 3069e9ec..d942389a 100755 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/PlugIns/SwiftFormat.appex/Contents/MacOS/SwiftFormat and b/EditorExtension/SwiftFormat for Xcode.app/Contents/PlugIns/SwiftFormat.appex/Contents/MacOS/SwiftFormat differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/AppIcon.icns b/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/AppIcon.icns index 70b706fd..ee42b740 100644 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/AppIcon.icns and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/AppIcon.icns differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Assets.car b/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Assets.car index 343e5052..1ea66cfe 100644 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Assets.car and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Assets.car differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/9ZR-Qg-hHT-view-iDF-FY-6tf.nib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/9ZR-Qg-hHT-view-iDF-FY-6tf.nib index 77cb422e..1a406316 100644 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/9ZR-Qg-hHT-view-iDF-FY-6tf.nib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/9ZR-Qg-hHT-view-iDF-FY-6tf.nib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/Info.plist b/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/Info.plist index c1140f62..a074a12d 100644 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/Info.plist and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/Info.plist differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/MainMenu.nib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/MainMenu.nib index 10b65b99..87a6e029 100644 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/MainMenu.nib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/MainMenu.nib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/MainWindowTabViewController.nib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/MainWindowTabViewController.nib index bdc9494b..f2b49f67 100644 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/MainWindowTabViewController.nib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/MainWindowTabViewController.nib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/NSWindowController-B8D-0N-5wS.nib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/NSWindowController-B8D-0N-5wS.nib index a24a055d..438fb8fd 100644 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/NSWindowController-B8D-0N-5wS.nib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/NSWindowController-B8D-0N-5wS.nib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/RulesViewController.nib b/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/RulesViewController.nib index f78090b3..690dacfe 100644 Binary files a/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/RulesViewController.nib and b/EditorExtension/SwiftFormat for Xcode.app/Contents/Resources/Base.lproj/Main.storyboardc/RulesViewController.nib differ diff --git a/EditorExtension/SwiftFormat for Xcode.app/Contents/_CodeSignature/CodeResources b/EditorExtension/SwiftFormat for Xcode.app/Contents/_CodeSignature/CodeResources index f0b2fd15..2573c270 100644 --- a/EditorExtension/SwiftFormat for Xcode.app/Contents/_CodeSignature/CodeResources +++ b/EditorExtension/SwiftFormat for Xcode.app/Contents/_CodeSignature/CodeResources @@ -6,35 +6,35 @@ Resources/AppIcon.icns - IsifGQ6u46Cp81IAOcJIcLkuxGw= + 49ta0JaEQzFhN907kv6SiH1kchU= Resources/Assets.car - DQYLd/jkRuxSrE6Ok/kK1WIbH3c= + YiheLpyp+SRLXxFU721UYx8oI6c= Resources/Base.lproj/Main.storyboardc/9ZR-Qg-hHT-view-iDF-FY-6tf.nib - PJwNXgb4aiLkgOZKY5pNFVMSGPI= + NgkuD0ekbF0F8PCXGo2XyDdQEZ4= Resources/Base.lproj/Main.storyboardc/Info.plist - BMnGqn+yx3vG3HhgYj1L+IMJwXE= + +/DJ7okKXSwZ02uqruRsLmLrVME= Resources/Base.lproj/Main.storyboardc/MainMenu.nib - 0z5SQhAJml+mZe/49kSf+ItZEEw= + lI7KbQws4PaWE18pL0llqYlccT0= Resources/Base.lproj/Main.storyboardc/MainWindowTabViewController.nib - yUeufolLhKGDGDqfc5SVyDRHM2s= + sWeCbAsRLvNYKtIy6w6ivu/7Qpk= Resources/Base.lproj/Main.storyboardc/NSWindowController-B8D-0N-5wS.nib - LIEBJbRA6qayOSDiWRqj5U0u4gI= + Zdo9GV43c2YGAWk8LB9vj4kOsAg= Resources/Base.lproj/Main.storyboardc/RulesViewController.nib - rfAECe1mswYnskltpW21hKH6NEU= + beYTI7gNKts02jpMY7lcElpKxsk= Resources/Base.lproj/Main.storyboardc/XfG-lQ-9wD-view-m2S-Jp-Qdl.nib @@ -182,105 +182,69 @@ cdhash - kToM7834srzzv6TNZRRMlyNBEJY= + Z+z+6QsyriEW62UtZKRx4w/hNm8= requirement anchor apple generic and identifier "com.charcoaldesign.SwiftFormat-for-Xcode.SourceEditorExtension" and (certificate leaf[field.1.2.840.113635.100.6.1.9] /* exists */ or certificate 1[field.1.2.840.113635.100.6.2.6] /* exists */ and certificate leaf[field.1.2.840.113635.100.6.1.13] /* exists */ and certificate leaf[subject.OU] = "8VQKF583ED") Resources/AppIcon.icns - hash - - IsifGQ6u46Cp81IAOcJIcLkuxGw= - hash2 - HhzZtyg6k6342A4NxIR25LPdwke/QvBhXVxaZPTv/yo= + uL7GZda4f/Vm3xjeFMdkmr5AHvnjhcJFkLlXkF6xsf4= Resources/Assets.car - hash - - DQYLd/jkRuxSrE6Ok/kK1WIbH3c= - hash2 - FhMDp6Rp6kAYwyARB6RKT01iGzwDEcGs5vR1R9duSdI= + GYXZ11g7/0ggejEJ1zVPKm8+O+vtpstht2WvFXURoag= Resources/Base.lproj/Main.storyboardc/9ZR-Qg-hHT-view-iDF-FY-6tf.nib - hash - - PJwNXgb4aiLkgOZKY5pNFVMSGPI= - hash2 - RyofvGzD02vsmP570BUBJf3H2iKlskFLkNM2Yqiz5iE= + SFlU7n9v5kte+wKrebcdyk34TT5YjcOgi20SozeVsAE= Resources/Base.lproj/Main.storyboardc/Info.plist - hash - - BMnGqn+yx3vG3HhgYj1L+IMJwXE= - hash2 - mfZctbb5otaIASUXlSGMkw6F/3XSJvLj7h838758854= + or34ef1CtY0IAqy8rCQrmLYSv5sn6jdShMx4sLw+9s4= Resources/Base.lproj/Main.storyboardc/MainMenu.nib - hash - - 0z5SQhAJml+mZe/49kSf+ItZEEw= - hash2 - I0QWL2U6Mi/KJrb8xHWabsuuba7PjN4HXmlXIgFZdIU= + qrLtyVyZiN2qpsNs1XdrvF9DrmXStoXqUyrcRkYL+WQ= Resources/Base.lproj/Main.storyboardc/MainWindowTabViewController.nib - hash - - yUeufolLhKGDGDqfc5SVyDRHM2s= - hash2 - O5MH3wMtEvTuxPNzVokAZkPqJ622y8tIqvKzGQFwBvM= + 0niOKWSTJYKDJD/YK454D67ykqdcOV63t7geVMDXWcg= Resources/Base.lproj/Main.storyboardc/NSWindowController-B8D-0N-5wS.nib - hash - - LIEBJbRA6qayOSDiWRqj5U0u4gI= - hash2 - 69SnBbVIaXKwhMt6LtIIXP6vk0ZxA4x9hx3kTVT9RYU= + 22rpMYPNyrK/2QRHNpkFjy8pphfvIWoc+SmS6USZBB4= Resources/Base.lproj/Main.storyboardc/RulesViewController.nib - hash - - rfAECe1mswYnskltpW21hKH6NEU= - hash2 - 4qql7sNGICUVfrzSrHX9eiNz8k8XkuAPGmpLI2LN2Ck= + LsMenOnAUxX3uVz8bsmLY81eVMjvyPE5PJrkBheicG0= Resources/Base.lproj/Main.storyboardc/XfG-lQ-9wD-view-m2S-Jp-Qdl.nib - hash - - 9QDdTtGXlGK6L/lwYL92eAU0kMk= - hash2 8/fmpfJkFkrcEOjAXPJGkiexY7vP8bksKSro2h6+1O0= diff --git a/Sources/Arguments.swift b/Sources/Arguments.swift index 53adb3cd..48c53ec0 100644 --- a/Sources/Arguments.swift +++ b/Sources/Arguments.swift @@ -131,11 +131,11 @@ func preprocessArguments(_ args: [String], _ names: [String]) throws -> [String: arg = String(arg.dropLast()) } if let existing = namedArgs[name], !existing.isEmpty, - // TODO: find a more general way to represent merge-able options - ["exclude", "unexclude", "disable", "enable", "rules"].contains(name) || - Descriptors.all.contains(where: { - $0.argumentName == name && $0.isSetType - }) + // TODO: find a more general way to represent merge-able options + ["exclude", "unexclude", "disable", "enable", "rules"].contains(name) || + Descriptors.all.contains(where: { + $0.argumentName == name && $0.isSetType + }) { namedArgs[name] = existing + "," + arg } else { @@ -250,14 +250,14 @@ func mergeArguments(_ args: [String: String], into config: [String: String]) thr var output = args // Merge excluded urls if let exclude = output["exclude"].map(parseCommaDelimitedList), - var excluded = input["exclude"].map({ Set(parseCommaDelimitedList($0)) }) + var excluded = input["exclude"].map({ Set(parseCommaDelimitedList($0)) }) { excluded.formUnion(exclude) output["exclude"] = Array(excluded).sorted().joined(separator: ",") } // Merge unexcluded urls if let unexclude = output["unexclude"].map(parseCommaDelimitedList), - var unexcluded = input["unexclude"].map({ Set(parseCommaDelimitedList($0)) }) + var unexcluded = input["unexclude"].map({ Set(parseCommaDelimitedList($0)) }) { unexcluded.formUnion(unexclude) output["unexclude"] = Array(unexcluded).sorted().joined(separator: ",") @@ -443,7 +443,7 @@ func argumentsFor(_ options: Options, excludingDefaults: Bool = false) -> [Strin // Special case for swiftVersion // TODO: find a better solution for this if descriptor.argumentName == Descriptors.swiftVersion.argumentName, - value == Version.undefined.rawValue + value == Version.undefined.rawValue { continue } diff --git a/Sources/CommandLine.swift b/Sources/CommandLine.swift index e4a8e216..2bdc7605 100644 --- a/Sources/CommandLine.swift +++ b/Sources/CommandLine.swift @@ -485,8 +485,8 @@ func processArguments(_ args: [String], in directory: String) -> ExitCode { $0.trimmingCharacters(in: .whitespacesAndNewlines) } guard (1 ... 2).contains(parts.count), - let start = parts.first.flatMap(Int.init), - let end = parts.last.flatMap(Int.init) + let start = parts.first.flatMap(Int.init), + let end = parts.last.flatMap(Int.init) else { throw FormatError.options("Unsupported --linerange value '\(arg)'") } @@ -1013,7 +1013,7 @@ func processInput(_ inputURLs: [URL], } // Save cache if outputFlags.filesChecked > 0, let cache = cache, let cacheURL = cacheURL, - let cacheDirectory = cacheDirectory + let cacheDirectory = cacheDirectory { do { let data = try JSONEncoder().encode(cache) diff --git a/Sources/Formatter.swift b/Sources/Formatter.swift index fd10ecd9..be3abfbf 100644 --- a/Sources/Formatter.swift +++ b/Sources/Formatter.swift @@ -2,7 +2,7 @@ // Formatter.swift // SwiftFormat // -// Version 0.46.3 +// Version 0.47.0 // // Created by Nick Lockwood on 12/08/2016. // Copyright 2016 Nick Lockwood diff --git a/Sources/FormattingHelpers.swift b/Sources/FormattingHelpers.swift index 0655dc67..71766537 100644 --- a/Sources/FormattingHelpers.swift +++ b/Sources/FormattingHelpers.swift @@ -86,13 +86,13 @@ extension Formatter { break case .ifMultiline: guard let openBracket = index(of: .startOfScope("{"), after: endOfFunctionScope), - let returnArrowIndex = index(of: .operator("->", .infix), after: endOfFunctionScope), - returnArrowIndex < openBracket + let returnArrowIndex = index(of: .operator("->", .infix), after: endOfFunctionScope), + returnArrowIndex < openBracket else { return } // If the return arrow isnt on its own line, wrap it if let previousNonSpaceOrComment = index(of: .nonSpaceOrComment, before: returnArrowIndex), - startOfLine(at: returnArrowIndex) < previousNonSpaceOrComment + startOfLine(at: returnArrowIndex) < previousNonSpaceOrComment { insertSpace(indentForLine(at: returnArrowIndex), at: returnArrowIndex) insertLinebreak(at: returnArrowIndex) @@ -136,7 +136,7 @@ extension Formatter { index += 1 } while let commaIndex = self.lastIndex(of: .delimiter(","), in: i + 1 ..< index), - var linebreakIndex = self.index(of: .nonSpaceOrComment, after: commaIndex) + var linebreakIndex = self.index(of: .nonSpaceOrComment, after: commaIndex) { if let index = self.index(of: .nonSpace, before: linebreakIndex) { linebreakIndex = index + 1 @@ -193,7 +193,7 @@ extension Formatter { var lastBreakIndex: Int? var index = firstArgumentIndex while let commaIndex = self.index(of: .delimiter(","), in: index ..< endOfScope), - var linebreakIndex = self.index(of: .nonSpaceOrComment, after: commaIndex) + var linebreakIndex = self.index(of: .nonSpaceOrComment, after: commaIndex) { if let index = self.index(of: .nonSpace, before: linebreakIndex) { linebreakIndex = index + 1 @@ -253,8 +253,8 @@ extension Formatter { case "(": /// Don't wrap color/image literals due to Xcode bug guard let prevToken = self.token(at: i - 1), - prevToken != .keyword("#colorLiteral"), - prevToken != .keyword("#imageLiteral") + prevToken != .keyword("#colorLiteral"), + prevToken != .keyword("#imageLiteral") else { return } @@ -294,7 +294,7 @@ extension Formatter { } if completePartialWrapping, - let firstLinebreakIndex = index(of: .linebreak, in: i + 1 ..< endOfScope) + let firstLinebreakIndex = index(of: .linebreak, in: i + 1 ..< endOfScope) { switch mode { case .beforeFirst: @@ -375,8 +375,8 @@ extension Formatter { if currentRule == FormatRules.wrap { let nextWrapIndex = indexOfNextWrap() ?? endOfLine(at: i) if nextWrapIndex > lastIndex, - maxWidth < lineLength(from: max(lastIndex, 0), upTo: nextWrapIndex), - !willWrapAtStartOfReturnType(maxWidth: maxWidth) + maxWidth < lineLength(from: max(lastIndex, 0), upTo: nextWrapIndex), + !willWrapAtStartOfReturnType(maxWidth: maxWidth) { wrapArgumentsWithoutPartialWrapping() lastIndex = nextWrapIndex @@ -413,13 +413,13 @@ extension Formatter { } if token(at: index - 1)?.isSpace == true, - token(at: index + 1)?.isSpace == true + token(at: index + 1)?.isSpace == true { // Need to remove one removeToken(at: index + 1) } else if case .startOfScope = tokens[index] { if tokenOutsideParenRequiresSpacing(at: index - 1), - tokenInsideParenRequiresSpacing(at: index + 1) + tokenInsideParenRequiresSpacing(at: index + 1) { // Need to insert one insert(.space(" "), at: index + 1) @@ -705,8 +705,8 @@ extension Formatter { // immediately follows the `func` keyword: // https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#grammar_function-name if keyword == "func", - let methodName = parser.next(.nonSpaceOrCommentOrLinebreak, after: keywordIndex), - options.lifecycleMethods.contains(methodName.string) + let methodName = parser.next(.nonSpaceOrCommentOrLinebreak, after: keywordIndex), + options.lifecycleMethods.contains(methodName.string) { return .lifecycle } @@ -744,7 +744,7 @@ extension Formatter { while searchIndex < keywordIndex { if let visibility = Visibility(rawValue: parser.tokens[searchIndex].string), - parser.next(.nonSpaceOrComment, after: searchIndex) != .startOfScope("(") + parser.next(.nonSpaceOrComment, after: searchIndex) != .startOfScope("(") { return visibility } @@ -794,10 +794,10 @@ extension Formatter { // If there is a code block at the end of the declaration that is _not_ a closure, // then this declaration has a body. if let lastClosingBraceIndex = declarationParser.index(of: .endOfScope("}"), before: declarationParser.tokens.count), - let lastOpeningBraceIndex = declarationParser.index(of: .startOfScope("{"), before: lastClosingBraceIndex), - declarationTypeTokenIndex < lastOpeningBraceIndex, - declarationTypeTokenIndex < lastClosingBraceIndex, - !declarationParser.isStartOfClosure(at: lastOpeningBraceIndex) + let lastOpeningBraceIndex = declarationParser.index(of: .startOfScope("{"), before: lastClosingBraceIndex), + declarationTypeTokenIndex < lastOpeningBraceIndex, + declarationTypeTokenIndex < lastClosingBraceIndex, + !declarationParser.isStartOfClosure(at: lastOpeningBraceIndex) { hasBody = true } else { @@ -856,8 +856,8 @@ extension Formatter { var searchIndex = parser.tokens.count - 1 while searchIndex > 0, - let token = parser.token(at: searchIndex), - token.isSpaceOrCommentOrLinebreak + let token = parser.token(at: searchIndex), + token.isSpaceOrCommentOrLinebreak { if token.isLinebreak { numberOfTrailingLinebreaks += 1 @@ -914,7 +914,7 @@ extension Formatter { let potentialSeparatorRange = commentStartIndex ..< (commentStartIndex + potentialCategorySeparator.count) guard parser.tokens.indices.contains(potentialSeparatorRange.upperBound), - let nextNonwhitespaceIndex = parser.index(of: .nonSpaceOrLinebreak, after: potentialSeparatorRange.upperBound) + let nextNonwhitespaceIndex = parser.index(of: .nonSpaceOrLinebreak, after: potentialSeparatorRange.upperBound) else { continue } // Check the edit distance of this existing comment with the potential @@ -1025,22 +1025,22 @@ extension Formatter { // Sort primarily by category if sortByCategory, - let lhsCategorySortOrder = Formatter.categoryOrdering.index(of: lhs.category), - let rhsCategorySortOrder = Formatter.categoryOrdering.index(of: rhs.category), - lhsCategorySortOrder != rhsCategorySortOrder + let lhsCategorySortOrder = Formatter.categoryOrdering.index(of: lhs.category), + let rhsCategorySortOrder = Formatter.categoryOrdering.index(of: rhs.category), + lhsCategorySortOrder != rhsCategorySortOrder { return lhsCategorySortOrder < rhsCategorySortOrder } // Within individual categories (excluding .beforeMarks), sort by the declaration type if sortByType, - lhs.category != .beforeMarks, - rhs.category != .beforeMarks, - let lhsType = lhs.type, - let rhsType = rhs.type, - let lhsTypeSortOrder = Formatter.categorySubordering.index(of: lhsType), - let rhsTypeSortOrder = Formatter.categorySubordering.index(of: rhsType), - lhsTypeSortOrder != rhsTypeSortOrder + lhs.category != .beforeMarks, + rhs.category != .beforeMarks, + let lhsType = lhs.type, + let rhsType = rhs.type, + let lhsTypeSortOrder = Formatter.categorySubordering.index(of: lhsType), + let rhsTypeSortOrder = Formatter.categorySubordering.index(of: rhsType), + lhsTypeSortOrder != rhsTypeSortOrder { return lhsTypeSortOrder < rhsTypeSortOrder } @@ -1058,7 +1058,7 @@ extension Formatter { // declarations that don't have an `init` declaration. // We have to take care to not reorder any properties (but reordering functions etc is ok!) if typeDeclaration.kind == "struct", - !typeDeclaration.body.contains(where: { $0.keyword == "init" }) + !typeDeclaration.body.contains(where: { $0.keyword == "init" }) { /// Whether or not this declaration is an instance property that can affect /// the parameters struct's synthesized memberwise initializer @@ -1083,8 +1083,8 @@ extension Formatter { var hasWillSetOrDidSetBlock = false if let bodyOpenBrace = parser.index(of: .startOfScope("{"), after: -1), - let firstBodyToken = parser.next(.nonSpaceOrCommentOrLinebreak, after: bodyOpenBrace), - firstBodyToken.string == "willSet" || firstBodyToken.string == "didSet" + let firstBodyToken = parser.next(.nonSpaceOrCommentOrLinebreak, after: bodyOpenBrace), + firstBodyToken.string == "willSet" || firstBodyToken.string == "didSet" { hasWillSetOrDidSetBlock = true } @@ -1139,7 +1139,7 @@ extension Formatter { // Build the MARK declaration, but only when there is more than one category present. if numberOfCategories > 1, - let markComment = category.markComment(from: options.categoryMarkComment) + let markComment = category.markComment(from: options.categoryMarkComment) { let firstDeclaration = sortedDeclarations[indexOfFirstDeclaration].declaration let declarationParser = Formatter(firstDeclaration.tokens) @@ -1220,14 +1220,14 @@ extension Formatter { // If there are any annotations, skip past them while startOfModifiers < indexOfKeyword, - openTokensFormatter.tokens[startOfModifiers].string.hasPrefix("@") - || openTokensFormatter.tokens[startOfModifiers].isSpaceOrCommentOrLinebreak + openTokensFormatter.tokens[startOfModifiers].string.hasPrefix("@") + || openTokensFormatter.tokens[startOfModifiers].isSpaceOrCommentOrLinebreak { startOfModifiers += 1 // Also skip through annotation bodies, like in `@available(iOS 14.0, *)` if openTokensFormatter.tokens[startOfModifiers] == .startOfScope("("), - let endOfScope = openTokensFormatter.endOfScope(at: startOfModifiers) + let endOfScope = openTokensFormatter.endOfScope(at: startOfModifiers) { startOfModifiers = endOfScope + 1 } diff --git a/Sources/Inference.swift b/Sources/Inference.swift index a29df5c0..cb48c68c 100644 --- a/Sources/Inference.swift +++ b/Sources/Inference.swift @@ -143,8 +143,8 @@ private struct Inference { var spaced = [String: Int](), unspaced = [String: Int]() formatter.forEach(.operator) { i, token in guard case let .operator(name, .infix) = token, name != ".", - let nextToken = formatter.next(.nonSpaceOrCommentOrLinebreak, after: i), - nextToken.string != ")", nextToken.string != "," + let nextToken = formatter.next(.nonSpaceOrCommentOrLinebreak, after: i), + nextToken.string != ")", nextToken.string != "," else { return } @@ -166,8 +166,8 @@ private struct Inference { ] for pair in relatedPairs { if noSpaceOperators.contains(pair.0), - !noSpaceOperators.contains(pair.1), - !operators.contains(pair.1) + !noSpaceOperators.contains(pair.1), + !operators.contains(pair.1) { noSpaceOperators.insert(pair.1) } else if noSpaceOperators.contains(pair.1), @@ -184,7 +184,7 @@ private struct Inference { var voids = 0, tuples = 0 formatter.forEach(.identifier("Void")) { i, _ in if let prevToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: i), - [.operator(".", .prefix), .operator(".", .infix), .keyword("typealias")].contains(prevToken) + [.operator(".", .prefix), .operator(".", .infix), .keyword("typealias")].contains(prevToken) { return } @@ -192,10 +192,10 @@ private struct Inference { } formatter.forEach(.startOfScope("(")) { i, _ in if let prevIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: i), - let prevToken = formatter.token(at: prevIndex), prevToken == .operator("->", .infix), - let nextIndex = formatter.index(of: .nonSpaceOrLinebreak, after: i), - let nextToken = formatter.token(at: nextIndex), nextToken.string == ")", - formatter.next(.nonSpaceOrCommentOrLinebreak, after: nextIndex) != .operator("->", .infix) + let prevToken = formatter.token(at: prevIndex), prevToken == .operator("->", .infix), + let nextIndex = formatter.index(of: .nonSpaceOrLinebreak, after: i), + let nextToken = formatter.token(at: nextIndex), nextToken.string == ")", + formatter.next(.nonSpaceOrCommentOrLinebreak, after: nextIndex) != .operator("->", .infix) { tuples += 1 } @@ -207,9 +207,9 @@ private struct Inference { var trailing = 0, noTrailing = 0 formatter.forEach(.endOfScope("]")) { i, token in guard let linebreakIndex = formatter.index(of: .nonSpaceOrComment, before: i), - case .linebreak = formatter.tokens[linebreakIndex], - let prevTokenIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: linebreakIndex + 1), - let token = formatter.token(at: prevTokenIndex) + case .linebreak = formatter.tokens[linebreakIndex], + let prevTokenIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: linebreakIndex + 1), + let token = formatter.token(at: prevTokenIndex) else { return } @@ -261,13 +261,13 @@ private struct Inference { formatter.forEach(.startOfScope("{")) { i, _ in // Check this isn't an inline block guard let closingBraceIndex = formatter.index(of: .endOfScope("}"), after: i), - formatter.index(of: .linebreak, in: i + 1 ..< closingBraceIndex) != nil + formatter.index(of: .linebreak, in: i + 1 ..< closingBraceIndex) != nil else { return } // Check if brace is wrapped if let prevTokenIndex = formatter.index(of: .nonSpace, before: i), - let prevToken = formatter.token(at: prevTokenIndex) + let prevToken = formatter.token(at: prevTokenIndex) { switch prevToken { case .identifier, .keyword, .endOfScope, .operator("?", .postfix), .operator("!", .postfix): @@ -286,11 +286,11 @@ private struct Inference { var indented = 0, notIndented = 0, outdented = 0 formatter.forEach(.startOfScope("#if")) { i, _ in if let indent = formatter.token(at: i - 1), case let .space(string) = indent, - !string.isEmpty + !string.isEmpty { // Indented, check next line if let nextLineIndex = formatter.index(of: .linebreak, after: i), - let nextIndex = formatter.index(of: .nonSpaceOrLinebreak, after: nextLineIndex) + let nextIndex = formatter.index(of: .nonSpaceOrLinebreak, after: nextLineIndex) { switch formatter.tokens[nextIndex - 1] { case let .space(innerString): @@ -316,7 +316,7 @@ private struct Inference { } // Not indented, check next line if let nextLineIndex = formatter.index(of: .linebreak, after: i), - let nextIndex = formatter.index(of: .nonSpaceOrLinebreak, after: nextLineIndex) + let nextIndex = formatter.index(of: .nonSpaceOrLinebreak, after: nextLineIndex) { switch formatter.tokens[nextIndex - 1] { case let .space(string): @@ -363,8 +363,8 @@ private struct Inference { var balanced = 0, sameLine = 0 formatter.forEach(.startOfScope("(")) { i, _ in guard let closingBraceIndex = formatter.endOfScope(at: i), - let linebreakIndex = formatter.index(of: .linebreak, after: i), - formatter.index(of: .nonSpaceOrComment, after: i) == linebreakIndex + let linebreakIndex = formatter.index(of: .linebreak, after: i), + formatter.index(of: .nonSpaceOrComment, after: i) == linebreakIndex else { return } @@ -490,7 +490,7 @@ private struct Inference { switch prevToken { case .keyword("let"), .keyword("var"): guard let prevPrevToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: prevIndex), - [.keyword("case"), .endOfScope("case"), .delimiter(",")].contains(prevPrevToken) + [.keyword("case"), .endOfScope("case"), .delimiter(",")].contains(prevPrevToken) else { // Tuple assignment, not a pattern return @@ -516,9 +516,9 @@ private struct Inference { for i in range { let token = formatter.tokens[i] if case .identifier = token, let index = argNames.index(of: token.unescaped()), - formatter.last(.nonSpaceOrCommentOrLinebreak, before: i)?.isOperator(".") == false, - formatter.next(.nonSpaceOrCommentOrLinebreak, after: i) != .delimiter(":") || - formatter.currentScope(at: i) == .startOfScope("[") + formatter.last(.nonSpaceOrCommentOrLinebreak, before: i)?.isOperator(".") == false, + formatter.next(.nonSpaceOrCommentOrLinebreak, after: i) != .delimiter(":") || + formatter.currentScope(at: i) == .startOfScope("[") { argNames.remove(at: index) associatedData.remove(at: index) @@ -531,8 +531,8 @@ private struct Inference { // Function arguments formatter.forEachToken { i, token in guard case let .keyword(keyword) = token, ["func", "init", "subscript"].contains(keyword), - let startIndex = formatter.index(of: .startOfScope("("), after: i), - let endIndex = formatter.index(of: .endOfScope(")"), after: startIndex) else { return } + let startIndex = formatter.index(of: .startOfScope("("), after: i), + let endIndex = formatter.index(of: .endOfScope(")"), after: startIndex) else { return } let isOperator = (keyword == "subscript") || (keyword == "func" && formatter.next(.nonSpaceOrCommentOrLinebreak, after: i)?.isOperator == true) var index = startIndex @@ -658,13 +658,13 @@ private struct Inference { continue case .keyword("switch"): guard let nextIndex = formatter.index(of: .startOfScope("{"), after: i), - var endIndex = formatter.index(of: .endOfScope, after: nextIndex) + var endIndex = formatter.index(of: .endOfScope, after: nextIndex) else { return // error } while formatter.tokens[endIndex] != .endOfScope("}") { guard let nextIndex = formatter.index(of: .startOfScope(":"), after: endIndex), - let _endIndex = formatter.index(of: .endOfScope, after: nextIndex) + let _endIndex = formatter.index(of: .endOfScope, after: nextIndex) else { return // error } @@ -756,9 +756,9 @@ private struct Inference { } case .keyword("extension"), .keyword("struct"), .keyword("enum"): guard formatter.last(.nonSpaceOrCommentOrLinebreak, before: index) != .keyword("import"), - let scopeStart = formatter.index(of: .startOfScope("{"), after: index) else { return } + let scopeStart = formatter.index(of: .startOfScope("{"), after: index) else { return } guard let nameToken = formatter.next(.identifier, after: index), - case let .identifier(name) = nameToken + case let .identifier(name) = nameToken else { return // error } @@ -811,9 +811,9 @@ private struct Inference { lastKeyword = "" var localNames = localNames guard let keywordIndex = formatter.index(of: .keyword, before: index), - let prevKeywordIndex = formatter.index(of: .keyword, before: keywordIndex), - let prevKeywordToken = formatter.token(at: prevKeywordIndex), - case .keyword("for") = prevKeywordToken else { return } + let prevKeywordIndex = formatter.index(of: .keyword, before: keywordIndex), + let prevKeywordToken = formatter.token(at: prevKeywordIndex), + case .keyword("for") = prevKeywordToken else { return } for token in formatter.tokens[prevKeywordIndex + 1 ..< keywordIndex] { if case let .identifier(name) = token, name != "_" { localNames.insert(token.unescaped()) @@ -841,7 +841,7 @@ private struct Inference { index = formatter.endOfScope(at: index) ?? (formatter.tokens.count - 1) case .startOfScope("("): if case let .identifier(fn)? = formatter.last(.nonSpaceOrCommentOrLinebreak, before: index), - selfRequired.contains(fn) + selfRequired.contains(fn) { index = formatter.index(of: .endOfScope(")"), after: index) ?? index break @@ -866,9 +866,9 @@ private struct Inference { lastKeyword = "" var localNames = localNames guard let keywordIndex = formatter.index(of: .keyword, before: index), - let prevKeywordIndex = formatter.index(of: .keyword, before: keywordIndex), - let prevKeywordToken = formatter.token(at: prevKeywordIndex), - case .keyword("for") = prevKeywordToken else { return } + let prevKeywordIndex = formatter.index(of: .keyword, before: keywordIndex), + let prevKeywordToken = formatter.token(at: prevKeywordIndex), + case .keyword("for") = prevKeywordToken else { return } for token in formatter.tokens[prevKeywordIndex + 1 ..< keywordIndex] { if case let .identifier(name) = token, name != "_" { localNames.insert(token.unescaped()) @@ -961,11 +961,11 @@ private struct Inference { index = formatter.endOfScope(at: index) ?? (formatter.tokens.count - 1) case .identifier("self"): guard !isTypeRoot, !localNames.contains("self"), - let dotIndex = formatter.index(of: .nonSpaceOrLinebreak, after: index, if: { - $0 == .operator(".", .infix) - }), let nextIndex = formatter.index(of: .nonSpaceOrLinebreak, after: dotIndex, if: { - $0.isIdentifier && !localNames.contains($0.unescaped()) - }) + let dotIndex = formatter.index(of: .nonSpaceOrLinebreak, after: index, if: { + $0 == .operator(".", .infix) + }), let nextIndex = formatter.index(of: .nonSpaceOrLinebreak, after: dotIndex, if: { + $0.isIdentifier && !localNames.contains($0.unescaped()) + }) else { break } @@ -998,7 +998,7 @@ private struct Inference { } let isAssignment: Bool if ["for", "var", "let"].contains(lastKeyword), - let prevToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: index) + let prevToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: index) { switch prevToken { case .identifier, .number, @@ -1024,7 +1024,7 @@ private struct Inference { break } if let lastToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: index), - lastToken.isOperator(".") + lastToken.isOperator(".") { break } @@ -1130,7 +1130,7 @@ private struct Inference { let startToken = formatter.tokens[index] var localNames = localNames guard let startIndex = formatter.index(of: .startOfScope("("), after: index), - let endIndex = formatter.index(of: .endOfScope(")"), after: startIndex) + let endIndex = formatter.index(of: .endOfScope(")"), after: startIndex) else { index += 1 // Prevent endless loop return @@ -1139,7 +1139,7 @@ private struct Inference { index = startIndex while index < endIndex { guard let externalNameIndex = formatter.index(of: .identifier, after: index), - let nextIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: externalNameIndex) + let nextIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: externalNameIndex) else { break } let token = formatter.tokens[nextIndex] switch token { @@ -1215,8 +1215,8 @@ private struct Inference { var space = 0, nospace = 0 formatter.forEach(.operator) { i, token in guard case .operator(_, .none) = token, - formatter.last(.nonSpaceOrCommentOrLinebreak, before: i) == .keyword("func"), - let token = formatter.token(at: i + 1) + formatter.last(.nonSpaceOrCommentOrLinebreak, before: i) == .keyword("func"), + let token = formatter.token(at: i + 1) else { return } @@ -1239,7 +1239,7 @@ private struct Inference { }) else { return } // Check this isn't an inline block guard let prevBraceIndex = formatter.index(of: .startOfScope("{"), before: braceIndex), - formatter.lastIndex(of: .linebreak, in: prevBraceIndex + 1 ..< braceIndex) != nil + formatter.lastIndex(of: .linebreak, in: prevBraceIndex + 1 ..< braceIndex) != nil else { return } @@ -1264,9 +1264,9 @@ private struct Inference { switchIndent = space } guard let openBraceIndex = formatter.index(of: .startOfScope("{"), after: i), - let caseIndex = formatter.index(of: .endOfScope("case"), after: openBraceIndex) ?? - formatter.index(of: .endOfScope("default"), after: openBraceIndex), - let indentToken = formatter.token(at: caseIndex - 1) + let caseIndex = formatter.index(of: .endOfScope("case"), after: openBraceIndex) ?? + formatter.index(of: .endOfScope("default"), after: openBraceIndex), + let indentToken = formatter.token(at: caseIndex - 1) else { return } @@ -1288,8 +1288,8 @@ private extension Formatter { var beforeFirst = 0, afterFirst = 0 forEachToken(where: { [.startOfScope("("), .startOfScope("<")].contains($0) }) { i, _ in guard isParameterList(at: i) == parameters, - let closingBraceIndex = endOfScope(at: i), - index(of: .linebreak, in: i + 1 ..< closingBraceIndex) != nil + let closingBraceIndex = endOfScope(at: i), + index(of: .linebreak, in: i + 1 ..< closingBraceIndex) != nil else { return } @@ -1313,7 +1313,7 @@ private extension Formatter { var beforeFirst = 0, afterFirst = 0 forEachToken(where: { $0.isStartOfScope && scopes.contains($0.string) }) { i, _ in guard let closingBraceIndex = endOfScope(at: i), - index(of: .linebreak, in: i + 1 ..< closingBraceIndex) != nil + index(of: .linebreak, in: i + 1 ..< closingBraceIndex) != nil else { return } diff --git a/Sources/Info.plist b/Sources/Info.plist index f4e4884e..4061c918 100644 --- a/Sources/Info.plist +++ b/Sources/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.46.3 + $(MARKETING_VERSION) CFBundleSignature ???? CFBundleVersion diff --git a/Sources/Options.swift b/Sources/Options.swift index 66dc6a69..5b1fb0f9 100644 --- a/Sources/Options.swift +++ b/Sources/Options.swift @@ -221,8 +221,8 @@ public enum Grouping: Equatable, RawRepresentable, CustomStringConvertible { $0.trimmingCharacters(in: .whitespacesAndNewlines) } guard (1 ... 2).contains(parts.count), - let group = parts.first.flatMap(Int.init), - let threshold = parts.last.flatMap(Int.init) + let group = parts.first.flatMap(Int.init), + let threshold = parts.last.flatMap(Int.init) else { return nil } diff --git a/Sources/ParsingHelpers.swift b/Sources/ParsingHelpers.swift index b0ab5c99..5d2a742b 100644 --- a/Sources/ParsingHelpers.swift +++ b/Sources/ParsingHelpers.swift @@ -73,7 +73,7 @@ public extension Formatter { switch token { case .keyword("#colorLiteral"), .keyword("#imageLiteral"): guard let startIndex = self.index(of: .startOfScope("("), after: index), - let endIndex = endOfScope(at: startIndex) + let endIndex = endOfScope(at: startIndex) else { fallthrough } @@ -111,7 +111,7 @@ public extension Formatter { result += string case .keyword("#colorLiteral"), .keyword("#imageLiteral"): guard let startIndex = self.index(of: .startOfScope("("), after: index), - let endIndex = endOfScope(at: startIndex) + let endIndex = endOfScope(at: startIndex) else { fallthrough } @@ -179,7 +179,7 @@ public extension Formatter { case .keyword("as"): wasOperator = true if case let .operator(name, .postfix)? = self.token(at: i + 1), - ["?", "!"].contains(name) + ["?", "!"].contains(name) { i += 1 } @@ -223,9 +223,9 @@ extension Formatter { } case .endOfScope(")"): guard let startIndex = self.index(of: .startOfScope("("), before: prevIndex), - last(.nonSpaceOrCommentOrLinebreak, before: startIndex, if: { - $0.isAttribute || _FormatRules.aclModifiers.contains($0.string) - }) != nil + last(.nonSpaceOrCommentOrLinebreak, before: startIndex, if: { + $0.isAttribute || _FormatRules.aclModifiers.contains($0.string) + }) != nil else { return false } @@ -358,8 +358,8 @@ extension Formatter { if isConditionalStatement(at: i) { if let endIndex = endOfScope(at: i), - next(.nonSpaceOrComment, after: endIndex) == .startOfScope("(") || - next(.nonSpaceOrCommentOrLinebreak, after: endIndex) == .startOfScope("{") + next(.nonSpaceOrComment, after: endIndex) == .startOfScope("(") || + next(.nonSpaceOrCommentOrLinebreak, after: endIndex) == .startOfScope("{") { return true } @@ -384,7 +384,7 @@ extension Formatter { return !isStartOfClosure(at: startOfScope) case .endOfScope(")"), .endOfScope(">"): guard var startOfScope = index(of: .startOfScope, before: prevIndex), - var prev = index(of: .nonSpaceOrCommentOrLinebreak, before: startOfScope) + var prev = index(of: .nonSpaceOrCommentOrLinebreak, before: startOfScope) else { return true } @@ -409,7 +409,7 @@ extension Formatter { return false default: if let nextIndex = index(of: .nonSpaceOrCommentOrLinebreak, after: i), - isAccessorKeyword(at: nextIndex) || isAccessorKeyword(at: prevIndex) + isAccessorKeyword(at: nextIndex) || isAccessorKeyword(at: prevIndex) { return false } else { @@ -419,7 +419,7 @@ extension Formatter { fallthrough case .endOfScope where tokens[prevIndex].isStringDelimiter, .identifier, .number: if let nextIndex = index(of: .nonSpaceOrCommentOrLinebreak, after: i), - isAccessorKeyword(at: nextIndex) || isAccessorKeyword(at: prevIndex) + isAccessorKeyword(at: nextIndex) || isAccessorKeyword(at: prevIndex) { return false } @@ -433,7 +433,7 @@ extension Formatter { } var index = prevKeywordIndex while let nextIndex = self.index(of: .nonSpaceOrComment, after: index), - nextIndex < i + nextIndex < i { switch tokens[nextIndex] { case .operator("=", .infix): @@ -445,7 +445,7 @@ extension Formatter { return true } if tokens[nextIndex] != .startOfScope("{"), - isEndOfStatement(at: index), isStartOfStatement(at: nextIndex) + isEndOfStatement(at: index), isStartOfStatement(at: nextIndex) { return true } @@ -498,13 +498,13 @@ extension Formatter { return false } if tokens[prevIndex] == .endOfScope("}"), - let startIndex = index(of: .startOfScope("{"), before: prevIndex), - let prev = index(of: .nonSpaceOrCommentOrLinebreak, before: startIndex) + let startIndex = index(of: .startOfScope("{"), before: prevIndex), + let prev = index(of: .nonSpaceOrCommentOrLinebreak, before: startIndex) { prevIndex = prev if tokens[prevIndex] == .endOfScope(")"), - let startIndex = index(of: .startOfScope("("), before: prevIndex), - let prev = index(of: .nonSpaceOrCommentOrLinebreak, before: startIndex) + let startIndex = index(of: .startOfScope("("), before: prevIndex), + let prev = index(of: .nonSpaceOrCommentOrLinebreak, before: startIndex) { prevIndex = prev } @@ -561,7 +561,7 @@ extension Formatter { func lastSignificantKeyword(at i: Int, excluding: [String] = []) -> String? { guard let index = indexOfLastSignificantKeyword(at: i, excluding: excluding), - case let .keyword(keyword) = tokens[index] + case let .keyword(keyword) = tokens[index] else { return nil } @@ -570,8 +570,8 @@ extension Formatter { func indexOfLastSignificantKeyword(at i: Int, excluding: [String] = []) -> Int? { guard let token = token(at: i), - let index = token.isKeyword ? i : index(of: .keyword, before: i), - case let .keyword(keyword) = tokens[index] + let index = token.isKeyword ? i : index(of: .keyword, before: i), + case let .keyword(keyword) = tokens[index] else { return nil } @@ -583,8 +583,8 @@ extension Formatter { return indexOfLastSignificantKeyword(at: index - 1, excluding: excluding) default: guard let braceIndex = self.index(of: .startOfScope("{"), in: index ..< i), - let endIndex = endOfScope(at: braceIndex), - next(.nonSpaceOrComment, after: endIndex) != .startOfScope("(") + let endIndex = endOfScope(at: braceIndex), + next(.nonSpaceOrComment, after: endIndex) != .startOfScope("(") else { return index } @@ -608,8 +608,8 @@ extension Formatter { return i case .endOfScope(")"): guard let openParenIndex = index(of: .startOfScope("("), before: i), - let prevTokenIndex = index(of: .nonSpaceOrComment, before: openParenIndex), - tokens[prevTokenIndex].isAttribute + let prevTokenIndex = index(of: .nonSpaceOrComment, before: openParenIndex), + tokens[prevTokenIndex].isAttribute else { return nil } @@ -673,7 +673,7 @@ extension Formatter { } default: if let attributeIndex = startOfAttribute(at: i), - let prevIndex = index(of: .nonSpaceOrCommentOrLinebreak, before: attributeIndex) + let prevIndex = index(of: .nonSpaceOrCommentOrLinebreak, before: attributeIndex) { return isEndOfStatement(at: prevIndex, in: scope) } @@ -711,8 +711,8 @@ extension Formatter { return false case .startOfScope("{") where isStartOfClosure(at: i): guard last(.nonSpaceOrComment, before: i)?.isLinebreak == true, - let prevIndex = index(of: .nonSpaceOrCommentOrLinebreak, before: i), - let prevToken = self.token(at: prevIndex) + let prevIndex = index(of: .nonSpaceOrCommentOrLinebreak, before: i), + let prevToken = self.token(at: prevIndex) else { return false } @@ -720,9 +720,9 @@ extension Formatter { return false } if [.endOfScope(")"), .endOfScope("]")].contains(prevToken), - let startIndex = index(of: .startOfScope, before: prevIndex), - !tokens[startIndex ..< prevIndex].contains(where: { $0.isLinebreak }) - || indentForLine(at: startIndex) == indentForLine(at: prevIndex) + let startIndex = index(of: .startOfScope, before: prevIndex), + !tokens[startIndex ..< prevIndex].contains(where: { $0.isLinebreak }) + || indentForLine(at: startIndex) == indentForLine(at: prevIndex) { return false } @@ -734,7 +734,7 @@ extension Formatter { func isSubscriptOrFunctionCall(at i: Int) -> Bool { guard case let .startOfScope(string)? = token(at: i), ["[", "("].contains(string), - let prevToken = last(.nonSpaceOrComment, before: i) + let prevToken = last(.nonSpaceOrComment, before: i) else { return false } @@ -771,8 +771,8 @@ extension Formatter { return false } if tokens[prevIndex] == .identifier("View"), - let prevToken = last(.nonSpaceOrCommentOrLinebreak, before: prevIndex), - [.delimiter(":"), .identifier("some")].contains(prevToken) + let prevToken = last(.nonSpaceOrCommentOrLinebreak, before: prevIndex), + [.delimiter(":"), .identifier("some")].contains(prevToken) { return true } @@ -795,7 +795,7 @@ extension Formatter { } case "Self", "Any": if let prevToken = last(.nonSpaceOrCommentOrLinebreak, before: i), - [.delimiter(":"), .operator("->", .infix)].contains(prevToken) + [.delimiter(":"), .operator("->", .infix)].contains(prevToken) { // TODO: check for other cases where it's safe to use unescaped return false @@ -865,8 +865,8 @@ extension Formatter { func isParameterList(at i: Int) -> Bool { assert([.startOfScope("("), .startOfScope("<")].contains(tokens[i])) if let endIndex = endOfScope(at: i), - let nextToken = next(.nonSpaceOrCommentOrLinebreak, after: endIndex), - [.operator("->", .infix), .keyword("throws"), .keyword("rethrows")].contains(nextToken) + let nextToken = next(.nonSpaceOrCommentOrLinebreak, after: endIndex), + [.operator("->", .infix), .keyword("throws"), .keyword("rethrows")].contains(nextToken) { return true } @@ -930,8 +930,8 @@ extension Formatter { // Gather comments var prevIndex = index(of: .linebreak, before: startIndex) ?? 0 while startIndex > 0, - next(.nonSpace, after: prevIndex)?.isComment == true, - next(.nonSpaceOrComment, after: prevIndex)?.isLinebreak == true + next(.nonSpace, after: prevIndex)?.isComment == true, + next(.nonSpaceOrComment, after: prevIndex)?.isLinebreak == true { if prevIndex == 0, index(of: .startOfScope("#if"), before: startIndex) != nil { break @@ -1084,8 +1084,8 @@ extension Formatter { var index = index while token(at: index + 1)?.string == ".", - let nextIdentifier = token(at: index + 2), - nextIdentifier.is(.identifier) == true + let nextIdentifier = token(at: index + 2), + nextIdentifier.is(.identifier) == true { name = "\(name).\(nextIdentifier.string)" index += 2 @@ -1097,13 +1097,13 @@ extension Formatter { // get type of declaration starting at index of declaration keyword func declarationType(at index: Int) -> String? { guard let token = self.token(at: index), token.isDeclarationTypeKeyword, - case let .keyword(keyword) = token + case let .keyword(keyword) = token else { return nil } if keyword == "class", - let nextToken = next(.nonSpaceOrCommentOrLinebreak, after: index), - nextToken.isDeclarationTypeKeyword, case let .keyword(keyword) = nextToken + let nextToken = next(.nonSpaceOrCommentOrLinebreak, after: index), + nextToken.isDeclarationTypeKeyword, case let .keyword(keyword) = nextToken { return keyword } @@ -1184,7 +1184,7 @@ extension Formatter { // For conditional compilation blocks, the `declarationKeyword` _is_ the `startOfScope` // so we can immediately skip to the corresponding #endif if declarationKeyword == "#if", - let endOfConditionalCompilationScope = parser.endOfScope(at: searchIndex) + let endOfConditionalCompilationScope = parser.endOfScope(at: searchIndex) { searchIndex = endOfConditionalCompilationScope } @@ -1192,8 +1192,8 @@ extension Formatter { // Symbol imports (like `import class Module.Type`) will have an extra `isDeclarationTypeKeyword` // immediately following their `declarationKeyword`, so we need to skip them. if declarationKeyword == "import", - let symbolTypeKeywordIndex = parser.index(of: .nonSpaceOrComment, after: declarationTypeKeywordIndex), - parser.tokens[symbolTypeKeywordIndex].isDeclarationTypeKeyword + let symbolTypeKeywordIndex = parser.index(of: .nonSpaceOrComment, after: declarationTypeKeywordIndex), + parser.tokens[symbolTypeKeywordIndex].isDeclarationTypeKeyword { searchIndex = symbolTypeKeywordIndex + 1 } @@ -1206,9 +1206,9 @@ extension Formatter { // - `#if` tokens are `startOfScope`, but immediately represent the start of a new // conditional compilation declaration so we can't skip through them. if parser.tokens[searchIndex].isStartOfScope, - !parser.tokens[searchIndex].isComment, - parser.tokens[searchIndex].string != "#if", - let endOfScope = parser.endOfScope(at: searchIndex) + !parser.tokens[searchIndex].isComment, + parser.tokens[searchIndex].string != "#if", + let endOfScope = parser.endOfScope(at: searchIndex) { searchIndex = endOfScope + 1 } else if parser.tokens[searchIndex].isDeclarationTypeKeyword @@ -1240,7 +1240,7 @@ extension Formatter { while searchIndex > declarationTypeKeywordIndex, startOfScope == nil { if parser.tokens[searchIndex].isStartOfScope, - parser.endOfScope(at: searchIndex) == encounteredEndOfScope + parser.endOfScope(at: searchIndex) == encounteredEndOfScope { startOfScope = searchIndex // Confirm whether or not this scope should be grouped with the @@ -1278,7 +1278,7 @@ extension Formatter { // Prefer keeping linebreaks at the end of a declaration's tokens, // instead of the start of the next delaration's tokens while let linebreakSearchIndex = endOfDeclaration, - parser.token(at: linebreakSearchIndex + 1)?.isLinebreak == true + parser.token(at: linebreakSearchIndex + 1)?.isLinebreak == true { endOfDeclaration = linebreakSearchIndex + 1 } @@ -1320,11 +1320,11 @@ extension Formatter { let typelikeKeywords = ["class", "struct", "enum", "protocol", "extension"] if typelikeKeywords.contains(declaration.keyword), - let declarationTypeKeywordIndex = declarationParser - .index(after: -1, where: { $0.string == declaration.keyword }), - let startOfBody = declarationParser - .index(of: .startOfScope("{"), after: declarationTypeKeywordIndex), - let endOfBody = declarationParser.endOfScope(at: startOfBody) + let declarationTypeKeywordIndex = declarationParser + .index(after: -1, where: { $0.string == declaration.keyword }), + let startOfBody = declarationParser + .index(of: .startOfScope("{"), after: declarationTypeKeywordIndex), + let endOfBody = declarationParser.endOfScope(at: startOfBody) { let (startTokens, bodyDeclarations, endTokens) = parseBody(in: startOfBody ... endOfBody) @@ -1406,7 +1406,7 @@ extension Formatter { func addBreakPoint(at i: Int, relativePriority: Int) { guard stringLiteralDepth == 0, currentPriority + relativePriority >= lastBreakPointPriority, - !isInClosureArguments(at: i + 1) + !isInClosureArguments(at: i + 1) else { return } @@ -1427,7 +1427,7 @@ extension Formatter { return nil case .keyword("#colorLiteral"), .keyword("#imageLiteral"): guard let startIndex = self.index(of: .startOfScope("("), after: i), - let endIndex = endOfScope(at: startIndex) + let endIndex = endOfScope(at: startIndex) else { return nil // error } diff --git a/Sources/Rules.swift b/Sources/Rules.swift index fd943614..f9322318 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -190,7 +190,7 @@ public struct _FormatRules { switch keyword { case "@autoclosure": if let nextIndex = formatter.index(of: .nonSpaceOrLinebreak, after: index), - formatter.next(.nonSpaceOrCommentOrLinebreak, after: nextIndex) == .identifier("escaping") + formatter.next(.nonSpaceOrCommentOrLinebreak, after: nextIndex) == .identifier("escaping") { assert(formatter.tokens[nextIndex] == .startOfScope("(")) return false @@ -269,14 +269,14 @@ public struct _FormatRules { ) { formatter in formatter.forEach(.startOfScope("(")) { i, _ in if formatter.token(at: i + 1)?.isSpace == true, - formatter.token(at: i + 2)?.isComment == false + formatter.token(at: i + 2)?.isComment == false { formatter.removeToken(at: i + 1) } } formatter.forEach(.endOfScope(")")) { i, _ in if formatter.token(at: i - 1)?.isSpace == true, - formatter.token(at: i - 2)?.isCommentOrLinebreak == false + formatter.token(at: i - 2)?.isCommentOrLinebreak == false { formatter.removeToken(at: i - 1) } @@ -334,14 +334,14 @@ public struct _FormatRules { ) { formatter in formatter.forEach(.startOfScope("[")) { i, _ in if formatter.token(at: i + 1)?.isSpace == true, - formatter.token(at: i + 2)?.isComment == false + formatter.token(at: i + 2)?.isComment == false { formatter.removeToken(at: i + 1) } } formatter.forEach(.endOfScope("]")) { i, _ in if formatter.token(at: i - 1)?.isSpace == true, - formatter.token(at: i - 2)?.isCommentOrLinebreak == false + formatter.token(at: i - 2)?.isCommentOrLinebreak == false { formatter.removeToken(at: i - 1) } @@ -383,7 +383,7 @@ public struct _FormatRules { formatter.forEach(.startOfScope("{")) { i, _ in if let nextToken = formatter.token(at: i + 1) { if !nextToken.isSpaceOrLinebreak, - ![.endOfScope("}"), .startOfScope("{")].contains(nextToken) + ![.endOfScope("}"), .startOfScope("{")].contains(nextToken) { formatter.insert(.space(" "), at: i + 1) } @@ -392,7 +392,7 @@ public struct _FormatRules { formatter.forEach(.endOfScope("}")) { i, _ in if let prevToken = formatter.token(at: i - 1) { if !prevToken.isSpaceOrLinebreak, - ![.endOfScope("}"), .startOfScope("{")].contains(prevToken) + ![.endOfScope("}"), .startOfScope("{")].contains(prevToken) { formatter.insert(.space(" "), at: i) } @@ -406,7 +406,7 @@ public struct _FormatRules { ) { formatter in formatter.forEach(.startOfScope("<")) { i, _ in if formatter.token(at: i - 1)?.isSpace == true, - formatter.token(at: i - 2)?.isIdentifierOrKeyword == true + formatter.token(at: i - 2)?.isIdentifierOrKeyword == true { formatter.removeToken(at: i - 1) } @@ -424,7 +424,7 @@ public struct _FormatRules { } formatter.forEach(.endOfScope(">")) { i, _ in if formatter.token(at: i - 1)?.isSpace == true, - formatter.token(at: i - 2)?.isLinebreak == false + formatter.token(at: i - 2)?.isLinebreak == false { formatter.removeToken(at: i - 1) } @@ -461,8 +461,8 @@ public struct _FormatRules { } case .operator("?", .postfix), .operator("!", .postfix): if let prevToken = formatter.token(at: i - 1), - formatter.token(at: i + 1)?.isSpaceOrLinebreak == false, - [.keyword("as"), .keyword("try")].contains(prevToken) + formatter.token(at: i + 1)?.isSpaceOrLinebreak == false, + [.keyword("as"), .keyword("try")].contains(prevToken) { formatter.insert(.space(" "), at: i + 1) } @@ -480,7 +480,7 @@ public struct _FormatRules { return case let token where token.isUnwrapOperator: if let prevToken = formatter.last(.nonSpace, before: prevIndex), - [.keyword("try"), .keyword("as")].contains(prevToken) + [.keyword("try"), .keyword("as")].contains(prevToken) { spaceRequired = true } else { @@ -503,11 +503,11 @@ public struct _FormatRules { case let .operator(name, .infix) where formatter.options.noSpaceOperators.contains(name) || (!formatter.options.spaceAroundRangeOperators && token.isRangeOperator): if formatter.token(at: i + 1)?.isSpace == true, - formatter.token(at: i - 1)?.isSpace == true, - let nextToken = formatter.next(.nonSpace, after: i), - !nextToken.isCommentOrLinebreak, !nextToken.isOperator, - let prevToken = formatter.last(.nonSpace, before: i), - !prevToken.isCommentOrLinebreak, !prevToken.isOperator || prevToken.isUnwrapOperator + formatter.token(at: i - 1)?.isSpace == true, + let nextToken = formatter.next(.nonSpace, after: i), + !nextToken.isCommentOrLinebreak, !nextToken.isOperator, + let prevToken = formatter.last(.nonSpace, before: i), + !prevToken.isCommentOrLinebreak, !prevToken.isOperator || prevToken.isUnwrapOperator { formatter.removeToken(at: i + 1) formatter.removeToken(at: i - 1) @@ -530,7 +530,7 @@ public struct _FormatRules { case .delimiter(":"): // TODO: make this check more robust, and remove redundant space if formatter.token(at: i + 1)?.isIdentifier == true, - formatter.token(at: i + 2) == .delimiter(":") + formatter.token(at: i + 2) == .delimiter(":") { // It's a selector break @@ -545,7 +545,7 @@ public struct _FormatRules { formatter.insert(.space(" "), at: i + 1) } if formatter.token(at: i - 1)?.isSpace == true, - formatter.token(at: i - 2)?.isLinebreak == false + formatter.token(at: i - 2)?.isLinebreak == false { // Remove space before the token formatter.removeToken(at: i - 1) @@ -567,9 +567,9 @@ public struct _FormatRules { } formatter.forEach(.endOfScope("*/")) { i, _ in guard let startIndex = formatter.index(of: .startOfScope("/*"), before: i), - case let .commentBody(commentStart)? = formatter.next(.nonSpaceOrLinebreak, after: startIndex), - case let .commentBody(commentEnd)? = formatter.last(.nonSpaceOrLinebreak, before: i), - !commentStart.hasPrefix("@"), !commentEnd.hasSuffix("@") + case let .commentBody(commentStart)? = formatter.next(.nonSpaceOrLinebreak, after: startIndex), + case let .commentBody(commentEnd)? = formatter.last(.nonSpaceOrLinebreak, before: i), + !commentStart.hasPrefix("@"), !commentEnd.hasSuffix("@") else { return } @@ -598,7 +598,7 @@ public struct _FormatRules { ) { formatter in formatter.forEach(.startOfScope("//")) { i, _ in guard let nextToken = formatter.token(at: i + 1), - case let .commentBody(string) = nextToken else { return } + case let .commentBody(string) = nextToken else { return } guard let first = string.first else { return } if "/!:".contains(first) { let nextIndex = string.index(after: string.startIndex) @@ -612,14 +612,14 @@ public struct _FormatRules { } formatter.forEach(.startOfScope("/*")) { i, _ in guard let nextToken = formatter.token(at: i + 1), case let .commentBody(string) = nextToken, - !string.hasPrefix("---"), !string.hasPrefix("@"), !string.hasSuffix("---"), !string.hasSuffix("@") + !string.hasPrefix("---"), !string.hasPrefix("@"), !string.hasSuffix("---"), !string.hasSuffix("@") else { return } if let first = string.first, "*!:".contains(first) { let nextIndex = string.index(after: string.startIndex) if nextIndex < string.endIndex, case let next = string[nextIndex], - !" /t".contains(next), !string.hasPrefix("**"), !string.hasPrefix("*/") + !" /t".contains(next), !string.hasPrefix("**"), !string.hasPrefix("*/") { let string = String(string.first!) + " " + String(string.dropFirst()) formatter.replaceToken(at: i + 1, with: .commentBody(string)) @@ -652,8 +652,8 @@ public struct _FormatRules { // Check types match var i = colonIndex, j = equalsIndex while let typeIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: i), - typeIndex <= endIndex, - let valueIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: j) + typeIndex <= endIndex, + let valueIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: j) { guard formatter.tokens[typeIndex] == formatter.tokens[valueIndex] else { return @@ -667,7 +667,7 @@ public struct _FormatRules { // Check for ternary if let endOfExpression = formatter.endOfExpression(at: j, upTo: [.operator("?", .infix)]), - formatter.next(.nonSpaceOrCommentOrLinebreak, after: endOfExpression) == .operator("?", .infix) + formatter.next(.nonSpaceOrCommentOrLinebreak, after: endOfExpression) == .operator("?", .infix) { return } @@ -693,7 +693,7 @@ public struct _FormatRules { break default: guard let prevToken = formatter.token(at: i - 1), - let nextToken = formatter.token(at: i + 1) + let nextToken = formatter.token(at: i + 1) else { return } @@ -769,7 +769,7 @@ public struct _FormatRules { let start = formatter.startOfModifiers(at: i) if formatter.modifiersForType(at: i, contains: "final"), - let finalIndex = formatter.lastIndex(in: start ..< i, where: { $0 == .identifier("final") }) + let finalIndex = formatter.lastIndex(in: start ..< i, where: { $0 == .identifier("final") }) { formatter.removeTokens(in: finalIndex ... finalIndex + 1) } @@ -785,7 +785,7 @@ public struct _FormatRules { ) { formatter in formatter.forEach(.space) { i, _ in if formatter.token(at: i + 1)?.isLinebreak ?? true, - formatter.options.truncateBlankLines || formatter.token(at: i - 1)?.isLinebreak == false + formatter.options.truncateBlankLines || formatter.token(at: i - 1)?.isLinebreak == false { formatter.removeToken(at: i) } @@ -825,9 +825,9 @@ public struct _FormatRules { ) { formatter in formatter.forEach(.startOfScope) { i, token in guard ["{", "(", "[", "<"].contains(token.string), - let indexOfFirstLineBreak = formatter.index(of: .nonSpaceOrComment, after: i), - // If there is extra code on the same line, ignore it - formatter.tokens[indexOfFirstLineBreak].isLinebreak + let indexOfFirstLineBreak = formatter.index(of: .nonSpaceOrComment, after: i), + // If there is extra code on the same line, ignore it + formatter.tokens[indexOfFirstLineBreak].isLinebreak else { return } // Find next non-space token var index = indexOfFirstLineBreak + 1 @@ -858,8 +858,8 @@ public struct _FormatRules { ) { formatter in formatter.forEach(.endOfScope) { i, token in guard ["}", ")", "]", ">"].contains(token.string), - // If there is extra code after the closing scope on the same line, ignore it - (formatter.next(.nonSpaceOrComment, after: i).map { $0.isLinebreak }) ?? true + // If there is extra code after the closing scope on the same line, ignore it + (formatter.next(.nonSpaceOrComment, after: i).map { $0.isLinebreak }) ?? true else { return } // Find previous non-space token var index = i - 1 @@ -880,8 +880,8 @@ public struct _FormatRules { index -= 1 } if formatter.options.removeBlankLines, - let indexOfFirstLineBreak = indexOfFirstLineBreak, - indexOfFirstLineBreak != indexOfLastLineBreak + let indexOfFirstLineBreak = indexOfFirstLineBreak, + indexOfFirstLineBreak != indexOfLastLineBreak { formatter.removeTokens(in: indexOfFirstLineBreak ..< indexOfLastLineBreak!) return @@ -915,8 +915,8 @@ public struct _FormatRules { case .endOfScope("}"): spaceableScopeStack.removeLast() guard spaceableScopeStack.last == true, - let openingBraceIndex = formatter.index(of: .startOfScope("{"), before: i), - formatter.lastIndex(of: .linebreak, in: openingBraceIndex + 1 ..< i) != nil + let openingBraceIndex = formatter.index(of: .startOfScope("{"), before: i), + formatter.lastIndex(of: .linebreak, in: openingBraceIndex + 1 ..< i) != nil else { // Inline braces break @@ -939,15 +939,15 @@ public struct _FormatRules { break case .keyword("while"): if let previousBraceIndex = formatter.index(of: .startOfScope("{"), before: i), - formatter.last(.nonSpaceOrCommentOrLinebreak, before: previousBraceIndex) - != .keyword("repeat") + formatter.last(.nonSpaceOrCommentOrLinebreak, before: previousBraceIndex) + != .keyword("repeat") { fallthrough } default: if formatter.isEnabled, formatter.options.insertBlankLines, - let firstLinebreakIndex = formatter.index(of: .linebreak, in: i + 1 ..< nextTokenIndex), - formatter.index(of: .linebreak, in: firstLinebreakIndex + 1 ..< nextTokenIndex) == nil + let firstLinebreakIndex = formatter.index(of: .linebreak, in: i + 1 ..< nextTokenIndex), + formatter.index(of: .linebreak, in: firstLinebreakIndex + 1 ..< nextTokenIndex) == nil { formatter.insertLinebreak(at: firstLinebreakIndex) } @@ -965,18 +965,18 @@ public struct _FormatRules { ) { formatter in formatter.forEachToken { i, token in guard case let .commentBody(comment) = token, comment.hasPrefix("MARK:"), - let startIndex = formatter.index(of: .nonSpace, before: i), - formatter.tokens[startIndex] == .startOfScope("//") else { return } + let startIndex = formatter.index(of: .nonSpace, before: i), + formatter.tokens[startIndex] == .startOfScope("//") else { return } if let nextIndex = formatter.index(of: .linebreak, after: i), - let nextToken = formatter.next(.nonSpace, after: nextIndex), - !nextToken.isLinebreak, nextToken != .endOfScope("}") + let nextToken = formatter.next(.nonSpace, after: nextIndex), + !nextToken.isLinebreak, nextToken != .endOfScope("}") { formatter.insertLinebreak(at: nextIndex) } if formatter.options.insertBlankLines, - let lastIndex = formatter.index(of: .linebreak, before: startIndex), - let lastToken = formatter.last(.nonSpace, before: lastIndex), - !lastToken.isLinebreak, lastToken != .startOfScope("{") + let lastIndex = formatter.index(of: .linebreak, before: startIndex), + let lastToken = formatter.last(.nonSpace, before: lastIndex), + !lastToken.isLinebreak, lastToken != .startOfScope("{") { formatter.insertLinebreak(at: lastIndex) } @@ -1035,14 +1035,14 @@ public struct _FormatRules { func isFirstStackedClosureArgument(at i: Int) -> Bool { assert(formatter.tokens[i] == .startOfScope("{")) if let prevIndex = formatter.index(of: .nonSpace, before: i), - let prevToken = formatter.token(at: prevIndex), prevToken == .startOfScope("(") || - (prevToken == .delimiter(":") && formatter.token(at: prevIndex - 1)?.isIdentifier == true - && formatter.last(.nonSpace, before: prevIndex - 1) == .startOfScope("(")), - let endIndex = formatter.endOfScope(at: i), - let commaIndex = formatter.index(of: .nonSpace, after: endIndex, if: { - $0 == .delimiter(",") - }), - formatter.next(.nonSpaceOrComment, after: commaIndex)?.isLinebreak == true + let prevToken = formatter.token(at: prevIndex), prevToken == .startOfScope("(") || + (prevToken == .delimiter(":") && formatter.token(at: prevIndex - 1)?.isIdentifier == true + && formatter.last(.nonSpace, before: prevIndex - 1) == .startOfScope("(")), + let endIndex = formatter.endOfScope(at: i), + let commaIndex = formatter.index(of: .nonSpace, after: endIndex, if: { + $0 == .delimiter(",") + }), + formatter.next(.nonSpaceOrComment, after: commaIndex)?.isLinebreak == true { return true } @@ -1050,8 +1050,8 @@ public struct _FormatRules { } if formatter.options.fragment, - let firstIndex = formatter.index(of: .nonSpaceOrLinebreak, after: -1), - let indentToken = formatter.token(at: firstIndex - 1), case let .space(string) = indentToken + let firstIndex = formatter.index(of: .nonSpaceOrLinebreak, after: -1), + let indentToken = formatter.token(at: firstIndex - 1), case let .space(string) = indentToken { indentStack[0] = string } @@ -1073,8 +1073,8 @@ public struct _FormatRules { var space = "" let start = formatter.startOfLine(at: i) if let index = formatter.index(of: .nonSpace, in: start ..< i), - case let .stringBody(string) = formatter.tokens[index], - string.unicodeScalars.first?.isSpace == true + case let .stringBody(string) = formatter.tokens[index], + string.unicodeScalars.first?.isSpace == true { var index = string.startIndex while index < string.endIndex, string[index].unicodeScalars.first!.isSpace { @@ -1122,15 +1122,15 @@ public struct _FormatRules { case ":": indent += formatter.options.indent if formatter.options.indentCase, - scopeStack.count < 2 || scopeStack[scopeStack.count - 2] != .startOfScope("#if") + scopeStack.count < 2 || scopeStack[scopeStack.count - 2] != .startOfScope("#if") { indent += formatter.options.indent } case "#if": if let lineIndex = formatter.index(of: .linebreak, after: i), - let nextKeyword = formatter.next(.nonSpaceOrCommentOrLinebreak, after: lineIndex), [ - .endOfScope("case"), .endOfScope("default"), .keyword("@unknown"), - ].contains(nextKeyword) + let nextKeyword = formatter.next(.nonSpaceOrCommentOrLinebreak, after: lineIndex), [ + .endOfScope("case"), .endOfScope("default"), .keyword("@unknown"), + ].contains(nextKeyword) { indent = indentStack[indentStack.count - indentCount - 1] if formatter.options.indentCase { @@ -1153,9 +1153,9 @@ public struct _FormatRules { } if formatter.tokens[prevIndex] == .delimiter(":") { guard formatter.token(at: prevIndex - 1)?.isIdentifier == true, - let parenIndex = formatter.index(of: .nonSpace, before: prevIndex - 1, if: { - $0 == .startOfScope("(") - }) + let parenIndex = formatter.index(of: .nonSpace, before: prevIndex - 1, if: { + $0 == .startOfScope("(") + }) else { let stringIndent = stringBodyIndent(at: i) stringBodyIndentStack[stringBodyIndentStack.count - 1] = stringIndent @@ -1177,10 +1177,10 @@ public struct _FormatRules { // But _not_ on the same line as the start of the parameter list let startOfLine = formatter.startOfLine(at: i) if let previousTokenIndex = formatter.index(of: .nonSpaceOrComment, before: i), - formatter.tokens[previousTokenIndex] == .endOfScope(")"), - startOfLine < previousTokenIndex, - let startOfParameterList = formatter.index(of: .startOfScope("("), before: previousTokenIndex), - startOfParameterList < startOfLine + formatter.tokens[previousTokenIndex] == .endOfScope(")"), + startOfLine < previousTokenIndex, + let startOfParameterList = formatter.index(of: .startOfScope("("), before: previousTokenIndex), + startOfParameterList < startOfLine { indent += formatter.options.indent + formatter.options.indent } else { @@ -1190,13 +1190,13 @@ public struct _FormatRules { break case "[", "(": guard let linebreakIndex = formatter.index(of: .linebreak, after: i), - let nextIndex = formatter.index(of: .nonSpace, after: i), - nextIndex != linebreakIndex + let nextIndex = formatter.index(of: .nonSpace, after: i), + nextIndex != linebreakIndex else { fallthrough } if formatter.last(.nonSpaceOrComment, before: linebreakIndex) != .delimiter(","), - formatter.next(.nonSpaceOrComment, after: linebreakIndex) != .delimiter(",") + formatter.next(.nonSpaceOrComment, after: linebreakIndex) != .delimiter(",") { fallthrough } @@ -1216,7 +1216,7 @@ public struct _FormatRules { linewrapStack.append(false) case .space: if i == 0, !formatter.options.fragment, - formatter.token(at: i + 1)?.isLinebreak != true + formatter.token(at: i + 1)?.isLinebreak != true { formatter.removeToken(at: i) } @@ -1286,7 +1286,7 @@ public struct _FormatRules { } else { var indent = indentStack.last ?? "" if [.endOfScope("case"), .endOfScope("default")].contains(token), - formatter.options.indentCase, scopeStack.last != .startOfScope("#if") + formatter.options.indentCase, scopeStack.last != .startOfScope("#if") { indent += formatter.options.indent } @@ -1344,15 +1344,15 @@ public struct _FormatRules { } index -= 1 while let prevToken = formatter.token(at: index - 1), prevToken.isComment, - let startIndex = formatter.index(of: .nonSpaceOrComment, before: index), - formatter.tokens[startIndex].isLinebreak + let startIndex = formatter.index(of: .nonSpaceOrComment, before: index), + formatter.tokens[startIndex].isLinebreak { // Set indent for comment immediately before this line to match this line if !formatter.isCommentedCode(at: startIndex + 1) { formatter.insertSpaceIfEnabled(indent, at: startIndex + 1) } if case .endOfScope("*/") = prevToken, - var index = formatter.index(of: .startOfScope("/*"), after: startIndex) + var index = formatter.index(of: .startOfScope("/*"), after: startIndex) { while let linebreakIndex = formatter.index(of: .linebreak, after: index) { formatter.insertSpaceIfEnabled(indent + " ", at: linebreakIndex + 1) @@ -1400,7 +1400,7 @@ public struct _FormatRules { } guard var nextNonSpaceIndex = formatter.index(of: .nonSpace, after: i), - let nextToken = formatter.token(at: nextNonSpaceIndex) + let nextToken = formatter.token(at: nextNonSpaceIndex) else { break } @@ -1425,7 +1425,7 @@ public struct _FormatRules { let end = formatter.endOfLine(at: i + 1) guard let lastToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: end + 1), - [.startOfScope("{"), .endOfScope("}")].contains(lastToken) else { return false } + [.startOfScope("{"), .endOfScope("}")].contains(lastToken) else { return false } return true } @@ -1433,7 +1433,7 @@ public struct _FormatRules { // Don't indent line starting with dot if previous line was just a closing brace let lastToken = formatter.tokens[lastNonSpaceOrLinebreakIndex] if formatter.options.allmanBraces, nextToken == .startOfScope("{"), - formatter.isStartOfClosure(at: nextNonSpaceIndex) + formatter.isStartOfClosure(at: nextNonSpaceIndex) { // Don't indent further } else if formatter.token(at: nextTokenIndex ?? -1) == .operator(".", .infix), @@ -1473,17 +1473,17 @@ public struct _FormatRules { nextNonSpaceIndex = formatter.index(of: .nonSpaceOrLinebreak, before: nextNonSpaceIndex) ?? nextNonSpaceIndex if let lineIndex = formatter.index(of: .linebreak, after: nextNonSpaceIndex), - let nextToken = formatter.next(.nonSpace, after: lineIndex), - [.startOfScope("#if"), .keyword("#else"), .keyword("#elseif")].contains(nextToken) + let nextToken = formatter.next(.nonSpace, after: lineIndex), + [.startOfScope("#if"), .keyword("#else"), .keyword("#elseif")].contains(nextToken) { break } fallthrough case .startOfScope("#if"): if let lineIndex = formatter.index(of: .linebreak, after: nextNonSpaceIndex), - let nextKeyword = formatter.next(.nonSpaceOrCommentOrLinebreak, after: lineIndex), [ - .endOfScope("case"), .endOfScope("default"), .keyword("@unknown"), - ].contains(nextKeyword) + let nextKeyword = formatter.next(.nonSpaceOrCommentOrLinebreak, after: lineIndex), [ + .endOfScope("case"), .endOfScope("default"), .keyword("@unknown"), + ].contains(nextKeyword) { break } @@ -1570,16 +1570,16 @@ public struct _FormatRules { ) { formatter in formatter.forEach(.startOfScope("{")) { i, _ in guard let closingBraceIndex = formatter.endOfScope(at: i), - // Check this isn't an inline block - formatter.index(of: .linebreak, in: i + 1 ..< closingBraceIndex) != nil, - let prevToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: i), - ![.delimiter(","), .keyword("in")].contains(prevToken), - !prevToken.is(.startOfScope) + // Check this isn't an inline block + formatter.index(of: .linebreak, in: i + 1 ..< closingBraceIndex) != nil, + let prevToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: i), + ![.delimiter(","), .keyword("in")].contains(prevToken), + !prevToken.is(.startOfScope) else { return } if let penultimateToken = formatter.last(.nonSpaceOrComment, before: closingBraceIndex), - !penultimateToken.isLinebreak + !penultimateToken.isLinebreak { formatter.insertSpace(formatter.indentForLine(at: i), at: closingBraceIndex) formatter.insertLinebreak(at: closingBraceIndex) @@ -1594,7 +1594,7 @@ public struct _FormatRules { .operator("?", .postfix), .operator("!", .postfix): formatter.insertLinebreak(at: i) if let breakIndex = formatter.index(of: .linebreak, after: i + 1), - let nextIndex = formatter.index(of: .nonSpace, after: breakIndex, if: { $0.isLinebreak }) + let nextIndex = formatter.index(of: .nonSpace, after: breakIndex, if: { $0.isLinebreak }) { formatter.removeTokens(in: breakIndex ..< nextIndex) } @@ -1608,8 +1608,8 @@ public struct _FormatRules { } else { // Implement K&R-style braces, where opening brace appears on the same line guard let prevIndex = formatter.index(of: .nonSpaceOrLinebreak, before: i), - formatter.tokens[prevIndex ..< i].contains(where: { $0.isLinebreak }), - !formatter.tokens[prevIndex].isComment + formatter.tokens[prevIndex ..< i].contains(where: { $0.isLinebreak }), + !formatter.tokens[prevIndex].isComment else { return } @@ -1674,9 +1674,9 @@ public struct _FormatRules { } case .keyword("else"): guard var prevIndex = formatter.index(of: .nonSpace, before: i), - let nextIndex = formatter.index(of: .nonSpaceOrLinebreak, after: i, if: { - !$0.isComment - }) + let nextIndex = formatter.index(of: .nonSpaceOrLinebreak, after: i, if: { + !$0.isComment + }) else { return } @@ -1754,7 +1754,7 @@ public struct _FormatRules { formatter.forEach(.endOfScope("]")) { i, _ in guard let prevTokenIndex = formatter.index(of: .nonSpaceOrComment, before: i) else { return } if let startIndex = formatter.index(of: .startOfScope("["), before: i), - let prevToken = formatter.last(.nonSpaceOrComment, before: startIndex) + let prevToken = formatter.last(.nonSpaceOrComment, before: startIndex) { switch prevToken { case .identifier, @@ -1765,7 +1765,7 @@ public struct _FormatRules { case .delimiter(":"): // Check for type declaration if let scopeStart = formatter.index(of: .startOfScope, before: startIndex), - formatter.tokens[scopeStart] == .startOfScope("(") + formatter.tokens[scopeStart] == .startOfScope("(") { if formatter.last(.keyword, before: scopeStart) == .keyword("func") { return @@ -1944,9 +1944,9 @@ public struct _FormatRules { lastIndex = index case .endOfScope(")"): if formatter.last(.nonSpaceOrCommentOrLinebreak, before: index) == .identifier("set"), - let openParenIndex = formatter.index(of: .startOfScope("("), before: index), - let index = formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: openParenIndex), - case let .keyword(string)? = formatter.token(at: index), aclModifiers.contains(string) + let openParenIndex = formatter.index(of: .startOfScope("("), before: index), + let index = formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: openParenIndex), + case let .keyword(string)? = formatter.token(at: index), aclModifiers.contains(string) { lastModifier.map { modifiers[$0.0] = $0.1 } lastModifier = (string + "(set)", [Token](formatter.tokens[index ..< lastIndex])) @@ -1986,8 +1986,8 @@ public struct _FormatRules { formatter.forEach(.startOfScope("(")) { i, _ in guard let prevToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: i), - case let .identifier(name) = prevToken, // TODO: are trailing closures allowed in other cases? - !blacklist.contains(name), !formatter.isConditionalStatement(at: i) + case let .identifier(name) = prevToken, // TODO: are trailing closures allowed in other cases? + !blacklist.contains(name), !formatter.isConditionalStatement(at: i) else { return } @@ -1999,7 +1999,7 @@ public struct _FormatRules { return } guard formatter.next(.nonSpaceOrCommentOrLinebreak, after: closingIndex) != .startOfScope("{"), - var startIndex = formatter.index(of: .nonSpaceOrLinebreak, before: openingBraceIndex) + var startIndex = formatter.index(of: .nonSpaceOrLinebreak, before: openingBraceIndex) else { return } @@ -2067,14 +2067,14 @@ public struct _FormatRules { switch token { case .endOfScope("]"): if let startIndex = formatter.index(of: .startOfScope("["), before: previousIndex), - formatter.last(.nonSpaceOrCommentOrLinebreak, before: startIndex) == .startOfScope("{") + formatter.last(.nonSpaceOrCommentOrLinebreak, before: startIndex) == .startOfScope("{") { fallthrough // Could be a capture list } case .startOfScope("{"): guard formatter.next(.nonSpaceOrCommentOrLinebreak, after: closingIndex) == .keyword("in"), - formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: i) != closingIndex, - formatter.index(of: .delimiter(":"), in: i + 1 ..< closingIndex) == nil + formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: i) != closingIndex, + formatter.index(of: .delimiter(":"), in: i + 1 ..< closingIndex) == nil else { // Not a closure if formatter.last(.nonSpaceOrComment, before: i) == .endOfScope("]") { @@ -2086,7 +2086,7 @@ public struct _FormatRules { return } if let index = formatter.tokens[i + 1 ..< closingIndex].index(of: .identifier("_")), - formatter.next(.nonSpaceOrComment, after: index)?.isIdentifier == true + formatter.next(.nonSpaceOrComment, after: index)?.isIdentifier == true { return } @@ -2098,10 +2098,10 @@ public struct _FormatRules { case .identifier: // TODO: are trailing closures allowed in other cases? // Parens before closure guard closingIndex == formatter.index(of: .nonSpace, after: i), - let openingIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: closingIndex, if: { - $0 == .startOfScope("{") - }), - formatter.isStartOfClosure(at: openingIndex) + let openingIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: closingIndex, if: { + $0 == .startOfScope("{") + }), + formatter.isStartOfClosure(at: openingIndex) else { return } @@ -2117,15 +2117,15 @@ public struct _FormatRules { case .delimiter(","), .endOfScope, .keyword: let nextToken = formatter.next(.nonSpaceOrCommentOrLinebreak, after: closingIndex) ?? .space("") guard formatter.index(of: .endOfScope("}"), before: closingIndex) == nil, - ![.endOfScope("}"), .endOfScope(">")].contains(token) || - ![.startOfScope("{"), .delimiter(",")].contains(nextToken) + ![.endOfScope("}"), .endOfScope(">")].contains(token) || + ![.startOfScope("{"), .delimiter(",")].contains(nextToken) else { return } let string = token.string if ![.startOfScope("{"), .delimiter(","), .startOfScope(":")].contains(nextToken), - !(string == "for" && nextToken == .keyword("in")), - !(string == "guard" && nextToken == .keyword("else")) + !(string == "for" && nextToken == .keyword("in")), + !(string == "guard" && nextToken == .keyword("else")) { // TODO: this is confusing - refactor to move fallthrough to end of case fallthrough @@ -2157,7 +2157,7 @@ public struct _FormatRules { innerParens = nil } if token == .startOfScope("("), - formatter.last(.nonSpaceOrComment, before: previousIndex) == .identifier("Selector") + formatter.last(.nonSpaceOrComment, before: previousIndex) == .identifier("Selector") { return } @@ -2170,29 +2170,29 @@ public struct _FormatRules { } } guard formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: i) != closingIndex, - formatter.index(in: i + 1 ..< closingIndex, where: { - switch $0 { - case .operator(".", _): - return false - case .operator, .keyword("as"), .keyword("is"), .keyword("try"): - switch token { - case .operator(_, .prefix), .operator(_, .infix), .keyword("as"), .keyword("is"): - return true - default: - break - } - switch nextToken { - case .operator(_, .postfix), .operator(_, .infix), .keyword("as"), .keyword("is"): - return true - default: - return false - } - case .delimiter(","), .delimiter(":"), .delimiter(";"), .startOfScope("{"): - return true - default: - return false - } - }) == nil + formatter.index(in: i + 1 ..< closingIndex, where: { + switch $0 { + case .operator(".", _): + return false + case .operator, .keyword("as"), .keyword("is"), .keyword("try"): + switch token { + case .operator(_, .prefix), .operator(_, .infix), .keyword("as"), .keyword("is"): + return true + default: + break + } + switch nextToken { + case .operator(_, .postfix), .operator(_, .infix), .keyword("as"), .keyword("is"): + return true + default: + return false + } + case .delimiter(","), .delimiter(":"), .delimiter(";"), .startOfScope("{"): + return true + default: + return false + } + }) == nil else { return } @@ -2211,13 +2211,13 @@ public struct _FormatRules { ) { formatter in formatter.forEach(.identifier("get")) { i, _ in if formatter.isAccessorKeyword(at: i, checkKeyword: false), - let prevIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: i, if: { - $0 == .startOfScope("{") - }), let openIndex = formatter.index(of: .startOfScope("{"), after: i), - let closeIndex = formatter.index(of: .endOfScope("}"), after: openIndex), - let nextIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: closeIndex, if: { - $0 == .endOfScope("}") - }) + let prevIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: i, if: { + $0 == .startOfScope("{") + }), let openIndex = formatter.index(of: .startOfScope("{"), after: i), + let closeIndex = formatter.index(of: .endOfScope("}"), after: openIndex), + let nextIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: closeIndex, if: { + $0 == .endOfScope("}") + }) { formatter.removeTokens(in: closeIndex ..< nextIndex) formatter.removeTokens(in: prevIndex + 1 ... openIndex) @@ -2236,11 +2236,11 @@ public struct _FormatRules { return } if !formatter.tokens[optionalIndex - 1].isSpaceOrCommentOrLinebreak, - let equalsIndex = formatter.index(of: .nonSpaceOrLinebreak, after: optionalIndex, if: { - $0 == .operator("=", .infix) - }), let nilIndex = formatter.index(of: .nonSpaceOrLinebreak, after: equalsIndex, if: { - $0 == .identifier("nil") - }) + let equalsIndex = formatter.index(of: .nonSpaceOrLinebreak, after: optionalIndex, if: { + $0 == .operator("=", .infix) + }), let nilIndex = formatter.index(of: .nonSpaceOrLinebreak, after: equalsIndex, if: { + $0 == .identifier("nil") + }) { formatter.removeTokens(in: optionalIndex + 1 ... nilIndex) } @@ -2286,10 +2286,10 @@ public struct _FormatRules { ) { formatter in formatter.forEach(.identifier("_")) { i, _ in guard formatter.next(.nonSpaceOrCommentOrLinebreak, after: i) != .delimiter(":"), - let prevIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: i, if: { - [.keyword("let"), .keyword("var")].contains($0) - }), - let nextNonSpaceIndex = formatter.index(of: .nonSpaceOrLinebreak, after: prevIndex) + let prevIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: i, if: { + [.keyword("let"), .keyword("var")].contains($0) + }), + let nextNonSpaceIndex = formatter.index(of: .nonSpaceOrLinebreak, after: prevIndex) else { return } @@ -2328,21 +2328,21 @@ public struct _FormatRules { formatter.forEach(.startOfScope("(")) { i, _ in let prevIndex = formatter.index(of: .nonSpaceOrComment, before: i) if let prevIndex = prevIndex, let prevToken = formatter.token(at: prevIndex), - [.keyword("case"), .endOfScope("case")].contains(prevToken) + [.keyword("case"), .endOfScope("case")].contains(prevToken) { // Not safe to remove return } guard let endIndex = formatter.index(of: .endOfScope(")"), after: i), - let nextToken = formatter.next(.nonSpaceOrCommentOrLinebreak, after: endIndex), - [.startOfScope(":"), .operator("=", .infix)].contains(nextToken), - redundantBindings(in: i + 1 ..< endIndex) + let nextToken = formatter.next(.nonSpaceOrCommentOrLinebreak, after: endIndex), + [.startOfScope(":"), .operator("=", .infix)].contains(nextToken), + redundantBindings(in: i + 1 ..< endIndex) else { return } formatter.removeTokens(in: i ... endIndex) if let prevIndex = prevIndex, formatter.tokens[prevIndex].isIdentifier, - formatter.last(.nonSpaceOrComment, before: prevIndex)?.string == "." + formatter.last(.nonSpaceOrComment, before: prevIndex)?.string == "." { // Was an enum case return @@ -2411,7 +2411,7 @@ public struct _FormatRules { endIndex = nextIndex case .identifier("Void"): guard let nextIndex = formatter.index(of: .nonSpace, after: nextIndex), - case .endOfScope(")") = formatter.tokens[nextIndex] else { return } + case .endOfScope(")") = formatter.tokens[nextIndex] else { return } endIndex = nextIndex default: return @@ -2423,11 +2423,11 @@ public struct _FormatRules { return } guard let prevToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: i), - [.endOfScope(")"), .keyword("throws"), .keyword("rethrows")].contains(prevToken) else { return } + [.endOfScope(")"), .keyword("throws"), .keyword("rethrows")].contains(prevToken) else { return } guard let prevIndex = formatter.index(of: .endOfScope(")"), before: i), - let startIndex = formatter.index(of: .startOfScope("("), before: prevIndex), - let startToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: startIndex), - startToken.isIdentifier || [.startOfScope("{"), .endOfScope("]")].contains(startToken) + let startIndex = formatter.index(of: .startOfScope("("), before: prevIndex), + let startToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: startIndex), + startToken.isIdentifier || [.startOfScope("{"), .endOfScope("]")].contains(startToken) else { return } @@ -2454,7 +2454,7 @@ public struct _FormatRules { return } if formatter.tokens[prevIndex] == .endOfScope(")"), - let j = formatter.index(of: .startOfScope("("), before: prevIndex) + let j = formatter.index(of: .startOfScope("("), before: prevIndex) { prevIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: j) ?? j if formatter.tokens[prevIndex] == .operator("?", .postfix) { @@ -2686,16 +2686,16 @@ public struct _FormatRules { } case .keyword("where") where lastKeyword == "protocol", .keyword("protocol"): if let startIndex = formatter.index(of: .startOfScope("{"), after: index), - let endIndex = formatter.endOfScope(at: startIndex) + let endIndex = formatter.endOfScope(at: startIndex) { index = endIndex } case .keyword("extension"), .keyword("struct"), .keyword("enum"), .keyword("class"), .keyword("where") where ["extension", "struct", "enum", "class"].contains(lastKeyword): guard formatter.last(.nonSpaceOrCommentOrLinebreak, before: index) != .keyword("import"), - let scopeStart = formatter.index(of: .startOfScope("{"), after: index) else { return } + let scopeStart = formatter.index(of: .startOfScope("{"), after: index) else { return } guard let nameToken = formatter.next(.identifier, after: index), - case let .identifier(name) = nameToken + case let .identifier(name) = nameToken else { return formatter.fatalError("Expected identifier", at: index) } @@ -2746,7 +2746,7 @@ public struct _FormatRules { lastKeyword = "" var localNames = localNames guard let keywordIndex = formatter.index(of: .keyword("in"), before: index), - let prevKeywordIndex = formatter.index(of: .keyword("for"), before: keywordIndex) + let prevKeywordIndex = formatter.index(of: .keyword("for"), before: keywordIndex) else { return formatter.fatalError("Expected for keyword", at: index) } @@ -2775,7 +2775,7 @@ public struct _FormatRules { index = formatter.endOfScope(at: index) ?? (formatter.tokens.count - 1) case .startOfScope("("): if case let .identifier(fn)? = formatter.last(.nonSpaceOrCommentOrLinebreak, before: index), - selfRequired.contains(fn) + selfRequired.contains(fn) { index = formatter.index(of: .endOfScope(")"), after: index) ?? index break @@ -2858,11 +2858,11 @@ public struct _FormatRules { index = formatter.endOfScope(at: index) ?? (formatter.tokens.count - 1) case .identifier("self"): guard formatter.isEnabled, !isTypeRoot, !localNames.contains("self"), - let dotIndex = formatter.index(of: .nonSpaceOrLinebreak, after: index, if: { - $0 == .operator(".", .infix) - }), let nextIndex = formatter.index(of: .nonSpaceOrLinebreak, after: dotIndex, if: { - $0.isIdentifier && !localNames.contains($0.unescaped()) - }) + let dotIndex = formatter.index(of: .nonSpaceOrLinebreak, after: index, if: { + $0 == .operator(".", .infix) + }), let nextIndex = formatter.index(of: .nonSpaceOrLinebreak, after: dotIndex, if: { + $0.isIdentifier && !localNames.contains($0.unescaped()) + }) else { break } @@ -2915,7 +2915,7 @@ public struct _FormatRules { } let isAssignment: Bool if ["for", "var", "let"].contains(lastKeyword), - let prevToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: index) + let prevToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: index) { switch prevToken { case .identifier, .number, @@ -2941,7 +2941,7 @@ public struct _FormatRules { break } if let lastToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: index), - lastToken.isOperator(".") + lastToken.isOperator(".") { break } @@ -3030,7 +3030,7 @@ public struct _FormatRules { let startToken = formatter.tokens[index] var localNames = localNames guard let startIndex = formatter.index(of: .startOfScope("("), after: index), - let endIndex = formatter.index(of: .endOfScope(")"), after: startIndex) + let endIndex = formatter.index(of: .endOfScope(")"), after: startIndex) else { index += 1 // Prevent endless loop return @@ -3039,7 +3039,7 @@ public struct _FormatRules { index = startIndex while index < endIndex { guard let externalNameIndex = formatter.index(of: .identifier, after: index), - let nextIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: externalNameIndex) + let nextIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: externalNameIndex) else { break } let token = formatter.tokens[nextIndex] switch token { @@ -3110,9 +3110,9 @@ public struct _FormatRules { for i in range { let token = formatter.tokens[i] if case .identifier = token, let index = argNames.index(of: token.unescaped()), - formatter.last(.nonSpaceOrCommentOrLinebreak, before: i)?.isOperator(".") == false, - formatter.next(.nonSpaceOrCommentOrLinebreak, after: i) != .delimiter(":") || - formatter.currentScope(at: i) == .startOfScope("[") + formatter.last(.nonSpaceOrCommentOrLinebreak, before: i)?.isOperator(".") == false, + formatter.next(.nonSpaceOrCommentOrLinebreak, after: i) != .delimiter(":") || + formatter.currentScope(at: i) == .startOfScope("[") { argNames.remove(at: index) associatedData.remove(at: index) @@ -3149,16 +3149,16 @@ public struct _FormatRules { nameIndexPairs.removeSubrange(count ..< nameIndexPairs.count) case .identifier: guard argCountStack.count < 3, - let prevToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: index), [ - .delimiter(","), .startOfScope("("), .startOfScope("{"), .endOfScope("]"), - ].contains(prevToken), let scopeStart = formatter.index(of: .startOfScope, before: index), - ![.startOfScope("["), .startOfScope("<")].contains(formatter.tokens[scopeStart]) + let prevToken = formatter.last(.nonSpaceOrCommentOrLinebreak, before: index), [ + .delimiter(","), .startOfScope("("), .startOfScope("{"), .endOfScope("]"), + ].contains(prevToken), let scopeStart = formatter.index(of: .startOfScope, before: index), + ![.startOfScope("["), .startOfScope("<")].contains(formatter.tokens[scopeStart]) else { break } let name = token.unescaped() if let nextIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: index), - let nextToken = formatter.token(at: nextIndex), case .identifier = nextToken + let nextToken = formatter.token(at: nextIndex), case .identifier = nextToken { let internalName = nextToken.unescaped() if internalName != "_" { @@ -3196,8 +3196,8 @@ public struct _FormatRules { } formatter.forEachToken { i, token in guard case let .keyword(keyword) = token, ["func", "init", "subscript"].contains(keyword), - let startIndex = formatter.index(of: .startOfScope("("), after: i), - let endIndex = formatter.index(of: .endOfScope(")"), after: startIndex) else { return } + let startIndex = formatter.index(of: .startOfScope("("), after: i), + let endIndex = formatter.index(of: .endOfScope(")"), after: startIndex) else { return } let isOperator = (keyword == "subscript") || (keyword == "func" && formatter.next(.nonSpaceOrCommentOrLinebreak, after: i)?.isOperator == true) var index = startIndex @@ -3370,7 +3370,7 @@ public struct _FormatRules { formatter.insert(.keyword(keyword), at: startIndex) formatter.insert(.space(" "), at: startIndex + 1) if let prevToken = formatter.token(at: startIndex - 1), - !prevToken.isSpaceOrCommentOrLinebreak, !prevToken.isStartOfScope + !prevToken.isSpaceOrCommentOrLinebreak, !prevToken.isStartOfScope { formatter.insert(.space(" "), at: startIndex) } @@ -3490,16 +3490,16 @@ public struct _FormatRules { let startOfLine = formatter.startOfLine(at: openBraceIndex) // Make sure the brace is on a separate line from the if / guard guard i < startOfLine, - // If token before the brace isn't a newline or guard else then insert a newline - let prevIndex = formatter.index(of: .nonSpace, before: openBraceIndex), - let prevToken = formatter.token(at: prevIndex), - !prevToken.isLinebreak, !(prevToken == .keyword("else") && - prevIndex == formatter.index(of: .nonSpace, after: startOfLine)), - // Only wrap when the brace's line is more indented than the if / guard - formatter.indentForLine(at: i) < formatter.indentForLine(at: openBraceIndex), - // And only when closing brace is not on same line - let closingIndex = formatter.endOfScope(at: openBraceIndex), - formatter.tokens[openBraceIndex ..< closingIndex].contains(where: { $0.isLinebreak }) + // If token before the brace isn't a newline or guard else then insert a newline + let prevIndex = formatter.index(of: .nonSpace, before: openBraceIndex), + let prevToken = formatter.token(at: prevIndex), + !prevToken.isLinebreak, !(prevToken == .keyword("else") && + prevIndex == formatter.index(of: .nonSpace, after: startOfLine)), + // Only wrap when the brace's line is more indented than the if / guard + formatter.indentForLine(at: i) < formatter.indentForLine(at: openBraceIndex), + // And only when closing brace is not on same line + let closingIndex = formatter.endOfScope(at: openBraceIndex), + formatter.tokens[openBraceIndex ..< closingIndex].contains(where: { $0.isLinebreak }) else { return } @@ -3532,7 +3532,7 @@ public struct _FormatRules { var index = i let indent = formatter.indentForLine(at: i) while let commaIndex = formatter.index(of: .delimiter(","), in: (index + 1) ..< end), - let nextIndex = formatter.index(of: .nonSpaceOrLinebreak, after: commaIndex) + let nextIndex = formatter.index(of: .nonSpaceOrLinebreak, after: commaIndex) { formatter.replaceToken(at: commaIndex, with: .keyword("case")) let delta = formatter @@ -3558,7 +3558,7 @@ public struct _FormatRules { var startIndex = i while let commaIndex = formatter.index(of: .delimiter(","), in: startIndex + 1 ..< endIndex), - let nextIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: commaIndex) + let nextIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: commaIndex) { if formatter.index(of: .linebreak, in: commaIndex ..< nextIndex) == nil { formatter.insertLinebreak(at: commaIndex + 1) @@ -3584,17 +3584,17 @@ public struct _FormatRules { return true case .startOfScope("{"): if formatter.tokens[index] == .endOfScope(")"), - let index = formatter.index(of: .startOfScope("("), before: index), - let nameIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: index, if: { - $0.isIdentifier - }), formatter.last(.nonSpaceOrCommentOrLinebreak, before: nameIndex) == .keyword("func") + let index = formatter.index(of: .startOfScope("("), before: index), + let nameIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: index, if: { + $0.isIdentifier + }), formatter.last(.nonSpaceOrCommentOrLinebreak, before: nameIndex) == .keyword("func") { return true } return false case .keyword("in"): if formatter.tokens[index] == .endOfScope(")"), - let index = formatter.index(of: .startOfScope("("), before: index) + let index = formatter.index(of: .startOfScope("("), before: index) { return formatter.last(.nonSpaceOrCommentOrLinebreak, before: index) == .startOfScope("{") } @@ -3610,10 +3610,10 @@ public struct _FormatRules { }), var prevIndex = formatter.index(of: .nonSpaceOrLinebreak, before: i), { let token = formatter.tokens[prevIndex] if token == .delimiter(":"), - let prevPrevIndex = formatter.index(of: .nonSpaceOrLinebreak, before: prevIndex), - formatter.tokens[prevPrevIndex] == .identifier("_"), - let startIndex = formatter.index(of: .nonSpaceOrLinebreak, before: prevPrevIndex), - formatter.tokens[startIndex] == .startOfScope("(") + let prevPrevIndex = formatter.index(of: .nonSpaceOrLinebreak, before: prevIndex), + formatter.tokens[prevPrevIndex] == .identifier("_"), + let startIndex = formatter.index(of: .nonSpaceOrLinebreak, before: prevPrevIndex), + formatter.tokens[startIndex] == .startOfScope("(") { prevIndex = startIndex return true @@ -3780,7 +3780,7 @@ public struct _FormatRules { return case var .replace(string): if let range = string.range(of: "{file}"), - let file = formatter.options.fileInfo.fileName + let file = formatter.options.fileInfo.fileName { string.replaceSubrange(range, with: file) } @@ -3788,12 +3788,12 @@ public struct _FormatRules { string.replaceSubrange(range, with: currentYear) } if let range = string.range(of: "{created}"), - let date = formatter.options.fileInfo.creationDate + let date = formatter.options.fileInfo.creationDate { string.replaceSubrange(range, with: shortDateFormatter(date)) } if let range = string.range(of: "{created.year}"), - let date = formatter.options.fileInfo.creationDate + let date = formatter.options.fileInfo.creationDate { string.replaceSubrange(range, with: yearFormatter(date)) } @@ -3818,7 +3818,7 @@ public struct _FormatRules { switch formatter.token(at: index + 1) ?? .space("") { case .startOfScope("//"): if case let .commentBody(body)? = formatter.next(.nonSpace, after: index + 1), - body.isFormattingDirective + body.isFormattingDirective { break } @@ -3927,11 +3927,11 @@ public struct _FormatRules { var enums: [Range] = [] while let startIndex = nextStartIndex, - let delimiterIndex = nextDelimiterIndex, - delimiterIndex < endIndex, - startIndex < endIndex, - let end = formatter.lastIndex(of: .nonSpaceOrCommentOrLinebreak, - in: Range(uncheckedBounds: (lower: startIndex, upper: delimiterIndex))) + let delimiterIndex = nextDelimiterIndex, + delimiterIndex < endIndex, + startIndex < endIndex, + let end = formatter.lastIndex(of: .nonSpaceOrCommentOrLinebreak, + in: Range(uncheckedBounds: (lower: startIndex, upper: delimiterIndex))) { enums.append(Range(startIndex ... end)) nextStartIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, @@ -3941,8 +3941,8 @@ public struct _FormatRules { // last one from the cases list if let nextStart = nextStartIndex, - let nextEnd = formatter.lastIndex(of: .nonSpaceOrCommentOrLinebreak, in: nextStart ..< endIndex), - nextStart < nextEnd + let nextEnd = formatter.lastIndex(of: .nonSpaceOrCommentOrLinebreak, in: nextStart ..< endIndex), + nextStart < nextEnd { enums.append(Range(nextStart ... nextEnd)) } @@ -4041,8 +4041,8 @@ public struct _FormatRules { ) { formatter in formatter.forEach(.keyword("@IBOutlet")) { i, _ in guard let varIndex = formatter.index(of: .keyword("var"), after: i), - let weakIndex = (i ..< varIndex).first(where: { formatter.tokens[$0] == .identifier("weak") }), - case let .identifier(name)? = formatter.next(.identifier, after: varIndex) + let weakIndex = (i ..< varIndex).first(where: { formatter.tokens[$0] == .identifier("weak") }), + case let .identifier(name)? = formatter.next(.identifier, after: varIndex) else { return } @@ -4070,7 +4070,7 @@ public struct _FormatRules { return } if let token = formatter.next(.nonSpaceOrCommentOrLinebreak, after: closingIndex), - [.keyword("else"), .keyword("catch")].contains(token) + [.keyword("else"), .keyword("catch")].contains(token) { return } @@ -4095,8 +4095,8 @@ public struct _FormatRules { } // Crude check for Function Builder if let nextToken = formatter.next(.nonSpaceOrCommentOrLinebreak, after: endIndex), - case let .identifier(name) = nextToken, let firstChar = name.first.map(String.init), - firstChar == firstChar.uppercased() + case let .identifier(name) = nextToken, let firstChar = name.first.map(String.init), + firstChar == firstChar.uppercased() { return } else if formatter.isInViewBuilder(at: i) { @@ -4250,13 +4250,13 @@ public struct _FormatRules { ) { formatter in formatter.forEach(.keyword("protocol")) { i, _ in guard formatter.options.swiftVersion >= "4.1", - let nameIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: i, if: { - $0.isIdentifier - }), let colonIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: nameIndex, if: { - $0 == .delimiter(":") - }), let classIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: colonIndex, if: { - $0 == .keyword("class") - }) + let nameIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: i, if: { + $0.isIdentifier + }), let colonIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: nameIndex, if: { + $0 == .delimiter(":") + }), let classIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: colonIndex, if: { + $0 == .keyword("class") + }) else { return } @@ -4270,10 +4270,10 @@ public struct _FormatRules { ) { formatter in formatter.forEach(.keyword("break")) { i, _ in guard formatter.last(.nonSpaceOrCommentOrLinebreak, before: i) != .startOfScope(":"), - formatter.next(.nonSpaceOrCommentOrLinebreak, after: i)?.isEndOfScope == true, - var startIndex = formatter.index(of: .nonSpace, before: i), - let endIndex = formatter.index(of: .nonSpace, after: i), - formatter.currentScope(at: i) == .startOfScope(":") + formatter.next(.nonSpaceOrCommentOrLinebreak, after: i)?.isEndOfScope == true, + var startIndex = formatter.index(of: .nonSpace, before: i), + let endIndex = formatter.index(of: .nonSpace, after: i), + formatter.currentScope(at: i) == .startOfScope(":") else { return } @@ -4290,9 +4290,9 @@ public struct _FormatRules { ) { formatter in formatter.forEach(.identifier("`self`")) { i, _ in guard formatter.options.swiftVersion >= "4.2", - let equalIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: i, if: { - $0 == .operator("=", .infix) - }), formatter.next(.nonSpaceOrCommentOrLinebreak, after: equalIndex) == .identifier("self") + let equalIndex = formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: i, if: { + $0 == .operator("=", .infix) + }), formatter.next(.nonSpaceOrCommentOrLinebreak, after: equalIndex) == .identifier("self") else { return } @@ -4325,7 +4325,7 @@ public struct _FormatRules { return case let token where token.isAttribute: if let startIndex = formatter.index(of: .startOfScope("("), after: nextIndex), - let endIndex = formatter.index(of: .endOfScope(")"), after: startIndex) + let endIndex = formatter.index(of: .endOfScope(")"), after: startIndex) { nextIndex = endIndex } @@ -4355,7 +4355,7 @@ public struct _FormatRules { return } guard let scopeStart = formatter.index(of: .startOfScope("{"), before: i), - let keywordIndex = formatter.index(of: .keyword, before: scopeStart) + let keywordIndex = formatter.index(of: .keyword, before: scopeStart) else { return } @@ -4435,15 +4435,15 @@ public struct _FormatRules { dropSwiftNamespaceIfPresent() case .identifier("Optional"): if formatter.options.shortOptionals == .exceptProperties, - let lastKeyword = formatter.lastSignificantKeyword(at: i), - ["var", "let"].contains(lastKeyword) + let lastKeyword = formatter.lastSignificantKeyword(at: i), + ["var", "let"].contains(lastKeyword) { return } var typeTokens = formatter.tokens[typeStart ... typeEnd] if formatter.tokens[typeStart] == .startOfScope("("), - let commaEnd = formatter.index(of: .endOfScope(")"), after: typeStart), - commaEnd < typeEnd + let commaEnd = formatter.index(of: .endOfScope(")"), after: typeStart), + commaEnd < typeEnd { typeTokens.insert(.startOfScope("("), at: typeTokens.startIndex) typeTokens.append(.endOfScope(")")) @@ -4555,11 +4555,11 @@ public struct _FormatRules { func isInitOverridden(for type: String, in range: CountableRange) -> Bool { for i in range { guard case .keyword("init") = formatter.tokens[i], - formatter.modifiersForType(at: i, contains: "override"), - let scopeIndex = formatter.index(of: .startOfScope("{"), before: i), - let colonIndex = formatter.index(of: .delimiter(":"), before: scopeIndex), - formatter.next(.nonSpaceOrCommentOrLinebreak, in: colonIndex + 1 ..< scopeIndex) - == .identifier(type) + formatter.modifiersForType(at: i, contains: "override"), + let scopeIndex = formatter.index(of: .startOfScope("{"), before: i), + let colonIndex = formatter.index(of: .delimiter(":"), before: scopeIndex), + formatter.next(.nonSpaceOrCommentOrLinebreak, in: colonIndex + 1 ..< scopeIndex) + == .identifier(type) else { continue } @@ -4588,21 +4588,21 @@ public struct _FormatRules { formatter.forEach(.keyword("fileprivate")) { i, _ in // Check if definition is a member of a file-scope type guard formatter.options.swiftVersion >= "4", - let scopeIndex = formatter.index(of: .startOfScope, before: i, if: { - $0 == .startOfScope("{") - }), let typeIndex = formatter.index(of: .keyword, before: scopeIndex, if: { - ["class", "struct", "enum"].contains($0.string) - }), case let .identifier(typeName)? = formatter.next(.identifier, after: typeIndex), - let endIndex = formatter.index(of: .endOfScope, after: scopeIndex), - formatter.currentScope(at: typeIndex) == nil + let scopeIndex = formatter.index(of: .startOfScope, before: i, if: { + $0 == .startOfScope("{") + }), let typeIndex = formatter.index(of: .keyword, before: scopeIndex, if: { + ["class", "struct", "enum"].contains($0.string) + }), case let .identifier(typeName)? = formatter.next(.identifier, after: typeIndex), + let endIndex = formatter.index(of: .endOfScope, after: scopeIndex), + formatter.currentScope(at: typeIndex) == nil else { return } // Get member type guard let keywordIndex = formatter.index(of: .keyword, in: i + 1 ..< endIndex), - let memberType = formatter.declarationType(at: keywordIndex), - // TODO: check if member types are exposed in the interface, otherwise convert them too - ["let", "var", "func", "init"].contains(memberType) + let memberType = formatter.declarationType(at: keywordIndex), + // TODO: check if member types are exposed in the interface, otherwise convert them too + ["let", "var", "func", "init"].contains(memberType) else { return } @@ -4625,8 +4625,8 @@ public struct _FormatRules { // change any fileprivate members in case we break memberwise initializer // TODO: check if struct contains an overridden init; if so we can skip this check if formatter.tokens[typeIndex] == .keyword("struct"), - isTypeInitialized(typeName, in: 0 ..< startIndex) || - isTypeInitialized(typeName, in: endIndex + 1 ..< formatter.tokens.count) + isTypeInitialized(typeName, in: 0 ..< startIndex) || + isTypeInitialized(typeName, in: endIndex + 1 ..< formatter.tokens.count) { return } @@ -4634,9 +4634,9 @@ public struct _FormatRules { if memberType == "init" { // Make initializer private if it's not called anywhere if !isTypeInitialized(typeName, in: 0 ..< startIndex), - !isTypeInitialized(typeName, in: endIndex + 1 ..< formatter.tokens.count), - !isInitOverridden(for: typeName, in: 0 ..< startIndex), - !isInitOverridden(for: typeName, in: endIndex + 1 ..< formatter.tokens.count) + !isTypeInitialized(typeName, in: endIndex + 1 ..< formatter.tokens.count), + !isInitOverridden(for: typeName, in: 0 ..< startIndex), + !isInitOverridden(for: typeName, in: endIndex + 1 ..< formatter.tokens.count) { formatter.replaceToken(at: i, with: .keyword("private")) } @@ -4696,14 +4696,14 @@ public struct _FormatRules { return true case .endOfScope("]"), .endOfScope(")"): guard let startIndex = formatter.index(of: .startOfScope, before: index), - !formatter.isSubscriptOrFunctionCall(at: startIndex) + !formatter.isSubscriptOrFunctionCall(at: startIndex) else { return false } return valuesInRangeAreConstant(startIndex + 1 ..< index) case .startOfScope("["), .startOfScope("("): guard !formatter.isSubscriptOrFunctionCall(at: index), - let endIndex = formatter.index(of: .endOfScope, after: index) + let endIndex = formatter.index(of: .endOfScope, after: index) else { return false } @@ -4757,16 +4757,16 @@ public struct _FormatRules { formatter.forEachToken { i, token in guard case let .operator(op, .infix) = token, - let opIndex = ["==", "!=", "<", "<=", ">", ">="].index(of: op), - let prevIndex = formatter.index(of: .nonSpace, before: i), - isConstant(at: prevIndex), let startIndex = startOfValue(at: prevIndex), - !isOperator(at: formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: startIndex)), - let nextIndex = formatter.index(of: .nonSpace, after: i), !isConstant(at: nextIndex) || - isOperator(at: formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: nextIndex)), - let endIndex = formatter.endOfExpression(at: nextIndex, upTo: [ - .operator("&&", .infix), .operator("||", .infix), - .operator("?", .infix), .operator(":", .infix), - ]) + let opIndex = ["==", "!=", "<", "<=", ">", ">="].index(of: op), + let prevIndex = formatter.index(of: .nonSpace, before: i), + isConstant(at: prevIndex), let startIndex = startOfValue(at: prevIndex), + !isOperator(at: formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: startIndex)), + let nextIndex = formatter.index(of: .nonSpace, after: i), !isConstant(at: nextIndex) || + isOperator(at: formatter.index(of: .nonSpaceOrCommentOrLinebreak, after: nextIndex)), + let endIndex = formatter.endOfExpression(at: nextIndex, upTo: [ + .operator("&&", .infix), .operator("||", .infix), + .operator("?", .infix), .operator(":", .infix), + ]) else { return } @@ -4815,8 +4815,8 @@ public struct _FormatRules { // Determine the end index of the attribute let endIndex: Int if let argumentStartIndex = formatter.index(of: .nonSpaceOrComment, after: i), - formatter.token(at: argumentStartIndex) == .startOfScope("("), - let endOfArgumentScope = formatter.endOfScope(at: argumentStartIndex) + formatter.token(at: argumentStartIndex) == .startOfScope("("), + let endOfArgumentScope = formatter.endOfScope(at: argumentStartIndex) { endIndex = endOfArgumentScope } else { @@ -4825,8 +4825,8 @@ public struct _FormatRules { // Ignore sequential attributes guard var keywordIndex = formatter.index(of: .keyword, after: endIndex), - var keyword = formatter.token(at: keywordIndex), - !formatter.tokens[keywordIndex].isAttribute + var keyword = formatter.token(at: keywordIndex), + !formatter.tokens[keywordIndex].isAttribute else { return } @@ -4864,7 +4864,7 @@ public struct _FormatRules { case .prevLine: // Make sure there's a newline immediately following the attribute if let nextIndex = formatter.index(of: .nonSpaceOrComment, after: endIndex), - formatter.token(at: nextIndex)?.isLinebreak != true + formatter.token(at: nextIndex)?.isLinebreak != true { formatter.insertLinebreak(at: nextIndex) // Remove any trailing whitespace left on the line with the attributes @@ -4875,7 +4875,7 @@ public struct _FormatRules { case .sameLine: // Make sure there isn't a newline immediately following the attribute if let nextIndex = formatter.index(of: .nonSpaceOrComment, after: endIndex), - formatter.token(at: nextIndex)?.isLinebreak != false + formatter.token(at: nextIndex)?.isLinebreak != false { // Replace the newline with a space so the attribute doesn't // merge with the next token. @@ -4890,7 +4890,7 @@ public struct _FormatRules { ) { formatter in formatter.forEach(.startOfScope("{")) { i, _ in guard formatter.options.swiftVersion >= "5.2", - let prevIndex = formatter.index(of: .nonSpaceOrLinebreak, before: i) + let prevIndex = formatter.index(of: .nonSpaceOrLinebreak, before: i) else { return } @@ -4973,8 +4973,8 @@ public struct _FormatRules { // place it on the extension itself. case .onExtension: if extensionVisibility == nil, - let delimiterIndex = declaration.openTokens.index(of: .delimiter(":")), - declaration.openTokens.index(of: .keyword("where")).map({ $0 > delimiterIndex }) ?? true + let delimiterIndex = declaration.openTokens.index(of: .delimiter(":")), + declaration.openTokens.index(of: .keyword("where")).map({ $0 > delimiterIndex }) ?? true { // Extension adds protocol conformance so can't have visibility modifier return declaration @@ -4991,12 +4991,12 @@ public struct _FormatRules { } guard let memberVisibility = counts.max(by: { $0.count < $1.count })?.0, - memberVisibility <= extensionVisibility ?? .public, - // Check that most common level is also most visible - memberVisibility == visibilityOfBodyDeclarations.max(), - // `private` can't be hoisted without changing code behavior - // (private applied at extension level is equivalent to `fileprivate`) - memberVisibility > .private + memberVisibility <= extensionVisibility ?? .public, + // Check that most common level is also most visible + memberVisibility == visibilityOfBodyDeclarations.max(), + // `private` can't be hoisted without changing code behavior + // (private applied at extension level is equivalent to `fileprivate`) + memberVisibility > .private else { return declaration } let extensionWithUpdatedVisibility: Formatter.Declaration @@ -5126,7 +5126,7 @@ public struct _FormatRules { ?? openingFormatter.tokens.count while let token = openingFormatter.token(at: conformanceSearchIndex), - conformanceSearchIndex < endOfConformances + conformanceSearchIndex < endOfConformances { if token.isIdentifier { let (fullyQualifiedName, next) = openingFormatter.fullyQualifiedName(startingAt: conformanceSearchIndex) @@ -5192,7 +5192,7 @@ public struct _FormatRules { endOfFileHeader += 1 if openingFormatter.token(at: endOfFileHeader)?.isLinebreak == true, - openingFormatter.next(.nonSpace, after: endOfFileHeader)?.isLinebreak == true + openingFormatter.next(.nonSpace, after: endOfFileHeader)?.isLinebreak == true { markInsertIndex = endOfFileHeader + 2 break diff --git a/Sources/SwiftFormat.swift b/Sources/SwiftFormat.swift index cc0bda98..69a260b2 100644 --- a/Sources/SwiftFormat.swift +++ b/Sources/SwiftFormat.swift @@ -32,7 +32,7 @@ import Foundation /// The current SwiftFormat version -let swiftFormatVersion = "0.46.3" +let swiftFormatVersion = "0.47.0" public let version = swiftFormatVersion /// The standard SwiftFormat config file name @@ -150,7 +150,7 @@ public func enumerateFiles(withInputURL inputURL: URL, do { let resourceValues = try getResourceValues(for: inputURL.standardizedFileURL, keys: keys) if !fileOptions.followSymlinks, - resourceValues.isAliasFile == true || resourceValues.isSymbolicLink == true + resourceValues.isAliasFile == true || resourceValues.isSymbolicLink == true { return [FormatError.options("Symbolic link or alias was skipped: \(inputURL.path)")] } @@ -161,7 +161,7 @@ public func enumerateFiles(withInputURL inputURL: URL, return [] } if resourceValues.isDirectory == false, - !fileOptions.supportedFileExtensions.contains(inputURL.pathExtension) + !fileOptions.supportedFileExtensions.contains(inputURL.pathExtension) { return [FormatError.options("Unsupported file type: \(inputURL.path)")] } @@ -298,7 +298,7 @@ private func shouldSkipFile(_ inputURL: URL, with options: Options) -> Bool { continue } if let unexcluded = options.fileOptions?.unexcludedGlobs, - unexcluded.contains(where: { $0.matches(path) }) + unexcluded.contains(where: { $0.matches(path) }) { return false } @@ -506,14 +506,14 @@ private func applyRules( // Check if required FileInfo is available if rules.contains(FormatRules.fileHeader) { if options.fileHeader.rawValue.contains("{created"), - options.fileInfo.creationDate == nil + options.fileInfo.creationDate == nil { throw FormatError.options( "Failed to apply {created} template in file header as file info is unavailable" ) } if options.fileHeader.rawValue.contains("{file"), - options.fileInfo.fileName == nil + options.fileInfo.fileName == nil { throw FormatError.options( "Failed to apply {file} template in file header as file name was not provided" diff --git a/Sources/Tokenizer.swift b/Sources/Tokenizer.swift index 6130d626..58069f50 100644 --- a/Sources/Tokenizer.swift +++ b/Sources/Tokenizer.swift @@ -2,7 +2,7 @@ // Tokenizer.swift // SwiftFormat // -// Version 0.46.3 +// Version 0.47.0 // // Created by Nick Lockwood on 11/08/2016. // Copyright 2016 Nick Lockwood @@ -261,10 +261,10 @@ public extension Token { output.append("\'") case "u": guard input.read("{"), - let hex = input.readCharacters(where: { $0.isHexDigit }), - input.read("}"), - let codepoint = Int(hex, radix: 16), - let c = UnicodeScalar(codepoint) + let hex = input.readCharacters(where: { $0.isHexDigit }), + input.read("}"), + let codepoint = Int(hex, radix: 16), + let c = UnicodeScalar(codepoint) else { // Invalid. Recover and continue continue @@ -1118,8 +1118,8 @@ public func tokenize(_ source: String) -> [Token] { for index in (scopeIndexStack.last! ..< tokens.count - 1).reversed() { let nextToken = tokens[index + 1] guard case let .space(indent) = tokens[index], tokens[index - 1].isLinebreak, - (nextToken.isMultilineStringDelimiter && nextToken.isEndOfScope) || - nextToken.isStringBody + (nextToken.isMultilineStringDelimiter && nextToken.isEndOfScope) || + nextToken.isStringBody else { if nextToken.isMultilineStringDelimiter, nextToken.isStartOfScope { offsetStack.removeLast() @@ -1246,7 +1246,7 @@ public func tokenize(_ source: String) -> [Token] { } for index in range.reversed() { guard case let .space(indent) = tokens[index], tokens[index - 1].isLinebreak, - indent.hasPrefix(baseIndent) + indent.hasPrefix(baseIndent) else { continue } @@ -1315,7 +1315,7 @@ public func tokenize(_ source: String) -> [Token] { tokens[i] = .operator(">", .none) stitchOperators(at: i) if let previousIndex = index(of: .nonSpaceOrComment, before: i), - tokens[previousIndex] == .endOfScope(">") + tokens[previousIndex] == .endOfScope(">") { convertClosingChevronToOperator(at: previousIndex, andOpeningChevron: true) } @@ -1340,8 +1340,8 @@ public func tokenize(_ source: String) -> [Token] { return } while let nextToken: Token = index + 1 < tokens.count ? tokens[index + 1] : nil, - case let .operator(nextString, _) = nextToken, - string.hasPrefix(".") || !nextString.contains(".") + case let .operator(nextString, _) = nextToken, + string.hasPrefix(".") || !nextString.contains(".") { if scopeIndexStack.last == index { // In case of a ? previously interpreted as a ternary @@ -1353,8 +1353,8 @@ public func tokenize(_ source: String) -> [Token] { } var index = index while let prevToken: Token = index > 0 ? tokens[index - 1] : nil, - case let .operator(prevString, _) = prevToken, !isUnwrapOperator(at: index - 1), - prevString.hasPrefix(".") || !string.contains(".") + case let .operator(prevString, _) = prevToken, !isUnwrapOperator(at: index - 1), + prevString.hasPrefix(".") || !string.contains(".") { if scopeIndexStack.last == index - 1 { // In case of a ? previously interpreted as a ternary @@ -1477,7 +1477,7 @@ public func tokenize(_ source: String) -> [Token] { case let .keyword(string): // Track switch/case statements if let prevIndex = index(of: .nonSpaceOrCommentOrLinebreak, before: count - 1), - case .operator(".", _) = tokens[prevIndex] + case .operator(".", _) = tokens[prevIndex] { tokens[tokens.count - 1] = .identifier(string) processToken() @@ -1501,9 +1501,9 @@ public func tokenize(_ source: String) -> [Token] { stitchOperators(at: count - 1) case .startOfScope("<") where count >= 2: if tokens[count - 2].isOperator, - index(of: .nonSpaceOrCommentOrLinebreak, before: count - 2).map({ - ![.keyword("func"), .keyword("init")].contains(tokens[$0]) - }) ?? true + index(of: .nonSpaceOrCommentOrLinebreak, before: count - 2).map({ + ![.keyword("func"), .keyword("init")].contains(tokens[$0]) + }) ?? true { tokens[tokens.count - 1] = .operator("<", .none) stitchOperators(at: count - 1) @@ -1525,17 +1525,17 @@ public func tokenize(_ source: String) -> [Token] { } if !token.isSpaceOrCommentOrLinebreak { if let prevIndex = index(of: .nonSpaceOrComment, before: count - 1), - case .endOfScope(">") = tokens[prevIndex] + case .endOfScope(">") = tokens[prevIndex] { // Fix up misidentified generic that is actually a pair of operators switch token { case .operator("=", _) where prevIndex == count - 2: guard let startIndex = index(of: .startOfScope, before: count - 1), - tokens[startIndex] == .startOfScope("<"), - let prevIndex = index(of: .nonSpaceOrComment, before: startIndex), - case .identifier = tokens[prevIndex], - let prevPrevIndex = index(of: .nonSpaceOrCommentOrLinebreak, before: prevIndex), - tokens[prevPrevIndex] == .delimiter(":") + tokens[startIndex] == .startOfScope("<"), + let prevIndex = index(of: .nonSpaceOrComment, before: startIndex), + case .identifier = tokens[prevIndex], + let prevPrevIndex = index(of: .nonSpaceOrCommentOrLinebreak, before: prevIndex), + tokens[prevPrevIndex] == .delimiter(":") else { fallthrough } @@ -1655,7 +1655,7 @@ public func tokenize(_ source: String) -> [Token] { if let keywordIndex = index(of: .keyword, before: scopeIndex) { var keyword = tokens[keywordIndex] if case .keyword("where") = keyword, - let keywordIndex = index(of: .keyword, before: keywordIndex) + let keywordIndex = index(of: .keyword, before: keywordIndex) { keyword = tokens[keywordIndex] } @@ -1718,7 +1718,7 @@ public func tokenize(_ source: String) -> [Token] { return case let .endOfScope(string): if ["case", "default"].contains(string), let scopeIndex = scopeIndexStack.last, - tokens[scopeIndex] == .startOfScope("#if") + tokens[scopeIndex] == .startOfScope("#if") { scopeIndexStack.append(tokens.count - 1) return diff --git a/SwiftFormat.podspec.json b/SwiftFormat.podspec.json index 15daabcc..d168c60c 100644 --- a/SwiftFormat.podspec.json +++ b/SwiftFormat.podspec.json @@ -1,6 +1,6 @@ { "name": "SwiftFormat", - "version": "0.46.3", + "version": "0.47.0", "license": { "type": "MIT", "file": "LICENSE.md" @@ -10,7 +10,7 @@ "authors": "Nick Lockwood", "source": { "git": "https://github.com/nicklockwood/SwiftFormat.git", - "tag": "0.46.3" + "tag": "0.47.0" }, "default_subspecs": "Core", "subspecs": [ @@ -24,7 +24,7 @@ "platforms": { "ios": "7.0", "tvos": "9.0", - "osx": "10.9" + "osx": "10.12" } } ], diff --git a/SwiftFormat.xcodeproj/project.pbxproj b/SwiftFormat.xcodeproj/project.pbxproj index 89adebf4..188a6cd2 100644 --- a/SwiftFormat.xcodeproj/project.pbxproj +++ b/SwiftFormat.xcodeproj/project.pbxproj @@ -1043,7 +1043,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - MACOSX_DEPLOYMENT_TARGET = 10.11; + MACOSX_DEPLOYMENT_TARGET = 10.12; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = macosx; @@ -1098,7 +1098,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - MACOSX_DEPLOYMENT_TARGET = 10.11; + MACOSX_DEPLOYMENT_TARGET = 10.12; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = macosx; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; @@ -1122,6 +1122,7 @@ INFOPLIST_FILE = Sources/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks"; + MARKETING_VERSION = 0.47.0; PRODUCT_BUNDLE_IDENTIFIER = com.charcoaldesign.SwiftFormat; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; @@ -1145,6 +1146,7 @@ INFOPLIST_FILE = Sources/Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks"; + MARKETING_VERSION = 0.47.0; PRODUCT_BUNDLE_IDENTIFIER = com.charcoaldesign.SwiftFormat; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; @@ -1219,8 +1221,8 @@ ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = EditorExtension/Application/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; - MACOSX_DEPLOYMENT_TARGET = 10.11; - MARKETING_VERSION = 0.46.3; + MACOSX_DEPLOYMENT_TARGET = 10.14; + MARKETING_VERSION = 0.47.0; PRODUCT_BUNDLE_IDENTIFIER = "com.charcoaldesign.SwiftFormat-for-Xcode"; PRODUCT_NAME = "SwiftFormat for Xcode"; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -1246,8 +1248,8 @@ ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = EditorExtension/Application/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; - MACOSX_DEPLOYMENT_TARGET = 10.11; - MARKETING_VERSION = 0.46.3; + MACOSX_DEPLOYMENT_TARGET = 10.14; + MARKETING_VERSION = 0.47.0; PRODUCT_BUNDLE_IDENTIFIER = "com.charcoaldesign.SwiftFormat-for-Xcode"; PRODUCT_NAME = "SwiftFormat for Xcode"; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -1270,7 +1272,8 @@ ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = EditorExtension/Extension/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @executable_path/../../../../Frameworks"; - MARKETING_VERSION = 0.46.3; + MACOSX_DEPLOYMENT_TARGET = 10.14; + MARKETING_VERSION = 0.47.0; PRODUCT_BUNDLE_IDENTIFIER = "com.charcoaldesign.SwiftFormat-for-Xcode.SourceEditorExtension"; PRODUCT_NAME = SwiftFormat; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -1295,7 +1298,8 @@ ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = EditorExtension/Extension/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @executable_path/../../../../Frameworks"; - MARKETING_VERSION = 0.46.3; + MACOSX_DEPLOYMENT_TARGET = 10.14; + MARKETING_VERSION = 0.47.0; PRODUCT_BUNDLE_IDENTIFIER = "com.charcoaldesign.SwiftFormat-for-Xcode.SourceEditorExtension"; PRODUCT_NAME = SwiftFormat; PROVISIONING_PROFILE_SPECIFIER = ""; diff --git a/Tests/Info.plist b/Tests/Info.plist index bd4f4985..f5636b83 100644 --- a/Tests/Info.plist +++ b/Tests/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType BNDL CFBundleShortVersionString - 0.46.3 + 0.47.0 CFBundleSignature ???? CFBundleVersion diff --git a/Tests/MetadataTests.swift b/Tests/MetadataTests.swift index 59c58b87..21eaf25d 100644 --- a/Tests/MetadataTests.swift +++ b/Tests/MetadataTests.swift @@ -103,10 +103,10 @@ class MetadataTests: XCTestCase { var rulesByOption = [String: String]() formatter.forEach(.identifier("FormatRule")) { i, _ in guard formatter.next(.nonSpaceOrLinebreak, after: i) == .startOfScope("("), - case let .identifier(name)? = formatter.last(.identifier, before: i), - let scopeStart = formatter.index(of: .startOfScope("{"), after: i), - let scopeEnd = formatter.index(of: .endOfScope("}"), after: scopeStart), - let rule = FormatRules.byName[name] + case let .identifier(name)? = formatter.last(.identifier, before: i), + let scopeStart = formatter.index(of: .startOfScope("{"), after: i), + let scopeEnd = formatter.index(of: .endOfScope("}"), after: scopeStart), + let rule = FormatRules.byName[name] else { return } @@ -120,7 +120,7 @@ class MetadataTests: XCTestCase { var referencedOptions = [OptionDescriptor]() for index in scopeStart + 1 ..< scopeEnd { guard formatter.token(at: index - 1) == .operator(".", .infix), - formatter.token(at: index - 2) == .identifier("formatter") + formatter.token(at: index - 2) == .identifier("formatter") else { continue } @@ -156,7 +156,7 @@ class MetadataTests: XCTestCase { referencedOptions.append(Descriptors.modifierOrder) case .identifier("options") where formatter.token(at: index + 1) == .operator(".", .infix): if case let .identifier(property)? = formatter.token(at: index + 2), - let option = optionsByProperty[property] + let option = optionsByProperty[property] { referencedOptions.append(option) }