Skip to content

Commit

Permalink
Reformat to standard style (ktlint) & enforce with kotlinter (#30)
Browse files Browse the repository at this point in the history
* Fix format - use common static imports

* Reformat to standard ktlint (2 space) format, enforced with kotlinter
  • Loading branch information
kdubb authored Aug 20, 2020
1 parent cede9e9 commit 4c59a1d
Show file tree
Hide file tree
Showing 34 changed files with 866 additions and 850 deletions.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ plugins {
id 'com.palantir.git-version' version '0.10.0'
id 'net.minecrell.licenser' version '0.4.1'
id 'org.ajoberstar.git-publish' version '0.3.2'
id 'org.jmailen.kotlinter' version '2.4.1'
}


Expand Down Expand Up @@ -89,6 +90,12 @@ dependencies {
}


kotlinter {
ignoreFailures = false
indentSize = 2
}


apply from: "$gradleFilesDir/compile-java.gradle"
apply from: "$gradleFilesDir/compile-kotlin.gradle"
apply from: "$gradleFilesDir/code-coverage.gradle"
Expand Down
1 change: 0 additions & 1 deletion src/main/java/io/outfoxx/swiftpoet/AnyTypeSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ abstract class AnyTypeSpec(
internal open val typeSpecs: List<AnyTypeSpec> = listOf()

internal abstract fun emit(codeWriter: CodeWriter)

}
4 changes: 1 addition & 3 deletions src/main/java/io/outfoxx/swiftpoet/AttributeSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ class AttributeSpec internal constructor(builder: Builder) {
}

fun build(): AttributeSpec =
AttributeSpec(this)

AttributeSpec(this)
}

companion object {
Expand All @@ -66,5 +65,4 @@ class AttributeSpec internal constructor(builder: Builder) {

val DISCARDABLE_RESULT = builder("discardableResult").build()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
package io.outfoxx.swiftpoet

open class AttributedSpec(
val attributes: List<AttributeSpec> = listOf()
val attributes: List<AttributeSpec> = listOf()
)
11 changes: 7 additions & 4 deletions src/main/java/io/outfoxx/swiftpoet/CodeBlock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class CodeBlock private constructor(
* placeholders don't match their arguments.
*/
internal fun replaceAll(oldValue: String, newValue: String) =
CodeBlock(formatParts.map { it.replace(oldValue, newValue) }, args)
CodeBlock(formatParts.map { it.replace(oldValue, newValue) }, args)

override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand Down Expand Up @@ -282,8 +282,10 @@ class CodeBlock private constructor(
}

require(index >= 0 && index < args.size) {
"index ${index + 1} for '${format.substring(indexStart - 1,
indexEnd + 1)}' not in range (received ${args.size} arguments)"
"index ${index + 1} for '${format.substring(
indexStart - 1,
indexEnd + 1
)}' not in range (received ${args.size} arguments)"
}
require(!hasIndexed || !hasRelative) { "cannot mix indexed and positional parameters" }

