1. Install SwiftFormat

What is a SwiftFormat

SwiftFormat is a command line tool for formatting Swift code.

We can install it directly with Homebrew, if you have already installed it, you can skip this step:

brew install swiftformat
Copy the code

Then install swiftFormat’s Xcode plugin:

brew install --cask swiftformat-for-xcode
open "/Applications/SwiftFormat For Xcode.app"
Copy the code

SwiftFormat For Xcode.app is automatically installed on Xcode. You need to restart Xcode to take effect. If you open Xcode again and see the SwiftFormat menu in the Editor directory of Xcode, the plug-in is successfully installed.

Here, we can manually select the Format File menu to Format the current File. However, this is not convenient enough. It would be nice if the code could be automatically formatted when the Flutter was saved, just as the Flutter was developed.

2. Use Automator to add automation scripts

Automator is a system software program whose Chinese name is Automatic operation.

Open Automator and select Quick Action:

Then search for Script in the search bar, double-click and select Run AppleScript:

Then replace the above script with the following script code:

on run {input, parameters}
	tell application "System Events"
		tell process "Xcode"
			set frontmost to true
			if menu item "Format File" of menu of menu item "SwiftFormat" of menu "Editor" of menu bar 1 exists then
				click menu item "Format File" of menu of menu item "SwiftFormat" of menu "Editor" of menu bar 1
			end if
			click menu item "Save" of menu "File" of menu bar 1
		end tell
	end tell
	return input
end run
Copy the code

The script does the following:

  1. Check whether the SwiftFormat > Format File menu item exists in the Editor menu in Xcode.
  2. If it exists, it is automatically triggeredFormat FileMenu click to format the code.
  3. Finally, auto clickSaveMenu, save file modification.

Click the Build button and the code inside will be automatically highlighted:

Then, modify the workflow to receive this configuration as no input and change it here to Xcode.

Last ⌘ + S saves, name the automatic operation XcodeFormatAndSave and it will automatically save to ~/Library/Services/. Re-open Xcode and we’ll see it under Xcode -> Services, but it doesn’t work yet and needs further configuration.

3. Add keyboard shortcuts

Our ultimate goal is to automate code formatting while saving files (⌘ + S).

Open system Preferences -> Keyboard -> Shortcuts, select App shortcuts on the left, add a new shortcut key, select Xcdoe for application, and the menu title is the same as the automatic action name saved just now: ⌘ XcodeFormatAndSave, keyboard shortcut set to ⌘ + S, and click the Add button.

Finally, we also need to configure run permissions for it, otherwise an error will be reported.

Go to System Preferences -> Security & Privacy -> Privacy and select Accessibility from the list on the left to grant Xcode control rights.

This will trigger automatic code formatting when saving (⌘ + S) to the Swift file in Xcode. More comfortable.

Refer to the link

  • Xcode Format and Save using SwiftFormat and Automator