From 295cbce75b891a7270aab1ac2548a2c2724ef7ea Mon Sep 17 00:00:00 2001 From: Yahor Berdnikau Date: Thu, 14 Oct 2021 19:17:14 +0200 Subject: [PATCH] Revert "Fix Kotlin compiler may not in all cases be released." This reverts commit 3d2b20fcf0d51a96ea0fd2b5d564da9ac544c720. --- .../main/kotlin/com/pinterest/ktlint/core/KtLint.kt | 7 ++----- .../ktlint/core/internal/KotlinPsiFileFactory.kt | 12 +++++++----- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/KtLint.kt b/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/KtLint.kt index 4ddebcce21..e259477fb3 100644 --- a/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/KtLint.kt +++ b/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/KtLint.kt @@ -169,16 +169,16 @@ public object KtLint { } } catch (e: Exception) { val (line, col) = preparedCode.positionInTextLocator(node.startOffset) - kotlinPsiFileFactory.releasePsiFileFactory() throw RuleExecutionException(line, col, fqRuleId, e) } } } - kotlinPsiFileFactory.releasePsiFileFactory() errors .sortedWith { l, r -> if (l.line != r.line) l.line - r.line else l.col - r.col } .forEach { e -> params.cb(e, false) } + + kotlinPsiFileFactory.releasePsiFileFactory() } @OptIn(FeatureInAlphaState::class) @@ -400,7 +400,6 @@ public object KtLint { } } } catch (e: Exception) { - kotlinPsiFileFactory.releasePsiFileFactory() // line/col cannot be reliably mapped as exception might originate from a node not present // in the original AST throw RuleExecutionException(0, 0, fqRuleId, e) @@ -429,7 +428,6 @@ public object KtLint { } } catch (e: Exception) { val (line, col) = preparedCode.positionInTextLocator(node.startOffset) - kotlinPsiFileFactory.releasePsiFileFactory() throw RuleExecutionException(line, col, fqRuleId, e) } } @@ -441,7 +439,6 @@ public object KtLint { } if (!mutated) { - kotlinPsiFileFactory.releasePsiFileFactory() return params.text } diff --git a/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/internal/KotlinPsiFileFactory.kt b/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/internal/KotlinPsiFileFactory.kt index e4d5edf477..5fc64da889 100644 --- a/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/internal/KotlinPsiFileFactory.kt +++ b/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/internal/KotlinPsiFileFactory.kt @@ -34,16 +34,18 @@ internal class KotlinPsiFileFactory { private val acquireCounter = AtomicInteger(0) private val initializerLock = Any() - internal fun acquirePsiFileFactory(isFromCli: Boolean): PsiFileFactory = synchronized(initializerLock) { + internal fun acquirePsiFileFactory(isFromCli: Boolean): PsiFileFactory { acquireCounter.incrementAndGet() - return psiFileFactory ?: initializePsiFileFactory(isFromCli).also { - psiFileFactory = it + return psiFileFactory ?: synchronized(initializerLock) { + initializePsiFileFactory(isFromCli).also { + psiFileFactory = it + } } } - internal fun releasePsiFileFactory() = synchronized(initializerLock) { + internal fun releasePsiFileFactory() { val acquiredCount = acquireCounter.decrementAndGet() - if (acquiredCount == 0) { + if (acquiredCount == 0) synchronized(initializerLock) { tempFiles?.toFile()?.deleteRecursively() tempFiles = null psiFileFactory = null