Code Style
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.
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 - In
/rustroverdirectory, except/rustrover/build, you must always use standard Kotlin formatting rules with 4 spaces indentation.
Threading annotations
- When you add
@RequiresEdt,@RequiresBackgroundThread,@RequiresReadLock,@RequiresWriteLock,
or @RequiresReadLockAbsence, do not pass generateAssertion. Keep the default value unless the user explicitly requests a different one.
- Never write
generateAssertion = false /* IJPL-115548 */. It is a legacy mass-migration marker. Do not copy it from existing code. - Set
generateAssertion = falseonly when the user explicitly requests it, or when the runtime assertion is known to fail. State the reason in a comment.
Error handling
- In a
catchblock forThrowableorException, callcom.intellij.diagnostic.rethrowControlFlowException(e)first.
It rethrows a control flow exception, such as CancellationException. 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)
}