Skip to content

Commit bea0e7c

Browse files
committed
#8, eclipse code formatter
1 parent 4714079 commit bea0e7c

File tree

6 files changed

+463
-3
lines changed

6 files changed

+463
-3
lines changed

gradle/libs.versions.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ swagger = "io.swagger.parser.v3:swagger-parser:2.0.28"
3030

3131
antlr = "org.antlr:antlr4:4.11.1"
3232

33-
java-format = "com.google.googlejavaformat:google-java-format:1.15.0"
33+
format-java-eclipse = "org.eclipse.jdt:org.eclipse.jdt.core:3.32.0"
34+
format-java-google = "com.google.googlejavaformat:google-java-format:1.15.0"
3435
guava = "com.google.guava:guava:31.1-jre"
3536
commons-text = "org.apache.commons:commons-text:1.10.0"
3637
commonmark = "com.atlassian.commonmark:commonmark:0.17.0"

openapi-processor-core/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ dependencies {
7676
implementation (libs.jackson.kotlin)
7777
implementation (libs.commons.text)
7878
implementation (libs.commonmark)
79-
implementation (libs.java.format)
79+
implementation (libs.format.java.google)
80+
implementation (libs.format.java.eclipse)
8081
implementation (libs.json.schema.validator)
8182
implementation (libs.slf4j)
8283

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/writer/java/ApiWriter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ApiWriter(
2828
private val dataTypeWriter: DataTypeWriter,
2929
private val enumWriter: StringEnumWriter,
3030
private val interfaceDataTypeWriter: InterfaceDataTypeWriter,
31-
private val formatter: SourceFormatter = GoogleFormatter(),
31+
private val formatter: SourceFormatter = EclipseFormatter(), //GoogleFormatter(),
3232
private val writerFactory: WriterFactory = DefaultWriterFactory()
3333
) {
3434
init {
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package io.openapiprocessor.core.writer.java
2+
3+
import io.openapiprocessor.core.writer.SourceFormatter
4+
import org.eclipse.jdt.core.JavaCore
5+
import org.eclipse.jdt.core.ToolFactory
6+
import org.eclipse.jdt.core.formatter.CodeFormatter
7+
import org.eclipse.jface.text.Document
8+
import java.io.StringReader
9+
import java.util.*
10+
import java.util.stream.Collectors
11+
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants as Formatter
12+
13+
class EclipseFormatter: SourceFormatter {
14+
private lateinit var formatter: CodeFormatter
15+
16+
init {
17+
initFormatter()
18+
}
19+
20+
override fun format(raw: String): String {
21+
try {
22+
val textEdit = formatter.format(
23+
CodeFormatter.K_COMPILATION_UNIT + CodeFormatter.F_INCLUDE_COMMENTS,
24+
raw,
25+
0,
26+
raw.length,
27+
0,
28+
"\n"
29+
)
30+
31+
val document = Document(raw)
32+
textEdit.apply(document)
33+
34+
return correctEndOfFile(document.get())
35+
} catch (e: Exception) {
36+
throw FormattingException(raw, e)
37+
}
38+
}
39+
40+
private fun correctEndOfFile(formatted: String): String {
41+
val index = formatted.lastIndexOf("}")
42+
43+
return StringBuilder()
44+
.append(formatted.substring(0, index))
45+
.append("}\n")
46+
.toString()
47+
}
48+
49+
private fun initFormatter() {
50+
formatter = ToolFactory.createCodeFormatter(getOptions())
51+
}
52+
53+
private fun getOptions(): Map<String, String> {
54+
val options = mutableMapOf<String, String>()
55+
56+
// options.putAll(JavaCore.getDefaultOptions())
57+
options.putAll(getJavaOptions())
58+
59+
// options.putAll(convertOptions(loadStyleOptions()))
60+
// options[DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR] = JavaCore.SPACE
61+
// options[DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT] = "80"
62+
// options[DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION] = "4"
63+
// options[DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION] =
64+
// DefaultCodeFormatterConstants.createAlignmentValue(
65+
// false,
66+
// DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE,
67+
// DefaultCodeFormatterConstants.INDENT_BY_ONE)
68+
//
69+
// options[DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_METHOD_DECLARATION] =
70+
// DefaultCodeFormatterConstants.createAlignmentValue(
71+
// false,
72+
// DefaultCodeFormatterConstants.WRAP_COMPACT,
73+
// DefaultCodeFormatterConstants.INDENT_BY_ONE)
74+
//
75+
// FORMATTER_ALIGNMENT_FOR_METHOD_DECLARATION
76+
77+
return options
78+
}
79+
80+
@Suppress("UNCHECKED_CAST")
81+
private fun getJavaOptions(): Map<String, String> {
82+
return Formatter.getJavaConventionsSettings() as Map<String, String>
83+
}
84+
85+
private fun loadStyleOptions(): Properties {
86+
val content = this.javaClass.getResource("/google-style.properties")?.readText()
87+
val reader = StringReader(content)
88+
val properties = Properties()
89+
properties.load(reader)
90+
return properties
91+
}
92+
93+
private fun convertOptions(prop: Properties): HashMap<String, String> {
94+
return prop.entries
95+
.stream()
96+
.collect(
97+
Collectors.toMap(
98+
{ e -> e.toString() },
99+
{ v -> v.toString() },
100+
{ _, next -> next })
101+
{ HashMap() }
102+
)
103+
}
104+
}

0 commit comments

Comments
 (0)