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
17 changes: 14 additions & 3 deletions src/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export function canAttachComment(node: SyntaxNode, ancestors: SyntaxNode[]) {
switch (node.type) {
case SyntaxType.EnumBodyDeclarations:
case SyntaxType.EscapeSequence:
case SyntaxType.FormalParameters:
case SyntaxType.Modifier:
case SyntaxType.MultilineStringFragment:
case SyntaxType.Program:
Expand Down Expand Up @@ -113,6 +112,7 @@ export function handleLineComment(
) {
return [
handleBinaryExpressionComments,
handleFormalParametersComments,
handleFqnOrRefTypeComments,
handleIfStatementComments,
handleJumpStatementComments,
Expand Down Expand Up @@ -158,6 +158,19 @@ function handleBinaryExpressionComments(
return false;
}

function handleFormalParametersComments(commentNode: CommentNode) {
const { enclosingNode, precedingNode, followingNode } = commentNode;
if (
enclosingNode?.type === SyntaxType.FormalParameters &&
!precedingNode &&
!followingNode
) {
util.addDanglingComment(enclosingNode, commentNode, undefined);
return true;
}
return false;
}

function handleFqnOrRefTypeComments(commentNode: CommentNode) {
const { enclosingNode, followingNode } = commentNode;
if (
Expand Down Expand Up @@ -368,7 +381,6 @@ type PrettierIgnoreRange = {

function printLeadingComment(path: AstPath<CommentNode>) {
const comment = path.node;
comment.printed = true;
const parts: Doc[] = [printComment(comment)];

const originalText = path.root.value;
Expand Down Expand Up @@ -410,7 +422,6 @@ function printTrailingComment(
previousComment?: { doc: Doc; isBlock: boolean; hasLineSuffix: boolean }
): NonNullable<typeof previousComment> {
const comment = path.node;
comment.printed = true;
const printed = printComment(comment);

const originalText = path.root.value;
Expand Down
17 changes: 7 additions & 10 deletions src/printers/blocks-and-statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export default {
}

const danglingComments = printDanglingComments(path);
if (danglingComments.length) {
statement.push(hardline, ...danglingComments, hardline);
if (danglingComments) {
statement.push(hardline, danglingComments, hardline);
} else {
const ifHasBlock = path.node.consequenceNode.type === SyntaxType.Block;
statement.push(ifHasBlock ? " " : hardline);
Expand Down Expand Up @@ -212,10 +212,6 @@ export default {

for_statement(path, print) {
const danglingComments = printDanglingComments(path);
if (danglingComments.length) {
danglingComments.push(hardline);
}

const hasInit = path.node.initNodes.length > 0;
const hasCondition = hasChild(path, "conditionNode");
const hasUpdate = path.node.updateNodes.length > 0;
Expand All @@ -231,7 +227,7 @@ export default {

const hasEmptyStatement = path.node.bodyNode.type === ";";
const parts = [
...danglingComments,
danglingComments && [danglingComments, hardline],
"for ",
hasInit || hasCondition || hasUpdate
? group(indentInParentheses(join(line, expressions)))
Expand All @@ -247,14 +243,15 @@ export default {
},

enhanced_for_statement(path, print) {
const forStatement = printDanglingComments(path);
forStatement.push(
const danglingComments = printDanglingComments(path);
const forStatement = [
danglingComments && [danglingComments, hardline],
"for (",
...printModifiers(path, print),
path.call(print, "typeNode"),
" ",
path.call(print, "nameNode")
);
];

if (hasChild(path, "dimensionsNode")) {
forStatement.push(path.call(print, "dimensionsNode"));
Expand Down
38 changes: 29 additions & 9 deletions src/printers/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import {
printBlock,
printBlockStatements,
printBodyDeclarations,
printDanglingComments,
printModifiers,
printTypeParameters,
printValue,
printVariableDeclaration,
type NamedNodePath,
type NamedNodePrinters
} from "./helpers.ts";

const { group, hardline, indent, join, line } = builders;
const { group, hardline, indent, join, line, softline } = builders;

export default {
class_declaration(path, print) {
Expand Down Expand Up @@ -165,14 +167,16 @@ export default {
},

formal_parameters(path, print) {
return indentInParentheses(
join(
[",", line],
(path.parent as NamedNode | null)?.type === SyntaxType.RecordDeclaration
? printBodyDeclarations(path, print)
: path.map(print, "namedChildren")
)
);
const parameters =
(path.parent as NamedNode | null)?.type === SyntaxType.RecordDeclaration
? printBodyDeclarations(path, print)
: path.map(print, "namedChildren");

if (parameters.length === 0) {
return ["(", printDanglingCommentsInList(path), ")"];
}

return indentInParentheses(join([",", line], parameters));
},

formal_parameter(path, print) {
Expand Down Expand Up @@ -440,3 +444,19 @@ const indexByModifier = [
"non-sealed",
"strictfp"
].reduce((map, name, index) => map.set(name, index), new Map<string, number>());

function printDanglingCommentsInList(path: NamedNodePath) {
const { node } = path;

return node.comments?.some(comment => !(comment.leading || comment.trailing))
? [
indent([softline, printDanglingComments(path)]),
node.comments.some(
({ type, leading, trailing }) =>
!(leading || trailing) && type === SyntaxType.LineComment
)
? hardline
: softline
]
: "";
}
6 changes: 3 additions & 3 deletions src/printers/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ function printLambdaExpressionSignature(
}

const dangling = printDanglingComments(path);
if (dangling.length) {
if (dangling) {
parts.push(" ", dangling);
}
return parts;
Expand Down Expand Up @@ -980,9 +980,9 @@ function printMemberChain(
}

const danglingComments = printDanglingComments(path);
if (danglingComments.length) {
if (danglingComments) {
printedNodes[0].printed = [
...danglingComments,
danglingComments,
hardline,
printedNodes[0].printed
];
Expand Down
52 changes: 31 additions & 21 deletions src/printers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,35 @@ export function lineEndWithComments(node: SyntaxNode) {
: node.end.row;
}

export function printDanglingComments(path: NamedNodePath) {
if (!path.node.comments?.length) {
return [];
export function printDanglingComments(
path: NamedNodePath,
danglingCommentsPrintOptions: { indent?: boolean } = {}
): Doc {
const { indent: shouldIndent = false } = danglingCommentsPrintOptions;
const danglingComments = new Set(
path.node.comments?.filter(
comment => !(comment.leading || comment.trailing)
)
);

if (danglingComments.size === 0) {
return "";
}
const comments: Doc[] = [];
path.each(commentPath => {
const comment = commentPath.node;
if (comment.leading || comment.trailing) {
return;
}
comment.printed = true;
comments.push(printComment(comment));
}, "comments");
return join(hardline, comments);

const parts = path
.map(
({ node: comment }) =>
danglingComments.has(comment) ? printComment(comment) : "",
"comments"
)
.filter(Boolean);

const doc = join(hardline, parts);
return shouldIndent ? indent([hardline, doc]) : doc;
}

export function printComment(comment: CommentNode) {
comment.printed = true;
const lines = comment.value.split("\n").map(line => line.trim());
return lines.length > 1 &&
lines[0].startsWith("/*") &&
Expand All @@ -129,7 +141,7 @@ export function hasLeadingComments(node: SyntaxNode) {
}

export function indentInParentheses(contents: Doc) {
return !Array.isArray(contents) || contents.length
return (contents && !Array.isArray(contents)) || contents.length
? ["(", indent([softline, contents]), softline, ")"]
: "()";
}
Expand All @@ -142,10 +154,8 @@ export function printArrayInitializer(
options: JavaParserOptions
) {
if (!path.node.namedChildren.length) {
const danglingComments = printDanglingComments(path);
return danglingComments.length
? ["{", indent([hardline, ...danglingComments]), hardline, "}"]
: "{}";
const danglingComments = printDanglingComments(path, { indent: true });
return danglingComments ? ["{", danglingComments, hardline, "}"] : "{}";
}

const list = join([",", line], path.map(print, "namedChildren"));
Expand All @@ -166,9 +176,9 @@ export function printBlock(path: NamedNodePath, contents: Doc[]) {
"}"
]);
}
const danglingComments = printDanglingComments(path);
if (danglingComments.length) {
return ["{", indent([hardline, ...danglingComments]), hardline, "}"];
const danglingComments = printDanglingComments(path, { indent: true });
if (danglingComments) {
return ["{", danglingComments, hardline, "}"];
}
const parent = path.parent;
const grandparent = path.grandparent;
Expand Down
2 changes: 1 addition & 1 deletion src/printers/packages-and-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { group, hardline, indent, join, line } = builders;
export default {
program(path, print) {
if (!path.node.namedChildren.length) {
return [...printDanglingComments(path), hardline];
return [printDanglingComments(path), hardline];
}

const parts: Doc[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,52 +48,64 @@ class J {

void one() {}

void two() // alpha
{}
void two(
// alpha
) {}

void three() // alpha
// beta
{}
void three(
// alpha
// beta
) {}

void four() // alpha
// beta
/* gamma */
{}
void four(
// alpha
// beta
/* gamma */
) {}

void five() {} // alpha
void five(
// alpha
) {}

void fiveBis() { // alpha
void fiveBis(
// alpha
) {
int i;
}

void six /* alpha */() {}
void six(/* alpha */) {}

void seven() /* alpha */
/* beta */
{}
void seven(
/* alpha */
/* beta */
) {}

void eight() /* alpha */
// beta
{}
void eight(
/* alpha */
// beta
) {}

void nine() /* alpha */
{}
void nine(/* alpha */) {}

void one(String one) {}

void two(String one) // alpha
{}
void two(
String one
// alpha
) {}

void three(String one) // alpha
// beta
{}
void three(
String one
// alpha
// beta
) {}

void four(
// alpha
String one
) // beta
/* gamma */
{}
// beta
/* gamma */
) {}

void five(
String one // alpha
Expand All @@ -104,15 +116,17 @@ void six(String one /* alpha */) {}
void seven(
/* alpha */
String one
) /* beta */
{}
/* beta */
) {}

void eight(
/* alpha */
String one
) // beta
{}
// beta
) {}

void nine(String one) /* alpha */
{}
void nine(
String one
/* alpha */
) {}
}
Loading