|
| 1 | +# Style |
| 2 | + |
| 3 | +open-java-format has one style and no settings. It is the style of palantir-java-format 2.x, which |
| 4 | +grew out of google-java-format: lines of up to 120 characters, 4 spaces for each level of |
| 5 | +indentation, and 8 more for a line that continues a statement. Every example on this page is the |
| 6 | +formatter's own output. |
| 7 | + |
| 8 | +## Wrapping |
| 9 | + |
| 10 | +When a declaration does not fit on one line, its parameters move to a continuation line together. |
| 11 | + |
| 12 | +``` java |
| 13 | +class Params { |
| 14 | + public static ResolvedConfiguration resolveConfigurationForProject( |
| 15 | + Project project, String configurationName, boolean includeTransitiveDependencies) { |
| 16 | + return resolve(project, configurationName, includeTransitiveDependencies); |
| 17 | + } |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +## Lambdas |
| 22 | + |
| 23 | +A lambda starts on the line of the call it is passed to, and its body is indented by one level from |
| 24 | +that line, however deeply the call is nested. |
| 25 | + |
| 26 | +``` java |
| 27 | +class Tasks { |
| 28 | + void register(Project project) { |
| 29 | + project.getTasks().register("formatDiff", FormatDiffTask.class, task -> { |
| 30 | + task.setGroup("formatting"); |
| 31 | + task.setDescription("Formats the lines you changed"); |
| 32 | + }); |
| 33 | + executor.submit(() -> { |
| 34 | + runChecks(); |
| 35 | + }); |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +A short lambda stays on the line of its call, also inside a chain. |
| 41 | + |
| 42 | +``` java |
| 43 | +class Lambda { |
| 44 | + private static GradleException notFound(String group, String name, Configuration configuration) { |
| 45 | + String actual = configuration.getIncoming().getResolutionResult().getAllComponents().stream() |
| 46 | + .map(ResolvedComponentResult::getModuleVersion) |
| 47 | + .map(mvi -> String.format("\t- %s:%s:%s", mvi.getGroup(), mvi.getName(), mvi.getVersion())) |
| 48 | + .collect(Collectors.joining("\n")); |
| 49 | + return new GradleException(actual); |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +The [home page](index.md#what-the-output-looks-like) shows the same kind of code next to the output |
| 55 | +of google-java-format. |
| 56 | + |
| 57 | +## Call chains |
| 58 | + |
| 59 | +A chain of calls stays on one line only if everything before its last dot fits in 80 columns, even |
| 60 | +when the whole statement would fit in 120. Otherwise each call goes on a line of its own, which keeps |
| 61 | +builders and streams easy to read and to diff. |
| 62 | + |
| 63 | +``` java |
| 64 | +class Chains { |
| 65 | + void f() { |
| 66 | + var request = HttpRequest.newBuilder() |
| 67 | + .uri(uri) |
| 68 | + .header("Accept", "application/json") |
| 69 | + .timeout(timeout) |
| 70 | + .build(); |
| 71 | + var user = User.builder().name(name).email(email).build(); |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +On one line the first statement would be 118 characters long, with its last dot in column 110. The |
| 77 | +last dot of the second one is in column 58. |
| 78 | + |
| 79 | +When moving the whole chain onto the next line brings its last dot within the limit, the formatter |
| 80 | +does that instead of splitting it. |
| 81 | + |
| 82 | +``` java |
| 83 | +class Chains { |
| 84 | + void f() { |
| 85 | + var foo = |
| 86 | + SomeType.builder().thing1(thing1).thing2(thing2).thing3(thing3).build(); |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +## Long strings |
| 92 | + |
| 93 | +A string literal that runs past column 120 is split between words, and the rest continues after a |
| 94 | +`+` on the next line. On the command line, `--skip-reflowing-long-strings` turns this off. |
| 95 | + |
| 96 | +``` java |
| 97 | +class Strings { |
| 98 | + String message = |
| 99 | + "The formatter reflows a string literal that runs past the column limit, and it keeps the words intact" |
| 100 | + + " while doing so."; |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +## Imports |
| 105 | + |
| 106 | +Static imports come first, then a blank line and the other imports, each group in ASCII order. |
| 107 | +Imports the file does not use are removed: the input of this example also imported `java.util.Map` |
| 108 | +and `java.util.Set`. On the command line, `--skip-sorting-imports` and |
| 109 | +`--skip-removing-unused-imports` turn these off. |
| 110 | + |
| 111 | +``` java |
| 112 | +package com.example; |
| 113 | + |
| 114 | +import static java.util.Objects.requireNonNull; |
| 115 | + |
| 116 | +import com.google.common.collect.ImmutableList; |
| 117 | +import java.util.List; |
| 118 | + |
| 119 | +class Imports { |
| 120 | + List<String> names = ImmutableList.of(requireNonNull("a")); |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +## Comments |
| 125 | + |
| 126 | +A `//` comment that runs past column 120 is wrapped onto a new `//` line. Javadoc and `/* */` |
| 127 | +comments are kept exactly as written, however long their lines are. |
| 128 | + |
| 129 | +``` java |
| 130 | +class Comments { |
| 131 | + // A line comment that runs past the limit of one hundred and twenty characters is wrapped, and the rest continues |
| 132 | + // on a line of its own. |
| 133 | + int x; |
| 134 | + |
| 135 | + /** Javadoc is kept exactly as it is written, however long its lines are, because the formatter does not reflow it. */ |
| 136 | + int y; |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +## Trade-offs |
| 141 | + |
| 142 | +- **The layout follows the syntax.** The formatter does not know which grouping of arguments reads |
| 143 | + best. When a layout comes out awkward, a local variable or a small method usually fixes it, and the |
| 144 | + fix holds on every later run. |
| 145 | +- **A `$NON-NLS$` marker can end up on another line.** Eclipse expects the marker on the line of the |
| 146 | + string it marks. When the formatter wraps such a statement, the string moves to a line of its own |
| 147 | + and the marker stays at the end of the statement: |
| 148 | + |
| 149 | + ``` java |
| 150 | + class Messages { |
| 151 | + void f() { |
| 152 | + label.setText(Messages.format( |
| 153 | + "The configuration of the project could not be read", |
| 154 | + projectName, |
| 155 | + configurationFileName)); // $NON-NLS-1$ |
| 156 | + } |
| 157 | + } |
| 158 | + ``` |
| 159 | + |
| 160 | +- **Layout fixes of our own wait for 3.x.** For the whole 2.x line the output is byte-for-byte the |
| 161 | + same as the palantir-java-format release with the same version number. The |
| 162 | + [manifesto](manifesto.md) explains why output stability comes first. |
0 commit comments