-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Limit the max width of method chain dots #70
Changes from all commits
11f2fa9
daf63cd
b6ed578
b53f947
cfebb3b
926d94f
b5ea3b7
e1c020e
263df96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
type: improvement | ||
improvement: | ||
description: |- | ||
Limit how far dots may appear in long method chains to 80 chars. | ||
|
||
Note that this doesn't currently apply to prefixes (such as `foo.bar().baz().stream()`) because prefixes are also used to group together fully qualified class names and it's a bit trickier to handle that case. | ||
links: | ||
- https://github.com/palantir/palantir-java-format/pull/70 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ | |
|
||
package com.palantir.javaformat.doc; | ||
|
||
import com.google.common.base.MoreObjects; | ||
import com.google.common.collect.Range; | ||
import com.google.errorprone.annotations.Immutable; | ||
import com.palantir.javaformat.CommentsHelper; | ||
|
@@ -25,25 +24,19 @@ | |
import com.palantir.javaformat.Output; | ||
import com.palantir.javaformat.doc.State.BreakState; | ||
import java.util.Optional; | ||
import org.immutables.value.Value; | ||
|
||
/** A leaf node in a {@link Doc} for an optional break. */ | ||
@Immutable | ||
public final class Break extends Doc implements Op { | ||
private final FillMode fillMode; | ||
private final String flat; | ||
private final Indent plusIndent; | ||
private final Optional<BreakTag> optTag; | ||
|
||
private Break(FillMode fillMode, String flat, Indent plusIndent, Optional<BreakTag> optTag) { | ||
this.fillMode = fillMode; | ||
this.flat = flat; | ||
this.plusIndent = plusIndent; | ||
this.optTag = optTag; | ||
} | ||
@Value.Immutable | ||
public abstract class Break extends Doc implements Op { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I converted this to an immutable thinking I wanted to add new fields to it but ended up not doing that. |
||
public abstract FillMode fillMode(); | ||
|
||
public FillMode getFillMode() { | ||
return fillMode; | ||
} | ||
public abstract String flat(); | ||
|
||
public abstract Indent plusIndent(); | ||
|
||
public abstract Optional<BreakTag> optTag(); | ||
|
||
/** | ||
* Make a {@code Break}. | ||
|
@@ -54,7 +47,7 @@ public FillMode getFillMode() { | |
* @return the new {@code Break} | ||
*/ | ||
public static Break make(FillMode fillMode, String flat, Indent plusIndent) { | ||
return new Break(fillMode, flat, plusIndent, /* optTag= */ Optional.empty()); | ||
return builder().fillMode(fillMode).flat(flat).plusIndent(plusIndent).build(); | ||
} | ||
|
||
/** | ||
|
@@ -67,7 +60,7 @@ public static Break make(FillMode fillMode, String flat, Indent plusIndent) { | |
* @return the new {@code Break} | ||
*/ | ||
public static Break make(FillMode fillMode, String flat, Indent plusIndent, Optional<BreakTag> optTag) { | ||
return new Break(fillMode, flat, plusIndent, optTag); | ||
return builder().fillMode(fillMode).flat(flat).plusIndent(plusIndent).optTag(optTag).build(); | ||
} | ||
|
||
/** | ||
|
@@ -76,21 +69,16 @@ public static Break make(FillMode fillMode, String flat, Indent plusIndent, Opti | |
* @return the new forced {@code Break} | ||
*/ | ||
public static Break makeForced() { | ||
return make(FillMode.FORCED, "", Indent.Const.ZERO); | ||
return builder().fillMode(FillMode.FORCED).flat("").plusIndent(Indent.Const.ZERO).build(); | ||
} | ||
|
||
/** | ||
* Return the {@code Break}'s extra indent. | ||
* | ||
* @return the extra indent | ||
* @param state | ||
*/ | ||
public int evalPlusIndent(State state) { | ||
return plusIndent.eval(state); | ||
} | ||
|
||
Indent getPlusIndent() { | ||
return plusIndent; | ||
return plusIndent().eval(state); | ||
} | ||
|
||
/** | ||
|
@@ -99,7 +87,7 @@ Indent getPlusIndent() { | |
* @return whether the {@code Break} is forced | ||
*/ | ||
public boolean isForced() { | ||
return fillMode == FillMode.FORCED; | ||
return fillMode() == FillMode.FORCED; | ||
} | ||
|
||
@Override | ||
|
@@ -108,22 +96,22 @@ public void add(DocBuilder builder) { | |
} | ||
|
||
@Override | ||
float computeWidth() { | ||
return isForced() ? Float.POSITIVE_INFINITY : (float) flat.length(); | ||
protected float computeWidth() { | ||
return isForced() ? Float.POSITIVE_INFINITY : (float) flat().length(); | ||
} | ||
|
||
@Override | ||
String computeFlat() { | ||
return flat; | ||
protected String computeFlat() { | ||
return flat(); | ||
} | ||
|
||
@Override | ||
Range<Integer> computeRange() { | ||
protected Range<Integer> computeRange() { | ||
return EMPTY_RANGE; | ||
} | ||
|
||
public State computeBreaks(State stateIn, boolean broken) { | ||
State state = optTag.map(breakTag -> stateIn.breakTaken(breakTag, broken)).orElse(stateIn); | ||
State state = optTag().map(breakTag -> stateIn.breakTaken(breakTag, broken)).orElse(stateIn); | ||
return state.withBreak(this, broken); | ||
} | ||
|
||
|
@@ -143,17 +131,13 @@ public void write(State state, Output output) { | |
output.append(state, "\n", EMPTY_RANGE); | ||
output.indent(breakState.newIndent()); | ||
} else { | ||
output.append(state, flat, range()); | ||
output.append(state, flat(), range()); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice |
||
return MoreObjects.toStringHelper(this) | ||
.add("fillMode", fillMode) | ||
.add("flat", flat) | ||
.add("plusIndent", plusIndent) | ||
.add("optTag", optTag) | ||
.toString(); | ||
public static class Builder extends ImmutableBreak.Builder {} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've also taken the opportunity of passing through OpenOp right into Level rather than unpacking its properties through multiple methods, as it was getting laborious to add new fields.