Skip to content

Commit

Permalink
Introduce "applyToIDEA" subcommand. (#554)
Browse files Browse the repository at this point in the history
This subcommand replaces --apply-to-idea option. Option itself is still
supported, but deprecated.

Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
  • Loading branch information
Tapchicoma authored and shashachu committed Sep 3, 2019
1 parent b3f7cf4 commit b63cd7f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 8 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,17 @@ ktlint --apply-to-idea-project --android

##### Option #2

Apply to all IDEA projects:
```shell
$ ./ktlint applyToIDEA
```
Or if you want to use android specific code style:
```shell
$ ./ktlint --android applyToIDEA
```

##### Option #3

Go to <kbd>File</kbd> -> <kbd>Settings...</kbd> -> <kbd>Editor</kbd>
- <kbd>General</kbd> -> <kbd>Auto Import</kbd>
- check `Optimize imports on the fly (for current project)`.
Expand Down
12 changes: 4 additions & 8 deletions ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.pinterest.ktlint.core.Reporter
import com.pinterest.ktlint.core.ReporterProvider
import com.pinterest.ktlint.core.RuleExecutionException
import com.pinterest.ktlint.core.RuleSetProvider
import com.pinterest.ktlint.internal.ApplyToIDEAGloballySubCommand
import com.pinterest.ktlint.internal.GitPreCommitHookSubCommand
import com.pinterest.ktlint.internal.GitPrePushHookSubCommand
import com.pinterest.ktlint.internal.IntellijIDEAIntegration
Expand Down Expand Up @@ -51,6 +52,7 @@ fun main(args: Array<String>) {
.addSubcommand(GitPreCommitHookSubCommand.COMMAND_NAME, GitPreCommitHookSubCommand())
.addSubcommand(GitPrePushHookSubCommand.COMMAND_NAME, GitPrePushHookSubCommand())
.addSubcommand(PrintASTSubCommand.COMMAND_NAME, PrintASTSubCommand())
.addSubcommand(ApplyToIDEAGloballySubCommand.COMMAND_NAME, ApplyToIDEAGloballySubCommand())
val parseResult = commandLine.parseArgs(*args)

commandLine.printHelpOrVersionUsage()
Expand All @@ -70,6 +72,7 @@ fun handleSubCommand(
is GitPreCommitHookSubCommand -> subCommand.run()
is GitPrePushHookSubCommand -> subCommand.run()
is PrintASTSubCommand -> subCommand.run()
is ApplyToIDEAGloballySubCommand -> subCommand.run()
else -> commandLine.usage(System.out, CommandLine.Help.Ansi.OFF)
}
}
Expand Down Expand Up @@ -117,13 +120,6 @@ class KtlintCommandLine {
)
var android: Boolean = false

// todo: make it a command in 1.0.0 (it's too late now as we might interfere with valid "lint" patterns)
@Option(
names = ["--apply-to-idea"],
description = ["Update Intellij IDEA settings (global)"]
)
private var apply: Boolean = false

// todo: make it a command in 1.0.0 (it's too late now as we might interfere with valid "lint" patterns)
@Option(
names = ["--apply-to-idea-project"],
Expand Down Expand Up @@ -221,7 +217,7 @@ class KtlintCommandLine {
private var patterns = ArrayList<String>()

fun run() {
if (apply || applyToProject) {
if (applyToProject) {
applyToIDEA()
exitProcess(0)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.pinterest.ktlint.internal

import com.pinterest.ktlint.KtlintCommandLine
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.system.exitProcess
import picocli.CommandLine

@CommandLine.Command(
description = [
"Update Intellij IDEA Kotlin codestyle settings (global)"
],
aliases = ["--apply-to-idea"],
mixinStandardHelpOptions = true,
versionProvider = KtlintVersionProvider::class
)
class ApplyToIDEAGloballySubCommand : Runnable {
@CommandLine.ParentCommand
private lateinit var ktlintCommand: KtlintCommandLine

@CommandLine.Spec
private lateinit var commandSpec: CommandLine.Model.CommandSpec

@CommandLine.Option(
names = ["-y"],
description = ["Overwrite existing Kotlin codestyle settings without asking"]
)
private var forceApply: Boolean = false

override fun run() {
commandSpec.commandLine().printHelpOrVersionUsage()

try {
val workDir = Paths.get(".")

if (!forceApply && !getUserAcceptanceToUpdateFiles(workDir)) {
println("Update canceled.")
exitProcess(1)
}

IntellijIDEAIntegration.apply(workDir, false, ktlintCommand.android, false)
} catch (e: IntellijIDEAIntegration.ProjectNotFoundException) {
println(".idea directory not found. Are you sure you are inside project root directory?")
exitProcess(1)
}

println(
"""
|Updated.
|Please restart your IDE.
|If you experience any issues please report them at https://github.com/pinterest/ktlint/issues.
""".trimMargin()
)
}

private fun getUserAcceptanceToUpdateFiles(workDir: Path): Boolean {
val fileList = IntellijIDEAIntegration.apply(workDir, true, ktlintCommand.android, false)
println(
"""
|The following files are going to be updated:
|${fileList.joinToString(prefix = "\t", separator = "\n\t")}
|
|Do you wish to proceed? [y/n]
|(in future, use -y flag if you wish to skip confirmation)
""".trimMargin()
)

val userInput = generateSequence { readLine() }
.filter { it.trim().isNotBlank() }
.first()

return "y".equals(userInput, ignoreCase = true)
}

companion object {
const val COMMAND_NAME = "applyToIDEA"
}
}

0 comments on commit b63cd7f

Please sign in to comment.