Expand Down Expand Up @@ -316,7 +318,8 @@ class CodeBlock private constructor(
'S' -> this.args += argToString(arg)
'T' -> this.args += argToType(arg)
else -> throw IllegalArgumentException(
String.format("invalid format string: '%s'", format))
String.format("invalid format string: '%s'", format)
)
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/main/java/io/outfoxx/swiftpoet/CodeWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ internal class CodeWriter constructor(
* Emits `attributes` in declaration order.
*/
fun emitAttributes(
attributes: List<AttributeSpec>,
separator: String = "\n",
suffix: String = "\n"
attributes: List<AttributeSpec>,
separator: String = "\n",
suffix: String = "\n"
) {
if (attributes.isNotEmpty()) {
var firstAttribute = true
Expand Down Expand Up @@ -187,9 +187,11 @@ internal class CodeWriter constructor(
"%S" -> {
val string = codeBlock.args[a++] as String?
// Emit null as a literal null: no quotes.
emit(if (string != null)
stringLiteralWithQuotes(string) else
"null")
emit(
if (string != null)
stringLiteralWithQuotes(string) else
"null"
)
}

"%T" -> {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/io/outfoxx/swiftpoet/ComposedTypeName.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,5 @@ class ComposedTypeName private constructor(
internal fun composed(vararg types: String): ComposedTypeName {
return ComposedTypeName(types.map { DeclaredTypeName.typeName(it) })
}

}
}
4 changes: 2 additions & 2 deletions src/main/java/io/outfoxx/swiftpoet/DeclaredTypeName.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class DeclaredTypeName internal constructor(
* Returns a type name created from the given parts. For example, calling this with module name
* `"Swift"` and simple names `"Array"`, `"Iterator"` yields `Swift.Array.Iterator`.
*/
constructor(moduleName: String, simpleName: String, vararg simpleNames: String)
: this(listOf(moduleName, simpleName, *simpleNames))
constructor(moduleName: String, simpleName: String, vararg simpleNames: String) :
this(listOf(moduleName, simpleName, *simpleNames))

/** From top to bottom. This will be `["Swift", "Array", "Iterator"]` for `Swift.Array.Iterator`. */
private val names = names.toImmutableList()
Expand Down
1 change: 0 additions & 1 deletion src/main/java/io/outfoxx/swiftpoet/EnumerationCaseSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,4 @@ class EnumerationCaseSpec private constructor(

@JvmStatic fun builder(name: String, constant: Int) = Builder(name, CodeBlock.of("%L", constant.toString()))
}

}
5 changes: 2 additions & 3 deletions src/main/java/io/outfoxx/swiftpoet/ExtensionSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ class ExtensionSpec private constructor(builder: ExtensionSpec.Builder) {
codeWriter.popType()

codeWriter.emit("}\n")

} finally {
codeWriter.statementLine = previousStatementLine
}
Expand Down Expand Up @@ -157,8 +156,8 @@ class ExtensionSpec private constructor(builder: ExtensionSpec.Builder) {
propertySpecs += propertySpec
}

fun addProperty(name: String, type: TypeName, vararg modifiers: Modifier)
= addProperty(PropertySpec.builder(name, type, *modifiers).build())
fun addProperty(name: String, type: TypeName, vararg modifiers: Modifier) =
addProperty(PropertySpec.builder(name, type, *modifiers).build())

fun addFunctions(functionSpecs: Iterable<FunctionSpec>) = apply {
functionSpecs.forEach { addFunction(it) }
Expand Down
1 change: 0 additions & 1 deletion src/main/java/io/outfoxx/swiftpoet/FileMemberSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,4 @@ class FileMemberSpec internal constructor(builder: Builder) {

@JvmStatic fun builder(member: ExtensionSpec) = Builder(member)
}

}
6 changes: 3 additions & 3 deletions src/main/java/io/outfoxx/swiftpoet/FileSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ class FileSpec private constructor(builder: FileSpec.Builder) {

fun addImport(moduleName: String, vararg attributes: AttributeSpec) = apply {
addImport(
ImportSpec.builder(moduleName)
.apply { attributes.forEach { addAttribute(it) } }
.build()
ImportSpec.builder(moduleName)
.apply { attributes.forEach { addAttribute(it) } }
.build()
)
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/io/outfoxx/swiftpoet/FunctionSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package io.outfoxx.swiftpoet

/** A generated function declaration. */
class FunctionSpec private constructor(
builder: Builder
builder: Builder
) : AttributedSpec(builder.attributes) {
val name = builder.name
val doc = builder.doc.build()
Expand Down Expand Up @@ -213,11 +213,11 @@ class FunctionSpec private constructor(
parameters += parameterSpec
}

fun addParameter(name: String, type: TypeName, vararg modifiers: Modifier)
= addParameter(ParameterSpec.builder(name, type, *modifiers).build())
fun addParameter(name: String, type: TypeName, vararg modifiers: Modifier) =
addParameter(ParameterSpec.builder(name, type, *modifiers).build())

fun addParameter(label: String, name: String, type: TypeName, vararg modifiers: Modifier)
= addParameter(ParameterSpec.builder(label, name, type, *modifiers).build())
fun addParameter(label: String, name: String, type: TypeName, vararg modifiers: Modifier) =
addParameter(ParameterSpec.builder(label, name, type, *modifiers).build())

fun addCode(format: String, vararg args: Any) = apply {
body.add(format, *args)
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/io/outfoxx/swiftpoet/FunctionTypeName.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ class FunctionTypeName internal constructor(
returnType: TypeName
): FunctionTypeName {
return FunctionTypeName(
parameters.toList().map { ParameterSpec.unnamed(it) },
returnType)
parameters.toList().map { ParameterSpec.unnamed(it) },
returnType
)
}

/** Returns a lambda type with `returnType` and parameters listed in `parameters`. */
Expand Down
1 change: 0 additions & 1 deletion src/main/java/io/outfoxx/swiftpoet/ImportSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,4 @@ class ImportSpec internal constructor(
companion object {
@JvmStatic fun builder(name: String) = Builder(name)
}

}
3 changes: 1 addition & 2 deletions src/main/java/io/outfoxx/swiftpoet/ParameterSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package io.outfoxx.swiftpoet

/** A generated parameter declaration. */
class ParameterSpec private constructor(
builder: ParameterSpec.Builder
builder: ParameterSpec.Builder
) : AttributedSpec(builder.attributes) {
val argumentLabel = builder.argumentLabel
val parameterName = builder.parameterName
Expand Down Expand Up @@ -120,7 +120,6 @@ class ParameterSpec private constructor(
@JvmStatic fun unnamed(typeName: TypeName): ParameterSpec {
return Builder("", "", typeName).build()
}

}
}

Expand Down
35 changes: 17 additions & 18 deletions src/main/java/io/outfoxx/swiftpoet/ParameterizedTypeName.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ class ParameterizedTypeName internal constructor(
override val optional get() = rawType == OPTIONAL

override fun makeOptional() =
if (optional)
this
else
wrapOptional()
if (optional)
this
else
wrapOptional()

override fun makeNonOptional() =
if (optional)
typeArguments[0].makeNonOptional()
else
this
if (optional)
typeArguments[0].makeNonOptional()
else
this

override fun unwrapOptional() =
if (optional)
typeArguments[0]
else
this
if (optional)
typeArguments[0]
else
this

override fun emit(out: CodeWriter): CodeWriter {
when (rawType) {
Expand All @@ -58,8 +58,7 @@ class ParameterizedTypeName internal constructor(
if (enclosingType != null) {
enclosingType.emit(out)
out.emit("." + rawType.simpleName)
}
else {
} else {
rawType.emit(out)
}
if (typeArguments.isNotEmpty()) {
Expand All @@ -79,13 +78,13 @@ class ParameterizedTypeName internal constructor(
* Returns a new [ParameterizedTypeName] instance for the specified `name` as nested inside this
* type, with the specified `typeArguments`.
*/
fun nestedType(name: String, typeArguments: List<TypeName>)
= ParameterizedTypeName(this, rawType.nestedType(name), typeArguments)
fun nestedType(name: String, typeArguments: List<TypeName>) =
ParameterizedTypeName(this, rawType.nestedType(name), typeArguments)

companion object {
/** Returns a parameterized type, applying `typeArguments` to `rawType`. */
}
}

fun DeclaredTypeName.parameterizedBy(vararg typeArguments: TypeName)
= ParameterizedTypeName(null, this, typeArguments.toList())
fun DeclaredTypeName.parameterizedBy(vararg typeArguments: TypeName) =
ParameterizedTypeName(null, this, typeArguments.toList())
16 changes: 8 additions & 8 deletions src/main/java/io/outfoxx/swiftpoet/PropertySpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import io.outfoxx.swiftpoet.FunctionSpec.Companion.SETTER

/** A generated property declaration. */
class PropertySpec private constructor(
builder: Builder
builder: Builder
) : AttributedSpec(builder.attributes) {
val mutable = builder.mutable
val name = builder.name
Expand Down Expand Up @@ -51,7 +51,8 @@ class PropertySpec private constructor(

// Support concise syntax (e.g. "{ get set }") for protocol property declarations
if ((getter == null || getter.body == ABSTRACT) &&
(setter == null || setter.body == ABSTRACT)) {
(setter == null || setter.body == ABSTRACT)
) {
codeWriter.emit(" { ")
if (getter != null) codeWriter.emit("${getter.name} ")
if (setter != null) codeWriter.emit("${setter.name} ")
Expand Down Expand Up @@ -118,7 +119,6 @@ class PropertySpec private constructor(
doc.add(block)
}


fun addAttribute(attribute: AttributeSpec) = apply {
this.attributes += attribute
}
Expand Down Expand Up @@ -165,19 +165,19 @@ class PropertySpec private constructor(
companion object {
@JvmStatic fun builder(name: String, type: TypeName, vararg modifiers: Modifier): Builder {
return Builder(name, type)
.addModifiers(*modifiers)
.addModifiers(*modifiers)
}

@JvmStatic fun varBuilder(name: String, type: TypeName, vararg modifiers: Modifier): Builder {
return Builder(name, type)
.mutable(true)
.addModifiers(*modifiers)
.mutable(true)
.addModifiers(*modifiers)
}

@JvmStatic fun abstractBuilder(name: String, type: TypeName, vararg modifiers: Modifier): Builder {
return Builder(name, type)
.mutable(true)
.addModifiers(*modifiers)
.mutable(true)
.addModifiers(*modifiers)
}
}
}
3 changes: 1 addition & 2 deletions src/main/java/io/outfoxx/swiftpoet/SelfTypeName.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

package io.outfoxx.swiftpoet

class SelfTypeName private constructor(
) : TypeName() {
class SelfTypeName private constructor() : TypeName() {

override fun emit(out: CodeWriter): CodeWriter {
out.emit("Self")
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/io/outfoxx/swiftpoet/TypeAliasSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package io.outfoxx.swiftpoet

import io.outfoxx.swiftpoet.Modifier.*
import io.outfoxx.swiftpoet.Modifier.INTERNAL
import io.outfoxx.swiftpoet.Modifier.PRIVATE
import io.outfoxx.swiftpoet.Modifier.PUBLIC

/** A generated typealias declaration */
class TypeAliasSpec private constructor(
Expand Down
1 change: 0 additions & 1 deletion src/main/java/io/outfoxx/swiftpoet/TypeName.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,4 @@ abstract class TypeName internal constructor() {
override fun toString() = cachedString

internal abstract fun emit(out: CodeWriter): CodeWriter

}
Loading

0 comments on commit 4c59a1d

Please sign in to comment.