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

Wrap more multiline expressions #1876

Merged
merged 2 commits into from
Mar 21, 2023
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
15 changes: 8 additions & 7 deletions build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ repositories {
}

dependencies {
val kotlinPlugin = if (project.hasProperty("kotlinDev")) {
// Pass '-PkotlinDev' to command line to enable kotlin-in-development version
logger.warn("Enabling kotlin dev version!")
libs.kotlin.plugin.dev
} else {
libs.kotlin.plugin
}
val kotlinPlugin =
if (project.hasProperty("kotlinDev")) {
// Pass '-PkotlinDev' to command line to enable kotlin-in-development version
logger.warn("Enabling kotlin dev version!")
libs.kotlin.plugin.dev
} else {
libs.kotlin.plugin
}
implementation(kotlinPlugin)
implementation(libs.dokka)
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,29 @@ public class FormatReporter(

override fun after(file: String) {
val canNotBeAutocorrected = countCanNotBeAutoCorrected.getOrDefault(file, 0)
val result = when {
canNotBeAutocorrected == 1 ->
if (format) {
"Format not completed (1 violation needs manual fixing)"
} else {
"Format required (1 violation needs manual fixing)"
}
canNotBeAutocorrected > 1 ->
if (format) {
"Format not completed ($canNotBeAutocorrected violations need manual fixing)"
} else {
"Format required ($canNotBeAutocorrected violations need manual fixing)"
}
countAutoCorrectPossibleOrDone.getOrDefault(file, 0) > 0 ->
if (format) {
"Format completed (all violations have been fixed)"
} else {
"Format required (all violations can be autocorrected)"
}
else ->
"Format not needed (no violations found)"
}
val result =
when {
canNotBeAutocorrected == 1 ->
if (format) {
"Format not completed (1 violation needs manual fixing)"
} else {
"Format required (1 violation needs manual fixing)"
}
canNotBeAutocorrected > 1 ->
if (format) {
"Format not completed ($canNotBeAutocorrected violations need manual fixing)"
} else {
"Format required ($canNotBeAutocorrected violations need manual fixing)"
}
countAutoCorrectPossibleOrDone.getOrDefault(file, 0) > 0 ->
if (format) {
"Format completed (all violations have been fixed)"
} else {
"Format required (all violations can be autocorrected)"
}
else ->
"Format not needed (no violations found)"
}
out.println(
"${colorFileName(file)}${":".colored()} $result",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ public class PlainReporter(
val errList = acc[file] ?: return
out.println(colorFileName(file))
for ((line, col, ruleId, detail) in errList) {
val column = if (pad) {
String.format("%-3s", col)
} else {
col
}
val column =
if (pad) {
String.format("%-3s", col)
} else {
col
}
out.println(
" $line${":$column".colored()} $detail ${"($ruleId)".colored()}",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,46 +178,47 @@ private fun FileSystem.toGlob(
path: String,
rootDir: Path,
): List<String> {
val negation = if (path.startsWith(NEGATION_PREFIX)) {
NEGATION_PREFIX
} else {
""
}
val negation =
if (path.startsWith(NEGATION_PREFIX)) {
NEGATION_PREFIX
} else {
""
}
val pathWithoutNegationPrefix =
path
.removePrefix(NEGATION_PREFIX)
val expandedPatterns = try {
val resolvedPath =
rootDir
.resolve(pathWithoutNegationPrefix)
.normalize()
if (resolvedPath.isDirectory()) {
resolvedPath
.expandPathToDefaultPatterns()
.also {
LOGGER.trace { "Expanding resolved directory path '$resolvedPath' to patterns: [$it]" }
}
} else {
resolvedPath
.pathString
.expandDoubleStarPatterns()
.also {
LOGGER.trace { "Expanding resolved path '$resolvedPath` to patterns: [$it]" }
}
}
} catch (e: InvalidPathException) {
if (onWindowsOS) {
// Windows throws an exception when passing a wildcard (*) to Path#resolve.
pathWithoutNegationPrefix
.expandDoubleStarPatterns()
.also {
LOGGER.trace { "On WindowsOS: expanding unresolved path '$pathWithoutNegationPrefix` to patterns: [$it]" }
}
} else {
emptyList()
val expandedPatterns =
try {
val resolvedPath =
rootDir
.resolve(pathWithoutNegationPrefix)
.normalize()
if (resolvedPath.isDirectory()) {
resolvedPath
.expandPathToDefaultPatterns()
.also {
LOGGER.trace { "Expanding resolved directory path '$resolvedPath' to patterns: [$it]" }
}
} else {
resolvedPath
.pathString
.expandDoubleStarPatterns()
.also {
LOGGER.trace { "Expanding resolved path '$resolvedPath` to patterns: [$it]" }
}
}
} catch (e: InvalidPathException) {
if (onWindowsOS) {
// Windows throws an exception when passing a wildcard (*) to Path#resolve.
pathWithoutNegationPrefix
.expandDoubleStarPatterns()
.also {
LOGGER.trace { "On WindowsOS: expanding unresolved path '$pathWithoutNegationPrefix` to patterns: [$it]" }
}
} else {
emptyList()
}
}
}

return expandedPatterns
.map { originalPattern ->
if (onWindowsOS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ internal object GitHookInstaller {
gitHookName: String,
hookContentProvider: () -> ByteArray,
) {
val gitHooksDir = try {
resolveGitHooksDir()
} catch (e: IOException) {
System.err.println(e.message)
exitKtLintProcess(1)
}

val gitHooksDir =
try {
resolveGitHooksDir()
} catch (e: IOException) {
System.err.println(e.message)
exitKtLintProcess(1)
}
val gitHookFile = gitHooksDir.resolve(gitHookName)
val hookContent = hookContentProvider()

Expand Down Expand Up @@ -54,19 +54,19 @@ internal object GitHookInstaller {

// Try to find the .git directory automatically, falling back to `./.git`
private fun getGitDir(): File {
val gitDir = try {
val root =
Runtime.getRuntime().exec("git rev-parse --show-toplevel")
.inputStream
.bufferedReader()
.readText()
.trim()

File(root).resolve(".git")
} catch (_: IOException) {
File(".git")
}

val gitDir =
try {
val root =
Runtime.getRuntime().exec("git rev-parse --show-toplevel")
.inputStream
.bufferedReader()
.readText()
.trim()

File(root).resolve(".git")
} catch (_: IOException) {
File(".git")
}
if (!gitDir.isDirectory) {
throw IOException(".git directory not found. Are you sure you are inside project directory?")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,12 @@ internal class KtlintCommandLine {
isInvokedFromCli = true,
)

val baseline = if (stdin || baselinePath.isBlank()) {
Baseline(status = Baseline.Status.DISABLED)
} else {
loadBaseline(baselinePath)
}

val baseline =
if (stdin || baselinePath.isBlank()) {
Baseline(status = Baseline.Status.DISABLED)
} else {
loadBaseline(baselinePath)
}
val aggregatedReporter =
ReporterAggregator(
baseline,
Expand Down Expand Up @@ -484,11 +484,12 @@ internal class KtlintCommandLine {
lintError
.detail
.applyIf(corrected) { "$this (cannot be auto-corrected)" },
status = if (corrected) {
FORMAT_IS_AUTOCORRECTED
} else {
LINT_CAN_NOT_BE_AUTOCORRECTED
},
status =
if (corrected) {
FORMAT_IS_AUTOCORRECTED
} else {
LINT_CAN_NOT_BE_AUTOCORRECTED
},
)
if (baselineLintErrors.doesNotContain(ktlintCliError)) {
ktlintCliErrors.add(ktlintCliError)
Expand Down Expand Up @@ -543,11 +544,12 @@ internal class KtlintCommandLine {
col = lintError.col,
ruleId = lintError.ruleId.value,
detail = lintError.detail,
status = if (lintError.canBeAutoCorrected) {
LINT_CAN_BE_AUTOCORRECTED
} else {
LINT_CAN_NOT_BE_AUTOCORRECTED
},
status =
if (lintError.canBeAutoCorrected) {
LINT_CAN_BE_AUTOCORRECTED
} else {
LINT_CAN_NOT_BE_AUTOCORRECTED
},
)
if (baselineLintErrors.doesNotContain(ktlintCliError)) {
ktlintCliErrors.add(ktlintCliError)
Expand Down Expand Up @@ -588,11 +590,12 @@ internal class KtlintCommandLine {
status = KOTLIN_PARSE_EXCEPTION,
)
is KtLintRuleException -> {
val codeSource = if (code.isStdIn) {
"code"
} else {
"file '${code.fileName}'"
}
val codeSource =
if (code.isStdIn) {
"code"
} else {
"file '${code.fileName}'"
}
logger.debug("Internal Error (${e.ruleId}) in $codeSource at position '${e.line}:${e.col}", e)
KtlintCliError(
line = e.line,
Expand Down Expand Up @@ -646,30 +649,31 @@ internal class KtlintCommandLine {
cb: (T) -> Unit,
numberOfThreads: Int = Runtime.getRuntime().availableProcessors(),
) {
val pill = object : Future<T> {
override fun isDone(): Boolean {
throw UnsupportedOperationException()
}
val pill =
object : Future<T> {
override fun isDone(): Boolean {
throw UnsupportedOperationException()
}

override fun get(
timeout: Long,
unit: TimeUnit,
): T {
throw UnsupportedOperationException()
}
override fun get(
timeout: Long,
unit: TimeUnit,
): T {
throw UnsupportedOperationException()
}

override fun get(): T {
throw UnsupportedOperationException()
}
override fun get(): T {
throw UnsupportedOperationException()
}

override fun cancel(mayInterruptIfRunning: Boolean): Boolean {
throw UnsupportedOperationException()
}
override fun cancel(mayInterruptIfRunning: Boolean): Boolean {
throw UnsupportedOperationException()
}

override fun isCancelled(): Boolean {
throw UnsupportedOperationException()
override fun isCancelled(): Boolean {
throw UnsupportedOperationException()
}
}
}
val q = ArrayBlockingQueue<Future<T>>(numberOfThreads)
val producer =
thread(start = true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,20 @@ internal class ReporterAggregator(
"Initializing \"${reporterConfiguration.id}\" reporter with ${reporterConfiguration.additionalConfig}" +
(reporterConfiguration.output?.let { ", output=$it" } ?: "")
}
val stream = when {
reporterConfiguration.output != null -> {
File(reporterConfiguration.output).parentFile?.mkdirsOrFail(); PrintStream(reporterConfiguration.output, "UTF-8")
}
val stream =
when {
reporterConfiguration.output != null -> {
File(reporterConfiguration.output).parentFile?.mkdirsOrFail(); PrintStream(reporterConfiguration.output, "UTF-8")
}

stdin -> {
System.err
}
stdin -> {
System.err
}

else -> {
System.out
else -> {
System.out
}
}
}
return get(stream, reporterConfiguration.additionalConfig)
.let { reporterV2 ->
if (reporterConfiguration.output != null) {
Expand Down
Loading