Skip to content
Open
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
56 changes: 56 additions & 0 deletions src/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,62 @@ function printTrailingComments(path: NamedNodePath) {
return docs;
}

export function isFirstImportDeclaration(path: AstPath<SyntaxNode>) {
const { node, parent } = path;
return (
node.type === SyntaxType.ImportDeclaration &&
parent?.type === SyntaxType.Program &&
parent.namedChildren.find(
({ type }) => type === SyntaxType.ImportDeclaration
) === node
);
}

export function printImportComments(path: NamedNodePath) {
const header: Doc[] = [];
const leading: Doc[] = [];
if (hasChild(path, "comments")) {
const leadingComments = path.node.comments.filter(
comment => comment.leading
);
const ignoreIndex = leadingComments.findIndex(isPrettierIgnore);
let splitIndex = ignoreIndex === -1 ? leadingComments.length : ignoreIndex;
let stopRow =
ignoreIndex === -1
? path.node.start.row
: leadingComments[ignoreIndex].start.row;

while (
splitIndex > 0 &&
leadingComments[splitIndex - 1].end.row >= stopRow
) {
splitIndex--;
stopRow = Math.min(stopRow, leadingComments[splitIndex].start.row);
}

const headerComments = new Set(leadingComments.slice(0, splitIndex));

path.each(commentPath => {
const { node: comment } = commentPath;
if (!comment.leading) {
return;
}

if (headerComments.has(comment)) {
header.push(printLeadingComment(commentPath));
} else {
leading.push(printLeadingComment(commentPath));
}
}, "comments");
}

return {
header,
leading,
trailing: printTrailingComments(path)
};
}

export function printCommentsSeparately(path: NamedNodePath) {
return {
leading: printLeadingComments(path),
Expand Down
2 changes: 2 additions & 0 deletions src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
canAttachComment,
handleLineComment,
handleRemainingComment,
isFirstImportDeclaration,
isFullyBetweenPrettierIgnore,
isPrettierIgnore
} from "./comments.ts";
Expand Down Expand Up @@ -40,6 +41,7 @@ export default {
);
},
canAttachComment,
willPrintOwnComments: isFirstImportDeclaration,
isBlockComment(node) {
return (node as unknown as CommentNode).type === SyntaxType.BlockComment;
},
Expand Down
41 changes: 27 additions & 14 deletions src/printers/packages-and-modules.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Doc } from "prettier";
import { builders } from "prettier/doc";
import { printImportComments } from "../comments.ts";
import { SyntaxType, type NamedNode } from "../node-types.ts";
import {
printBlock,
Expand Down Expand Up @@ -28,11 +29,20 @@ export default {
const staticImports: { doc: Doc; names: string[] }[] = [];
const imports: { doc: Doc; names: string[] }[] = [];
const otherDeclarations: Doc[] = [];
let importHeaderComments: Doc[] = [];
let hasSeenImport = false;

path.each(child => {
const doc = print(child);

if (child.node.type === SyntaxType.ImportDeclaration) {
let doc = print(child);
if (!hasSeenImport) {
hasSeenImport = true;
const { header, leading, trailing } = printImportComments(child);
importHeaderComments = header;
if (leading.length || trailing.length) {
doc = [leading, doc, trailing];
}
}
const names = extractNames(
child.node.namedChildren[0] as NamedNode<
SyntaxType.Identifier | SyntaxType.ScopedIdentifier
Expand All @@ -53,21 +63,24 @@ export default {
: imports
).push({ doc, names });
} else if (!child.isFirst) {
otherDeclarations.push(doc);
otherDeclarations.push(print(child));
}
}, "namedChildren");

parts.push(
...[moduleImports, staticImports, imports]
.filter(imports => imports.length)
.map(imports =>
join(
hardline,
imports.sort(compareFqn).map(({ doc }) => doc)
)
),
...otherDeclarations
);
const importGroups = [moduleImports, staticImports, imports]
.filter(imports => imports.length)
.map(imports =>
join(
hardline,
imports.sort(compareFqn).map(({ doc }) => doc)
)
);

if (importGroups.length && importHeaderComments.length) {
importGroups[0] = [...importHeaderComments, importGroups[0]];
}

parts.push(...importGroups, ...otherDeclarations);

return [...join([hardline, hardline], parts), hardline];
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.mypackage;

/*-
* License header
*/

import java.lang.Util;
import java.util.Map;
import com.me.Util;

public class Prettier {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.mypackage;

/*-
* License header
*/

import com.me.Util;
import java.lang.Util;
import java.util.Map;

public class Prettier {}
Loading