Consider support for tab-based indent, if only programmatically
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.
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
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.
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.
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.
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?
Okay I missed that
JavaOutputisfinal, 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
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.
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?
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.
Support for tab-based indent has been added to the API in https://github.com/google/google-java-format/commit/6b868494b55eb91f322abd1af41deb434fbe133d
I also filed https://github.com/google/google-java-format/issues/1470 to track demand for a CLI flag for this.