Skip to content

Commit 5e81574

Browse files
authored
update: official code style (#5201)
* update: official code style * chore: intro
1 parent b757a49 commit 5e81574

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed
Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
[//]: # (title: Migrate to Kotlin code style)
22

3+
<no-index/>
4+
5+
> Starting with Kotlin 1.4.0, the official code style formatting is enabled by default for all projects in IntelliJ IDEA.
6+
>
7+
{style="note"}
8+
39
## Kotlin coding conventions and IntelliJ IDEA formatter
410

511
[Kotlin coding conventions](coding-conventions.md) affect several aspects of writing idiomatic Kotlin, and a set of
612
formatting recommendations aimed at improving Kotlin code readability is among them.
713

8-
Unfortunately, the code formatter built into IntelliJ IDEA had to work long before this document was released and now
9-
has a default setup that produces different formatting from what is now recommended.
10-
11-
It may seem a logical next step to remove this obscurity by switching the defaults in IntelliJ IDEA and make formatting
12-
consistent with the Kotlin coding conventions. But this would mean that all the existing Kotlin projects will have a new
13-
code style enabled the moment the Kotlin plugin is installed. Not really the expected result for plugin update, right?
14+
The code formatter built into IntelliJ IDEA used to have a default setup that produced different formatting from what
15+
is now recommended.
1416

15-
That's why we have the following migration plan instead:
17+
We'd like to remove this inconsistency by switching the defaults in IntelliJ IDEA and making formatting consistent with
18+
Kotlin coding conventions. That's why the following migration plan was implemented:
1619

17-
* Enable the official code style formatting by default starting from Kotlin 1.3 and only for new projects (old formatting
18-
can be enabled manually)
19-
* Authors of existing projects may choose to migrate to the Kotlin coding conventions
20-
* Authors of existing projects may choose to explicitly declare using the old code style in a project (this way the project
21-
won't be affected by switching to the defaults in the future)
22-
* Switch to the default formatting and make it consistent with Kotlin coding conventions in Kotlin 1.4
20+
* Starting with Kotlin 1.3.0, the official code style formatting is enabled by default and only for new projects
21+
(old formatting can be enabled manually).
22+
* Authors of existing projects can choose to migrate to the Kotlin coding conventions.
23+
* Authors of existing projects can choose to explicitly declare using the old code style in a project
24+
(this way, the project won't be affected by switching to the defaults in the future)
25+
* Starting with Kotlin 1.4.0, the default formatting is enabled for all projects to make it consistent with Kotlin coding
26+
conventions.
2327

2428
## Differences between "Kotlin coding conventions" and "IntelliJ IDEA default code style"
2529

2630
The most notable change is in the continuation indentation policy. There's a nice idea to use the double indent for showing
27-
that a multi-line expression hasn't ended on the previous line. This is a very simple and general rule, but several Kotlin
31+
that a multi-line expression hasn't ended on the previous line. This is a simple and general rule, but several Kotlin
2832
constructions look a bit awkward when they are formatted this way. In Kotlin coding conventions, it's recommended to use
2933
a single indent in cases where the long continuation indent has been forced before.
3034

@@ -34,11 +38,11 @@ In practice, quite a bit of code is affected, so this can be considered a major
3438

3539
## Migration to a new code style discussion
3640

37-
A new code style adoption might be a very natural process if it starts with a new project, when there's no code formatted
38-
in the old way. That is why starting from version 1.3, the Kotlin IntelliJ Plugin creates new projects with formatting from
39-
the [Coding conventions](coding-conventions.md) document which is enabled by default.
41+
A new code style adoption might be a very natural process if it starts with a new project when there's no code formatted
42+
in the old way. That's why, starting with version 1.3.0, the Kotlin IntelliJ plugin creates new projects with formatting from
43+
the [Coding conventions](coding-conventions.md) document, which is enabled by default.
4044

41-
Changing formatting in an existing project is a far more demanding task, and should probably be started with discussing
45+
Changing formatting in an existing project is a far more demanding task and should probably be started with discussing
4246
all the caveats with the team.
4347

4448
The main disadvantage of changing the code style in an existing project is that the blame/annotate VCS feature will point
@@ -47,32 +51,32 @@ to irrelevant commits more often. While each VCS has some kind of way to deal wi
4751
it's important to decide if a new style is worth all the effort. The practice of separating reformatting commits from
4852
meaningful changes can help a lot with later investigations.
4953

50-
Also migrating can be harder for larger teams because committing a lot of files in several subsystems may produce merging
54+
Also, migrating can be harder for larger teams because committing a lot of files in several subsystems may produce merging
5155
conflicts in personal branches. And while each conflict resolution is usually trivial, it's still wise to know if there are
5256
large feature branches currently in work.
5357

5458
In general, for small projects, we recommend converting all the files at once.
5559

56-
For medium and large projects the decision may be tough. If you are not ready to update many files right away you may
57-
decide to migrate module by module, or continue with gradual migration for modified files only.
60+
For medium and large projects the decision may be tough. If you are not ready to update many files right away, you may
61+
decide to migrate module by module or continue with gradual migration for modified files only.
5862

5963
## Migration to a new code style
6064

61-
Switching to the Kotlin Coding Conventions code style can be done in **Settings/Preferences** | **Editor** | **Code Style** | **Kotlin**
62-
dialog. Switch scheme to **Project** and activate **Set from...** | **Kotlin style guide**.
65+
Switching to the Kotlin Coding Conventions code style can be done in the **Settings/Preferences** | **Editor** | **Code Style** | **Kotlin**
66+
dialog. Switch the scheme to **Project** and activate **Set from...** | **Kotlin style guide**.
6367

64-
In order to share those changes for all project developers `.idea/codeStyle` folder have to be committed to VCS.
68+
To share those changes with all project developers, the `.idea/codeStyle` folder has to be committed to VCS.
6569

66-
If an external build system is used for configuring the project, and it's been decided not to share `.idea/codeStyle` folder,
70+
If an external build system is used for configuring the project, and it's been decided not to share the `.idea/codeStyle` folder,
6771
Kotlin coding conventions can be forced with an additional property:
6872

6973
### In Gradle
7074

71-
Add `kotlin.code.style=official` property to the `gradle.properties` file at the project root and commit the file to VCS.
75+
Add the `kotlin.code.style=official` property to the `gradle.properties` file at the project root and commit the file to VCS.
7276

7377
### In Maven
7478

75-
Add `kotlin.code.style official` property to root `pom.xml` project file.
79+
Add the `kotlin.code.style official` property to root `pom.xml` project file.
7680

7781
```
7882
<properties>
@@ -100,5 +104,5 @@ It's always possible to explicitly set the IntelliJ IDEA code style as the corre
100104
1. In **Settings/Preferences** | **Editor** | **Code Style** | **Kotlin**, switch to the **Project** scheme.
101105
2. Open the **Load/Save** tab and in the **Use defaults from** select **Kotlin obsolete IntelliJ IDEA codestyle**.
102106

103-
In order to share the changes across the project developers `.idea/codeStyle` folder, it has to be committed to VCS.
107+
To share the changes across the project developers `.idea/codeStyle` folder, it has to be committed to VCS.
104108
Alternatively, **kotlin.code.style**=**obsolete** can be used for projects configured with Gradle or Maven.

0 commit comments

Comments
 (0)