@@ -4,7 +4,9 @@ package com.autonomousapps.internal.parse
44
55import com.autonomousapps.exception.BuildScriptParseException
66import com.autonomousapps.internal.advice.AdvicePrinter
7- import com.autonomousapps.internal.antlr.v4.runtime.*
7+ import com.autonomousapps.internal.antlr.v4.runtime.CharStream
8+ import com.autonomousapps.internal.antlr.v4.runtime.CommonTokenStream
9+ import com.autonomousapps.internal.antlr.v4.runtime.Token
810import com.autonomousapps.internal.cash.grammar.kotlindsl.parse.Parser
911import com.autonomousapps.internal.cash.grammar.kotlindsl.parse.Rewriter
1012import com.autonomousapps.internal.cash.grammar.kotlindsl.utils.Blocks.isBuildscript
@@ -82,7 +84,8 @@ internal class KotlinBuildScriptDependenciesRewriter(
8284 override fun exitNamedBlock (ctx : NamedBlockContext ) {
8385 if (ctx.isDependencies && ! inBuildscriptBlock) {
8486 hasDependenciesBlock = true
85- insertAdvice(advice, ctx.stop, withDependenciesBlock = false )
87+ val ktfmt = ctx.start.line == ctx.stop.line
88+ insertAdvice(advice, ctx.stop, withDependenciesBlock = false , ktfmt = ktfmt)
8689 }
8790
8891 // Must be last
@@ -97,21 +100,36 @@ internal class KotlinBuildScriptDependenciesRewriter(
97100 // Exit early if this build script has a dependencies block. If it doesn't, we may need to add missing dependencies.
98101 if (hasDependenciesBlock) return
99102
100- insertAdvice(advice, ctx.stop, withDependenciesBlock = true )
103+ insertAdvice(advice, ctx.stop, withDependenciesBlock = true , ktfmt = false )
101104 }
102105
103- private fun insertAdvice (advice : Set <Advice >, beforeToken : Token , withDependenciesBlock : Boolean ) {
104- val prefix = if (withDependenciesBlock) " \n dependencies {\n " else " "
106+ /* *
107+ * ktfmt has this extremely dumb behavior where it collapses blocks to be on one line whenever possible. It will do
108+ * this:
109+ * ```
110+ * dependencies { implementation(libs.foo.bar) }
111+ * ```
112+ * This probably happens with other tools but I'm feeling petty. If we detect that the start and stop of a
113+ * `dependencies` block is on the same line, we insert a single newline before adding new dependencies, else the build
114+ * script wouldn't compile because it was looking like this:
115+ * ```
116+ * dependencies { implementation(libs.foo.bar) implementation(libs.baz) }
117+ * ```
118+ */
119+ private fun insertAdvice (advice : Set <Advice >, beforeToken : Token , withDependenciesBlock : Boolean , ktfmt : Boolean ) {
120+ val prefix = if (withDependenciesBlock) " \n dependencies {\n " else if (ktfmt) " \n " else " "
105121 val postfix = if (withDependenciesBlock) " \n }\n " else " \n "
106122
107- advice.filterToOrderedSet { it.isAnyAdd() }.ifNotEmpty { addAdvice ->
108- rewriter.insertBefore(
109- beforeToken,
110- addAdvice.joinToString(prefix = prefix, postfix = postfix, separator = " \n " ) { a ->
111- printer.toDeclaration(a)
112- }
113- )
114- }
123+ advice
124+ .filterToOrderedSet { it.isAnyAdd() }
125+ .ifNotEmpty { addAdvice ->
126+ rewriter.insertBefore(
127+ beforeToken,
128+ addAdvice.joinToString(prefix = prefix, postfix = postfix, separator = " \n " ) { a ->
129+ printer.toDeclaration(a)
130+ }
131+ )
132+ }
115133 }
116134
117135 private fun handleDependencies (ctx : NamedBlockContext ) {
0 commit comments