New Code Style API in Intellij IDEA 2018.1

IntelliJ 2018.1 introduces a new API that we want to make plugin authors aware of. We’re changing the way you retrieve Code Style settings (such as formatting options) so that you can provide a PsiFile, instead of a Project. The ultimate goal is to support different code style settings per scope, rather than per project (see IDEA-69685).

As a bonus, the new API provides a shorter form for getting code style. So, for example, instead of writing CodeStyleSettings.getSettings(project).getCommonSettings(language) you can now simply write CodeStyle.getLanguageSettings(file).

For now, CodeStyle.getLanguageSettings(file) is equivalent to calling (deep breath) CodeStyleSettings.getSettings(file.getProject()).getCommonSettings(file.getLanguage()), and will return the per-project settings, but this will change in the future when the most essential parts are migrated.

The migration to the new CodeStyle API is currently ongoing, and will continue throughout the 2018.x time frame – there are a large number of changes required both in IntelliJ Platform and plug-ins.

Note that we are not deprecating CodeStyle.getSettings(project), and it is still acceptable to call this instead of CodeStyle.getSettings(file). However, this should only be used in cases when using main project settings is logically the only choice in a given context. It shouldn’t be used just because existing code doesn’t have immediate access to an instance of PsiFile, otherwise the code will not catch up with per file settings when they become available.

As a call to action for plugin authors, please check that you are not using CodeStyle.getSettings(project) in your code, and migrate to CodeStyle.getSettings(file), or helper methods like CodeStyle.getLanguageSettings(file) and CodeStyle.getCustomSettings(file, class).

Please see the CodeStyle class for more information on methods.

image description