Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8266666: Implementation for snippets #4795

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
b800f57
Initial commit
pavelrappo Jul 15, 2021
5491387
Do not use snippets
pavelrappo Jul 15, 2021
dd172b5
Update copyright years
pavelrappo Jul 15, 2021
c4e5b79
Update @since tags
pavelrappo Jul 27, 2021
4300903
Improve comments
pavelrappo Jul 27, 2021
34b274c
Make CSS rules more specific
pavelrappo Jul 27, 2021
6022df0
Restructure ...toolkit.taglets.snippet.** packages
pavelrappo Jul 27, 2021
01afad6
Change AnnotatedText<T> to StyledText
pavelrappo Jul 28, 2021
ae81871
Update src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/too…
pavelrappo Jul 29, 2021
0a47e9c
Update src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/too…
pavelrappo Jul 29, 2021
22d1e07
Pass through FIXMEs and TODOs
pavelrappo Jul 30, 2021
e1dbd98
Merge branch 'master' into 8266666
pavelrappo Aug 23, 2021
942b1e9
Remove JEP link from SnippetTree
pavelrappo Aug 23, 2021
731a1a6
Remove superfluous editor-fold
pavelrappo Aug 23, 2021
0510287
Add @summary to tests
pavelrappo Aug 23, 2021
a7cd155
Fix import layout and typos
pavelrappo Aug 24, 2021
26de340
Clarify the attributes order comment
pavelrappo Aug 24, 2021
be7ffe5
Merge branch 'master' into 8266666
pavelrappo Aug 24, 2021
87e765b
Format code closer to the ambient style
pavelrappo Aug 25, 2021
0d9c53a
Recover from a sloppy merge
pavelrappo Aug 25, 2021
a473e9a
Exclude unrelated changes to resources
pavelrappo Aug 25, 2021
98fbaab
Clean up tag parsing
pavelrappo Aug 30, 2021
b2b604f
Merge branch 'master' into 8266666
pavelrappo Aug 30, 2021
1b61b37
Merge branch 'master' into 8266666
pavelrappo Aug 31, 2021
e6d9f29
Be more specific when testing for syntax errors
pavelrappo Aug 31, 2021
2966876
Merge branch 'master' into 8266666
pavelrappo Sep 3, 2021
0a3adfb
Improve snippet attributes parsing
pavelrappo Sep 6, 2021
3df2d28
Refactor SnippetTaglet
pavelrappo Sep 6, 2021
b276f3f
Merge branch 'master' into 8266666
pavelrappo Sep 6, 2021
1705c93
Merge branch 'master' into 8266666
pavelrappo Sep 9, 2021
a217316
Clean up code
pavelrappo Sep 9, 2021
3c654e6
Remove trailing whitespace to satisfy jcheck
pavelrappo Sep 9, 2021
4a22c09
Address feedback
pavelrappo Sep 14, 2021
219d467
Address trivial feedback
pavelrappo Sep 15, 2021
0b3ea61
Merge branch 'master' into 8266666
pavelrappo Sep 17, 2021
1e3e5d5
Improve markup error messages
pavelrappo Sep 20, 2021
edb7cf8
Merge branch 'master' into 8266666
pavelrappo Sep 20, 2021
884dcb0
Address feedback
pavelrappo Sep 21, 2021
1050d4f
Use bug id, not JEP id
pavelrappo Sep 21, 2021
a7299d8
Merge branch 'master' into 8266666
pavelrappo Sep 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -49,7 +49,6 @@
import com.sun.source.doctree.DocCommentTree;
import com.sun.source.doctree.DocTree;
import com.sun.source.doctree.IdentifierTree;
import com.sun.source.doctree.LinkTree;
import com.sun.source.doctree.LiteralTree;
import com.sun.source.doctree.ParamTree;
import com.sun.source.doctree.ReferenceTree;
Expand Down Expand Up @@ -126,16 +125,6 @@ public DocTree makeSeeTree(String sig, Element e) {
return treeFactory.newSeeTree(List.of(treeFactory.newReferenceTree(sig)));
}

public LinkTree makeLinkTree(String sig, String label) {
ReferenceTree ref = treeFactory.newReferenceTree(sig);
return treeFactory.newLinkTree(ref, List.of(makeTextTree(label)));
}

public LinkTree makeLinkPlainTree(String sig, String label) {
ReferenceTree ref = treeFactory.newReferenceTree(sig);
return treeFactory.newLinkPlainTree(ref, List.of(makeTextTree(label)));
}

