diff --git a/topics/basics/plugin_structure/plugin_extensions.md b/topics/basics/plugin_structure/plugin_extensions.md index ea023c54e..d8d89d7b2 100644 --- a/topics/basics/plugin_structure/plugin_extensions.md +++ b/topics/basics/plugin_structure/plugin_extensions.md @@ -78,9 +78,11 @@ Please note the following important points: - Extension implementations should be stateless. Use explicit [](plugin_services.md) for managing (runtime) data. - Avoid any initialization in constructor, see also notes for [Services](plugin_services.md#constructor). -- Kotlin: Do not use `object` but `class` for implementation ([more details](using_kotlin.md#caution)). -- Kotlin: Do not use `companion object` to avoid excessive classloading/initialization when the extension class is loaded. -Use top-level declarations or objects instead. + +When using Kotlin: +- Do not use `object` but `class` for implementation. [More details](using_kotlin.md#object-vs-class) +- Do not use `companion object` to avoid excessive classloading/initialization when the extension class is loaded. +Use top-level declarations or objects instead. [More details](using_kotlin.md#companion-object-extensions) diff --git a/topics/tutorials/using_kotlin.md b/topics/tutorials/using_kotlin.md index b65f49ce1..fec3d9469 100644 --- a/topics/tutorials/using_kotlin.md +++ b/topics/tutorials/using_kotlin.md @@ -161,7 +161,10 @@ You can find the current state of the issue in [KT-57757](https://youtrack.jetbr Please see [Third-Party Software and Licenses](https://www.jetbrains.com/legal/third-party-software/). -## Caution +## Plugin Implementation Notes + +### Do not use "object" but "class" +{id="object-vs-class"} Plugins *may* use [Kotlin classes](https://kotlinlang.org/docs/classes.html) (`class` keyword) to implement declarations in the [plugin configuration file](plugin_configuration_file.md). When registering an extension, the platform uses a dependency injection framework to instantiate these classes at runtime. @@ -172,6 +175,15 @@ Problems are highlighted via these inspections (2023.2): - Plugin DevKit | Code | Kotlin object registered as extension for Kotlin code - Plugin DevKit | Plugin descriptor | Extension class is a Kotlin object for plugin.xml +### Do not use "companion object" in extensions +{id="companion-object-extensions"} + +Kotlin `companion object` is always created once you try to load its containing class, and [extension point implementations](plugin_extensions.md) are supposed to be cheap to create. +To avoid unnecessary classloading (and thus slowdown in IDE startup), `companion object` in extensions must only contain simple constants or [logger](ide_infrastructure.md#logging). +Anything else must be a top-level declaration or stored in an `object`. + +Use inspection Plugin DevKit | Code | Companion object in extensions to highlight such problems (2023.3). + ## Kotlin Code FAQ [How to shorten references](https://intellij-support.jetbrains.com/hc/en-us/community/posts/360010025120-Add-new-parameter-into-kotlin-data-class-from-IDEA-plugin?page=1#community_comment_360002950760)