google-java-format icon indicating copy to clipboard operation
google-java-format copied to clipboard

document/explain logic of ignoring --lines

Open maxandersen opened this issue 3 months ago • 2 comments

as seen in #942 and #1000 (and others) use of --lines does not reliably work.

I'm trying to grok how/why....

As an example:

///usr/bin/env jbang "$0" "$@" ; exit $?


import static java.lang.System.*;

public class hello {

    public static void main(String... args) {
        out.println("Hello World");
    }
}

I want google java format to ignore the first line - reading other isseues I should --skip-* as much as possible ...

jbang com.google.googlejavaformat:google-java-format:1.28.0 --lines 2:9999 --skip-javadoc-formatting --skip-reflowing-long-strings --skip-sorting-imports --skip-removing-unused-imports hello.java

result in first line being formatted as: /// usr/bin/env jbang "$0" "$@" ; exit $?

why ?

If I add --lines 1000:9999 it works...but now the code is not formatted at all.

is this really the intended behavior of --lines ?

any way to have it not try include outside the lines requested?

maxandersen avatar Sep 13 '25 15:09 maxandersen

This is related to this FAQ entry.

It can't reformat arbitrary regions of the program, it always reformats entire sections, so it expands --lines to include the sections that it can format. One example of that is that it will reformat leading comments before the section it's formatting.

cushon avatar Sep 21 '25 03:09 cushon

Yeah; unfortunately means it seem to format beyond what it been told afaics. The code is all valid so it has no need to go expand into lines explicitly told to ignore :/

maxandersen avatar Sep 21 '25 11:09 maxandersen