Skip to content

Commit 1ab848f

Browse files
compiler plugin example fixes
1 parent c5fba7c commit 1ab848f

File tree

7 files changed

+98
-39
lines changed

7 files changed

+98
-39
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[*]
2+
charset = utf-8
3+
end_of_line = lf
4+
insert_final_newline = true
5+
indent_style = space
6+
indent_size = 4
7+
max_line_length = 120
8+
9+
[*.json]
10+
indent_size = 2
11+
12+
[{*.yaml,*.yml}]
13+
indent_size = 2
14+
15+
[*.ipynb]
16+
insert_final_newline = false
17+
18+
[*.{kt,kts}]
19+
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL
20+
21+
# Disable wildcard imports entirely
22+
ij_kotlin_name_count_to_use_star_import = 2147483647
23+
ij_kotlin_name_count_to_use_star_import_for_members = 2147483647
24+
ij_kotlin_packages_to_use_import_on_demand = unset
25+
26+
ktlint_code_style = ktlint_official
27+
ktlint_experimental = enabled
28+
ktlint_standard_filename = disabled
29+
ktlint_standard_no-empty-first-line-in-class-body = disabled
30+
ktlint_class_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than = 4
31+
ktlint_function_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than = 4
32+
ktlint_standard_chain-method-continuation = disabled
33+
ktlint_ignore_back_ticked_identifier = true
34+
ktlint_standard_multiline-expression-wrapping = disabled
35+
ktlint_standard_when-entry-bracing = disabled
36+
37+
[{*/build/**/*,**/*keywords*/**,**/*.Generated.kt,**/*$Extensions.kt}]
38+
ktlint = disabled

