From 6258570ec8f96bc7e7983fed24b5cda8ed5a896a Mon Sep 17 00:00:00 2001 From: Jordan Kiesel Date: Sat, 5 Aug 2023 22:39:40 -0600 Subject: [PATCH] feat: adjust text block indentation closes #593 --- .../src/printers/lexical-structure.ts | 17 +++++- .../test/unit-test/text-blocks/_input.java | 21 ++++++++ .../test/unit-test/text-blocks/_output.java | 52 +++++++++++++------ 3 files changed, 73 insertions(+), 17 deletions(-) diff --git a/packages/prettier-plugin-java/src/printers/lexical-structure.ts b/packages/prettier-plugin-java/src/printers/lexical-structure.ts index b9808daf..9f74e4e2 100644 --- a/packages/prettier-plugin-java/src/printers/lexical-structure.ts +++ b/packages/prettier-plugin-java/src/printers/lexical-structure.ts @@ -1,4 +1,5 @@ import { printTokenWithComments } from "./comments/format-comments"; +import { join } from "./prettier-builder"; import { BaseCstPrettierPrinter } from "../base-cst-printer"; import { BooleanLiteralCtx, @@ -7,10 +8,24 @@ import { IToken, LiteralCtx } from "java-parser"; +import { builders } from "prettier/doc"; + +const { hardline } = builders; export class LexicalStructurePrettierVisitor extends BaseCstPrettierPrinter { literal(ctx: LiteralCtx) { - if (ctx.CharLiteral || ctx.TextBlock || ctx.StringLiteral || ctx.Null) { + if (ctx.TextBlock) { + const lines = ctx.TextBlock[0].image.split("\n"); + const open = lines.shift()!; + const baseIndent = Math.min( + ...lines.map(line => line.search(/\S/)).filter(indent => indent >= 0) + ); + return join(hardline, [ + open, + ...lines.map(line => line.slice(baseIndent)) + ]); + } + if (ctx.CharLiteral || ctx.StringLiteral || ctx.Null) { return printTokenWithComments(this.getSingle(ctx) as IToken); } return this.visitSingle(ctx); diff --git a/packages/prettier-plugin-java/test/unit-test/text-blocks/_input.java b/packages/prettier-plugin-java/test/unit-test/text-blocks/_input.java index b04b7499..d404faf7 100644 --- a/packages/prettier-plugin-java/test/unit-test/text-blocks/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/text-blocks/_input.java @@ -24,6 +24,27 @@ public void print(%s object) { \r \r """; + + System.out.println( + // leading comment + """ + abaoeu + euaoeu + aoeu + + oaeu + abc""" // trailing comment + ); + + System.out.println( + """ + abaoeu + euaoeu +aoeu + + oaeu + abc""" + ); } } diff --git a/packages/prettier-plugin-java/test/unit-test/text-blocks/_output.java b/packages/prettier-plugin-java/test/unit-test/text-blocks/_output.java index 7b32aff6..89bff6d8 100644 --- a/packages/prettier-plugin-java/test/unit-test/text-blocks/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/text-blocks/_output.java @@ -1,30 +1,50 @@ public class TextBlock { void method() { - String myTextBlock = """ - my text + String myTextBlock = + """ + 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 = """ - \r - \r -

Hello, world

\r - \r - \r - """; + \r + \r +

Hello, world

\r + \r + \r + """; + + System.out.println( + // leading comment + """ + abaoeu + euaoeu + aoeu + + oaeu + abc""" // trailing comment + ); + + System.out.println( + """ + abaoeu + euaoeu + aoeu + + oaeu + abc""" + ); } }