|
1 | 1 | # Migrating from K1
|
2 | 2 |
|
3 | 3 | For many years, the only practical way to analyze Kotlin code in IntelliJ IDEA and other tools was to use the Kotlin
|
4 |
| -compiler internals, an unsafe API not designed for external usage. |
| 4 | +compiler's internals, an unsafe API not designed for external usage. |
5 | 5 |
|
6 | 6 | The Analysis API, on the other hand, offers a much cleaner and robust set of utilities. It exposes almost the same set
|
7 | 7 | of concepts. Thus, if you already have code that depends on the Kotlin compiler, migrating it to the new API should not
|
8 | 8 | be time-consuming. This migration guide outlines the differences between the APIs and explains how to port the
|
9 | 9 | descriptor-based resolution logic to the Analysis API.
|
10 | 10 |
|
| 11 | +Once an IntelliJ plugin has been migrated to the Analysis API, it will need to [declare its compatibility with the K2 |
| 12 | +Kotlin mode](#declaring-compatibility-with-the-k2-kotlin-mode). Otherwise, the plugin will not be loaded when the K2 |
| 13 | +mode is active. |
| 14 | + |
11 | 15 | ## The Conceptual Difference
|
12 | 16 |
|
13 | 17 | The cornerstone of the old compiler API is a `BindingContext`, a universal dictionary:
|
@@ -422,4 +426,32 @@ fun hasAnnotation(declaration: KtDeclaration): Boolean {
|
422 | 426 | return SPECIAL_ANNOTATION_CLASS_ID in declaration.symbol.annotations
|
423 | 427 | }
|
424 | 428 | }
|
425 |
| -``` |
| 429 | +``` |
| 430 | + |
| 431 | +## Declaring compatibility with the K2 Kotlin mode |
| 432 | + |
| 433 | +The Kotlin IntelliJ plugin assumes that a dependent third-party plugin *does not* support K2 Kotlin out of the box. Such |
| 434 | +incompatible plugins will not be loaded if the K2 mode is currently enabled. |
| 435 | + |
| 436 | +Once a plugin has been migrated to the Analysis API, a setting should be added to its `plugin.xml` to declare its |
| 437 | +compatibility with the K2 mode. Even if the plugin does not use any of the old K1 analysis functions and no migration to |
| 438 | +the Analysis API is needed, compatibility with the K2 Kotlin plugin should be declared explicitly nonetheless. |
| 439 | + |
| 440 | +Starting from IntelliJ 2024.2.1 (EAP), the following setting in the `plugin.xml` can be used to declare compatibility |
| 441 | +with the K2 mode: |
| 442 | + |
| 443 | +```xml |
| 444 | +<extensions defaultExtensionNs="org.jetbrains.kotlin"> |
| 445 | + <supportsKotlinPluginMode supportsK2="true" /> |
| 446 | +</extensions> |
| 447 | +``` |
| 448 | + |
| 449 | +It is also possible to declare compatibility with *only* the K2 mode: |
| 450 | + |
| 451 | +```xml |
| 452 | +<extensions defaultExtensionNs="org.jetbrains.kotlin"> |
| 453 | + <supportsKotlinPluginMode supportsK1="false" supportsK2="true" /> |
| 454 | +</extensions> |
| 455 | +``` |
| 456 | + |
| 457 | +Currently, the default setting for `supportsK1` is `true` and for `supportsK2` is `false`. |
0 commit comments