-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c3416f
commit b194c68
Showing
3 changed files
with
65 additions
and
36 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
12 changes: 12 additions & 0 deletions
12
ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/formatter/CodeFormatter.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,12 @@ | ||
package org.ktorm.ksp.compiler.formatter | ||
|
||
/** | ||
* Code formatter interface. | ||
*/ | ||
internal fun interface CodeFormatter { | ||
|
||
/** | ||
* Format the generated code to the community recommended coding style. | ||
*/ | ||
fun format(code: String): String | ||
} |
43 changes: 43 additions & 0 deletions
43
ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/formatter/KtLintCodeFormatter.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,43 @@ | ||
package org.ktorm.ksp.compiler.formatter | ||
|
||
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment | ||
import com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3 | ||
import com.pinterest.ktlint.rule.engine.api.Code | ||
import com.pinterest.ktlint.rule.engine.api.EditorConfigDefaults | ||
import com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine | ||
import org.ec4j.core.EditorConfigLoader | ||
import org.ec4j.core.Resource.Resources | ||
import java.util.* | ||
|
||
internal class KtLintCodeFormatter(val environment: SymbolProcessorEnvironment) : CodeFormatter { | ||
private val ktLintRuleEngine = KtLintRuleEngine( | ||
ruleProviders = ServiceLoader | ||
.load(RuleSetProviderV3::class.java, javaClass.classLoader) | ||
.flatMap { it.getRuleProviders() } | ||
.toSet(), | ||
editorConfigDefaults = EditorConfigDefaults( | ||
EditorConfigLoader.default_().load( | ||
Resources.ofClassPath(javaClass.classLoader, "/ktorm-ksp-compiler/.editorconfig", Charsets.UTF_8) | ||
) | ||
) | ||
) | ||
|
||
override fun format(code: String): String { | ||
try { | ||
// Manually fix some code styles before formatting. | ||
val snippet = code | ||
.replace(Regex("""\(\s*"""), "(") | ||
.replace(Regex("""\s*\)"""), ")") | ||
.replace(Regex(""",\s*"""), ", ") | ||
.replace(Regex(""",\s*\)"""), ")") | ||
.replace(Regex("""\s+get\(\)\s="""), " get() =") | ||
.replace(Regex("""\s+=\s+"""), " = ") | ||
.replace("import org.ktorm.ksp.`annotation`", "import org.ktorm.ksp.annotation") | ||
|
||
return ktLintRuleEngine.format(Code.fromSnippet(snippet)) | ||
} catch (e: Throwable) { | ||
environment.logger.exception(e) | ||
return code | ||
} | ||
} | ||
} |