public TextTree makeTextTree(String content) {
return treeFactory.newTextTree(content);
}
Expand Down
Expand Up @@ -289,21 +289,20 @@ private Pattern createRegexPattern(Optional<Attribute.Valued> substring,
// this Pattern.compile *should not* throw an exception
pattern = Pattern.compile(defaultRegex);
} else {
// Unlike string literals in Java source, attribute values in
// snippet markup do not use escapes. This is why indices of
// characters in the regex pattern directly map to their
// corresponding positions in snippet source.
final String value = regex.get().value();
assert value.equals(Pattern.compile(value).pattern()) : value;
try {
pattern = Pattern.compile(value);
} catch (PatternSyntaxException e) {
// Unlike string literals in Java source, attribute values in
// snippet markup do not use escape sequences. This is why
// indices of characters in the regex pattern directly map to
// their corresponding positions in snippet source. Refine
// position using e.getIndex() only if that index is relevant to
// the regex in the attribute value. Index might be irrelevant
// because it refers to an internal representation of regex,
// e.getPattern(), which might be a normalized or partial view
// of the original pattern.
int pos = offset + regex.get().valueStartPosition();
// Refine position using e.getIndex() only if that index is
// relevant to the regex in the attribute value. Index might be
// irrelevant because it refers to an internal representation of
// regex, e.getPattern(), which might be a normalized or partial
// view of the original pattern.
if (e.getIndex() > -1 && value.equals(e.getPattern())) {
pos += e.getIndex();
}
Expand Down
Expand Up @@ -243,8 +243,7 @@ public void testBadTagSyntax(Path base) throws IOException {
class Capture {
static final AtomicInteger counter = new AtomicInteger();

record TestCase(String input, String expectedError) {
}
record TestCase(String input, String expectedError) { }

void testErrors(List<TestCase> testCases) throws IOException {
List<String> inputs = testCases.stream().map(s -> s.input).toList();
Expand Down Expand Up @@ -2144,4 +2143,88 @@ record TestCase(Snippet snippet, String expectedOutput) { }
</div>""".formatted(index, t.expectedOutput()));
});
}

@Test
public void testInvalidRegexDiagnostics(Path base) throws Exception {

record TestCase(String input, String expectedError) { }

// WARNING: debugging these test cases by reading .jtr files might prove
// confusing. This is because of how jtharness, which is used by jtreg,
// represents special symbols it encounters in standard streams. While
// CR, LR and TAB are output as they are, \ is output as \\ and the rest
// of the escape sequences are output using the \\uxxxx notation. This
// might affect relative symbol positioning on adjacent lines. For
// example, it might be hard to judge the true (i.e. console) position
// of the caret. Try using -show:System.out jtreg option to remediate
// that.

final var testCases = List.of(
new TestCase("""
{@snippet :
hello there // @highlight regex ="\t**"
}""",
"""
error: snippet markup error: "Dangling meta character '*'"
hello there // @highlight regex ="\t**"
\t ^
"""),
new TestCase("""
{@snippet :
hello there // @highlight regex ="\\t**"
}""",
"""
error: snippet markup error: "Dangling meta character '*'"
hello there // @highlight regex ="\\t**"
^
"""),
new TestCase("""
{@snippet :
hello there // @highlight regex="\\.\\*\\+\\E"
}""",
"""
error: snippet markup error: "Illegal/unsupported escape sequence"
hello there // @highlight regex="\\.\\*\\+\\E"
\s\s\s\s ^
"""), // use \s to counteract shift introduced by \\ so as to visually align ^ right below E
new TestCase("""
{@snippet :
hello there // @highlight type="italics" regex =" ["
}""",
"""
error: snippet markup error: "Unclosed character class"
hello there // @highlight type="italics" regex =" ["
^
""")
);

List<String> inputs = testCases.stream().map(s -> s.input).toList();
StringBuilder methods = new StringBuilder();
forEachNumbered(inputs, (i, n) -> {
methods.append(
"""

/**
%s*/
public void case%s() {}
""".formatted(i, n));
});

String classString =
"""
public class A {
%s
}
""".formatted(methods.toString());

Path src = Files.createDirectories(base.resolve("src"));
tb.writeJavaFiles(src, classString);

javadoc("-d", base.resolve("out").toString(),
"-sourcepath", src.toString(),
src.resolve("A.java").toString());
checkExit(Exit.ERROR);
checkOrder(Output.OUT, testCases.stream().map(TestCase::expectedError).toArray(String[]::new));
checkNoCrashes();
}
}