examples/kotlin-dataframe-plugin-example/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ An IntelliJ IDEA project demonstrating the use of the
77
> See [build.gradle.kts](build.gradle.kts) for details.
88
99
For proper functionality in IntelliJ IDEA requires version 2025.2 or higher
10-
(Currently available only in [IntelliJ IDEA EAP](https://www.jetbrains.com/idea/nextversion/?_gl=1*1ojxffu*_gcl_au*MTk5NzUwODYzOS4xNzQ2NzkxMDMz*_ga*MTE0ODQ1MzY3OS4xNzM4OTY1NzM3*_ga_9J976DJZ68*czE3NDkyMTM4MzkkbzE5OCRnMSR0MTc0OTIxMzg0MSRqNTgkbDAkaDA.)
11-
).
10+
(Currently available only in [IntelliJ IDEA EAP](https://www.jetbrains.com/idea/nextversion/?_gl=1*1ojxffu*_gcl_au*MTk5NzUwODYzOS4xNzQ2NzkxMDMz*_ga*MTE0ODQ1MzY3OS4xNzM4OTY1NzM3*_ga_9J976DJZ68*czE3NDkyMTM4MzkkbzE5OCRnMSR0MTc0OTIxMzg0MSRqNTgkbDAkaDA.)).

examples/kotlin-dataframe-plugin-example/build.gradle.kts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
import org.gradle.kotlin.dsl.assign
2+
import org.gradle.kotlin.dsl.configure
3+
import org.jlleitschuh.gradle.ktlint.KtlintExtension
4+
15
plugins {
6+
id("org.jlleitschuh.gradle.ktlint") version "12.3.0"
27
kotlin("jvm") version "2.2.20-dev-3524"
38
kotlin("plugin.dataframe") version "2.2.20-dev-3524"
49
}
@@ -11,7 +16,6 @@ repositories {
1116
mavenCentral()
1217
}
1318

14-
1519
dependencies {
1620
implementation("org.jetbrains.kotlinx:dataframe:1.0.0-Beta2")
1721
testImplementation(kotlin("test"))
@@ -22,4 +26,9 @@ tasks.test {
2226
}
2327
kotlin {
2428
jvmToolchain(11)
25-
}
29+
}
30+
31+
configure<KtlintExtension> {
32+
version = "1.6.0"
33+
// rules are set up through .editorconfig
34+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
kotlin.code.style=official
2+
# Disabling incremental compilation will no longer be necessary
3+
# when https://youtrack.jetbrains.com/issue/KT-66735 is resolved.
24
kotlin.incremental=false

examples/kotlin-dataframe-plugin-example/settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ pluginManagement {
88
plugins {
99
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
1010
}
11-
rootProject.name = "kdf_plugin_example"
11+
rootProject.name = "kotlin-dataframe-plugin-example"

examples/kotlin-dataframe-plugin-example/src/main/kotlin/org/jetbrains/kotlinx/dataframe/examples/plugin/Main.kt

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
11
package org.jetbrains.kotlinx.dataframe.examples.plugin
22

33
import org.jetbrains.kotlinx.dataframe.DataFrame
4-
import org.jetbrains.kotlinx.dataframe.annotations.ColumnName
54
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
6-
import org.jetbrains.kotlinx.dataframe.api.*
5+
import org.jetbrains.kotlinx.dataframe.api.add
6+
import org.jetbrains.kotlinx.dataframe.api.convert
7+
import org.jetbrains.kotlinx.dataframe.api.convertTo
8+
import org.jetbrains.kotlinx.dataframe.api.filter
9+
import org.jetbrains.kotlinx.dataframe.api.into
10+
import org.jetbrains.kotlinx.dataframe.api.rename
11+
import org.jetbrains.kotlinx.dataframe.api.renameToCamelCase
12+
import org.jetbrains.kotlinx.dataframe.api.with
713
import org.jetbrains.kotlinx.dataframe.io.readCsv
814
import org.jetbrains.kotlinx.dataframe.io.writeCsv
915
import java.net.URL
1016

11-
/*
12-
* Declare data schema for the DataFrame from jetbrains_repositories.csv.
13-
*/
17+
// Declare data schema for the DataFrame from jetbrains_repositories.csv.
1418
@DataSchema
1519
data class Repositories(
16-
@ColumnName("full_name")
17-
val fullName: String,
18-
@ColumnName("html_url")
19-
val htmlUrl: URL,
20-
@ColumnName("stargazers_count")
21-
val stargazersCount: Int,
20+
val full_name: String,
21+
val html_url: URL,
22+
val stargazers_count: Int,
2223
val topics: String,
23-
val watchers: Int
24+
val watchers: Int,
2425
)
2526

26-
/*
27-
* Define kinds of repositories:
28-
*/
27+
// Define kinds of repositories.
2928
enum class RepoKind {
30-
Kotlin, IntelliJ, Other
29+
Kotlin,
30+
IntelliJ,
31+
Other,
3132
}
3233

33-
/*
34-
* A rule for determining the kind of repository based on its name and topics.
35-
*/
34+
// A rule for determining the kind of repository based on its name and topics.
3635
fun getKind(fullName: String, topics: List<String>): RepoKind {
3736
fun checkContains(name: String) = name in topics || fullName.lowercase().contains(name)
3837

@@ -43,30 +42,45 @@ fun getKind(fullName: String, topics: List<String>): RepoKind {
4342
}
4443
}
4544

46-
4745
fun main() {
4846
val repos = DataFrame
4947
// Read DataFrame from the CSV file.
5048
.readCsv("https://raw.githubusercontent.com/Kotlin/dataframe/master/data/jetbrains_repositories.csv")
5149
// And convert it to match the `Repositories` schema.
5250
.convertTo<Repositories>()
5351

54-
// Rename columns to CamelCase
55-
val reposUpdated = repos.renameToCamelCase()
52+
// With Compiler Plugin, the DataFrame schema changes immediately after each operation:
53+
// For example, if a new column is added or the old one is renamed (or its type is changed)
54+
// during the operation, you can use the new name immediately in the following operations:
55+
repos
56+
// Add a new "name" column...
57+
.add("name") { full_name.substringAfterLast("/") }
58+
// ... and now we can use "name" extension in DataFrame operations, such as `filter`.
59+
.filter { name.lowercase().contains("kotlin") }
60+
61+
// Let's update the DataFrame with some operations using these features.
62+
val reposUpdated = repos
63+
// Rename columns to CamelCase.
64+
// Note that after that, in the following operations, extension properties will have
65+
// new names corresponding to the column names.
66+
.renameToCamelCase()
67+
// Rename "stargazersCount" column to "stars".
68+
.rename { stargazersCount }.into("stars")
69+
// And we can immediately use the updated name in the filtering.
70+
.filter { stars > 50 }
5671
// Convert values in the "topic" column (which were `String` initially)
5772
// to the list of topics.
5873
.convert { topics }.with {
59-
val inner = it.removePrefix("[").removeSuffix("]")
74+
val inner = it.removeSurrounding("[", "]")
6075
if (inner.isEmpty()) emptyList() else inner.split(',').map(String::trim)
6176
}
62-
// Filter rows, keeping only repos with more than 150 stars.
63-
.filter { it.stargazersCount > 150 }
77+
// Now "topics" is a `List<String>` column.
6478
// Add a new column with the number of topics.
6579
.add("topicCount") { topics.size }
6680
// Add a new column with the kind of repository.
6781
.add("kind") { getKind(fullName, topics) }
6882

69-
// Write an updated DataFrame to a CSV file.
83+
// Write the updated DataFrame to a CSV file.
7084
reposUpdated.writeCsv("jetbrains_repositories_new.csv")
7185

7286
// TODO: Add Kandy Plot

settings.gradle.kts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ include("plugins:dataframe-gradle-plugin")
1111
include("plugins:symbol-processor")
1212
include("plugins:expressions-converter")
1313
include("plugins:kotlin-dataframe")
14+
include("plugins:public-api-modifier")
1415
include("tests")
1516
include("dataframe-json")
1617
include("dataframe-arrow")
@@ -19,12 +20,16 @@ include("dataframe-excel")
1920
include("dataframe-jdbc")
2021
include("dataframe-csv")
2122
include("dataframe-jupyter")
23+
include("dataframe-geo")
24+
include("dataframe-openapi-generator")
2225
include("core")
26+
include("dataframe-compiler-plugin-core")
2327

2428
include("examples:idea-examples:titanic")
2529
include("examples:idea-examples:movies")
2630
include("examples:idea-examples:youtube")
2731
include("examples:idea-examples:json")
32+
includeBuild("examples/kotlin-dataframe-plugin-example")
2833

2934
val jupyterApiTCRepo: String by settings
3035

@@ -44,11 +49,3 @@ pluginManagement {
4449
plugins {
4550
id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0"
4651
}
47-
include("dataframe-excel")
48-
include("core")
49-
include("dataframe-openapi-generator")
50-
include("dataframe-geo")
51-
include("plugins:public-api-modifier")
52-
include("dataframe-compiler-plugin-core")
53-
54-
includeBuild("examples/kotlin-dataframe-plugin-example")

0 commit comments

Comments
 (0)