Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,14 @@ jobs:
strategy:
fail-fast: false
matrix:
# The newest images. Arm Linux and Intel macOS have no free -latest label, so their images are
# named.
runner:
- ubuntu-latest
- ubuntu-24.04-arm
- macos-26
- macos-15-intel
- ubuntu-26.04-arm
# macOS on Apple silicon is built for a release only, in release.yml.
# - macos-latest
- macos-26-intel
- windows-latest

steps:
Expand Down
24 changes: 10 additions & 14 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ jobs:
retention-days: 7

# One job per platform, as in ci.yml, but here each one hands its binary to the deploy job below
# instead of throwing it away.
# instead of throwing it away. macOS on Apple silicon is built here only: ci.yml leaves it out.
native-images:
name: native image (${{ matrix.platform }})
name: native image (${{ matrix.runner }})
runs-on: ${{ matrix.runner }}
timeout-minutes: 90
# bash on Windows too, as in ci.yml.
Expand All @@ -132,17 +132,13 @@ jobs:
strategy:
fail-fast: false
matrix:
include:
- platform: linux-x86-64
runner: ubuntu-latest
- platform: linux-aarch64
runner: ubuntu-24.04-arm
- platform: macos-aarch64
runner: macos-26
- platform: macos-x86-64
runner: macos-15-intel
- platform: windows-x86-64
runner: windows-latest
# The newest images, as in ci.yml.
runner:
- ubuntu-latest
- ubuntu-26.04-arm
- macos-latest
- macos-26-intel
- windows-latest

steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
Expand Down Expand Up @@ -173,7 +169,7 @@ jobs:
- name: Hand the binary to the deploy job
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: native-image-${{ matrix.platform }}
name: native-image-${{ matrix.runner }}
path: |
open-java-format-native/build/native/nativeCompile/open-java-format-*
!open-java-format-native/build/native/nativeCompile/*.txt
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ mise run ci:native # actually run the linux-aarch64 native job

Two caveats:

- **act cannot containerise macOS**, so the `macos-15-intel` leg has to be checked by building the
- **act cannot containerise macOS**, so the `macos-26-intel` leg has to be checked by building the
native image on a Mac.
- **On an Apple Silicon host** the `ubuntu-24.04` container runs as arm64, so the job labelled
`ubuntu-latest` produces an aarch64 binary locally. Fine for exercising the workflow, not a
Expand Down
2 changes: 1 addition & 1 deletion mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ run = "act push --job build --env JAVA_HOME=/opt/hostedtoolcache/Java_Temurin-Ho

[tasks."ci:native"]
description = "Run the linux-aarch64 native job locally in Docker"
run = "act push --job native --matrix runner:ubuntu-24.04-arm"
run = "act push --job native --matrix runner:ubuntu-26.04-arm"

# ---- GitHub Actions secrets ----
# Read from 1Password at apply time; nothing secret is stored in the repo. Requires gh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,18 @@ public String fixImports(String input) throws FormatterException {
@SuppressForbidden(reason = "FormatterService, kept in the SPI for compatibility, returns ImmutableList")
private ImmutableList<Replacement> getFormatReplacementsInternal(String input, Collection<Range<Integer>> ranges)
throws IOException {
var characterRanges = RangeUtils.toStringRanges(ranges);
if (characterRanges.isEmpty()) {
// Nothing to format, as in process: without --character-ranges the command would format the whole file.
return ImmutableList.of();
}
FormatterCliArgs command = new FormatterCliArgs(
jdkPath,
jvmArgsForVersion(jdkMajorVersion),
implementationClassPath,
/* fixImportsOnly= */ false,
/* outputReplacements= */ true,
ranges.stream().map(RangeUtils::toStringRange).toList());
characterRanges);

