Skip to content

google-java-format does not respect the --lines option #1000

Closed

Description

@timo-p

Version

1.18.1 all-deps.

Description

google-java-format formats the whole while even when --lines options is provided.

Example

I have a Java file that looks like this:

public class FormattingTest {
  protected void method1(Vehicle vehicle) {
    vehicle.getTechnicalDetails().getWheels().stream().anyMatch(wheel -> wheel.getManufacturer().equals("Goodyear"));
  }

  protected void method2(Vehicle vehicle) {
    vehicle.getTechnicalDetails().getWheels().stream().anyMatch(wheel -> wheel.getManufacturer().equals("Goodyear"));
  }
}

I wan't to only format method1 which is on lines 2 to 4 so I run google-java-format with the --lines option:

google-java-format --lines 2:4 FormattingTest.java

As output I get:

public class FormattingTest {
  protected void method1(Vehicle vehicle) {
    vehicle.getTechnicalDetails().getWheels().stream()
        .anyMatch(wheel -> wheel.getManufacturer().equals("Goodyear"));
  }

  protected void method2(Vehicle vehicle) {
    vehicle.getTechnicalDetails().getWheels().stream()
        .anyMatch(wheel -> wheel.getManufacturer().equals("Goodyear"));
  }
}

The whole file is formatted when only lines from 2 to 4 should be.

I expected to see this output:

public class FormattingTest {
  protected void method1(Vehicle vehicle) {
    vehicle.getTechnicalDetails().getWheels().stream()
        .anyMatch(wheel -> wheel.getManufacturer().equals("Goodyear"));
  }

  protected void method2(Vehicle vehicle) {
    vehicle.getTechnicalDetails().getWheels().stream().anyMatch(wheel -> wheel.getManufacturer().equals("Goodyear"));
  }
}

Changing the --lines option to any of the aliases -lines, --line, -line does not make a difference.
Also using --offset and --length does not make a difference. Although these options are so poorly documented I'm not sure if I am using them correctly.

google-java-format --offset 2 --length 2 FormattingTest.java

Activity

  1. cushon commented on Dec 8, 2023

    @cushon
    Collaborator

    The feature that wraps long string literals applies to the entire file, it doesn't support partial formatting. This is affecting this example because the lines are longer than 100 characters long, and contain string literals, so the logic string reflowing is applied.

    -lines has the expected behaviour if string reflowing is disabled:

    $ google-java-format --skip-reflowing-long-strings -lines 2:4 FormattingTest.java
    public class FormattingTest {
      protected void method1(Vehicle vehicle) {
        vehicle.getTechnicalDetails().getWheels().stream()
            .anyMatch(wheel -> wheel.getManufacturer().equals("Goodyear"));
      }
    
      protected void method2(Vehicle vehicle) {
        vehicle.getTechnicalDetails().getWheels().stream().anyMatch(wheel -> wheel.getManufacturer().equals("Goodyear"));
      }
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions