Skip to content
Draft
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
3 changes: 3 additions & 0 deletions packages/prettier-plugin-java/src/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ export function canAttachComment(node: JavaNode) {
}
switch (node.type) {
case SyntaxType.EnumBodyDeclarations:
case SyntaxType.EscapeSequence:
case SyntaxType.FormalParameters:
case SyntaxType.Modifier:
case SyntaxType.MultilineStringFragment:
case SyntaxType.ParenthesizedExpression:
case SyntaxType.Program:
case SyntaxType.StringFragment:
case SyntaxType.Visibility:
return false;
default:
Expand Down
13 changes: 12 additions & 1 deletion packages/prettier-plugin-java/src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import {
willPrintOwnComments
} from "./comments.js";
import {
embedTextBlock,
hasType,
printComment,
printValue,
type JavaComment,
type JavaNode,
type JavaNodePath
type JavaNodePath,
type JavaNodeType
} from "./printers/helpers.js";
import { printerForNodeType } from "./printers/index.js";
import { SyntaxType } from "./tree-sitter-java.js";
Expand All @@ -23,6 +26,11 @@ export default {
? printerForNodeType(path.node.type)(path, print, options, args)
: printValue(path);
},
embed(path: JavaNodePath<JavaNodeType>) {
return hasType(path, SyntaxType.StringLiteral)
? embedTextBlock(path)
: null;
},
hasPrettierIgnore(path) {
return (
path.node.comments?.some(isPrettierIgnore) === true ||
Expand All @@ -44,6 +52,9 @@ export default {
ownLine: handleLineComment,
endOfLine: handleLineComment,
remaining: handleRemainingComment
},
getVisitorKeys() {
return ["namedChildren"];
}
} satisfies Printer<JavaNode>;

Expand Down
122 changes: 114 additions & 8 deletions packages/prettier-plugin-java/src/printers/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AstPath, Doc, ParserOptions } from "prettier";
import { builders } from "prettier/doc";
import type { AstPath, Doc, Options, ParserOptions } from "prettier";
import { builders, utils } from "prettier/doc";
import {
SyntaxType,
type NodeOfType,
Expand All @@ -9,6 +9,7 @@ import {
} from "../tree-sitter-java.js";

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

export function hasType<T extends JavaTypeString>(
path: AstPath<JavaNode>,
Expand Down Expand Up @@ -315,12 +316,117 @@ export function printVariableDeclaration(
return declaration;
}

export function findBaseIndent(lines: string[]) {
return lines.length
? Math.min(
...lines.map(line => line.search(/\S/)).filter(indent => indent >= 0)
)
: 0;
export function printTextBlock(
path: JavaNodePath<SyntaxType.StringLiteral>,
contents: Doc
) {
const parts = ['"""', hardline, contents, '"""'];
const parentType = (path.parent as JavaNode | null)?.type;
const grandparentType = (path.grandparent as JavaNode | null)?.type;
return parentType === SyntaxType.AssignmentExpression ||
parentType === SyntaxType.VariableDeclarator ||
(path.node.fieldName === "object" &&
(grandparentType === SyntaxType.AssignmentExpression ||
grandparentType === SyntaxType.VariableDeclarator))
? indent(parts)
: parts;
}

export function embedTextBlock(path: JavaNodePath<SyntaxType.StringLiteral>) {
const hasInterpolations = path.node.namedChildren.some(
({ type }) => type === SyntaxType.StringInterpolation
);
if (hasInterpolations || path.node.children[0].value === '"') {
return null;
}

const language = findEmbeddedLanguage(path);
if (!language) {
return null;
}

const text = unescapeTextBlockContents(textBlockContents(path.node));

return async (
textToDoc: (text: string, options: Options) => Promise<Doc>
) => {
const doc = await textToDoc(text, { parser: language });
return printTextBlock(path, escapeDocForTextBlock(doc));
};
}

export function textBlockContents(node: JavaNode<SyntaxType.StringLiteral>) {
const lines = node.value
.replace(
/(?<=^|[^\\])((?:\\\\)*)\\u+([0-9a-fA-F]{4})/g,
(_, backslashPairs: string, hex: string) =>
backslashPairs + String.fromCharCode(parseInt(hex, 16))
)
.split("\n")
.slice(1);
const baseIndent = findBaseIndent(lines);
return lines
.map(line => line.slice(baseIndent))
.join("\n")
.slice(0, -3);
}

function findBaseIndent(lines: string[]) {
return Math.min(
...lines.map(line => line.search(/\S/)).filter(indent => indent >= 0)
);
}

function findEmbeddedLanguage(path: JavaNodePath) {
return path.ancestors
.find(
({ type, comments }) =>
type === SyntaxType.Block || comments?.some(({ leading }) => leading)
)
?.comments?.filter(({ leading }) => leading)
.map(
({ value }) => value.match(/^(?:\/\/|\/\*)\s*language\s*=\s*(\S+)/)?.[1]
)
.findLast(language => language)
?.toLowerCase();
}

function escapeDocForTextBlock(doc: Doc) {
return mapDoc(doc, currentDoc =>
typeof currentDoc === "string"
? currentDoc.replace(/\\|"""/g, match => `\\${match}`)
: currentDoc
);
}

function unescapeTextBlockContents(text: string) {
return text.replace(
/\\(?:([bstnfr"'\\])|\n|\r\n?|([0-3][0-7]{0,2}|[0-7]{1,2}))/g,
(_, single, octal) => {
if (single) {
switch (single) {
case "b":
return "\b";
case "s":
return " ";
case "t":
return "\t";
case "n":
return "\n";
case "f":
return "\f";
case "r":
return "\r";
default:
return single;
}
} else if (octal) {
return String.fromCharCode(parseInt(octal, 8));
} else {
return "";
}
}
);
}

export type JavaNode<T extends JavaTypeString = JavaTypeString> =
Expand Down
27 changes: 6 additions & 21 deletions packages/prettier-plugin-java/src/printers/lexical-structure.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { builders } from "prettier/doc";
import { SyntaxType } from "../tree-sitter-java.js";
import {
findBaseIndent,
printTextBlock,
printValue,
type JavaNode,
textBlockContents,
type JavaNodePrinters
} from "./helpers.js";

Expand All @@ -18,25 +18,10 @@ export default {
return path.map(print, "children");
}

const lines = path.node.children
.map(({ value }) => value)
.join("")
.split("\n")
.slice(1);
const baseIndent = findBaseIndent(lines);
const textBlock = join(hardline, [
'"""',
...lines.map(line => line.slice(baseIndent))
]);
const parentType = (path.parent as JavaNode | null)?.type;
const grandparentType = (path.grandparent as JavaNode | null)?.type;
return parentType === SyntaxType.AssignmentExpression ||
parentType === SyntaxType.VariableDeclarator ||
(path.node.fieldName === "object" &&
(grandparentType === SyntaxType.AssignmentExpression ||
grandparentType === SyntaxType.VariableDeclarator))
? indent(textBlock)
: textBlock;
return printTextBlock(
path,
join(hardline, textBlockContents(path.node).split("\n"))
);
},

string_fragment: printValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,26 @@ public void print(%s object) {
);
}

void json() {
// language = json
String someJson = """
{"glossary":{"title": "example glossary"}}
""";

// language=json
String config = """
{ "name":"example",
"enabled" :true,
"timeout":30}
""";

/* language = JSON */
String query = """
{
"sql":"SELECT * FROM users \
WHERE active=1 \
AND deleted=0",
"limit":10}
""";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,21 @@ public void print(%s object) {
abc"""
);
}

void json() {
// language = json
String someJson = """
{ "glossary": { "title": "example glossary" } }""";

// language=json
String config = """
{ "name": "example", "enabled": true, "timeout": 30 }""";

/* language = JSON */
String query = """
{
"sql": "SELECT * FROM users WHERE active=1 AND deleted=0",
"limit": 10
}""";
}
}