Skip to content

Commit fe294a5

Browse files
committed
Install open-java-format 2.98.0.5 and add JBang to the library rules
2.98.0.5 leaves a JBang script's first line and its directives, such as //DEPS, exactly as written when they come before the first line of code (openjavaformat/open-java-format#24). Until then the formatter put a space after the slashes, so JBang dropped the dependencies and a shell could no longer run the file. The new JBang page under Library rules follows the Flogger page: the header of a picocli script as the released native binaries of 2.98.0.4 and 2.98.0.5 format it, then where the rule applies. It also says that 2.98.0.5 does not take out spaces an earlier run added, which the same binaries confirm. The index now covers rules that go by directive names, and says that a rule fixing a bug can come before 3.0. The exclude example on the GitHub Action page gave JBang scripts as its reason, because the formatter rewrote their //DEPS. It now shows copied third-party code instead. Migrate stays as it is: code that palantir-java-format has already formatted has "// DEPS", and 2.98.0.5 leaves that alone.
1 parent 6544a35 commit fe294a5

4 files changed

Lines changed: 63 additions & 8 deletions

File tree

‎docs/get-started/github-actions.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ root. Every line that is not empty and not a comment is a git pathspec, and a Ja
5555
one is skipped.
5656

5757
``` gitignore title=".open-java-format-exclude"
58-
# Standalone jbang scripts: the formatter would rewrite their //DEPS directives
59-
samples/**
58+
# Code copied from another project, kept as it came
59+
third_party/**
6060
6161
# Generated sources
6262
**/build/generated/**

‎docs/library-rules/index.md‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# Library rules
22

33
open-java-format has one style and no options. A few libraries are written so that a chain of calls
4-
reads as one phrase, and the general rule of one call per line cuts that phrase apart. For those
5-
libraries the formatter carries a rule of its own.
4+
reads as one phrase, and the general rule of one call per line cuts that phrase apart. Some tools read
5+
instructions from comments in the code, and the general rule of a space after `//` hides them. For
6+
those the formatter carries a rule of its own.
67

7-
A library rule is built in, so there is nothing to configure. It goes by method names, because the
8-
formatter sees syntax and not types, and it leaves alone any code that does not match.
8+
A library rule is built in, so there is nothing to configure. It goes by names, of methods or of
9+
directives, because the formatter sees syntax and not types, and it leaves alone any code that does
10+
not match.
911

1012
| Library | What the rule does |
1113
| --- | --- |
1214
| [Flogger](flogger.md) | Keeps the logger and its fluent calls on one line in front of `log(` |
15+
| [JBang](jbang.md) | Keeps `//DEPS` and the other directives at the top of a script as written |
1316

1417
## Proposing a rule
1518

@@ -18,4 +21,5 @@ piece of code as it is formatted today, and the way it should look. The proposal
1821
[Mutiny](https://github.com/openjavaformat/open-java-format/issues/25) shows what that takes.
1922

2023
A new rule changes how existing code is formatted, so it ships only in a release that is allowed to
21-
change output.
24+
change output. The exception is a rule that fixes a bug: the JBang rule came in 2.98.0.5, because
25+
until then the formatter broke those scripts.

‎docs/library-rules/jbang.md‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# JBang
2+
3+
A [JBang](https://www.jbang.dev/) script keeps its first line and its directives, such as `//DEPS`,
4+
exactly as written: no space after the slashes and no wrapping.
5+
6+
``` java title="Default rules"
7+
/// usr/bin/env jbang "$0" "$@" ; exit $?
8+
// JAVA 21+
9+
// DEPS info.picocli:picocli:4.7.6
10+
11+
import picocli.CommandLine;
12+
```
13+
14+
``` java title="With the JBang rule"
15+
///usr/bin/env jbang "$0" "$@" ; exit $?
16+
//JAVA 21+
17+
//DEPS info.picocli:picocli:4.7.6
18+
19+
import picocli.CommandLine;
20+
```
21+
22+
JBang reads a directive only when its name comes right after the slashes. Under the default rules it
23+
drops the Java version and the dependency, so the script no longer compiles, and a shell that runs
24+
the file takes `///` for the command and fails.
25+
26+
## When it applies
27+
28+
Only in the comments at the top of the file, before the first line of code, which is where
29+
[JBang's documentation](https://www.jbang.dev/documentation/jbang/latest/script-directives.html)
30+
puts directives. After the `package` declaration, an import or a class, the same text is an ordinary
31+
comment and gets its space.
32+
33+
Two kinds of line stay as written there:
34+
35+
- A directive: one of JBang's names right after the slashes, followed by a space or the end of the
36+
line. The names are `CDS`, `COMPILE_OPTIONS`, `DEPS`, `DESCRIPTION`, `DOCS`, `FILES`, `GAV`,
37+
`GROOVY`, `JAVA`, `JAVAAGENT`, `JAVAC_OPTIONS`, `JAVA_OPTIONS`, `KOTLIN`, `MAIN`, `MANIFEST`,
38+
`MODULE`, `NATIVE_OPTIONS`, `NOINTEGRATIONS`, `PREVIEW`, `REPOS`, `RUNTIME_OPTIONS` and `SOURCES`.
39+
A name behind an integration's prefix, such as Quarkus's `//Q:CONFIG`, counts as well.
40+
- The first line of the file, when its first word is a path, as in `///usr/bin/env jbang` or
41+
`//usr/bin/env jbang`.
42+
43+
Other comments there, such as `//deps` or `//TODO`, get their space as before.
44+
45+
A script that was formatted before 2.98.0.5, or with palantir-java-format or google-java-format,
46+
already has the spaces, and the formatter does not take them out. Remove them by hand once.
47+
48+
The rule is our own and came in 2.98.0.5
49+
([#24](https://github.com/openjavaformat/open-java-format/issues/24)). In google-java-format the same
50+
request is still open as [#1217](https://github.com/google/google-java-format/issues/1217).

‎zensical.toml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ nav = [
3131
{ "Library rules" = [
3232
"library-rules/index.md",
3333
{ "Flogger" = "library-rules/flogger.md" },
34+
{ "JBang" = "library-rules/jbang.md" },
3435
] },
3536
{ "Migrate" = "migrate.md" },
3637
{ "Manifesto" = "manifesto.md" },
@@ -106,7 +107,7 @@ toggle.name = "Switch to system preference"
106107
# plugin puts it in before the Markdown is rendered, so it lands in code blocks too. The key is not
107108
# extra.version: the theme reads that one as the switch for a version selector.
108109
[project.extra]
109-
ojf_version = "2.98.0.4"
110+
ojf_version = "2.98.0.5"
110111
# The Maven plugin is released from its own repository, openjavaformat/fmt-maven-plugin, with version
111112
# numbers of its own. Bump it on each release of the plugin.
112113
maven_plugin_version = "2.27.0.1"

0 commit comments

Comments
 (0)