Skip to content

Omit implicit modifiers on FileSpec.scriptBuilder #1813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,14 @@ internal class CodeWriter constructor(
codeBlock: CodeBlock,
isConstantContext: Boolean = false,
ensureTrailingNewline: Boolean = false,
omitImplicitModifiers: Boolean = false,
) = apply {
var a = 0
var deferredTypeName: ClassName? = null // used by "import static" logic
val partIterator = codeBlock.formatParts.listIterator()
while (partIterator.hasNext()) {
when (val part = partIterator.next()) {
"%L" -> emitLiteral(codeBlock.args[a++], isConstantContext)
"%L" -> emitLiteral(codeBlock.args[a++], isConstantContext, omitImplicitModifiers)

"%N" -> emit(codeBlock.args[a++] as String)

Expand Down Expand Up @@ -393,15 +394,15 @@ internal class CodeWriter constructor(
return false
}

private fun emitLiteral(o: Any?, isConstantContext: Boolean) {
private fun emitLiteral(o: Any?, isConstantContext: Boolean, omitImplicitModifiers: Boolean) {
when (o) {
is TypeSpec -> o.emit(this, null)
is AnnotationSpec -> o.emit(this, inline = true, asParameter = isConstantContext)
is PropertySpec -> o.emit(this, emptySet())
is FunSpec -> o.emit(
codeWriter = this,
enclosingName = null,
implicitModifiers = setOf(KModifier.PUBLIC),
implicitModifiers = if (omitImplicitModifiers) emptySet() else setOf(KModifier.PUBLIC),
includeKdocTags = true,
)
is TypeAliasSpec -> o.emit(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public class FileSpec private constructor(
}

if (isScript) {
codeWriter.emitCode(body)
codeWriter.emitCode(body, omitImplicitModifiers = true)
} else {
members.forEachIndexed { index, member ->
if (index > 0) codeWriter.emit("\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ class FileSpecTest {
|
|println("hello!")
|
|public fun localFun() {
|fun localFun() {
|}
|
|public class Yay
Expand Down