Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ class BlocksAndStatementPrettierVisitor {
delete newStatement.leadingComments;
}

newLabelStatement.leadingComments = labeledStatementLeadingComments;
if (labeledStatementLeadingComments.length !== 0) {
newLabelStatement.leadingComments = labeledStatementLeadingComments;
}
newLabelStatement.children.Colon[0] = newColon;
newLabelStatement.children.statement[0] = newStatement;

Expand Down
32 changes: 24 additions & 8 deletions packages/prettier-plugin-java/src/printers/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,34 @@ function getNodeLeadingComments(node) {
function getLeadingComments(nodeOrToken, location) {
const arr = [];
if (Object.prototype.hasOwnProperty.call(nodeOrToken, "leadingComments")) {
nodeOrToken.leadingComments.forEach(comment => {
let previousEndLine = nodeOrToken.leadingComments[0].endLine;
let step;
arr.push(concat(formatComment(nodeOrToken.leadingComments[0])));
for (let i = 1; i < nodeOrToken.leadingComments.length; i++) {
step = nodeOrToken.leadingComments[i].startLine - previousEndLine;
if (
comment.tokenType.name === "LineComment" ||
comment.endLine !== location.startLine ||
comment.startOffset > location.startOffset
step === 1 ||
nodeOrToken.leadingComments[i].startOffset > location.startOffset
) {
arr.push(concat(formatComment(comment)));
arr.push(hardline);
} else {
arr.push(concat(formatComment(comment)));
} else if (step > 1) {
arr.push(hardline, hardline);
}
});

arr.push(concat(formatComment(nodeOrToken.leadingComments[i])));
previousEndLine = nodeOrToken.leadingComments[i].endLine;
}

step = location.startLine - previousEndLine;
if (
step === 1 ||
nodeOrToken.leadingComments[nodeOrToken.leadingComments.length - 1]
.startOffset > location.startOffset
) {
arr.push(hardline);
} else if (step > 1) {
arr.push(hardline, hardline);
}
}

return arr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ class PackagesAndModulesPrettierVisitor {
const compilationUnit =
ctx.ordinaryCompilationUnit || ctx.modularCompilationUnit;

// Do not add additional line if only comments in file
const additionalLine = isNaN(compilationUnit[0].location.startOffset)
? ""
: line;

return concat([this.visit(compilationUnit[0]), additionalLine]);
return concat([this.visit(compilationUnit[0]), line]);
}

ordinaryCompilationUnit(ctx) {
Expand Down
13 changes: 10 additions & 3 deletions packages/prettier-plugin-java/src/printers/printer-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,17 @@ function getBlankLinesSeparator(ctx) {

const separators = [];
for (let i = 0; i < ctx.length - 1; i++) {
const previousRuleEndLine = ctx[i].location.endLine;
const nextRuleStartLine = ctx[i + 1].location.startLine;
const previousRuleEndLineWithComment =
ctx[i].trailingComments !== undefined
? ctx[i].trailingComments[ctx[i].trailingComments.length - 1].endLine
: ctx[i].location.endLine;

if (nextRuleStartLine > previousRuleEndLine + 1) {
const nextRuleStartLineWithComment =
ctx[i + 1].leadingComments !== undefined
? ctx[i + 1].leadingComments[0].startLine
: ctx[i + 1].location.startLine;

if (nextRuleStartLineWithComment - previousRuleEndLineWithComment > 1) {
separators.push(concat([hardline, hardline]));
} else {
separators.push(hardline);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public void shouldHandleBlankLinesInBlock() {
int l = 4;

int m = 4;

// Add a line before comment
int n = 4;
for (int p = 0; p < 3; p++);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.common.collect;

import static com.google.common.base.Preconditions.checkArgument;
Expand Down Expand Up @@ -113,6 +114,7 @@ public static <R, C, V> ArrayTable<R, C, V> create(
* custom serialization logic is needed to support different enum sizes during
* serialization and deserialization.
*/

/**
* Creates an {@code ArrayTable} with the mappings in the provided table.
*
Expand Down Expand Up @@ -174,7 +176,6 @@ private ArrayTable(
)
V[][] tmpArray = (V[][]) new Object[rowList.size()][columnList.size()];
array = tmpArray;

// Necessary because in GWT the arrays are initialized with "undefined" instead of null.
eraseAll();
}
Expand Down Expand Up @@ -272,6 +273,7 @@ Spliterator<Entry<K, V>> entrySpliterator() {
}

// TODO(lowasser): consider an optimized values() implementation

@Override
public boolean containsKey(@Nullable Object key) {
return keyIndex.containsKey(key);
Expand Down Expand Up @@ -492,6 +494,7 @@ public V put(R rowKey, C columnKey, @Nullable V value) {
* TODO(jlevy): Consider creating a merge() method, similar to putAll() but
* copying non-null values only.
*/

/**
* {@inheritDoc}
*
Expand Down Expand Up @@ -544,6 +547,7 @@ public V erase(@Nullable Object rowKey, @Nullable Object columnKey) {
}

// TODO(jlevy): Add eraseRow and eraseColumn methods?

@Override
public int size() {
return rowList.size() * columnList.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public void myMethod(
) {
// @formatter:off
System.out.println("This operation with two very long string should not break because the formatter is off");

// @formatter:on
System.out.println(
"This operation with two very long string should break because the formatter is on"
Expand Down