From b5bb65c73e9ad02e73c7e862629aefafe3804476 Mon Sep 17 00:00:00 2001 From: Yahor Berdnikau Date: Sat, 3 Aug 2019 13:11:30 +0200 Subject: [PATCH] Introduce "applyToIDEA" subcommand. This subcommand replaces --apply-to-idea option. Option itself is still supported, but deprecated. Signed-off-by: Yahor Berdnikau --- README.md | 11 +++ .../main/kotlin/com/pinterest/ktlint/Main.kt | 12 +-- .../internal/ApplyToIDEAGloballySubCommand.kt | 78 +++++++++++++++++++ 3 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 ktlint/src/main/kotlin/com/pinterest/ktlint/internal/ApplyToIDEAGloballySubCommand.kt diff --git a/README.md b/README.md index 594edddf6b..c54301e426 100644 --- a/README.md +++ b/README.md @@ -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 File -> Settings... -> Editor - General -> Auto Import - check `Optimize imports on the fly (for current project)`. diff --git a/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt b/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt index e15f2f1038..c444d0aac4 100644 --- a/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt +++ b/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt @@ -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 @@ -56,6 +57,7 @@ fun main(args: Array) { .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() @@ -75,6 +77,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) } } @@ -122,13 +125,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"], @@ -249,7 +245,7 @@ class KtlintCommandLine { private var patterns = ArrayList() fun run() { - if (apply || applyToProject) { + if (applyToProject) { applyToIDEA() exitProcess(0) } diff --git a/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/ApplyToIDEAGloballySubCommand.kt b/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/ApplyToIDEAGloballySubCommand.kt new file mode 100644 index 0000000000..24fd280ebe --- /dev/null +++ b/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/ApplyToIDEAGloballySubCommand.kt @@ -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" + } +}