Skip to content

Commit 19fbbe9

Browse files
committed
feat: Add support for IntelliJ IDEA via plugin
This change adds a plugin for IntelliJ that replaces the Gradle plugin jar with its own shaded compiler plugin in order to support the Kotlin compiler within IntelliJ. Fixes #4
1 parent e6c7c09 commit 19fbbe9

File tree

15 files changed

+309
-14
lines changed

15 files changed

+309
-14
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ To use the plugin, just apply the plugin in your `build.gradle`:
2222

2323
```groovy
2424
plugins {
25-
id 'nl.fabianm.kotlin.plugin.generated' version '1.3.1'
25+
id 'nl.fabianm.kotlin.plugin.generated' version '1.3.2'
2626
}
2727
```
2828

@@ -32,7 +32,7 @@ version you are using:
3232
| **Kotlin** | **Plugin** |
3333
|:----------:|:----------:|
3434
| 1.2.* | 1.0 |
35-
| 1.3.* | 1.3.1 |
35+
| 1.3.* | 1.3.2 |
3636

3737
You can optionally configure the plugin as shown below:
3838

@@ -46,6 +46,19 @@ kotlinGenerated {
4646
}
4747
```
4848

49+
## IntelliJ
50+
To be able to use projects utilizing this plugin within IntelliJ IDEA,
51+
you need to install the IntelliJ plugin (see [#4](https://github.com/fabianishere/kotlin-plugin-generated/issues/4) for more information). You can find the plugin on the
52+
[Releases](https://github.com/fabianishere/kotlin-plugin-generated/releases) page, labeled `plugin-idea-*.zip`
53+
54+
## Command Line
55+
To use the plugin from the command line, invoke `kotlinc` using the
56+
following command line arguments:
57+
58+
```bash
59+
kotlinc -Xplugin="<path-to-plugin-compiler.jar>" -P plugin:nl.fabianm.kotlin.plugin.generated:annotation="lombok.Generated" -P plugin:nl.fabianm.kotlin.plugin.generated:visible=true ...
60+
```
61+
4962
## License
5063
The code is released under the Apache version 2.0 license. See the
5164
[LICENSE.txt](/LICENSE.txt) file.

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ plugins {
2222

2323
allprojects {
2424
group = "nl.fabianm.kotlin.plugin.generated"
25-
version = "1.3.1"
25+
version = "1.3.2"
2626

2727
extra["junitJupiterVersion"] = "5.4.2"
2828
extra["junitPlatformVersion"] = "1.4.2"

plugin-compiler/build.gradle.kts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ tasks.withType<KotlinCompile> {
4040
}
4141
}
4242

43+
tasks.jar {
44+
manifest {
45+
attributes["Specification-Title"] = project.name
46+
attributes["Specification-Version"] = project.version
47+
attributes["Implementation-Title"] = "nl.fabianm.kotlin.plugin.generated.compiler"
48+
attributes["Implementation-Version"] = project.version
49+
}
50+
}
51+
4352
tasks.shadowJar {
4453
configurations = listOf()
4554
archiveClassifier.set("embeddable")
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 Fabian Mastenbroek.
2+
* Copyright 2019 Fabian Mastenbroek.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package nl.fabianm.kotlin.plugin.generated
17+
package nl.fabianm.kotlin.plugin.generated.compiler
1818

1919
import com.intellij.psi.PsiElement
2020
import jdk.internal.org.objectweb.asm.Type

plugin-compiler/src/main/kotlin/nl/fabianm/kotlin/plugin/generated/GeneratedPlugin.kt renamed to plugin-compiler/src/main/kotlin/nl/fabianm/kotlin/plugin/generated/compiler/GeneratedPlugin.kt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 Fabian Mastenbroek.
2+
* Copyright 2019 Fabian Mastenbroek.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,16 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
package nl.fabianm.kotlin.plugin.generated
17+
package nl.fabianm.kotlin.plugin.generated.compiler
1818

1919
import com.intellij.mock.MockProject
2020
import com.intellij.openapi.extensions.Extensions
2121
import com.intellij.openapi.extensions.LoadingOrder
2222
import com.intellij.openapi.extensions.impl.ExtensionPointImpl
2323
import com.intellij.openapi.project.Project
24-
import nl.fabianm.kotlin.plugin.generated.GeneratedConfigurationKeys.ANNOTATION
25-
import nl.fabianm.kotlin.plugin.generated.GeneratedConfigurationKeys.DEFAULT_ANNOTATION
26-
import nl.fabianm.kotlin.plugin.generated.GeneratedConfigurationKeys.VISIBLE
24+
import nl.fabianm.kotlin.plugin.generated.compiler.GeneratedConfigurationKeys.ANNOTATION
25+
import nl.fabianm.kotlin.plugin.generated.compiler.GeneratedConfigurationKeys.DEFAULT_ANNOTATION
26+
import nl.fabianm.kotlin.plugin.generated.compiler.GeneratedConfigurationKeys.VISIBLE
2727
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
2828
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
2929
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
@@ -39,15 +39,27 @@ import org.jetbrains.kotlin.extensions.ProjectExtensionDescriptor
3939
import org.jetbrains.kotlin.name.FqName
4040

4141
object GeneratedConfigurationKeys {
42+
/**
43+
* The configuration key for specifying the qualified name of the annotation to annotate generated methods with.
44+
*/
4245
val ANNOTATION: CompilerConfigurationKey<FqName> =
4346
CompilerConfigurationKey.create("annotation qualified name")
4447

48+
/**
49+
* The configuration key for specifying whether the annotations should be visible during runtime.
50+
*/
4551
val VISIBLE: CompilerConfigurationKey<Boolean> =
4652
CompilerConfigurationKey.create("annotation visibility")
4753

54+
/**
55+
* By default, the plugin will annotate generated methods with the `lombok.Generated` annotation.
56+
*/
4857
val DEFAULT_ANNOTATION: FqName = FqName("lombok.Generated")
4958
}
5059

60+
/**
61+
* The [CommandLineProcessor] for this compiler plugin.
62+
*/
5163
class GeneratedCommandLineProcessor : CommandLineProcessor {
5264
companion object {
5365
val ANNOTATION_OPTION = CliOption("annotation", "<fqname>", "Annotation qualified name", required = false)
@@ -66,6 +78,9 @@ class GeneratedCommandLineProcessor : CommandLineProcessor {
6678
}
6779
}
6880

81+
/**
82+
* The [ComponentRegistrar] for this plugin, which registers the [GeneratedClassBuilderInterceptorExtension].
83+
*/
6984
class GeneratedComponentRegistrar : ComponentRegistrar {
7085
override fun registerProjectComponents(
7186
project: MockProject,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nl.fabianm.kotlin.plugin.generated.GeneratedCommandLineProcessor
1+
nl.fabianm.kotlin.plugin.generated.compiler.GeneratedCommandLineProcessor
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nl.fabianm.kotlin.plugin.generated.GeneratedComponentRegistrar
1+
nl.fabianm.kotlin.plugin.generated.compiler.GeneratedComponentRegistrar

plugin-gradle/build.gradle.kts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ plugins {
2525
id("com.jfrog.bintray") version "1.8.4"
2626
}
2727

28+
description = "Gradle plugin for marking Kotlin-generated code with an annotation."
29+
2830
repositories {
2931
jcenter()
3032
}
@@ -46,6 +48,13 @@ tasks.withType<KotlinCompile> {
4648
tasks.jar {
4749
// Embed compiler plugin in jar
4850
from(zipTree(configurations.compileClasspath.get().first { it.name.startsWith("plugin-compiler") }))
51+
52+
manifest {
53+
attributes["Specification-Title"] = project.name
54+
attributes["Specification-Version"] = project.version
55+
attributes["Implementation-Title"] = "nl.fabianm.kotlin.plugin.generated.gradle"
56+
attributes["Implementation-Version"] = project.version
57+
}
4958
}
5059

5160
/* Gradle plugin */

plugin-gradle/src/main/kotlin/nl/fabianm/kotlin/plugin/generated/gradle/GeneratedExtension.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,30 @@
1616

1717
package nl.fabianm.kotlin.plugin.generated.gradle
1818

19+
/**
20+
* The available Gradle configuration for this plugin.
21+
*/
1922
open class GeneratedExtension {
23+
/**
24+
* The annotation to use for annotating generated methods.
25+
*/
2026
var annotation: String? = null
27+
28+
/**
29+
* A flag to make the annotations visible during runtime.
30+
*/
2131
var visible: Boolean = false
2232

33+
/**
34+
* Set the annotation to use for annotating generated methods.
35+
*/
2336
open fun annotation(fqName: String) {
2437
annotation = fqName
2538
}
2639

40+
/**
41+
* Set the runtime visibility of the annotations.
42+
*/
2743
open fun visible(visible: Boolean) {
2844
this.visible = visible
2945
}

plugin-gradle/src/main/kotlin/nl/fabianm/kotlin/plugin/generated/gradle/GeneratedSubplugin.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin
2525
import org.jetbrains.kotlin.gradle.plugin.SubpluginArtifact
2626
import org.jetbrains.kotlin.gradle.plugin.SubpluginOption
2727

28+
/**
29+
* The project-level Gradle plugin behavior that is used specifying the plugin's configuration through the
30+
* [GeneratedExtension] class.
31+
*/
2832
class GeneratedGradleSubplugin : Plugin<Project> {
2933
companion object {
3034
fun isEnabled(project: Project) = project.plugins.findPlugin(GeneratedGradleSubplugin::class.java) != null
@@ -39,19 +43,29 @@ class GeneratedGradleSubplugin : Plugin<Project> {
3943
}
4044
}
4145

46+
/**
47+
* The compilation-level Gradle plugin for applying the compiler plugin to the Kotlin compiler configuration.
48+
*/
4249
class GeneratedKotlinGradleSubplugin : KotlinGradleSubplugin<AbstractCompile> {
4350
companion object {
4451
private const val GENERATED_ARTIFACT_NAME = "plugin-gradle"
4552
private const val GENERATED_GROUP_ID = "nl.fabianm.kotlin.plugin.generated"
46-
private const val GENERATED_VERSION = "1.3.0"
53+
private const val GENERATED_VERSION = "1.3.2"
4754
private const val GENERATED_COMPILER_PLUGIN_ID = "nl.fabianm.kotlin.plugin.generated"
4855
private val ANNOTATION_ARG_NAME = "annotation"
4956
private val VISIBLE_ARG_NAME = "visible"
5057
}
5158

5259
override fun isApplicable(project: Project, task: AbstractCompile) = GeneratedGradleSubplugin.isEnabled(project)
5360

54-
override fun apply(project: Project, kotlinCompile: AbstractCompile, javaCompile: AbstractCompile?, variantData: Any?, androidProjectHandler: Any?, kotlinCompilation: KotlinCompilation<KotlinCommonOptions>?): List<SubpluginOption> {
61+
override fun apply(
62+
project: Project,
63+
kotlinCompile: AbstractCompile,
64+
javaCompile: AbstractCompile?,
65+
variantData: Any?,
66+
androidProjectHandler: Any?,
67+
kotlinCompilation: KotlinCompilation<KotlinCommonOptions>?
68+
): List<SubpluginOption> {
5569
if (!GeneratedGradleSubplugin.isEnabled(project)) {
5670
return emptyList()
5771
}

0 commit comments

Comments
 (0)