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
3 changes: 3 additions & 0 deletions src/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ export function canAttachComment(node: SyntaxNode) {
}
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
10 changes: 10 additions & 0 deletions src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
} from "./comments.ts";
import { SyntaxType, type CommentNode, type SyntaxNode } from "./node-types.ts";
import {
embedTextBlock,
hasType,
printComment,
printValue,
type NamedNodePath
Expand All @@ -21,6 +23,11 @@ export default {
? printerForNodeType(path.node.type)(path, print, options, args)
: printValue(path);
},
embed(path) {
return hasType(path, SyntaxType.StringLiteral)
? embedTextBlock(path)
: null;
},
hasPrettierIgnore(path) {
return (
path.node.comments?.some(isPrettierIgnore) === true ||
Expand All @@ -42,6 +49,9 @@ export default {
ownLine: handleLineComment,
endOfLine: handleLineComment,
remaining: handleRemainingComment
},
getVisitorKeys() {
return ["namedChildren"];
}
} satisfies Printer<SyntaxNode>;

Expand Down
122 changes: 114 additions & 8 deletions 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 CommentNode,
Expand All @@ -9,6 +9,7 @@ import {
} from "../node-types.ts";

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

export function hasType<T extends NamedType>(
path: AstPath<NamedNode>,
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: NamedNodePath<SyntaxType.StringLiteral>,
contents: Doc
) {
const parts = ['"""', hardline, contents, '"""'];
const parentType = (path.parent as NamedNode | null)?.type;
const grandparentType = (path.grandparent as NamedNode | 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: NamedNodePath<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: NamedNode<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: NamedNodePath) {
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 NamedNodePrinters = {
Expand Down
28 changes: 7 additions & 21 deletions src/printers/lexical-structure.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { builders } from "prettier/doc";
import { SyntaxType, type NamedNode } from "../node-types.ts";
import { SyntaxType } from "../node-types.ts";
import {
findBaseIndent,
printTextBlock,
printValue,
textBlockContents,
type NamedNodePrinters
} from "./helpers.ts";

Expand All @@ -17,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 NamedNode | null)?.type;
const grandparentType = (path.grandparent as NamedNode | 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
45 changes: 45 additions & 0 deletions test/unit-test/text-blocks/_input.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,49 @@ 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}
""";
}

void java() {
// language=Java
String java = """
class Class{void method() {
// comment
}}
""";
}

void html() {
// language=html
String html = """
<!DOCTYPE html><html><head><title>Page Title</title></head><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>
""";
}

void unsupported() {
// language=unsupported
String unsupported = """
function f(){let i=0;}
""";
}
}
50 changes: 50 additions & 0 deletions test/unit-test/text-blocks/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,54 @@ 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
}""";
}

void java() {
// language=Java
String java = """
class Class {

void method() {
// comment
}
}""";
}

void html() {
// language=html
String html = """
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>""";
}

void unsupported() {
// language=unsupported
String unsupported = """
function f(){let i=0;}
""";
}
}