|
| 1 | +diff --git a/java/java.source.base/src/org/netbeans/modules/java/source/builder/TreeFactory.java b/java/java.source.base/src/org/netbeans/modules/java/source/builder/TreeFactory.java |
| 2 | +index abb39db21f52..ab76a3d05889 100644 |
| 3 | +--- a/java/java.source.base/src/org/netbeans/modules/java/source/builder/TreeFactory.java |
| 4 | ++++ b/java/java.source.base/src/org/netbeans/modules/java/source/builder/TreeFactory.java |
| 5 | +@@ -1811,15 +1811,17 @@ public DeprecatedTree Deprecated(List<? extends DocTree> text) { |
| 6 | + } |
| 7 | + |
| 8 | + public DocCommentTree DocComment(List<? extends DocTree> fullBody, List<? extends DocTree> tags) { |
| 9 | +- DCDocComment temp = docMake.at(NOPOS).newDocCommentTree(fullBody, tags); |
| 10 | +- return DocComment(temp.getFirstSentence(), temp.getBody(), temp.getBlockTags()); |
| 11 | ++ return DocComment(HTML_JAVADOC_COMMENT, fullBody, tags); |
| 12 | + } |
| 13 | + |
| 14 | + public DocCommentTree MarkdownDocComment(List<? extends DocTree> fullBody, List<? extends DocTree> tags) { |
| 15 | +- DCDocComment temp = docMake.at(NOPOS).newDocCommentTree(fullBody, tags); |
| 16 | +- return MarkdownDocComment(temp.getFirstSentence(), temp.getBody(), temp.getBlockTags()); |
| 17 | ++ return DocComment(MARKDOWN_JAVADOC_COMMENT, fullBody, tags); |
| 18 | + } |
| 19 | + |
| 20 | ++ private DocCommentTree DocComment(Comment comment, List<? extends DocTree> fullBody, List<? extends DocTree> tags) { |
| 21 | ++ return docMake.at(NOPOS).newDocCommentTree(comment, fullBody, tags, Collections.emptyList(), Collections.emptyList()); |
| 22 | ++ } |
| 23 | ++ |
| 24 | + public DocTree Snippet(List<? extends DocTree> attributes, TextTree text){ |
| 25 | + try { |
| 26 | + return (DocTree) docMake.getClass().getMethod("newSnippetTree", List.class, TextTree.class).invoke(docMake.at(NOPOS), attributes, text); |
| 27 | +diff --git a/java/java.source.base/src/org/netbeans/modules/java/source/save/CasualDiff.java b/java/java.source.base/src/org/netbeans/modules/java/source/save/CasualDiff.java |
| 28 | +index 037eb8251775..8b77849102a8 100644 |
| 29 | +--- a/java/java.source.base/src/org/netbeans/modules/java/source/save/CasualDiff.java |
| 30 | ++++ b/java/java.source.base/src/org/netbeans/modules/java/source/save/CasualDiff.java |
| 31 | +@@ -45,6 +45,7 @@ |
| 32 | + import com.sun.tools.javac.tree.DCTree.DCLink; |
| 33 | + import com.sun.tools.javac.tree.DCTree.DCLiteral; |
| 34 | + import com.sun.tools.javac.tree.DCTree.DCParam; |
| 35 | ++import com.sun.tools.javac.tree.DCTree.DCRawText; |
| 36 | + import com.sun.tools.javac.tree.DCTree.DCReference; |
| 37 | + import com.sun.tools.javac.tree.DCTree.DCReturn; |
| 38 | + import com.sun.tools.javac.tree.DCTree.DCSee; |
| 39 | +@@ -4722,6 +4723,9 @@ private int diffDocTree(DCDocComment doc, DCTree oldT, DCTree newT, int[] elemen |
| 40 | + case TEXT: |
| 41 | + localpointer = diffText(doc, (DCText)oldT, (DCText)newT, elementBounds); |
| 42 | + break; |
| 43 | ++ case MARKDOWN: |
| 44 | ++ localpointer = diffRawText(doc, (DCRawText)oldT, (DCRawText)newT, elementBounds); |
| 45 | ++ break; |
| 46 | + case AUTHOR: |
| 47 | + localpointer = diffAuthor(doc, (DCAuthor)oldT, (DCAuthor)newT, elementBounds); |
| 48 | + break; |
| 49 | +@@ -4944,6 +4948,15 @@ private int diffText(DCDocComment doc, DCText oldT, DCText newT, int[] elementBo |
| 50 | + return elementBounds[1]; |
| 51 | + } |
| 52 | + |
| 53 | ++ private int diffRawText(DCDocComment doc, DCTree.DCRawText oldT, DCTree.DCRawText newT, int[] elementBounds) { |
| 54 | ++ if(oldT.code.equals(newT.code)) { |
| 55 | ++ copyTo(elementBounds[0], elementBounds[1]); |
| 56 | ++ } else { |
| 57 | ++ printer.print(newT.code); |
| 58 | ++ } |
| 59 | ++ return elementBounds[1]; |
| 60 | ++ } |
| 61 | ++ |
| 62 | + private int diffAuthor(DCDocComment doc, DCAuthor oldT, DCAuthor newT, int[] elementBounds) { |
| 63 | + int localpointer = oldT.name.isEmpty()? elementBounds[1] : getOldPos(oldT.name.get(0), doc); |
| 64 | + copyTo(elementBounds[0], localpointer); |
| 65 | +diff --git a/java/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/RewriteInCommentTest.java b/java/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/RewriteInCommentTest.java |
| 66 | +index 5c3ce2e65646..5effccaf023d 100644 |
| 67 | +--- a/java/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/RewriteInCommentTest.java |
| 68 | ++++ b/java/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/RewriteInCommentTest.java |
| 69 | +@@ -18,6 +18,13 @@ |
| 70 | + */ |
| 71 | + package org.netbeans.api.java.source.gen; |
| 72 | + |
| 73 | ++import com.sun.source.doctree.DocCommentTree; |
| 74 | ++import com.sun.source.doctree.LinkTree; |
| 75 | ++import com.sun.source.doctree.RawTextTree; |
| 76 | ++import com.sun.source.doctree.ReferenceTree; |
| 77 | ++import com.sun.source.util.DocTreePath; |
| 78 | ++import com.sun.source.util.DocTreePathScanner; |
| 79 | ++import com.sun.source.util.TreePath; |
| 80 | + import java.io.File; |
| 81 | + import java.io.IOException; |
| 82 | + import org.netbeans.api.java.source.ModificationResult; |
| 83 | +@@ -134,7 +141,128 @@ public void run(WorkingCopy copy) throws Exception { |
| 84 | + |
| 85 | + assertEquals(code.replace("test", "foo"), mr.getResultingSource(fo)); |
| 86 | + } |
| 87 | +- |
| 88 | ++ |
| 89 | ++ public void testDoNotBreakFormatting() throws Exception { |
| 90 | ++ File f = new File(getWorkDir(), "TestClass.java"); |
| 91 | ++ String code = """ |
| 92 | ++ package foo; |
| 93 | ++ /** |
| 94 | ++ * First line. |
| 95 | ++ * Test {@link #test}. |
| 96 | ++ */ |
| 97 | ++ public class TestClass{ |
| 98 | ++ } |
| 99 | ++ """; |
| 100 | ++ TestUtilities.copyStringToFile(f, code); |
| 101 | ++ FileObject fo = FileUtil.toFileObject(f); |
| 102 | ++ JavaSource javaSource = JavaSource.forFileObject(fo); |
| 103 | ++ ModificationResult mr = javaSource.runModificationTask(new Task<WorkingCopy>() { |
| 104 | ++ |
| 105 | ++ public void run(WorkingCopy copy) throws Exception { |
| 106 | ++ copy.toPhase(Phase.RESOLVED); |
| 107 | ++ |
| 108 | ++ TreePath topLevelClass = new TreePath(new TreePath(copy.getCompilationUnit()), |
| 109 | ++ copy.getCompilationUnit().getTypeDecls().get(0)); |
| 110 | ++ DocCommentTree docComment = copy.getDocTrees().getDocCommentTree(topLevelClass); |
| 111 | ++ |
| 112 | ++ new DocTreePathScanner<>() { |
| 113 | ++ @Override |
| 114 | ++ public Object visitReference(ReferenceTree rt, Object p) { |
| 115 | ++ copy.rewrite(topLevelClass.getLeaf(), rt, copy.getTreeMaker().Reference(null, "newName", null)); |
| 116 | ++ return null; |
| 117 | ++ } |
| 118 | ++ |
| 119 | ++ @Override |
| 120 | ++ public Object visitLink(LinkTree lt, Object p) { |
| 121 | ++ return super.visitLink(lt, p); |
| 122 | ++ } |
| 123 | ++ }.scan(new DocTreePath(topLevelClass, docComment), null); |
| 124 | ++ } |
| 125 | ++ }); |
| 126 | ++ |
| 127 | ++ assertEquals(code.replace("test", "newName"), mr.getResultingSource(fo)); |
| 128 | ++ } |
| 129 | ++ |
| 130 | ++ public void testDoNotBreakFormattingMarkdown() throws Exception { |
| 131 | ++ File f = new File(getWorkDir(), "TestClass.java"); |
| 132 | ++ String code = """ |
| 133 | ++ package foo; |
| 134 | ++ |
| 135 | ++ /// First line. |
| 136 | ++ /// Test {@link #test}. |
| 137 | ++ public class TestClass{ |
| 138 | ++ } |
| 139 | ++ """; |
| 140 | ++ TestUtilities.copyStringToFile(f, code); |
| 141 | ++ FileObject fo = FileUtil.toFileObject(f); |
| 142 | ++ JavaSource javaSource = JavaSource.forFileObject(fo); |
| 143 | ++ ModificationResult mr = javaSource.runModificationTask(new Task<WorkingCopy>() { |
| 144 | ++ |
| 145 | ++ public void run(WorkingCopy copy) throws Exception { |
| 146 | ++ copy.toPhase(Phase.RESOLVED); |
| 147 | ++ |
| 148 | ++ TreePath topLevelClass = new TreePath(new TreePath(copy.getCompilationUnit()), |
| 149 | ++ copy.getCompilationUnit().getTypeDecls().get(0)); |
| 150 | ++ DocCommentTree docComment = copy.getDocTrees().getDocCommentTree(topLevelClass); |
| 151 | ++ |
| 152 | ++ new DocTreePathScanner<>() { |
| 153 | ++ @Override |
| 154 | ++ public Object visitReference(ReferenceTree rt, Object p) { |
| 155 | ++ copy.rewrite(topLevelClass.getLeaf(), rt, copy.getTreeMaker().Reference(null, "newName", null)); |
| 156 | ++ return null; |
| 157 | ++ } |
| 158 | ++ |
| 159 | ++ @Override |
| 160 | ++ public Object visitLink(LinkTree lt, Object p) { |
| 161 | ++ return super.visitLink(lt, p); |
| 162 | ++ } |
| 163 | ++ }.scan(new DocTreePath(topLevelClass, docComment), null); |
| 164 | ++ } |
| 165 | ++ }); |
| 166 | ++ |
| 167 | ++ assertEquals(code.replace("test", "newName"), mr.getResultingSource(fo)); |
| 168 | ++ } |
| 169 | ++ |
| 170 | ++ public void testMarkdownChangeText() throws Exception { |
| 171 | ++ File f = new File(getWorkDir(), "TestClass.java"); |
| 172 | ++ String code = """ |
| 173 | ++ package foo; |
| 174 | ++ |
| 175 | ++ /// First line. |
| 176 | ++ /// Second line. |
| 177 | ++ public class TestClass{ |
| 178 | ++ } |
| 179 | ++ """; |
| 180 | ++ TestUtilities.copyStringToFile(f, code); |
| 181 | ++ FileObject fo = FileUtil.toFileObject(f); |
| 182 | ++ JavaSource javaSource = JavaSource.forFileObject(fo); |
| 183 | ++ ModificationResult mr = javaSource.runModificationTask(new Task<WorkingCopy>() { |
| 184 | ++ |
| 185 | ++ public void run(WorkingCopy copy) throws Exception { |
| 186 | ++ copy.toPhase(Phase.RESOLVED); |
| 187 | ++ |
| 188 | ++ TreePath topLevelClass = new TreePath(new TreePath(copy.getCompilationUnit()), |
| 189 | ++ copy.getCompilationUnit().getTypeDecls().get(0)); |
| 190 | ++ DocCommentTree docComment = copy.getDocTrees().getDocCommentTree(topLevelClass); |
| 191 | ++ |
| 192 | ++ new DocTreePathScanner<>() { |
| 193 | ++ @Override |
| 194 | ++ public Object visitDocComment(DocCommentTree dct, Object p) { |
| 195 | ++ //XXX: need to translate full body, as the split body has different split of the text trees, and the differ uses fullbody: |
| 196 | ++ return scan(dct.getFullBody(), p); |
| 197 | ++ } |
| 198 | ++ @Override |
| 199 | ++ public Object visitRawText(RawTextTree text, Object p) { |
| 200 | ++ copy.rewrite(topLevelClass.getLeaf(), text, copy.getTreeMaker().RawText(text.getContent().replace("line", "nueText"))); |
| 201 | ++ return null; |
| 202 | ++ } |
| 203 | ++ }.scan(new DocTreePath(topLevelClass, docComment), null); |
| 204 | ++ } |
| 205 | ++ }); |
| 206 | ++ |
| 207 | ++ assertEquals(code.replace("line", "nueText"), mr.getResultingSource(fo)); |
| 208 | ++ } |
| 209 | ++ |
| 210 | + String getGoldenPckg() { |
| 211 | + return ""; |
| 212 | + } |
0 commit comments