diff --git a/.cursor/rules/java-style.mdc b/.cursor/rules/java-style.mdc new file mode 100644 index 0000000..2a0ae46 --- /dev/null +++ b/.cursor/rules/java-style.mdc @@ -0,0 +1,116 @@ +--- +description: Java style taken from turtleisaac's pre-2026 Nds4j, PokEditor, and PokEditor-Core +globs: "**/*.java" +alwaysApply: false +--- + +# Java style + +The target is the style of code written for the split library stack, not PokEditor v2 and not the files copied out of it unchanged. + +PokEditor v2 (2021–2023, `com.turtleisaac.pokeditor`) was already mostly Allman in the editors, but `if(` and `for(` were the habit: about 42% of `if`s had a space. The third-party DSPRE reader under `com.jackhack96` was end-of-line braces and was rewritten, not kept. When that monolith was split, the new files (Nds4j parsers, PokEditor-Core, Nds4j-ToolUI) kept Allman and put the space in: new Nds4j files are 92% `if (` and 96% `for (`, new PokEditor-Core files 98% and 98%, and every hand-written ToolUI `if` and `for` has the space. `PersonalEditor.java` in v2 (`if(files.length > nameData.length)`) became `if (` in `PersonalParser`. + +These modern files are v2 carry-overs and still show the old habit. Do not copy them: `MemBuf`, `Buffer`, `BinaryWriter`, `CRC16`, `BitVector`, `BitStream`, `ArrayProcessor`, `CsvReader`, `XmlReader`, `CircleButton`, `TrainerPersonalityCalculator`. `MemBuf.java` in v2 is already `public class MemBuf {`. + +Code added in 2026 drifted again (long javadocs, same-line `if (x) return;`, column-aligned comments). Do not copy that drift either. + +JFormDesigner files (`Created by JFormDesigner`, including the generated `initComponents` blocks) are not a style sample. Generated ANTLR (`src/main/generated-sources`, gitignored) is not either. Hand-written GUI, including Nds4j-ToolUI classes such as `PanelManager` and `HexadecimalSpinner`, is. + +## Keywords are not methods + +Write a space after `if`, `for`, `while`, `switch`, `catch`, and `synchronized`: + +```java +if (folders != null) + this.folders = new HashMap<>(folders); + +for (byte[] data : files) +{ + fimgWriter.write(data); +} +``` + +Older files often use `if(` and `for(`. That was a slip, not the style. Do not replicate it. `if` and `for` are keywords, not methods. Method calls, constructor calls, and declarations stay tight: `readInt()`, `new MemBuf()`, `void save()`. + +The Eclipse profile inserts the keyword space and leaves call parentheses alone. Spotless applies that profile to a Java file the next time the file changes relative to `origin/main`. This repository is not being reformatted up front, so an old `if(` can still be sitting in a file you did not touch. When you do touch that file, let the formatter fix it. + +## Braces + +Allman braces for named types, constructors, methods, and `if` / `for` / `while` / `switch` blocks. The `{` is alone on the next line. `else`, `catch`, and `finally` each start their own line. + +```java +public class Fnt +{ + public int getIdOf(String name) + { + if (requestedPath.length == 1) + { + return searchFolder.firstId; + } + else + { + return -1; + } + } +} +``` + +Anonymous classes and array initializers keep `{` on the same line: `new FileProcessingData() {`, `{0, 0, 0, 0}`. + +End-of-line braces show up in a minority of files (`MemBuf`, `Buffer`, `BinaryWriter`, parts of PokEditor). Do not spread that shape. + +## Control-flow bodies + +A single-statement `if` or `else` may be unbraced. The statement goes on the next line, indented one level. Never `if (x) return;`. + +`for` and `while` are braced even when the body is one statement. Brace new loops. + +After a `for` or `while`, leave a blank line before the next statement. That applies to a one-line body and to a braced body. Skip the blank line when the loop is the last thing in the method: the closing brace of the method comes next. + +```java +for (String filename : files) + list.add(filename); + +for (String folderName : folders.keySet()) +{ + list.add(folderName + "/"); + list.addAll(folder.toStringList()); +} +``` + +## Methods, signatures, tests + +Leave one blank line between methods. The formatter preserves a blank line that is already there and will not insert one you forgot. + +Keep a method signature on one line, including the long ones. + +Break every chained `assertThat` onto its own indented line: + +```java +assertThat(folder.getIdOf("Alpha")) + .isEqualTo(0); +``` + +## Comments and binary I/O + +A javadoc is one summary line, plus `@param`, `@return`, or `@throws` only when they add something the signature does not. Most methods have no javadoc. Inline notes stay short. + +Structured bytes go through `MemBuf`. `Buffer` and `BinaryWriter` are the older Nds4j exceptions. + +## Layout that already holds + +Four spaces, no tabs. UTF-8, LF, newline at end of file. One space around operators and after commas. A cast has a space after it: `(int) value`. `Type[]`, not `Type []`. Lambdas are `x -> expr` with spaces around `->`. A ternary stays on one line. + +Imports are not reordered by the tools. When you write them, group project (`io.github.turtleisaac`), then other libraries, then `java` / `javax`, then static, with a blank line between groups. Wildcards are allowed. Hand-written Swing (`HexadecimalSpinner`, PokEditor cell editors) often leads with `javax` instead; that disagrees with Nds4j library code, so follow the file you are in. + +Nds4j switches are `case` / `break` with an Allman brace (the sources are Java 8). PokEditor and PokEditor-Core are Java 20 and mostly use arrow switches. Do not introduce arrow switches in Nds4j. + +## Tooling + +`mvn verify` runs Spotless in check mode and Checkstyle. Spotless uses `config/eclipse-formatter.xml` (Eclipse JDT 4.41) and only looks at Java files that differ from `origin/main`, so existing sources are not rewritten by this setup. Checkstyle (`config/checkstyle.xml`) checks every file for LF, a trailing newline, and no tab characters. + +`mvn spotless:apply` formats those changed files. It does not reorder imports, rewrap comments, or wrap long lines. + +## NitroViewer + +`nitroviewer-core` is its own Maven module (`com.nitroviewer.core`). Copy this rule, `config/eclipse-formatter.xml`, `config/checkstyle.xml`, and `.editorconfig` into that repo and add the same two plugins to `nitroviewer-core/pom.xml`, again with `ratchetFrom` set to `origin/main`. diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..5eaaad2 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,17 @@ +root = true + +# Java and the text files around it. Property bundles are left alone: +# some of them are ISO-8859-1, and a charset rule would rewrite them. +[*.{java,xml,yml,yaml,md}] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +indent_style = space +indent_size = 4 + +[*.java] +indent_style = space +indent_size = 4 +# Signatures and a few data tables run well past 120 columns on purpose. +max_line_length = off diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e86be26..ceee8c5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -16,6 +16,9 @@ jobs: steps: - uses: actions/checkout@v4 + with: + # Spotless ratchetFrom origin/main needs the base branch. + fetch-depth: 0 - name: Set up JDK uses: actions/setup-java@v4 @@ -41,7 +44,10 @@ jobs: echo "::group::$repo @ $target" git clone --quiet --depth 1 --branch "$target" \ "https://github.com/turtleisaac/$repo.git" ".upstream/$repo" - mvn -B -ntp -q install -DskipTests -f ".upstream/$repo/pom.xml" + # Each library is style-checked in its own CI. A depth-1 clone of a + # same-named branch has no origin/main, so Spotless ratchetFrom fails + # if these checks run here. + mvn -B -ntp -q install -DskipTests -Dspotless.check.skip=true -Dcheckstyle.skip=true -f ".upstream/$repo/pom.xml" echo "::endgroup::" done diff --git a/config/checkstyle-suppressions.xml b/config/checkstyle-suppressions.xml new file mode 100644 index 0000000..7f590e4 --- /dev/null +++ b/config/checkstyle-suppressions.xml @@ -0,0 +1,14 @@ + + + + + + + + + diff --git a/config/checkstyle.xml b/config/checkstyle.xml new file mode 100644 index 0000000..e66136a --- /dev/null +++ b/config/checkstyle.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/config/eclipse-formatter.xml b/config/eclipse-formatter.xml new file mode 100644 index 0000000..5dfe55e --- /dev/null +++ b/config/eclipse-formatter.xml @@ -0,0 +1,290 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index 7669055..45d091f 100644 --- a/pom.xml +++ b/pom.xml @@ -147,6 +147,86 @@ -Djava.awt.headless=true --add-opens java.desktop/java.awt=ALL-UNNAMED + + + com.diffplug.spotless + spotless-maven-plugin + 3.11.0 + + origin/main + + + src/main/java/io/github/turtleisaac/pokeditor/gui_old/tutorial/TutorialFrame.java + src/main/java/io/github/turtleisaac/pokeditor/gui_old/editors/encounters/SearchResultsFrame.java + src/main/java/io/github/turtleisaac/pokeditor/gui_old/editors/trainers/SmogonFrame.java + src/main/java/io/github/turtleisaac/pokeditor/gui_old/editors/trainers/text/TrainerTextEntryPanel.java + src/main/java/io/github/turtleisaac/pokeditor/gui_old/editors/trainers/text/TrainerTextFrame.java + src/main/java/io/github/turtleisaac/pokeditor/gui_old/editors/trainers/classes/ClassPanel.java + src/main/java/io/github/turtleisaac/pokeditor/gui/ConsoleWindow.java + src/main/java/io/github/turtleisaac/pokeditor/gui/sheets/FindDialog.java + src/main/java/io/github/turtleisaac/pokeditor/gui/sheets/DefaultSheetPanel.java + src/main/java/io/github/turtleisaac/pokeditor/gui/editors/data/DefaultDataEditorPanel.java + src/main/java/io/github/turtleisaac/pokeditor/gui/editors/data/formats/scripts/field/FieldScriptEditor.java + src/main/java/io/github/turtleisaac/pokeditor/gui/editors/data/formats/pokemon_sprite/PokemonSpriteEditor.java + src/main/java/io/github/turtleisaac/pokeditor/gui/editors/data/formats/trainer_sprite/TrainerSpriteEditor.java + src/main/java/io/github/turtleisaac/pokeditor/gui/editors/general/GeneralEditorPanel.java + src/main/java/io/github/turtleisaac/pokeditor/gui/editors/general/starters/StarterEditor.java + src/main/java/io/github/turtleisaac/variabletracker/gui/variables/VariableTracker.java + src/main/java/io/github/turtleisaac/variabletracker/gui/flags/FlagTracker.java + + + 4.41 + ${project.basedir}/config/eclipse-formatter.xml + + + + + + spotless-check + verify + + check + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 3.6.0 + + + com.puppycrawl.tools + checkstyle + 12.3.1 + + + + config/checkstyle.xml + true + false + false + true + true + false + + + + checkstyle-verify + verify + + check + + + +