8252172: Improve prettiness of printing HTML attributes by DocPretty

Reviewed-by: jjg
This commit is contained in:
Pavel Rappo 2020-08-28 15:26:34 +01:00
parent 7df86108cb
commit 26b48999df
5 changed files with 52 additions and 21 deletions

View File

@ -55,8 +55,8 @@ public interface AttributeTree extends DocTree {
Name getName();
/**
* Returns the kind of the attribute.
* @return the kind of the attribute
* Returns the kind of the attribute value.
* @return the kind of the attribute value
*/
ValueKind getValueKind();

View File

@ -81,7 +81,7 @@ public interface DocTreeFactory {
/**
* Creates a new {@code AttributeTree} object, to represent an HTML attribute in an HTML tag.
* @param name the name of the attribute
* @param vkind the kind of attribute value
* @param vkind the kind of the attribute value
* @param value the value, if any, of the attribute
* @return an {@code AttributeTree} object
*/

View File

@ -36,7 +36,7 @@ import com.sun.tools.javac.util.DefinedBy;
import com.sun.tools.javac.util.DefinedBy.Api;
/**
* Prints out a doc comment tree.
* Prints out a doc-comment tree.
*
* <p><b>This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own risk.
@ -50,11 +50,6 @@ public class DocPretty implements DocTreeVisitor<Void,Void> {
*/
final Writer out;
/**
* The left margin.
*/
int lmargin = 0;
public DocPretty(Writer out) {
this.out = out;
}
@ -90,7 +85,7 @@ public class DocPretty implements DocTreeVisitor<Void,Void> {
}
/**
* Print list., with separators
* Print list with separators.
*/
protected void print(List<? extends DocTree> list, String sep) throws IOException {
if (list.isEmpty())
@ -497,7 +492,7 @@ public class DocPretty implements DocTreeVisitor<Void,Void> {
List<? extends DocTree> attrs = node.getAttributes();
if (!attrs.isEmpty()) {
print(" ");
print(attrs);
print(attrs, " ");
DocTree last = node.getAttributes().get(attrs.size() - 1);
if (node.isSelfClosing() && last instanceof AttributeTree
&& ((AttributeTree) last).getValueKind() == ValueKind.UNQUOTED)

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -305,5 +305,44 @@ DocComment[DOC_COMMENT, pos:1
Text[TEXT, pos:26, jjg]
]
]
*/
/**
* <a name1="val1" name2='val2' name3=val3 name4>
*/
void multiple_attr() { }
/*
DocComment[DOC_COMMENT, pos:1
firstSentence: 1
StartElement[START_ELEMENT, pos:1
name:a
attributes: 4
Attribute[ATTRIBUTE, pos:4
name: name1
vkind: DOUBLE
value: 1
Text[TEXT, pos:11, val1]
]
Attribute[ATTRIBUTE, pos:17
name: name2
vkind: SINGLE
value: 1
Text[TEXT, pos:24, val2]
]
Attribute[ATTRIBUTE, pos:30
name: name3
vkind: UNQUOTED
value: 1
Text[TEXT, pos:36, val3]
]
Attribute[ATTRIBUTE, pos:41
name: name4
vkind: EMPTY
value: null
]
]
body: empty
block tags: empty
]
*/
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -66,7 +66,7 @@ public class DocCommentTester {
this.useBreakIterator = useBreakIterator;
}
public static void main(String... args) throws Exception {
ArrayList<String> list = new ArrayList(Arrays.asList(args));
ArrayList<String> list = new ArrayList<>(Arrays.asList(args));
if (!list.isEmpty() && "-useBreakIterator".equals(list.get(0))) {
list.remove(0);
new DocCommentTester(true).run(list);
@ -255,7 +255,7 @@ public class DocCommentTester {
* changes are approved, the new files can be used to replace the old.
*/
public static void main(String... args) throws Exception {
List<File> files = new ArrayList<File>();
List<File> files = new ArrayList<>();
File o = null;
for (int i = 0; i < args.length; i++) {
String arg = args[i];
@ -305,11 +305,8 @@ public class DocCommentTester {
File f = new File(tree.getSourceFile().getName());
File outFile = new File(outDir, f.getName());
try {
FileWriter out = new FileWriter(outFile);
try {
try (FileWriter out = new FileWriter(outFile)) {
out.write(source);
} finally {
out.close();
}
} catch (IOException e) {
System.err.println("Can't write " + tree.getSourceFile().getName()
@ -701,7 +698,7 @@ public class DocCommentTester {
/*
* Use this method to start printing a multi-line representation of a
* DocTree node. The representation should be termintated by calling
* DocTree node. The representation should be terminated by calling
* out.println("]").
*/
void header(DocTree node) {
@ -804,7 +801,7 @@ public class DocCommentTester {
final DCDocComment dc = (DCDocComment) trees.getDocCommentTree(path);
DCTree t = (DCTree) trees.getDocCommentTree(path);
DocTreeScanner scanner = new DocTreeScanner<Void,Void>() {
DocTreeScanner<Void, Void> scanner = new DocTreeScanner<>() {
@Override
public Void scan(DocTree node, Void ignore) {
if (node != null) {