@@ -10,6 +10,7 @@ import org.cqfn.diktat.common.config.rules.DIKTAT_CONF_PROPERTY
10
10
import org.cqfn.diktat.common.config.rules.DIKTAT_RULE_SET_ID
11
11
import org.cqfn.diktat.common.config.rules.RulesConfig
12
12
import org.cqfn.diktat.common.config.rules.RulesConfigReader
13
+ import org.cqfn.diktat.common.utils.LazyMessage
13
14
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
14
15
import org.cqfn.diktat.ruleset.constants.Warnings
15
16
import org.cqfn.diktat.ruleset.rules.OrderedRuleSet.Companion.ordered
@@ -90,10 +91,12 @@ import org.cqfn.diktat.ruleset.rules.chapter6.classes.StatelessClassesRule
90
91
91
92
import com.pinterest.ktlint.core.RuleSet
92
93
import com.pinterest.ktlint.core.RuleSetProvider
94
+ import mu.KLogger
93
95
import mu.KotlinLogging
94
96
import org.jetbrains.kotlin.org.jline.utils.Levenshtein
95
97
96
98
import java.io.File
99
+ import java.util.concurrent.atomic.AtomicBoolean
97
100
98
101
/* *
99
102
* [RuleSetProvider] that provides diKTat ruleset.
@@ -109,31 +112,32 @@ class DiktatRuleSetProvider(private var diktatConfigFile: String = DIKTAT_ANALYS
109
112
yield (resolveConfigFileFromSystemProperty())
110
113
}
111
114
112
- /* *
113
- * As of _KtLint_ **0.47**, each rule is expected to have a state and is executed
114
- * twice per file, and a new `Rule` instance is created per each file checked.
115
- *
116
- * Diktat rules have no mutable state yet and use the deprecated _KtLint_
117
- * API, so we initialize them only _once_ for performance reasons and also
118
- * to avoid redundant logging.
119
- */
120
- private val ruleSet: RuleSet by lazy {
121
- log.debug(" Will run $DIKTAT_RULE_SET_ID with $diktatConfigFile " +
122
- " (it can be placed to the run directory or the default file from resources will be used)" )
115
+ @Suppress(
116
+ " LongMethod" ,
117
+ " TOO_LONG_FUNCTION" ,
118
+ )
119
+ @Deprecated(
120
+ " Marked for removal in KtLint 0.48. See changelog or KDoc for more information." ,
121
+ )
122
+ override fun get (): RuleSet {
123
+ log.debugOnce {
124
+ " Will run $DIKTAT_RULE_SET_ID with $diktatConfigFile " +
125
+ " (it can be placed to the run directory or the default file from resources will be used)"
126
+ }
123
127
val configPath = possibleConfigs
124
128
.firstOrNull { it != null && File (it).exists() }
125
129
diktatConfigFile = configPath
126
130
? : run {
127
- val possibleConfigsList = possibleConfigs.toList()
128
- log.warn(
131
+ log.warnOnce {
132
+ val possibleConfigsList = possibleConfigs.toList()
129
133
" Configuration file not found in directory where diktat is run (${possibleConfigsList[0 ]} ) " +
130
134
" or in the directory where diktat.jar is stored (${possibleConfigsList[1 ]} ) " +
131
135
" or in system property <diktat.config.path> (${possibleConfigsList[2 ]} ), " +
132
136
" the default file included in jar will be used. " +
133
137
" Some configuration options will be disabled or substituted with defaults. " +
134
138
" Custom configuration file should be placed in diktat working directory if run from CLI " +
135
139
" or provided as configuration options in plugins."
136
- )
140
+ }
137
141
diktatConfigFile
138
142
}
139
143
@@ -229,18 +233,12 @@ class DiktatRuleSetProvider(private var diktatConfigFile: String = DIKTAT_ANALYS
229
233
.map {
230
234
it.invoke(configRules)
231
235
}
232
- RuleSet (
236
+ return RuleSet (
233
237
DIKTAT_RULE_SET_ID ,
234
238
rules = rules.toTypedArray()
235
239
).ordered()
236
240
}
237
241
238
- @Deprecated(
239
- " Marked for removal in KtLint 0.48. See changelog or KDoc for more information." ,
240
- )
241
- override fun get (): RuleSet =
242
- ruleSet
243
-
244
242
private fun validate (config : RulesConfig ) =
245
243
require(config.name == DIKTAT_COMMON || config.name in Warnings .names) {
246
244
val closestMatch = Warnings .names.minByOrNull { Levenshtein .distance(it, config.name) }
@@ -272,5 +270,37 @@ class DiktatRuleSetProvider(private var diktatConfigFile: String = DIKTAT_ANALYS
272
270
273
271
companion object {
274
272
private val log = KotlinLogging .loggerWithKtlintConfig(DiktatRuleSetProvider ::class )
273
+
274
+ /* *
275
+ * @see warnMessageLogged
276
+ */
277
+ private val debugMessageLogged = AtomicBoolean ()
278
+
279
+ /* *
280
+ * @see debugMessageLogged
281
+ */
282
+ private val warnMessageLogged = AtomicBoolean ()
283
+
284
+ /* *
285
+ * Don't use with more than a single [lazyMessage].
286
+ *
287
+ * @see KLogger.warnOnce
288
+ */
289
+ private fun KLogger.debugOnce (lazyMessage : LazyMessage ) {
290
+ if (debugMessageLogged.compareAndSet(false , true )) {
291
+ debug(lazyMessage)
292
+ }
293
+ }
294
+
295
+ /* *
296
+ * Don't use with more than a single [lazyMessage].
297
+ *
298
+ * @see KLogger.debugOnce
299
+ */
300
+ private fun KLogger.warnOnce (lazyMessage : LazyMessage ) {
301
+ if (warnMessageLogged.compareAndSet(false , true )) {
302
+ warn(lazyMessage)
303
+ }
304
+ }
275
305
}
276
306
}
0 commit comments