|
| 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