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

Consider support for tab-based indent, if only programmatically

Open JakeWharton opened this issue 10 months ago • 11 comments

I would like to produce formatting with tabs, and this is currently impossible to do due to internal modeling.

Tabs generally have a column-based width associated with them. I use two columns to achieve the same look as two space indent, for example. This allows the algorithms based on line length to still work correctly.

In output, however, spaces are hard coded to be used to represent indent values.

JakeWharton avatar Jan 23 '26 04:01 JakeWharton

One place this assumption is baked in is JavaOutput#indent, did you find others?

https://github.com/google/google-java-format/blob/737b0032b3a18eb6e458271ea440098c166f6c2d/core/src/main/java/com/google/googlejavaformat/java/JavaOutput.java#L182

cushon avatar Jan 27 '26 13:01 cushon

Yes, that's the one that I found that caused me to mostly give up even trying.

I'm somewhat confident the rest of the integer-based indent system can be used unmodified to model tabs. At worst the indent could always be specified as 1, but I suspect we could say it was whatever the user display preference specifies (e.g., 2 for me) and then convert that value back into a tab character count with a checked division.

I figured I'd raise the feature request first and see what your thoughts are before spending any time pushing it further.

JakeWharton avatar Jan 27 '26 16:01 JakeWharton

If all that's needed here is a hook to change that hard-coded " " in JavaOutput, I think that's probably fine. Would something like this be sufficient?

--- a/core/src/main/java/com/google/googlejavaformat/java/JavaOutput.java
+++ b/core/src/main/java/com/google/googlejavaformat/java/JavaOutput.java
@@ -179,7 +179,11 @@ public final class JavaOutput extends Output {

   @Override
   public void indent(int indent) {
-    spacesPending.append(Strings.repeat(" ", indent));
+    spacesPending.append(computeIndent(indent));
+  }
+
+  protected String computeIndent(int indent) {
+    return Strings.repeat(" ", indent);
   }

I think ktfmt could also create its own subclass of Output instead of extending JavaOutput to achieve the same result, but there's a bunch of logic in JavaOutput it would be nice to avoid a fork of.

cushon avatar Jan 28 '26 14:01 cushon

Yeah, I think so. I can try it with both projects locally first, but not tonight. I've been off work for a few months, but I'm getting back into the swing of things here soon so it'll be probably be a week or two before I try it out. I'll report back.

JakeWharton avatar Feb 12 '26 03:02 JakeWharton

Okay I missed that JavaOutput is final, but I added it as an additional "whitespace" constructor argument to test. I hacked ktfmt to supply a tab character, and it seems to work fine. How does that approach sound?

JakeWharton avatar Mar 06 '26 18:03 JakeWharton

Okay I missed that JavaOutput is final, but I added it as an additional "whitespace" constructor argument to test. I hacked ktfmt to supply a tab character, and it seems to work fine. How does that approach sound?

Sounds OK to me, thanks

cushon avatar Mar 09 '26 11:03 cushon

Okay so one other problem with this approach is that it essentially forces you to treat tabs as 1 "space", as opposed to being represented as one indent level with a configurable size. This then has problems with the maximum column width and wrapping.

A project using tabs usually defines the default render width. For example, all my projects that use tabs still visually represent them as moving text in by two character columns (i.e., two spaces). All the advantages of two-space indent with 50% fewer characters 😀.

Just wanted to gauge your interest in how this is modeled since there's a few approaches. Indent, as written, is not very semantic. It essentially represents a rendered indent, as the AOSP-style format has to create its own instances despite the single indent/double indent. This could be fixed in an incompatible manner. Alternately I can try to cram in this concept in parallel APIs.

JakeWharton avatar Mar 26 '26 03:03 JakeWharton

I think there's a very low but non-zero complexity budget for adding programmatic support for tabs :)

Would you want to decide how wide the rendered tabs were during formatting, so the formatter knows where two break lines? If it was possible to customize JavaOutput so the indent character and the width of the indent character were configurable, would that be sufficient? To make that concrete, if JavaOutput could be set up to do the equivalent of:

  @Override
  public void indent(int indent) {
    spacesPending.append(Strings.repeat("\t", indent / tabWidth));
  }

and then Indent could continue treating indents as rendered indent.

If you think there's a cleaner option, like a change to Indent, could you sketch what you have in mind?

cushon avatar Mar 26 '26 13:03 cushon

Hi @cushon I'd be interested in working on this issue if it's still open for contributions.

From the discussion, it seems there was agreement on keeping the implementation relatively low-complexity. If that's still the preferred direction, I'd be happy to work on it.

Please let me know if there's a specific approach you'd like me to follow before I get started. Thank you.

Prachi-Gupta2808 avatar Jul 03 '26 14:07 Prachi-Gupta2808

Support for tab-based indent has been added to the API in https://github.com/google/google-java-format/commit/6b868494b55eb91f322abd1af41deb434fbe133d

cushon avatar Oct 01 '26 09:10 cushon

I also filed https://github.com/google/google-java-format/issues/1470 to track demand for a CLI flag for this.

cushon avatar Oct 02 '26 11:10 cushon