Follow the IntelliJ Coding Guidelines with these IntelliJ-specific rules.
Language
- Use Kotlin for new files unless the user explicitly requests Java.
- Keep existing Java files in Java unless the task requires a language conversion.
- Before writing or reviewing Java, read Modern Java.
- Use idiomatic Kotlin: extension functions, data classes, null safety
- Mark experimental APIs with
@ApiStatus.Experimental - Build scripts Kotlin style: Follow
community/platform/build-scripts/kotlin-style-recommendations.mdfor build-scripts code:- Use
@JvmFieldfor internal data class fields - Use
.put()/.get()instead of[]operator - Use explicit
HashMap/HashSet/LinkedHashMapinstead ofmutableMapOf()/mutableSetOf()
- Use
Sealed hierarchies
- Before you list several subtypes in a
whenbranch, anischeck, or acatch, open the declaration of the type. - If a common supertype covers exactly those subtypes, use the supertype. It stays correct when someone adds a new subtype.
- Check the value of a sealed type or an enum with an exhaustive
when. Do not use anischeck, anas?cast, an==comparison, orelse. In Eel and IJent code, this is a rule, also for aprivatetype. Seecommunity/platform/eel/docs/api/exhaustive-when.md.
Formatting
- Read every applicable
.editorconfigbefore you review or change formatting. - Use the effective
.editorconfigvalue for each setting. If no file defines a setting, use these defaults:- Indentation: 2 spaces (4 for Go files)
- Line length: 140 characters max
- Braces:
else,catch, andfinallyon new lines
- Only in
/rustroverdirectory you must always use standard Kotlin formatting rules with 4 spaces indentation.
Error handling
- In a
catchblock forThrowableorException, callcom.intellij.diagnostic.rethrowControlFlowException(e)first. It rethrows a control flow exception, such asCancellationException. A control flow exception must never reach the log. - Do not catch or rethrow
CancellationExceptionexplicitly, and do not test the exception type by hand. - In Java, static-import
com.intellij.diagnostic.ControlFlowExceptionsKt.rethrowControlFlowException. - Do not use the deprecated
com.intellij.openapi.diagnostic.rethrowControlFlowException.
try {
doWork()
}
catch (e: Throwable) {
rethrowControlFlowException(e)
LOG.error(e)
}