Polish Your Code with AppCode’s Inspections and Intentions
AppCode is constantly analyzing your code to find parts that can be improved. It provides sets of code inspections and intention actions for each supported language for this exact purpose.
Inspections locate unused or redundant code, syntax errors, probable bugs, spelling mistakes, obsolete or redundant code constructs, in other words, everything that you may want to revise before compiling or committing your code. For most issues detected by the inspections, the IDE will provide quick-fixes that will help you repair the problems with a single click.
Intention actions suggest ways to alter specific code constructs to make them more compact or clear, quickly fix issues, transform expressions and control flow statements, and so on.
Apply context actions
Intention actions and inspection quick-fixes together are called context actions. If there are any context actions available for the code at the caret, you will see a yellow or red bulb in the editor. To apply an action, press
⌥⏎, select it from the list, and press
When fixing a problem detected by an inspection, you can apply the quick-fix to all the same problems in the current file:
Inspection widget and Problems tool window
In the top-right corner of the editor window, you may see the Inspection Widget that will appear if any issues are detected by the enabled inspections in the current file:
To jump between the issues, click the up and down arrows in the widget. To see the full list of issues, click the widget. It will open the Problems tool window:
By clicking a problem description in the tool window, you can jump to the related code in the editor and apply one of the available quick-fixes (
⌥⏎). If you want to see the fix before applying it, open the preview (
Running inspections on demand
Instead of fixing issues sporadically in the files you are working on, you may sometimes need to clean up your codebase, like when getting rid of unused or obsolete code. In AppCode, you can run a set of inspections for a specified scope of files or a single inspection.
Run a set of inspections
Select Code | Inspect Code from the main menu. In the dialog that opens, choose the files to be checked: the whole project, the current file, or a custom scope. When selecting the Custom scope option, you can choose one of the predefined scopes. For example, you can run inspections in only the files that have been recently changed:
If you want to check a specific set of files or locations, you can create a new scope by clicking … next to the Custom scope field.
The set of inspections to run can be modified in the Inspection profile section. Click … and either enable/disable inspections in the current profile or create a new profile and enable only the necessary inspections you’d like to have.
To create a new profile, just duplicate the current one:
In the new profile, you can deselect all inspections by clicking
– on the toolbar and selecting only the ones that you need.
Note: Code analysis is one of the most time-consuming features in the IDE, but you can speed it up by disabling any unnecessary inspections in the selected profile.
Run a single inspection
To run a specific inspection by its name, select Code | Analyze Code | Run Inspection by Name (or press
⌥⇧⌘I). Start typing the name and select an inspection for your file type or programming language from the list:
Afterwards, you can define the inspection scope and set up additional inspection options if needed.
Disable inspections and intentions
If you don’t want to be distracted by any of the intention actions, you can disable them directly from the editor. Just click the arrow next to the action name and select Disable ‘Action name’:
Similarly, you can disable inspections:
Alternatively, you can find an intention action or inspection that you want to disable in Preferences and disable it there in the list:
Here is a short overview of the inspections that we currently have for Swift. Go to Preferences | Editor | Inspections | Swift to see the full list:
All of AppCode’s Swift inspections can be divided into the following groups:
Inspections that detect redundant code elements
AppCode detects symbols that can be removed without breaking the code syntax, such as
return in single-expression functions, excessive usages of
self, redundant parentheses in the arithmetic or boolean expressions, and so on. All of these inspections provide quick-fixes that allow you to safely remove any unnecessary elements:
If your application supports several languages, the AppCode localization inspections may come in handy. The Missing localization inspection analyzes your Swift code and finds the localization keys that are not added to the .
There is also a set of inspections that work in the
.strings files (Preferences | Editor | Inspections | Strings files). They detect duplicate and unused localization keys, along with keys that are missing in some languages.
With the SourceKit inspections enabled, AppCode finds the parts of your Swift code that can be improved or may lead to compilation errors and highlights them (for example, syntax errors, incorrect usages of variables and constants, unhandled errors, and so on). Hover the cursor over the highlighted code element to see the error details. For some of these inspections, you can also apply SourceKit fix-its (press
⌥⏎ and click Apply Fix-it):
Swift intention actions
A full list of the Swift intention actions is available under AppCode | Preferences | Editor | Intentions | Swift:
Let’s take a look at the most common use cases.
⌥⏎, you can add an explicit type to a declaration or remove it and split or join declarations and assignments:
With intention actions applied to the string literals, you can localize your strings, make them multi-line or single-line, and increase or decrease the spring escape level:
You can convert a closure argument to a trailing closure and vice versa:
When working with boolean expressions, you can quickly negate operators, flip operands, replace
&& applying De Morgan’s laws, and more:
switch, and other control flow statements, there are plenty of intention actions available. For example, you can quickly invert
if statements, replace
guard, add missing
switch cases, and so on:
The list of Swift inspections and intention actions is constantly growing. We are trying to cover as many use cases as possible to make your coding process smoother and easier. If you have ideas for new inspections or intentions, please don’t hesitate to submit an issue in our project tracker, YouTrack. You can also vote for an existing one there. For more information about inspections and intention actions, go to the Code inspections and Intention actions sections of our online help.
Your AppCode team
AppCode 2022.1: Swift Support Improvements, Xcode 13.3 Support, and More
AppCode 2022.1 brings support for async sequences, postfix #if member expressions, isolation control specifiers for Swift actors, improvements for Swift Package Manager integration, better VoiceOver support, and more.
Writing Code Documentation in AppCode
Code documentation can save you a lot of time and headaches. On the other hand, it also takes a lot of effort to write and maintain it. In this article, we will look at the AppCode features that can help you create detailed and well-structured code documentation and keep it updated after code refact…
AppCode Starts 2022.1 EAP: Swift Support Improvements and Bug Fixes
Hi everyone, Today we are starting the Early Access Program for AppCode 2022.1, and the first build is already available for download. Language support Starting with this build, AppCode supports async sequences (SE-0298). Other improvements and bug fixes Separate color settings for associated …