diff options
author | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2019-09-25 01:19:55 +0200 |
---|---|---|
committer | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2019-09-25 01:19:55 +0200 |
commit | 69011b90d8e9d15dbf45479df2f4f08658970a16 (patch) | |
tree | 7fda0f5c5f70428740801cd4e18a71e1a6fd8324 /src/delombok/lombok | |
parent | 230cd667657ab5c1e07819b18294c698bf17f9a5 (diff) | |
download | lombok-69011b90d8e9d15dbf45479df2f4f08658970a16.tar.gz lombok-69011b90d8e9d15dbf45479df2f4f08658970a16.tar.bz2 lombok-69011b90d8e9d15dbf45479df2f4f08658970a16.zip |
[jdk13] Added support for printing text blocks (triple quoted strings) in text block form
Diffstat (limited to 'src/delombok/lombok')
-rwxr-xr-x | src/delombok/lombok/delombok/Delombok.java | 7 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/DelombokResult.java | 13 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/PrettyPrinter.java | 37 |
3 files changed, 48 insertions, 9 deletions
diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java index 76b2715a..7318a8ce 100755 --- a/src/delombok/lombok/delombok/Delombok.java +++ b/src/delombok/lombok/delombok/Delombok.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2018 The Project Lombok Authors. + * Copyright (C) 2009-2019 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -700,11 +700,12 @@ public class Delombok { String[] argv = argsList.toArray(new String[0]); args.init("javac", argv); options.put("diags.legacy", "TRUE"); + options.put("allowStringFolding", "FALSE"); } else { if (modulepath != null && !modulepath.isEmpty()) throw new IllegalStateException("DELOMBOK: Option --module-path requires usage of JDK9 or higher."); } - CommentCatcher catcher = CommentCatcher.create(context); + CommentCatcher catcher = CommentCatcher.create(context, Javac.getJavaCompilerVersion() >= 13); JavaCompiler compiler = catcher.getCompiler(); List<JCCompilationUnit> roots = new ArrayList<JCCompilationUnit>(); @@ -769,7 +770,7 @@ public class Delombok { FormatPreferences fps = new FormatPreferences(formatPrefs); for (JCCompilationUnit unit : roots) { - DelombokResult result = new DelombokResult(catcher.getComments(unit), unit, force || options.isChanged(unit), fps); + DelombokResult result = new DelombokResult(catcher.getComments(unit), catcher.getTextBlockStarts(unit), unit, force || options.isChanged(unit), fps); if (onlyChanged && !result.isChanged() && !options.isChanged(unit)) { if (verbose) feedback.printf("File: %s [%s]\n", unit.sourcefile.getName(), "unchanged (skipped)"); continue; diff --git a/src/delombok/lombok/delombok/DelombokResult.java b/src/delombok/lombok/delombok/DelombokResult.java index 8985b257..bc19c74d 100644 --- a/src/delombok/lombok/delombok/DelombokResult.java +++ b/src/delombok/lombok/delombok/DelombokResult.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2013 The Project Lombok Authors. + * Copyright (C) 2009-2019 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -34,12 +34,14 @@ import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; public class DelombokResult { private final List<CommentInfo> comments; + private final List<Integer> textBlockStarts; private final JCCompilationUnit compilationUnit; private final boolean changed; private final FormatPreferences formatPreferences; - public DelombokResult(List<CommentInfo> comments, JCCompilationUnit compilationUnit, boolean changed, FormatPreferences formatPreferences) { + public DelombokResult(List<CommentInfo> comments, List<Integer> textBlockStarts, JCCompilationUnit compilationUnit, boolean changed, FormatPreferences formatPreferences) { this.comments = comments; + this.textBlockStarts = textBlockStarts; this.compilationUnit = compilationUnit; this.changed = changed; this.formatPreferences = formatPreferences; @@ -61,12 +63,15 @@ public class DelombokResult { } com.sun.tools.javac.util.List<CommentInfo> comments_; + int[] textBlockStarts_; if (comments instanceof com.sun.tools.javac.util.List) comments_ = (com.sun.tools.javac.util.List<CommentInfo>) comments; else comments_ = com.sun.tools.javac.util.List.from(comments.toArray(new CommentInfo[0])); - + textBlockStarts_ = new int[textBlockStarts.size()]; + int idx = 0; + for (int tbs : textBlockStarts) textBlockStarts_[idx++] = tbs; FormatPreferences preferences = new FormatPreferenceScanner().scan(formatPreferences, getContent()); //compilationUnit.accept(new PrettyCommentsPrinter(out, compilationUnit, comments_, preferences)); - compilationUnit.accept(new PrettyPrinter(out, compilationUnit, comments_, preferences)); + compilationUnit.accept(new PrettyPrinter(out, compilationUnit, comments_, textBlockStarts_, preferences)); } private CharSequence getContent() throws IOException { diff --git a/src/delombok/lombok/delombok/PrettyPrinter.java b/src/delombok/lombok/delombok/PrettyPrinter.java index 55210fbb..e2742a27 100644 --- a/src/delombok/lombok/delombok/PrettyPrinter.java +++ b/src/delombok/lombok/delombok/PrettyPrinter.java @@ -31,6 +31,7 @@ import java.io.Writer; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -153,6 +154,7 @@ public class PrettyPrinter extends JCTree.Visitor { private final Writer out; private final JCCompilationUnit compilationUnit; private List<CommentInfo> comments; + private final int[] textBlockStarts; private final FormatPreferences formatPreferences; private final Map<JCTree, String> docComments; @@ -160,9 +162,10 @@ public class PrettyPrinter extends JCTree.Visitor { private int indent = 0; @SuppressWarnings({"unchecked", "rawtypes"}) - public PrettyPrinter(Writer out, JCCompilationUnit cu, List<CommentInfo> comments, FormatPreferences preferences) { + public PrettyPrinter(Writer out, JCCompilationUnit cu, List<CommentInfo> comments, int[] textBlockStarts, FormatPreferences preferences) { this.out = out; this.comments = comments; + this.textBlockStarts = textBlockStarts; this.compilationUnit = cu; this.formatPreferences = preferences; @@ -740,7 +743,37 @@ public class PrettyPrinter extends JCTree.Visitor { } else if (CTC_BOOLEAN.equals(typeTag)) print(((Number)tree.value).intValue() == 1 ? "true" : "false"); else if (CTC_BOT.equals(typeTag)) print("null"); - else print("\"" + quoteChars(tree.value.toString()) + "\""); + else { + if (Arrays.binarySearch(textBlockStarts, tree.pos) < 0) { + print("\"" + quoteChars(tree.value.toString()) + "\""); + } else { + printTextBlock(tree.value.toString()); + } + } + } + + private void printTextBlock(String s) { + println("\"\"\""); + needsAlign = true; + indent++; + StringBuilder sb = new StringBuilder(); + boolean lineStart = true; + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (c != ' ' && c != '\t') lineStart = false; + if (c == '\n') { + println(sb); + sb.setLength(0); + needsAlign = true; + lineStart = true; + continue; + } + if (c == '\t' && lineStart) sb.append("\t"); + else sb.append(quoteChar(s.charAt(i))); + } + print(sb); + print("\"\"\""); + indent--; } @Override public void visitMethodDef(JCMethodDecl tree) { |