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
18 changes: 3 additions & 15 deletions src/printers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,20 +403,8 @@ export function printAssignment(
]);
}

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 printTextBlock(contents: Doc) {
return ['"""', hardline, contents, '"""'];
}

export function embedTextBlock(path: NamedNodePath<SyntaxType.StringLiteral>) {
Expand All @@ -438,7 +426,7 @@ export function embedTextBlock(path: NamedNodePath<SyntaxType.StringLiteral>) {
textToDoc: (text: string, options: Options) => Promise<Doc>
) => {
const doc = await textToDoc(text, { parser: language });
return printTextBlock(path, [escapeDocForTextBlock(doc), hardline]);
return printTextBlock([escapeDocForTextBlock(doc), hardline]);
};
}

Expand Down
1 change: 0 additions & 1 deletion src/printers/lexical-structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export default {
}

return printTextBlock(
path,
join(hardline, textBlockContents(path.node).split("\n"))
);
},
Expand Down
116 changes: 58 additions & 58 deletions test/unit-test/text-blocks/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@ public class TextBlock {

void method() {
String myTextBlock = """
my text
my text


sentence\"""
sentence\"""

""";
""";

String source = """
public void print(%s object) {
System.out.println(Objects.toString(object));
}
""".formatted(type);
public void print(%s object) {
System.out.println(Objects.toString(object));
}
""".formatted(type);

String html = """
<html>\r
<body>\r
<p>Hello, world</p>\r
</body>\r
</html>\r
""";
<html>\r
<body>\r
<p>Hello, world</p>\r
</body>\r
</html>\r
""";

html = """
<html>\r
<body>\r
<p>Hello, world</p>\r
</body>\r
</html>\r
""";
<html>\r
<body>\r
<p>Hello, world</p>\r
</body>\r
</html>\r
""";

System.out.println(
// leading comment
Expand All @@ -54,83 +54,83 @@ public void print(%s object) {
}

String escapes = """
1+1 equals \
2 maybe
""";
1+1 equals \
2 maybe
""";

String escapes = """
\"""var msg = hello world!\""";
""";
\"""var msg = hello world!\""";
""";

String escapes = """
\n\t\r\f\b\s\\
\077
\u0041""";
\n\t\r\f\b\s\\
\077
\u0041""";

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

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

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

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

void method() {
// comment
}
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>
""";
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
""";
}

void typescript() {
// language=typescript
String typescript = """
const s = `""\"`;
""";
const s = `""\"`;
""";

// language=typescript
String typescript = """
const s = ""; // "
""";
const s = ""; // "
""";
}

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