|
| 1 | +/* |
| 2 | + * Copyright 2026 DiffPlug |
| 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 | +package com.diffplug.spotless.java; |
| 17 | + |
| 18 | +import java.io.Serial; |
| 19 | +import java.io.Serializable; |
| 20 | +import java.lang.reflect.Constructor; |
| 21 | +import java.util.Objects; |
| 22 | + |
| 23 | +import javax.annotation.Nullable; |
| 24 | + |
| 25 | +import com.diffplug.spotless.FormatterFunc; |
| 26 | +import com.diffplug.spotless.FormatterStep; |
| 27 | +import com.diffplug.spotless.JarState; |
| 28 | +import com.diffplug.spotless.Jvm; |
| 29 | +import com.diffplug.spotless.Provisioner; |
| 30 | + |
| 31 | +/** Wraps up <a href="https://github.com/agustafson/prince-of-space">prince-of-space</a> as a FormatterStep. */ |
| 32 | +public final class PrinceOfSpaceStep implements Serializable { |
| 33 | + @Serial |
| 34 | + private static final long serialVersionUID = 1L; |
| 35 | + private static final String NAME = "prince-of-space"; |
| 36 | + public static final String MAVEN_COORDINATE = "io.github.agustafson.princeofspace:prince-of-space-core:"; |
| 37 | + public static final String DEFAULT_VERSION = "2.2.0"; |
| 38 | + |
| 39 | + /** The jar that contains the formatter. */ |
| 40 | + private final JarState.Promised jarState; |
| 41 | + /** Version of the formatter jar. */ |
| 42 | + private final String formatterVersion; |
| 43 | + @Nullable private final Options options; |
| 44 | + |
| 45 | + private PrinceOfSpaceStep(JarState.Promised jarState, String formatterVersion, @Nullable Options options) { |
| 46 | + this.jarState = jarState; |
| 47 | + this.formatterVersion = formatterVersion; |
| 48 | + this.options = options; |
| 49 | + } |
| 50 | + |
| 51 | + /** Creates a step which formats Java source with prince-of-space's default options. */ |
| 52 | + public static FormatterStep create(Provisioner provisioner) { |
| 53 | + return create(defaultVersion(), provisioner); |
| 54 | + } |
| 55 | + |
| 56 | + /** Creates a step which formats Java source with prince-of-space's default options. */ |
| 57 | + public static FormatterStep create(String version, Provisioner provisioner) { |
| 58 | + return create(version, provisioner, null); |
| 59 | + } |
| 60 | + |
| 61 | + /** Creates a step which formats Java source with the given options. */ |
| 62 | + public static FormatterStep create(String version, Provisioner provisioner, @Nullable Options options) { |
| 63 | + Objects.requireNonNull(version, "version"); |
| 64 | + Objects.requireNonNull(provisioner, "provisioner"); |
| 65 | + return FormatterStep.create(NAME, |
| 66 | + new PrinceOfSpaceStep(JarState.promise(() -> JarState.from(MAVEN_COORDINATE + version, provisioner)), version, options), |
| 67 | + PrinceOfSpaceStep::equalityState, |
| 68 | + State::createFormat); |
| 69 | + } |
| 70 | + |
| 71 | + /** Get default formatter version */ |
| 72 | + public static String defaultVersion() { |
| 73 | + return DEFAULT_VERSION; |
| 74 | + } |
| 75 | + |
| 76 | + private State equalityState() { |
| 77 | + return new State(jarState.get(), formatterVersion, options); |
| 78 | + } |
| 79 | + |
| 80 | + /** Mutable bag of the optional prince-of-space {@code FormatterConfig} knobs; unset (null) fields keep prince-of-space's own defaults. */ |
| 81 | + public static class Options implements Serializable { |
| 82 | + @Serial |
| 83 | + private static final long serialVersionUID = 1L; |
| 84 | + |
| 85 | + @Nullable private String indentStyle; |
| 86 | + @Nullable private Integer indentSize; |
| 87 | + @Nullable private Integer lineLength; |
| 88 | + @Nullable private String wrapStyle; |
| 89 | + @Nullable private Boolean closingParenOnNewLine; |
| 90 | + @Nullable private Boolean trailingCommas; |
| 91 | + @Nullable private Integer javaLanguageLevel; |
| 92 | + |
| 93 | + public Options() {} |
| 94 | + |
| 95 | + public void setIndentStyle(String indentStyle) { |
| 96 | + this.indentStyle = indentStyle; |
| 97 | + } |
| 98 | + |
| 99 | + public void setIndentSize(int indentSize) { |
| 100 | + this.indentSize = indentSize; |
| 101 | + } |
| 102 | + |
| 103 | + public void setLineLength(int lineLength) { |
| 104 | + this.lineLength = lineLength; |
| 105 | + } |
| 106 | + |
| 107 | + public void setWrapStyle(String wrapStyle) { |
| 108 | + this.wrapStyle = wrapStyle; |
| 109 | + } |
| 110 | + |
| 111 | + public void setClosingParenOnNewLine(boolean closingParenOnNewLine) { |
| 112 | + this.closingParenOnNewLine = closingParenOnNewLine; |
| 113 | + } |
| 114 | + |
| 115 | + public void setTrailingCommas(boolean trailingCommas) { |
| 116 | + this.trailingCommas = trailingCommas; |
| 117 | + } |
| 118 | + |
| 119 | + public void setJavaLanguageLevel(int javaLanguageLevel) { |
| 120 | + this.javaLanguageLevel = javaLanguageLevel; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private static final class State implements Serializable { |
| 125 | + @Serial |
| 126 | + private static final long serialVersionUID = 1L; |
| 127 | + |
| 128 | + private final JarState jarState; |
| 129 | + private final String formatterVersion; |
| 130 | + @Nullable private final Options options; |
| 131 | + |
| 132 | + State(JarState jarState, String formatterVersion, @Nullable Options options) { |
| 133 | + if (Jvm.version() < 17) { |
| 134 | + throw new IllegalStateException("prince-of-space requires a JDK 17+ host runtime, this JVM is " + Jvm.version()); |
| 135 | + } |
| 136 | + this.jarState = jarState; |
| 137 | + this.formatterVersion = formatterVersion; |
| 138 | + this.options = options; |
| 139 | + } |
| 140 | + |
| 141 | + FormatterFunc createFormat() throws Exception { |
| 142 | + final ClassLoader classLoader = jarState.getClassLoader(); |
| 143 | + final Class<?> formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.pos.PrinceOfSpaceFormatterFunc"); |
| 144 | + final Constructor<?> constructor = formatterFunc.getConstructor( |
| 145 | + String.class, Integer.class, Integer.class, String.class, Boolean.class, Boolean.class, Integer.class); |
| 146 | + if (options == null) { |
| 147 | + return (FormatterFunc) constructor.newInstance(null, null, null, null, null, null, null); |
| 148 | + } |
| 149 | + return (FormatterFunc) constructor.newInstance( |
| 150 | + options.indentStyle, |
| 151 | + options.indentSize, |
| 152 | + options.lineLength, |
| 153 | + options.wrapStyle, |
| 154 | + options.closingParenOnNewLine, |
| 155 | + options.trailingCommas, |
| 156 | + options.javaLanguageLevel); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments