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