Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reformat a selection instead of entire file #503

Merged
merged 10 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:

# Validate wrapper
- name: Gradle Wrapper Validation
uses: gradle/wrapper-validation[email protected]
uses: gradle/actions/wrapper-validation@v3

# Set up Java environment for the next steps
- name: Setup Java
Expand Down
74 changes: 60 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,67 @@ Once the plugin has been tested with the `Run Plugin.run.xml` run configuration,
- Install the zip file manually using
<kbd>Preferences</kbd> > <kbd>Plugins</kbd> > <kbd>⚙️</kbd> > <kbd>Install plugin from disk...</kbd>

## Adding a ktlint ruleset

## Building with local ktlint SNAPSHOT
The `ktlint-lib` module contains all version of the ktlint rules which are supported by the plugin.

In case you need to build the plugin based on a SNAPSHOT version of ktlint on your local machine, then follow procedure below:

* In the "ktlint" project execute `./gradlew publishMavenPublicationToMavenLocal` to publish the SNAPSHOT artifacts to your local maven repository.
* In the "ktlint-intellij-plugin" project:
* Change the ktlint version in `libs.version.toml`
* Include `mavenLocal()` repository in *all* `build.gradle.kts` files as follows:
To support a new version (`X.Y.Z`) of ktlint, the following needs to be done:
* Duplicate the latest `ruleset-A-B-C` module in `ktlint-lib`. This module only contains a `build.gradle.kts` file. This file needs to be changed as follows:
* In the `dependencies` block refer to the new ktlint version `X.Y.Z` (use a snapshot version when applicable)
```kotlin
dependencies {
implementation("com.pinterest.ktlint:ktlint-ruleset-standard:X.Y.Z-SNAPSHOT")
}
```
* In the `relocate` block change the coordinates of the `StandardRulesetProvider` as follows (note that only the minor version `Y` needs to be left padded with a 0 to support up to 99 minor versions):
```kotlin
repositories {
mavenCentral()
mavenLocal()
}
relocate(
"com.pinterest.ktlint.ruleset.standard",
"com.pinterest.ktlint.ruleset.standard.VX_YY_Z",
)
```
* Build the `lib` module
* Build the `plugin` module
* Run the plugin
* In class `KtlintRulesetVersion` add a new enum entry below enum entry `DEFAULT`:
```kotlin
DEFAULT("default (recommended)", null),
VX_Y_Z("X.Y.Z", StandardRuleSetProviderVX_0Y_Z()),
```
Note: the required import `com.pinterest.ktlint.ruleset.standard.VX_0Y_Z.StandardRuleSetProvider as StandardRuleSetProviderVX_0Y_Z` will not be valid until all steps have been completed and the build has succeeded.
* In the `dependencies` block of `ktlint-lib/build.gradle.kts` add following:
```kotlin
compileOnly(project(":ktlint-lib:ruleset-X-Y-Z")) // Required for IDE
implementation(project(":ktlint-lib:ruleset-X-Y-Z", "shadow"))
```
* In field `rulesetVersion` in file `KtlintConfigForm` add the new option `X.Y.Z` just below value `default (recommended)`. Note that this value should be identical to the value of the `label` used in the enum entry in `KtlintRulesetVersion`.
* In the `include` block in the root `build.gradle.kts` add following:
```kotlin
"ktlint-lib:ruleset-X-Y-Z",
```

Note: the total size of the plugin grows with approximately 1 MB per ruleset version which is added.

## Building with ktlint SNAPSHOT version

Snapshots of ktlint are published on Sonatype https://oss.sonatype.org/content/repositories/snapshots/com/pinterest/ktlint/

Add following section to the `build.gradle.kts` of the `ktlint-lib` module:
```kotlin
allprojects {
repositories {
mavenCentral()
maven("https://oss.sonatype.org/content/repositories/snapshots")
}
}
```

In `gradle/libs.version.toml` change the `ktlint` setting to the snapshot-version.

In case you want to build with a local version of ktlint which is not yet published to Sonatype, then add following section to the `build.gradle.kts` of the `ktlint-lib` module instead:
```kotlin
allprojects {
repositories {
mavenCentral()
mavenLocal()
}
}
```
Note: In the "ktlint" project execute `./gradlew publishMavenPublicationToMavenLocal` to publish the SNAPSHOT artifacts to your local maven repository!
25 changes: 14 additions & 11 deletions RELEASING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ Note: These steps should be done directly in the `nbadal/ktlint-intellij-plugin`

