-
Notifications
You must be signed in to change notification settings - Fork 51
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
Nested method chains also adhere to column limit #123
Changes from all commits
63a5083
1a6239b
9dcf798
faa5158
e0fdb0d
b75369d
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,5 @@ | ||
type: fix | ||
fix: | ||
description: Nested method chains now also adhere to column limit. | ||
links: | ||
- https://github.com/palantir/palantir-java-format/pull/123 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,25 +123,33 @@ public State computeBreaks(CommentsHelper commentsHelper, int maxWidth, State st | |
* maxWidth} | ||
*/ | ||
private Optional<Integer> tryToFitOnOneLine(int maxWidth, State state, Iterable<Doc> docs) { | ||
if (getColumnLimitBeforeLastBreak().isPresent()) { | ||
float width = 0.0f; | ||
float widthBeforeLastBreak = 0.0f; | ||
for (Doc doc : docs) { | ||
if (doc instanceof Break) { | ||
widthBeforeLastBreak = width; | ||
int column = state.column(); | ||
int columnBeforeLastBreak = 0; // Not activated by default | ||
for (Doc doc : docs) { | ||
if (doc instanceof Break && ((Break) doc).hasColumnLimit()) { | ||
columnBeforeLastBreak = column; | ||
} else if (doc instanceof Level) { | ||
// Levels might have nested levels that have a 'columnLimitBeforeLastBreak' set, so recurse. | ||
State newState = state.withColumn(column); | ||
Level innerLevel = (Level) doc; | ||
Optional<Integer> newWidth = innerLevel.tryToFitOnOneLine(maxWidth, newState, innerLevel.getDocs()); | ||
if (!newWidth.isPresent()) { | ||
return Optional.empty(); | ||
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. why do we return early here? 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. It indicates that the innerLevel was not inlineable. |
||
} | ||
width += doc.getWidth(); | ||
} | ||
// Make an additional check that widthBeforeLastBreak fits in the column limit | ||
if (state.column() + widthBeforeLastBreak > getColumnLimitBeforeLastBreak().getAsInt()) { | ||
return Optional.empty(); | ||
column = newWidth.get(); | ||
continue; | ||
} | ||
column += doc.getWidth(); | ||
} | ||
// Make an additional check that widthBeforeLastBreak fits in the column limit | ||
if (getColumnLimitBeforeLastBreak().isPresent() | ||
&& columnBeforeLastBreak > getColumnLimitBeforeLastBreak().getAsInt()) { | ||
return Optional.empty(); | ||
} | ||
|
||
// Check that the entirety of this level fits on the current line. | ||
float thisWidth = getWidth(docs); | ||
if (state.column() + thisWidth <= maxWidth) { | ||
return Optional.of(state.column() + (int) thisWidth); | ||
if (column <= maxWidth) { | ||
return Optional.of(column); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
@@ -538,6 +546,10 @@ public OpenOp getOpenOp() { | |
return openOp; | ||
} | ||
|
||
/** | ||
* An optional, more restrictive column limit for inner breaks that are marked as {@link Break#hasColumnLimit()}. If | ||
* the level is to be considered one-lineable, the last such break must not start at a column higher than this. | ||
*/ | ||
public OptionalInt getColumnLimitBeforeLastBreak() { | ||
return openOp.columnLimitBeforeLastBreak(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
class B32114928 { | ||
{ | ||
Class<T> tClass = (Class<T>) verifyNotNull((ParameterizedType) getClass().getGenericSuperclass()) | ||
.getActualTypeArguments()[0]; | ||
Class<T> tClass = (Class<T>) | ||
verifyNotNull((ParameterizedType) getClass().getGenericSuperclass()) | ||
.getActualTypeArguments()[0]; | ||
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. Unclear if we want to be so strict with all methods, or if we should exempt non-side-effect-sounding methods like those that start with |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ public class Foo { | |
} | ||
|
||
static String baz(Optional<String> optional) { | ||
return optional.orElseGet( | ||
(String) () -> bar(Optional.of("some thing that is very very very looooooong")).get()); | ||
return optional.orElseGet((String) () -> | ||
bar(Optional.of("some thing that is very very very looooooong")).get()); | ||
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. This changed beause it doesn't allow the dot in the return optional.orElseGet((String) () -> bar(Optional.of("some thing that is very very very loooooooong"))
.get()); |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class PalantirLongMethodChainArg { | ||
void foo() { | ||
return cache.get(ImmutableRequest.builder().group(group).name(artifact).addRepositories("release-jar").build()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class PalantirLongMethodChainArg { | ||
void foo() { | ||
return cache.get(ImmutableRequest.builder() | ||
.group(group) | ||
.name(artifact) | ||
.addRepositories("release-jar") | ||
.build()); | ||
} | ||
} |
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.
This recursion seems like it could end up being pretty expensive
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.
the integration tests take about 15s with this block commented out, and 20s with it as it is, so I think this is fine. (it's pretty much just the extremely degenerate test case that suffers)