Skip to content

Commit 9113925

Browse files
committed
Update formatting to use ktfmt and spotless
Signed-off-by: Ryan Nett <[email protected]>
1 parent 9089d9d commit 9113925

File tree

13 files changed

+991
-1017
lines changed

13 files changed

+991
-1017
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ If you add operators or re-generate them from the native library, be sure to re-
143143

144144
#### Formatting
145145

146-
The Kotlin API is formatted with ktlint, which is ran on build.
147-
The build will not auto-format non-generated files.
148-
You can format them by installing ktlint as the IDE format and using its formatter, or by running `mvn antrun:run@ktlint-format`.
146+
[ktfmt](https://github.com/facebookincubator/ktfmt) is used to format the Kotlin files. This is
147+
checked and done via maven in the same way as Java formatting. To do the formatting via IntelliJ see
148+
ktfmt's repo.
149149

150150
## Adding Gradients
151151

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,93 @@
1-
root = true
1+
# This .editorconfig section approximates ktfmt's formatting rules. You can include it in an
2+
# existing .editorconfig file or use it standalone by copying it to <project root>/.editorconfig
3+
# and making sure your editor is set to read settings from .editorconfig files.
4+
#
5+
# It includes editor-specific config options for IntelliJ IDEA.
6+
#
7+
# If any option is wrong, PR are welcome
28

3-
[*.{kt, kts}]
4-
indent_size = 4
9+
[{*.kt,*.kts}]
10+
indent_style = space
511
insert_final_newline = true
6-
max_line_length = 120
12+
max_line_length = 100
13+
indent_size = 2
14+
ij_continuation_indent_size = 4
15+
ij_java_names_count_to_use_import_on_demand = 9999
16+
ij_kotlin_align_in_columns_case_branch = false
17+
ij_kotlin_align_multiline_binary_operation = false
18+
ij_kotlin_align_multiline_extends_list = false
19+
ij_kotlin_align_multiline_method_parentheses = false
20+
ij_kotlin_align_multiline_parameters = true
21+
ij_kotlin_align_multiline_parameters_in_calls = false
22+
ij_kotlin_allow_trailing_comma = true
23+
ij_kotlin_allow_trailing_comma_on_call_site = true
24+
ij_kotlin_assignment_wrap = normal
25+
ij_kotlin_blank_lines_after_class_header = 0
26+
ij_kotlin_blank_lines_around_block_when_branches = 0
27+
ij_kotlin_blank_lines_before_declaration_with_comment_or_annotation_on_separate_line = 1
28+
ij_kotlin_block_comment_at_first_column = true
29+
ij_kotlin_call_parameters_new_line_after_left_paren = true
30+
ij_kotlin_call_parameters_right_paren_on_new_line = false
31+
ij_kotlin_call_parameters_wrap = on_every_item
32+
ij_kotlin_catch_on_new_line = false
33+
ij_kotlin_class_annotation_wrap = split_into_lines
34+
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL
35+
ij_kotlin_continuation_indent_for_chained_calls = true
36+
ij_kotlin_continuation_indent_for_expression_bodies = true
37+
ij_kotlin_continuation_indent_in_argument_lists = true
38+
ij_kotlin_continuation_indent_in_elvis = false
39+
ij_kotlin_continuation_indent_in_if_conditions = false
40+
ij_kotlin_continuation_indent_in_parameter_lists = false
41+
ij_kotlin_continuation_indent_in_supertype_lists = false
42+
ij_kotlin_else_on_new_line = false
43+
ij_kotlin_enum_constants_wrap = off
44+
ij_kotlin_extends_list_wrap = normal
45+
ij_kotlin_field_annotation_wrap = split_into_lines
46+
ij_kotlin_finally_on_new_line = false
47+
ij_kotlin_if_rparen_on_new_line = false
48+
ij_kotlin_import_nested_classes = false
49+
ij_kotlin_insert_whitespaces_in_simple_one_line_method = true
50+
ij_kotlin_keep_blank_lines_before_right_brace = 2
51+
ij_kotlin_keep_blank_lines_in_code = 2
52+
ij_kotlin_keep_blank_lines_in_declarations = 2
53+
ij_kotlin_keep_first_column_comment = true
54+
ij_kotlin_keep_indents_on_empty_lines = false
55+
ij_kotlin_keep_line_breaks = true
56+
ij_kotlin_lbrace_on_next_line = false
57+
ij_kotlin_line_comment_add_space = false
58+
ij_kotlin_line_comment_at_first_column = true
59+
ij_kotlin_method_annotation_wrap = split_into_lines
60+
ij_kotlin_method_call_chain_wrap = normal
61+
ij_kotlin_method_parameters_new_line_after_left_paren = true
62+
ij_kotlin_method_parameters_right_paren_on_new_line = true
63+
ij_kotlin_method_parameters_wrap = on_every_item
64+
ij_kotlin_name_count_to_use_star_import = 9999
65+
ij_kotlin_name_count_to_use_star_import_for_members = 9999
66+
ij_kotlin_parameter_annotation_wrap = off
67+
ij_kotlin_space_after_comma = true
68+
ij_kotlin_space_after_extend_colon = true
69+
ij_kotlin_space_after_type_colon = true
70+
ij_kotlin_space_before_catch_parentheses = true
71+
ij_kotlin_space_before_comma = false
72+
ij_kotlin_space_before_extend_colon = true
73+
ij_kotlin_space_before_for_parentheses = true
74+
ij_kotlin_space_before_if_parentheses = true
75+
ij_kotlin_space_before_lambda_arrow = true
76+
ij_kotlin_space_before_type_colon = false
77+
ij_kotlin_space_before_when_parentheses = true
78+
ij_kotlin_space_before_while_parentheses = true
79+
ij_kotlin_spaces_around_additive_operators = true
80+
ij_kotlin_spaces_around_assignment_operators = true
81+
ij_kotlin_spaces_around_equality_operators = true
82+
ij_kotlin_spaces_around_function_type_arrow = true
83+
ij_kotlin_spaces_around_logical_operators = true
84+
ij_kotlin_spaces_around_multiplicative_operators = true
85+
ij_kotlin_spaces_around_range = false
86+
ij_kotlin_spaces_around_relational_operators = true
87+
ij_kotlin_spaces_around_unary_operator = false
88+
ij_kotlin_spaces_around_when_arrow = true
89+
ij_kotlin_variable_annotation_wrap = off
90+
ij_kotlin_while_on_new_line = false
91+
ij_kotlin_wrap_elvis_expressions = 1
92+
ij_kotlin_wrap_expression_body_functions = 1
93+
ij_kotlin_wrap_first_method_in_call_chain = false

tensorflow-kotlin-parent/pom.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
</dependencies>
4747

4848
<properties>
49-
<kotlin.version>1.5.0</kotlin.version>
49+
<kotlin.version>1.5.10</kotlin.version>
5050
<kotlin.jvmTarget>1.8</kotlin.jvmTarget>
5151
</properties>
5252

@@ -85,6 +85,16 @@
8585
</execution>
8686
</executions>
8787
</plugin>
88+
<plugin>
89+
<groupId>com.diffplug.spotless</groupId>
90+
<artifactId>spotless-maven-plugin</artifactId>
91+
<version>${spotless.version}</version>
92+
<configuration>
93+
<kotlin>
94+
<ktfmt/>
95+
</kotlin>
96+
</configuration>
97+
</plugin>
8898
</plugins>
8999
</build>
90100
</project>

tensorflow-kotlin-parent/tensorflow-core-kotlin/pom.xml

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -139,78 +139,6 @@
139139
</execution>
140140
</executions>
141141
</plugin>
142-
<plugin>
143-
<groupId>org.apache.maven.plugins</groupId>
144-
<artifactId>maven-antrun-plugin</artifactId>
145-
<version>1.8</version>
146-
<executions>
147-
<execution>
148-
<id>ktlint-format-generated</id>
149-
<phase>process-sources</phase>
150-
<configuration>
151-
<target name="ktlint">
152-
<java taskname="ktlint" dir="${basedir}" fork="true" failonerror="false"
153-
classpathref="maven.plugin.classpath" classname="com.pinterest.ktlint.Main">
154-
<arg value="-F"/>
155-
<arg value="--verbose"/>
156-
<arg value="src/gen/**/*.kt"/>
157-
</java>
158-
<!--Run twice because of https://github.com/pinterest/ktlint/issues/932-->
159-
<java taskname="ktlint2" dir="${basedir}" fork="true" failonerror="true"
160-
classpathref="maven.plugin.classpath" classname="com.pinterest.ktlint.Main">
161-
<arg value="-F"/>
162-
<arg value="--verbose"/>
163-
<arg value="src/gen/**/*.kt"/>
164-
</java>
165-
</target>
166-
</configuration>
167-
<goals>
168-
<goal>run</goal>
169-
</goals>
170-
</execution>
171-
<execution>
172-
<id>ktlint-format</id>
173-
<configuration>
174-
<target name="ktlint">
175-
<java taskname="ktlint" dir="${basedir}" fork="true" failonerror="true"
176-
classpathref="maven.plugin.classpath" classname="com.pinterest.ktlint.Main">
177-
<arg value="-F"/>
178-
<arg value="src/**/*.kt"/>
179-
<arg value="!src/gen/**/*.kt"/>
180-
<arg value="--reporter=plain?group_by_file"/>
181-
</java>
182-
</target>
183-
</configuration>
184-
<goals>
185-
<goal>run</goal>
186-
</goals>
187-
</execution>
188-
<execution>
189-
<id>ktlint</id>
190-
<phase>process-sources</phase>
191-
<configuration>
192-
<target name="ktlint">
193-
<java taskname="ktlint" dir="${basedir}" fork="true" failonerror="true"
194-
classpathref="maven.plugin.classpath" classname="com.pinterest.ktlint.Main">
195-
<arg value="src/**/*.kt"/>
196-
<arg value="!src/gen/**/*.kt"/>
197-
<arg value="--reporter=plain?group_by_file"/>
198-
</java>
199-
</target>
200-
</configuration>
201-
<goals>
202-
<goal>run</goal>
203-
</goals>
204-
</execution>
205-
</executions>
206-
<dependencies>
207-
<dependency>
208-
<groupId>com.pinterest</groupId>
209-
<artifactId>ktlint</artifactId>
210-
<version>0.41.0</version>
211-
</dependency>
212-
</dependencies>
213-
</plugin>
214142
<plugin>
215143
<groupId>org.apache.maven.plugins</groupId>
216144
<artifactId>maven-surefire-plugin</artifactId>
Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,60 @@
11
/*
2-
Copyright 2021 The TensorFlow Authors. All Rights Reserved.
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
==============================================================================
16-
*/
2+
Copyright 2021 The TensorFlow Authors. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
==============================================================================
16+
*/
1717
package org.tensorflow
1818

19-
import org.tensorflow.op.kotlin.KotlinOps
20-
import org.tensorflow.op.kotlin.kotlin
2119
import kotlin.contracts.InvocationKind
2220
import kotlin.contracts.contract
21+
import org.tensorflow.op.kotlin.KotlinOps
22+
import org.tensorflow.op.kotlin.kotlin
2323

2424
/**
2525
* Create a [ConcreteFunction] by building a new graph.
2626
* @see ConcreteFunction.create
2727
*/
28-
public inline fun ConcreteFunction(crossinline function: KotlinOps.() -> Signature): ConcreteFunction {
29-
contract { callsInPlace(function, InvocationKind.EXACTLY_ONCE) }
30-
return ConcreteFunction.create { function(it.kotlin) }
28+
public inline fun ConcreteFunction(
29+
crossinline function: KotlinOps.() -> Signature
30+
): ConcreteFunction {
31+
contract { callsInPlace(function, InvocationKind.EXACTLY_ONCE) }
32+
return ConcreteFunction.create { function(it.kotlin) }
3133
}
3234

3335
/**
3436
* Call this function with the specified arguments.
3537
* @see ConcreteFunction.call
3638
*/
37-
public operator fun ConcreteFunction.invoke(arguments: Map<String, Tensor>): Map<String, Tensor> = this.call(arguments)
39+
public operator fun ConcreteFunction.invoke(arguments: Map<String, Tensor>): Map<String, Tensor> =
40+
this.call(arguments)
3841

3942
/**
4043
* Call this function with the specified arguments.
4144
* @see ConcreteFunction.call
4245
*/
43-
public operator fun ConcreteFunction.invoke(vararg arguments: Pair<String, Tensor>): Map<String, Tensor> =
44-
this.invoke(arguments.toMap())
46+
public operator fun ConcreteFunction.invoke(
47+
vararg arguments: Pair<String, Tensor>
48+
): Map<String, Tensor> = this.invoke(arguments.toMap())
4549

4650
/**
47-
* Call this function with a single argument. Requires this function to be a single argument function.
51+
* Call this function with a single argument. Requires this function to be a single argument
52+
* function.
4853
* @see ConcreteFunction.call
4954
*/
5055
public operator fun ConcreteFunction.invoke(argument: Tensor): Tensor = this.call(argument)
5156

52-
/**
53-
* Create a [Signature] for a [ConcreteFunction].
54-
*/
57+
/** Create a [Signature] for a [ConcreteFunction]. */
5558
public fun Signature(
5659
methodName: String,
5760
inputs: Map<String, Operand<*>>,
@@ -60,64 +63,62 @@ public fun Signature(
6063
): Signature =
6164
Signature.builder().methodName(methodName).key(key).inputs(inputs).outputs(outputs).build()
6265

63-
/**
64-
* Create a [Signature] for a [ConcreteFunction].
65-
*/
66+
/** Create a [Signature] for a [ConcreteFunction]. */
6667
public fun Signature(
6768
methodName: String,
6869
inputs: Operand<*>,
6970
outputs: Map<String, Operand<*>>,
7071
key: String = Signature.DEFAULT_KEY,
7172
): Signature =
72-
Signature.builder().methodName(methodName).key(key).input("input", inputs).outputs(outputs).build()
73+
Signature.builder()
74+
.methodName(methodName)
75+
.key(key)
76+
.input("input", inputs)
77+
.outputs(outputs)
78+
.build()
7379

74-
/**
75-
* Create a [Signature] for a [ConcreteFunction].
76-
*/
80+
/** Create a [Signature] for a [ConcreteFunction]. */
7781
public fun Signature(
7882
methodName: String,
7983
inputs: Map<String, Operand<*>>,
8084
outputs: Operand<*>,
8185
key: String = Signature.DEFAULT_KEY,
8286
): Signature =
83-
Signature.builder().methodName(methodName).key(key).inputs(inputs).output("output", outputs).build()
87+
Signature.builder()
88+
.methodName(methodName)
89+
.key(key)
90+
.inputs(inputs)
91+
.output("output", outputs)
92+
.build()
8493

85-
/**
86-
* Create a [Signature] for a [ConcreteFunction].
87-
*/
94+
/** Create a [Signature] for a [ConcreteFunction]. */
8895
public fun Signature(
8996
methodName: String,
9097
inputs: Operand<*>,
9198
outputs: Operand<*>,
9299
key: String = Signature.DEFAULT_KEY,
93100
): Signature =
94-
Signature.builder().methodName(methodName).key(key).input("input", inputs).output("output", outputs).build()
101+
Signature.builder()
102+
.methodName(methodName)
103+
.key(key)
104+
.input("input", inputs)
105+
.output("output", outputs)
106+
.build()
95107

96-
/**
97-
* Add [inputs] to the signature.
98-
*/
108+
/** Add [inputs] to the signature. */
99109
public fun Signature.Builder.inputs(inputs: Map<String, Operand<*>>): Signature.Builder = apply {
100-
inputs.forEach {
101-
input(it.key, it.value)
102-
}
110+
inputs.forEach { input(it.key, it.value) }
103111
}
104112

105-
/**
106-
* Add [outputs] to the signature.
107-
*/
113+
/** Add [outputs] to the signature. */
108114
public fun Signature.Builder.outputs(outputs: Map<String, Operand<*>>): Signature.Builder = apply {
109-
outputs.forEach {
110-
output(it.key, it.value)
111-
}
115+
outputs.forEach { output(it.key, it.value) }
112116
}
113117

114-
/**
115-
* Add [inputs] to the signature.
116-
*/
117-
public fun Signature.Builder.inputs(vararg inputs: Pair<String, Operand<*>>): Signature.Builder = inputs(inputs.toMap())
118+
/** Add [inputs] to the signature. */
119+
public fun Signature.Builder.inputs(vararg inputs: Pair<String, Operand<*>>): Signature.Builder =
120+
inputs(inputs.toMap())
118121

119-
/**
120-
* Add [outputs] to the signature.
121-
*/
122+
/** Add [outputs] to the signature. */
122123
public fun Signature.Builder.outputs(vararg outputs: Pair<String, Operand<*>>): Signature.Builder =
123124
outputs(outputs.toMap())

0 commit comments

Comments
 (0)