Skip to content
Closed
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 @@ -49,7 +49,7 @@ class BlocksAndStatementPrettierVisitor {

localVariableDeclarationStatement(ctx) {
const localVariableDeclaration = this.visit(ctx.localVariableDeclaration);
return rejectAndConcat([localVariableDeclaration, ctx.Semicolon[0]]);
return group(rejectAndConcat([localVariableDeclaration, ctx.Semicolon[0]]));
}

localVariableDeclaration(ctx) {
Expand All @@ -58,9 +58,15 @@ class BlocksAndStatementPrettierVisitor {
const variableDeclaratorList = this.visit(ctx.variableDeclaratorList);

return rejectAndJoin(" ", [
rejectAndJoin(" ", variableModifiers),
localVariableType,
variableDeclaratorList
rejectAndJoin(" ", variableModifiers), //@SuppressWarnings("unchecked")
group(
concat([
softline,
localVariableType,
" ", //V[][]
variableDeclaratorList
])
) //tmpArray = (V[][]) new Object[rowList{BREKALINE}.size()][columnList.size()]
]);
}

Expand Down
13 changes: 7 additions & 6 deletions packages/prettier-plugin-java/src/printers/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,13 @@ class ClassesPrettierVisitor {
const variableDeclaratorId = this.visit(ctx.variableDeclaratorId);
if (ctx.Equals) {
const variableInitializer = this.visit(ctx.variableInitializer);
return rejectAndJoin(" ", [
variableDeclaratorId,
ctx.Equals[0],
variableInitializer
]);
return group(
rejectAndJoin(" ", [
variableDeclaratorId,
ctx.Equals[0],
variableInitializer
])
);
}
return variableDeclaratorId;
}
Expand Down Expand Up @@ -438,7 +440,6 @@ class ClassesPrettierVisitor {
if (ctx.block) {
return this.visit(ctx.block);
}

return getImageWithComments(this.getSingle(ctx));
}

Expand Down
83 changes: 48 additions & 35 deletions packages/prettier-plugin-java/src/printers/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
concat,
group,
indent,
dedent,
getImageWithComments
} = require("./prettier-builder");
const {
Expand Down Expand Up @@ -228,7 +229,7 @@ class ExpressionsPrettierVisitor {
: [];
return rejectAndConcat([
rejectAndConcat(unaryPrefixOperator),
primary,
primary, //System.out.println("..."); //userRepository.findAll().collectList().block().size();
rejectAndConcat(unarySuffixOperator)
]);
}
Expand All @@ -252,43 +253,54 @@ class ExpressionsPrettierVisitor {

primary(ctx) {
const primaryPrefix = this.visit(ctx.primaryPrefix);
const primarySuffixes = this.mapVisit(ctx.primarySuffix);

const suffixes = [];
let addIndent = false;
for (let i = 0; i < primarySuffixes.length; i++) {
if (ctx.primarySuffix[i].children.Dot !== undefined) {
suffixes.push(indent(softline), primarySuffixes[i]);
addIndent = true;
} else if (
ctx.primarySuffix[i].children.methodInvocationSuffix === undefined
) {
suffixes.push(softline, primarySuffixes[i]);
} else {
if (addIndent) {
suffixes.push(indent(primarySuffixes[i]));
addIndent = false;

if (ctx.primarySuffix !== undefined) {
const primarySuffixes = this.mapVisit(ctx.primarySuffix);

const separators = [];

ctx.primarySuffix.forEach(primarySuffix => {
if (primarySuffix.children.Dot !== undefined) {
separators.push(softline);
} else if (
primarySuffix.children.classLiteralSuffix !== undefined &&
primarySuffix.children.classLiteralSuffix[0].children.Dot !==
undefined
) {
separators.push(softline);
} else {
suffixes.push(primarySuffixes[i]);
separators.push("");
}
});

let firstSeparator = separators.shift();

if (ctx.primaryPrefix[0].children.This !== undefined) {
firstSeparator = "";
}
}
// console.log("=====================");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets not merge commented out code 😄

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is still WIP because I am still trying to fix 2 bugs and I still need the printing.

// console.log(ctx.primaryPrefix[0]);
// console.log("---------------------");
// console.log(primarySuffixes[0]);
// console.log("size -> " + primarySuffixes.length);
// console.log("=====================");

let firstSeparator = suffixes.shift();
if (
ctx.primaryPrefix[0].children.This !== undefined ||
firstSeparator === undefined
) {
firstSeparator = "";
return group(
indent(
concat([
// comment below overtakes priority
// primarySuffixes.length > 1 ? primaryPrefix : group(primaryPrefix), // System.out.println // "..." //userRepostiory.findAll()
primaryPrefix,
rejectAndConcat([
firstSeparator,
rejectAndJoinSeps(separators, primarySuffixes) // .collectList().block().size()
])
])
)
);
}

return group(
rejectAndConcat([
primaryPrefix,
firstSeparator,
rejectAndConcat(suffixes)
])
);
return primaryPrefix;
}

primaryPrefix(ctx) {
Expand Down Expand Up @@ -323,12 +335,14 @@ class ExpressionsPrettierVisitor {
const fqnOrRefTypePart = this.mapVisit(ctx.fqnOrRefTypePart);
const dims = this.visit(ctx.dims);
const dots = ctx.Dot ? ctx.Dot : [];
return rejectAndConcat([rejectAndJoinSeps(dots, fqnOrRefTypePart), dims]);
return rejectAndConcat([
rejectAndJoinSeps(dots, fqnOrRefTypePart, "", softline),
dims
]);
}

fqnOrRefTypePart(ctx) {
const annotation = this.mapVisit(ctx.annotation);

let fqnOrRefTypePart$methodTypeArguments = "";
if (
ctx.$methodTypeArguments &&
Expand All @@ -346,7 +360,6 @@ class ExpressionsPrettierVisitor {
} else {
keyWord = ctx.Super[0];
}

let fqnOrRefTypePart$classTypeArguments = "";
if (
ctx.$classTypeArguments &&
Expand Down
12 changes: 8 additions & 4 deletions packages/prettier-plugin-java/src/printers/printer-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ function buildFqn(tokens, dots) {
return rejectAndJoinSeps(dots ? dots : [], tokens);
}

function rejectAndJoinSeps(sepTokens, elems, sep) {
function rejectAndJoinSeps(sepTokens, elems, sepAfter = "", sepBefore = "") {
if (!Array.isArray(sepTokens)) {
return rejectAndJoin(sepTokens, elems);
}
const actualElements = reject(elems);
const res = [];

for (let i = 0; i < sepTokens.length; i++) {
res.push(actualElements[i], sepTokens[i]);
if (sep) {
res.push(sep);
res.push(actualElements[i]);
if (sepBefore) {
res.push(sepBefore);
}
res.push(sepTokens[i]);
if (sepAfter) {
res.push(sepAfter);
}
}
res.push(...actualElements.slice(sepTokens.length));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,8 @@ private ArrayTable(
rowKeyToIndex = Maps.indexMap(rowList);
columnKeyToIndex = Maps.indexMap(columnList);

@SuppressWarnings(
"unchecked"
) V[][] tmpArray = (V[][]) new Object[rowList.size()][columnList.size()];
@SuppressWarnings("unchecked")
V[][] tmpArray = (V[][]) new Object[rowList.size()][columnList.size()];
array = tmpArray;

// Necessary because in GWT the arrays are initialized with "undefined" instead of null.
Expand All @@ -170,9 +169,8 @@ private ArrayTable(ArrayTable<R, C, V> table) {
columnList = table.columnList;
rowKeyToIndex = table.rowKeyToIndex;
columnKeyToIndex = table.columnKeyToIndex;
@SuppressWarnings(
"unchecked"
) V[][] copy = (V[][]) new Object[rowList.size()][columnList.size()];
@SuppressWarnings("unchecked")
V[][] copy = (V[][]) new Object[rowList.size()][columnList.size()];
array = copy;
for (int i = 0; i < rowList.size(); i++) {
System.arraycopy(table.array[i], 0, copy[i], 0, table.array[i].length);
Expand Down Expand Up @@ -246,11 +244,8 @@ protected Entry<K, V> get(final int index) {

@Override
Spliterator<Entry<K, V>> entrySpliterator() {
return CollectSpliterators.indexed(
size(),
Spliterator.ORDERED,
this::getEntry
);
return CollectSpliterators
.indexed(size(), Spliterator.ORDERED, this::getEntry);
}

// TODO(lowasser): consider an optimized values() implementation
Expand Down Expand Up @@ -360,11 +355,9 @@ public V set(int rowIndex, int columnIndex, @Nullable V value) {
*/
@GwtIncompatible // reflection
public V[][] toArray(Class<V> valueClass) {
@SuppressWarnings("unchecked") V[][] copy = (V[][]) Array.newInstance( // TODO: safe?
valueClass,
rowList.size(),
columnList.size()
);
@SuppressWarnings("unchecked") // TODO: safe?
V[][] copy = (V[][]) Array
.newInstance(valueClass, rowList.size(), columnList.size());
for (int i = 0; i < rowList.size(); i++) {
System.arraycopy(array[i], 0, copy[i], 0, array[i].length);
}
Expand Down Expand Up @@ -558,10 +551,11 @@ protected Cell<R, C, V> get(final int index) {

@Override
Spliterator<Cell<R, C, V>> cellSpliterator() {
return CollectSpliterators.indexed(
size(),
Spliterator.ORDERED | Spliterator.NONNULL | Spliterator.DISTINCT,
this::getCell
return CollectSpliterators
.indexed(
size(),
Spliterator.ORDERED | Spliterator.NONNULL | Spliterator.DISTINCT,
this::getCell
);
}

Expand Down Expand Up @@ -799,11 +793,8 @@ protected V get(int index) {

@Override
Spliterator<V> valuesSpliterator() {
return CollectSpliterators.indexed(
size(),
Spliterator.ORDERED,
this::getValue
);
return CollectSpliterators
.indexed(size(), Spliterator.ORDERED, this::getValue);
}

private static final long serialVersionUID = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public void doSomethingLongNew2() {
}

public void doSomethingLongStatic() {
return Object.something()
return Object
.something()
.more()
.and()
.that()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
public class Variables {
public static int STATIC_VARIABLE = 123;
private static final Logger LOGGER = LoggerFactory.getLogger(
ComplexFilterTest.class
);
private static final Logger LOGGER = LoggerFactory
.getLogger(ComplexFilterTest.class);

int packageVariable = 234;
private float privateVariable = 0.9f;
Expand Down