Releasing a new version:

1. Create new branch, for example `0.20.0-beta-7-prep`.
2. Check whether plugin description in readme file of plugin-folder is up-to-date as this is used as description of the plugin on the Jetbrains Marketplace inside IntelliJ IDEA and for [Marketplace Overview](https://plugins.jetbrains.com/plugin/15057-ktlint?noRedirect=true).
3. Generate changelog
1. Verification before starting release:
* Check that none of the `build.gradle.kts` depends on `mavenLocal()` or `maven("https://oss.sonatype.org/content/repositories/snapshots")`. Those may only be used in snapshot versions of the plugin
* check that the ruleset for the latest ktlint version has been added to `ktlint-lib`. See `README.md` for more information.
2. Create new branch, for example `0.20.0-beta-7-prep`.
3. Check whether plugin description in readme file of plugin-folder is up-to-date as this is used as description of the plugin on the Jetbrains Marketplace inside IntelliJ IDEA and for [Marketplace Overview](https://plugins.jetbrains.com/plugin/15057-ktlint?noRedirect=true).
4. Generate changelog
* The generated changelog only contains the PR titles. For most changes this should be sufficient. For breaking API changes, it is better to add additional information. To indent this explanation correctly, append ` ` (two spaces) to the end of the previous line.
4. Do not change the heading `Unreleased` in `CHANGELOG.md` as it will be updated automatically. Also, the release will be added automatically to the bottom of the `CHANGELOG.md` file.
5. Set field `pluginVersion` in `gradle.properties` to the new version number. Note that when the version if suffixed with `-beta` it will be released on the beta channel of the plugin only. Only users that have configured the additional repository `https://plugins.jetbrains.com/plugins/list?pluginId=com.nbadal.ktlint&channel=beta` will see the new version after it is released.
6. Push your changes to the branch, and merge it to `master`.
7. Do *not* tag the release manually, but go to the latest [Draft Release](https://github.com/nbadal/ktlint-intellij-plugin/releases). Check that release notes are up-to-date. It is not needed to add the ZIP file containing the plugin, as it will be published via the marketplace.
8. Check that the release is available on [Marketplace Versions](https://plugins.jetbrains.com/plugin/15057-ktlint/versions?noRedirect=true). Note that it might take a full business day before the plugin is actually released due to a manual review by Jetbrains.
9. Check that screenshots on the [Marketplace Overview](https://plugins.jetbrains.com/plugin/15057-ktlint?noRedirect=true) are up-to-date.
10. Announce release on Ktlint Slack channel
11. Update `gradle.properties` with the new `beta` version, and add the section below to the top of `CHANGELOG.md` and commit. (This can be done directly in the main repo or in your fork.)
5. Do not change the heading `Unreleased` in `CHANGELOG.md` as it will be updated automatically. Also, the release will be added automatically to the bottom of the `CHANGELOG.md` file.
6. Set field `pluginVersion` in `gradle.properties` to the new version number. Note that when the version if suffixed with `-beta` it will be released on the beta channel of the plugin only. Only users that have configured the additional repository `https://plugins.jetbrains.com/plugins/list?pluginId=com.nbadal.ktlint&channel=beta` will see the new version after it is released.
7. Push your changes to the branch, and merge it to `master`.
8. Do *not* tag the release manually, but go to the latest [Draft Release](https://github.com/nbadal/ktlint-intellij-plugin/releases). Check that release notes are up-to-date. It is not needed to add the ZIP file containing the plugin, as it will be published via the marketplace.
9. Check that the release is available on [Marketplace Versions](https://plugins.jetbrains.com/plugin/15057-ktlint/versions?noRedirect=true). Note that it might take a full business day before the plugin is actually released due to a manual review by Jetbrains.
10. Check that screenshots on the [Marketplace Overview](https://plugins.jetbrains.com/plugin/15057-ktlint?noRedirect=true) are up-to-date.
11. Announce release on Ktlint Slack channel
12. Update `gradle.properties` with the new `beta` version, and add the section below to the top of `CHANGELOG.md` and commit. (This can be done directly in the main repo or in your fork.)
```markdown
## [Unreleased]
```
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ changelog = "2.2.0"
gradleIntelliJPlugin = "1.17.3"
kotlin = "1.9.23"
kover = "0.7.6"
ktlint = "1.2.1"
ktlint = "1.2.2-SNAPSHOT"
qodana = "0.1.13"
shadow = "8.1.1"

Expand Down
19 changes: 11 additions & 8 deletions ktlint-lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ plugins {
alias(libs.plugins.shadow)
}

repositories {
mavenCentral()
allprojects {
repositories {
mavenCentral()
maven("https://oss.sonatype.org/content/repositories/snapshots")
}
}

dependencies {
Expand All @@ -24,18 +27,18 @@ dependencies {
compileOnly(project(":ktlint-lib:ruleset-1-1-1")) // Required for IDE
implementation(project(":ktlint-lib:ruleset-1-1-1", "shadow"))

compileOnly(project(":ktlint-lib:ruleset-1-2-0")) // Required for IDE
implementation(project(":ktlint-lib:ruleset-1-2-0", "shadow"))

compileOnly(project(":ktlint-lib:ruleset-1-2-1")) // Required for IDE
implementation(project(":ktlint-lib:ruleset-1-2-1", "shadow"))

compileOnly(project(":ktlint-lib:ruleset-1-2-2")) // Required for IDE
implementation(project(":ktlint-lib:ruleset-1-2-2", "shadow"))

implementation("com.rollbar:rollbar-java:1.10.0") {
exclude(group = "org.slf4j") // Duplicated in IDE environment
}

// Tests:
testImplementation(project(":ktlint-lib:core"))
testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
testImplementation("org.junit.platform:junit-platform-launcher:1.10.2")
testImplementation("io.mockk:mockk:1.13.10")
}

tasks {
Expand Down
4 changes: 0 additions & 4 deletions ktlint-lib/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ plugins {
alias(libs.plugins.shadow)
}

repositories {
mavenCentral()
}

dependencies {
api(libs.ktlintRuleEngine)
api(libs.ktlintCliRulesetCore)
Expand Down
1 change: 1 addition & 0 deletions ktlint-lib/ruleset-0-50-0/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
alias(libs.plugins.shadow)
}

// Prevent that snapshot artifacts can be used for ktlint versions that have been released officially
repositories {
mavenCentral()
}
Expand Down
1 change: 1 addition & 0 deletions ktlint-lib/ruleset-1-0-1/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
alias(libs.plugins.shadow)
}

// Prevent that snapshot artifacts can be used for ktlint versions that have been released officially
repositories {
mavenCentral()
}
Expand Down
1 change: 1 addition & 0 deletions ktlint-lib/ruleset-1-1-1/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
alias(libs.plugins.shadow)
}

// Prevent that snapshot artifacts can be used for ktlint versions that have been released officially
repositories {
mavenCentral()
}
Expand Down
40 changes: 40 additions & 0 deletions ktlint-lib/ruleset-1-2-0/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("java") // Java support
alias(libs.plugins.kotlin) // Kotlin support
alias(libs.plugins.shadow)
}

// Prevent that snapshot artifacts can be used for ktlint versions that have been released officially
repositories {
mavenCentral()
}

dependencies {
implementation("com.pinterest.ktlint:ktlint-ruleset-standard:1.2.0")
}

tasks {
// Set the compatibility versions to 11
withType<JavaCompile> {
sourceCompatibility = "11"
targetCompatibility = "11"
}
withType<KotlinCompile> {
kotlinOptions.jvmTarget = "11"
}

withType<ShadowJar> {
val api = project.configurations.api.get()
val impl = project.configurations.implementation.get()

configurations = listOf(api, impl).map { it.apply { isCanBeResolved = true } }

relocate(
"com.pinterest.ktlint.ruleset.standard",
"com.pinterest.ktlint.ruleset.standard.V1_02_0",
)
}
}
1 change: 1 addition & 0 deletions ktlint-lib/ruleset-1-2-1/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
alias(libs.plugins.shadow)
}

// Prevent that snapshot artifacts can be used for ktlint versions that have been released officially
repositories {
mavenCentral()
}
Expand Down
42 changes: 42 additions & 0 deletions ktlint-lib/ruleset-1-2-2/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("java") // Java support
alias(libs.plugins.kotlin) // Kotlin support
alias(libs.plugins.shadow)
}

// TODO: Uncomment once ktlint 1.2.2 is released officially
// Prevent that snapshot artifacts can be used for ktlint versions that have been released officially
// repositories {
// mavenCentral()
// }

dependencies {
// TODO: remove `-SNAPSHOT` once ktlint 1.2.2 is released officially
implementation("com.pinterest.ktlint:ktlint-ruleset-standard:1.2.2-SNAPSHOT")
}

tasks {
// Set the compatibility versions to 11
withType<JavaCompile> {
sourceCompatibility = "11"
targetCompatibility = "11"
}
withType<KotlinCompile> {
kotlinOptions.jvmTarget = "11"
}

withType<ShadowJar> {
val api = project.configurations.api.get()
val impl = project.configurations.implementation.get()

configurations = listOf(api, impl).map { it.apply { isCanBeResolved = true } }

relocate(
"com.pinterest.ktlint.ruleset.standard",
"com.pinterest.ktlint.ruleset.standard.V1_02_2",
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3
import com.pinterest.ktlint.ruleset.standard.V0_50_0.StandardRuleSetProvider as StandardRuleSetProviderV0_50_0
import com.pinterest.ktlint.ruleset.standard.V1_00_1.StandardRuleSetProvider as StandardRuleSetProviderV1_00_1
import com.pinterest.ktlint.ruleset.standard.V1_01_1.StandardRuleSetProvider as StandardRuleSetProviderV1_01_1
import com.pinterest.ktlint.ruleset.standard.V1_02_0.StandardRuleSetProvider as StandardRuleSetProviderV1_02_0
import com.pinterest.ktlint.ruleset.standard.V1_02_1.StandardRuleSetProvider as StandardRuleSetProviderV1_02_1
import com.pinterest.ktlint.ruleset.standard.V1_02_2.StandardRuleSetProvider as StandardRuleSetProviderV1_02_2

/**
* Policies for supporting rulesets from older versions:
Expand All @@ -19,7 +21,9 @@ enum class KtlintRulesetVersion(
private val ruleSetProvider: RuleSetProviderV3?,
) {
DEFAULT("default (recommended)", null),
V1_2_2("1.2.2", StandardRuleSetProviderV1_02_2()),
V1_2_1("1.2.1", StandardRuleSetProviderV1_02_1()),
V1_2_0("1.2.0", StandardRuleSetProviderV1_02_0()),
V1_1_1("1.1.1", StandardRuleSetProviderV1_01_1()),
V1_0_1("1.0.1", StandardRuleSetProviderV1_00_1()),
V0_50_0("0.50.0", StandardRuleSetProviderV0_50_0()),
Expand Down
4 changes: 1 addition & 3 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ val platformDownloadSources: String by project
group = pluginGroup
version = pluginVersion

// Configure project's dependencies
repositories {
mavenCentral()
mavenLocal()
}

// Dependencies are managed with Gradle version catalog - read more: https://docs.gradle.org/current/userguide/platforms.html#sub:version-catalog
Expand All @@ -53,8 +53,6 @@ dependencies {
exclude(group = "org.slf4j") // Duplicated in IDE environment
}

// Tests:
testImplementation(project(":ktlint-lib:core"))
testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
testImplementation("org.junit.platform:junit-platform-launcher:1.10.2")
testImplementation("io.mockk:mockk:1.13.10")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ class KtlintActionOnSave : ActionOnSave() {
PsiManager
.getInstance(project)
.findFile(virtualFile)
?.let { psiFile -> ktlintFormat(psiFile, "KtlintActionOnSave") }
?.let { psiFile ->
ktlintFormat(psiFile, ktlintFormatRange = KtlintFileFormatRange, triggeredBy = "KtlintActionOnSave")
}
}
} else {
// Only format files which were modified
psiFiles.forEach { psiFile -> ktlintFormat(psiFile, "KtlintActionOnSave") }
psiFiles.forEach { psiFile ->
ktlintFormat(psiFile, ktlintFormatRange = KtlintFileFormatRange, triggeredBy = "KtlintActionOnSave")
}
}
}
}
Expand Down
Loading
Loading