Optional<String> output =
FormatterCommandRunner.runWithStdin(command.toArgs(), input, Optional.ofNullable(jdkPath.getParent()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ public NativeImageFormatterService(Path nativeImagePath) {
@Override
@SuppressForbidden(reason = "FormatterService, kept in the SPI for compatibility, returns ImmutableList")
public ImmutableList<Replacement> getFormatReplacements(String input, Collection<Range<Integer>> ranges) {
var characterRanges = RangeUtils.toStringRanges(ranges);
if (characterRanges.isEmpty()) {
// Nothing to format, as in process: without --character-ranges the command would format the whole file.
return ImmutableList.of();
}
Optional<String> output = Optional.empty();
try {
FormatterNativeImageArgs command = new FormatterNativeImageArgs(
nativeImagePath,
/* fixImportsOnly= */ false,
/* outputReplacements= */ true,
ranges.stream().map(RangeUtils::toStringRange).toList());
nativeImagePath, /* fixImportsOnly= */ false, /* outputReplacements= */ true, characterRanges);

output = FormatterCommandRunner.runWithStdin(
command.toArgs(), input, Optional.ofNullable(nativeImagePath.getParent()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package com.palantir.javaformat.bootstrap;

import com.google.common.collect.BoundType;
import com.google.common.collect.DiscreteDomain;
import com.google.common.collect.Range;
import java.util.Collection;
import java.util.List;

public final class RangeUtils {

Expand All @@ -31,5 +34,18 @@ public static String toStringRange(Range<Integer> range) {
return String.format("%s:%s", lower, higher);
}

/**
* Returns the ranges as "--character-ranges" takes them. Its ranges are closed, so an empty range, which IntelliJ
* IDEA passes among the others, has no form there: it is left out, as {@code FormatterServiceImpl} leaves it out
* in process.
*/
static List<String> toStringRanges(Collection<Range<Integer>> ranges) {
return ranges.stream()
.map(range -> range.canonical(DiscreteDomain.integers()))
.filter(range -> !range.isEmpty())
.map(RangeUtils::toStringRange)
.toList();
}

private RangeUtils() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,44 @@ class S {
assertThat(Utils.applyReplacements(input, replacements)).isEqualTo(expectedOutput);
}

@ParameterizedTest
@MethodSource("getFormatters")
void leaves_out_the_empty_ranges(FormatterService formatterService) throws FormatterException {
String input = """
class A {
int a;
int b;
}
""";
int a = input.indexOf(" int a");
int b = input.indexOf(" int b");

List<Replacement> replacements = formatterService.getFormatReplacements(
input,
List.of(
Range.closedOpen(a, input.indexOf('\n', a)),
Range.closedOpen(b, b),
Range.closedOpen(input.length(), input.length())));

assertThat(Utils.applyReplacements(input, replacements)).isEqualTo("""
class A {
int a;
int b;
}
""");
}

@ParameterizedTest
@MethodSource("getFormatters")
void formats_nothing_without_a_range_that_is_not_empty(FormatterService formatterService)
throws FormatterException {
String input = "class A { int a; }\n";

assertThat(formatterService.getFormatReplacements(input, List.of(Range.closedOpen(10, 10))))
.isEmpty();
assertThat(formatterService.getFormatReplacements(input, List.of())).isEmpty();
}

private static Stream<FormatterService> getFormatters() {
return Stream.of(
new BootstrappingFormatterService(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@

import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.collect.Range;
import com.palantir.javaformat.Utils;
import com.palantir.javaformat.java.FormatterException;
import com.palantir.javaformat.java.FormatterService;
import com.palantir.javaformat.java.FormatterServiceImpl;
import com.palantir.javaformat.java.Replacement;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -41,6 +45,44 @@ void fixes_only_the_imports(FormatterService formatterService) throws FormatterE
assertThat(formatterService.fixImports(input)).isEqualTo(expectedOutput);
}

@ParameterizedTest
@MethodSource("getFormatters")
void leaves_out_the_empty_ranges(FormatterService formatterService) throws FormatterException {
String input = """
class A {
int a;
int b;
}
""";
int a = input.indexOf(" int a");
int b = input.indexOf(" int b");

List<Replacement> replacements = formatterService.getFormatReplacements(
input,
List.of(
Range.closedOpen(a, input.indexOf('\n', a)),
Range.closedOpen(b, b),
Range.closedOpen(input.length(), input.length())));

assertThat(Utils.applyReplacements(input, replacements)).isEqualTo("""
class A {
int a;
int b;
}
""");
}

@ParameterizedTest
@MethodSource("getFormatters")
void formats_nothing_without_a_range_that_is_not_empty(FormatterService formatterService)
throws FormatterException {
String input = "class A { int a; }\n";

assertThat(formatterService.getFormatReplacements(input, List.of(Range.closedOpen(10, 10))))
.isEmpty();
assertThat(formatterService.getFormatReplacements(input, List.of())).isEmpty();
}

private static Stream<FormatterService> getFormatters() {
return Stream.of(
new FormatterServiceImpl(),
Expand Down
Loading