-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### What's done: - added a builder to run Diktat - reused it in maven plugin It's part of #1561
- Loading branch information
Showing
7 changed files
with
188 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
diktat-ruleset/src/main/kotlin/org/cqfn/diktat/DiktatProcessCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.cqfn.diktat | ||
|
||
import org.cqfn.diktat.api.DiktatCallback | ||
import org.cqfn.diktat.ktlint.wrap | ||
import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider | ||
import com.pinterest.ktlint.core.KtLint | ||
import com.pinterest.ktlint.core.api.EditorConfigDefaults | ||
import com.pinterest.ktlint.core.api.EditorConfigOverride | ||
import java.io.File | ||
|
||
/** | ||
* Command to run `diktat` | ||
*/ | ||
class DiktatProcessCommand private constructor( | ||
val file: File, | ||
val fileContent: String, | ||
private val callback: DiktatCallback, | ||
private val isScript: Boolean, | ||
) { | ||
/** | ||
* Run `diktat fix` using parameters from current command | ||
* | ||
* @return result of `diktat fix` | ||
*/ | ||
fun fix(): String { | ||
return KtLint.format(ktLintParams()) | ||
} | ||
|
||
/** | ||
* Run `diktat check` using parameters from current command | ||
*/ | ||
fun check() { | ||
KtLint.lint(ktLintParams()) | ||
} | ||
|
||
private fun ktLintParams(): KtLint.ExperimentalParams = KtLint.ExperimentalParams( | ||
fileName = file.absolutePath, | ||
text = fileContent, | ||
ruleSets = setOf(DiktatRuleSetProvider().get()), | ||
ruleProviders = emptySet(), | ||
userData = emptyMap(), | ||
cb = { e, corrected -> | ||
callback.accept(e.wrap(), corrected) | ||
}, | ||
script = isScript, | ||
editorConfigPath = null, | ||
debug = false, | ||
editorConfigDefaults = EditorConfigDefaults.emptyEditorConfigDefaults, | ||
editorConfigOverride = EditorConfigOverride.emptyEditorConfigOverride, | ||
isInvokedFromCli = false | ||
) | ||
|
||
/** | ||
* Builder for [DiktatProcessCommand] | ||
* | ||
* @property file | ||
* @property fileContent | ||
* @property callback | ||
* @property isScript | ||
*/ | ||
data class Builder( | ||
var file: File? = null, | ||
var fileContent: String? = null, | ||
var callback: DiktatCallback? = null, | ||
var isScript: Boolean? = null, | ||
) { | ||
fun file(file: File) = apply { this.file = file } | ||
fun fileContent(fileContent: String) = apply { this.fileContent = fileContent } | ||
fun callback(callback: DiktatCallback) = apply { this.callback = callback } | ||
fun isScript(isScript: Boolean) = apply { this.isScript = isScript } | ||
|
||
fun build() = DiktatProcessCommand( | ||
requireNotNull(file), | ||
requireNotNull(fileContent), | ||
requireNotNull(callback), | ||
requireNotNull(isScript), | ||
) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
diktat-ruleset/src/main/kotlin/org/cqfn/diktat/api/DiktatCallback.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.cqfn.diktat.api | ||
|
||
import java.util.function.BiConsumer | ||
|
||
/** | ||
* Callback for diktat process | ||
*/ | ||
@FunctionalInterface | ||
fun interface DiktatCallback : BiConsumer<DiktatError, Boolean> |
31 changes: 31 additions & 0 deletions
31
diktat-ruleset/src/main/kotlin/org/cqfn/diktat/api/DiktatError.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.cqfn.diktat.api | ||
|
||
/** | ||
* Error found by `diktat` | ||
*/ | ||
interface DiktatError { | ||
/** | ||
* @return line number (one-based) | ||
*/ | ||
fun getLine(): Int | ||
|
||
/** | ||
* @return column number (one-based) | ||
*/ | ||
fun getCol(): Int | ||
|
||
/** | ||
* @return rule id | ||
*/ | ||
fun getRuleId(): String | ||
|
||
/** | ||
* error message | ||
*/ | ||
fun getDetail(): String | ||
|
||
/** | ||
* @return true if the found error can be fixed | ||
*/ | ||
fun canBeAutoCorrected(): Boolean | ||
} |
33 changes: 33 additions & 0 deletions
33
diktat-ruleset/src/main/kotlin/org/cqfn/diktat/ktlint/LintErrorWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.cqfn.diktat.ktlint | ||
|
||
import org.cqfn.diktat.api.DiktatError | ||
import com.pinterest.ktlint.core.LintError | ||
|
||
/** | ||
* Wrapper for KtLint error | ||
* | ||
* @property lintError | ||
*/ | ||
data class LintErrorWrapper( | ||
val lintError: LintError | ||
) : DiktatError { | ||
override fun getLine(): Int = lintError.line | ||
|
||
override fun getCol(): Int = lintError.col | ||
|
||
override fun getRuleId(): String = lintError.ruleId | ||
|
||
override fun getDetail(): String = lintError.detail | ||
|
||
override fun canBeAutoCorrected(): Boolean = lintError.canBeAutoCorrected | ||
} | ||
|
||
/** | ||
* @return [DiktatError] from KtLint [LintError] | ||
*/ | ||
fun LintError.wrap(): DiktatError = LintErrorWrapper(this) | ||
|
||
/** | ||
* @return KtLint [LintError] from [DiktatError] or exception | ||
*/ | ||
fun DiktatError.unwrap(): LintError = (this as? LintErrorWrapper)?.lintError ?: error("Unsupported wrapper for ${DiktatError::class.java.simpleName}") |