Skip to content

Commit b2a9519

Browse files
committed
VSCodeASDocComment: static method to create a comment from an XML element
1 parent f54050a commit b2a9519

File tree

2 files changed

+112
-106
lines changed

2 files changed

+112
-106
lines changed

language-server/src/main/java/com/as3mxml/vscode/asdoc/VSCodeASDocComment.java

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package com.as3mxml.vscode.asdoc;
2121

22+
import java.io.BufferedReader;
23+
import java.io.StringReader;
2224
import java.util.ArrayList;
2325
import java.util.Collection;
2426
import java.util.HashMap;
@@ -31,6 +33,7 @@
3133
import org.apache.royale.compiler.asdoc.IASDocTag;
3234
import org.apache.royale.compiler.common.ISourceLocation;
3335
import org.apache.royale.compiler.common.SourceLocation;
36+
import org.dom4j.Element;
3437

3538
import antlr.Token;
3639

@@ -43,6 +46,114 @@ public class VSCodeASDocComment extends SourceLocation implements IASDocComment
4346
private static final Pattern markdownUnderscorePattern = Pattern.compile("(_{1,2})(\\w(?:[\\w ]+\\w)?)\\1");
4447
private static final Pattern markdownBacktickPattern = Pattern.compile("(`)(\\w(?:[\\w ]+\\w)?)\\1");
4548

49+
public static VSCodeASDocComment getComment(Element defElement) {
50+
try {
51+
String defName = defElement.getName();
52+
String description = null;
53+
Element descriptionElement = defElement.element("description");
54+
if (descriptionElement != null) {
55+
description = descriptionElement.asXML();
56+
} else {
57+
Element apiDetailElement = defElement.element(defName + "Detail");
58+
if (apiDetailElement == null) {
59+
return null;
60+
}
61+
Element apiDescElement = apiDetailElement.element("apiDesc");
62+
if (apiDescElement == null) {
63+
return null;
64+
}
65+
description = apiDescElement.asXML();
66+
}
67+
StringBuilder builder = new StringBuilder();
68+
builder.append("/**");
69+
BufferedReader reader = new BufferedReader(new StringReader(description));
70+
String line = null;
71+
while ((line = reader.readLine()) != null) {
72+
builder.append("\n * ");
73+
builder.append(line);
74+
}
75+
if ("apiValue".equals(defName)) {
76+
Element apiDetailElement = defElement.element(defName + "Detail");
77+
if (apiDetailElement != null) {
78+
Element apiDefElement = apiDetailElement.element(defName + "Def");
79+
if (apiDefElement != null) {
80+
Element apiDefaultValueElement = apiDefElement.element("apiDefaultValue");
81+
if (apiDefaultValueElement != null) {
82+
String defaultDescription = apiDefaultValueElement.getStringValue();
83+
defaultDescription = defaultDescription.replaceAll("\n", " ");
84+
builder.append("\n * @default ");
85+
builder.append(defaultDescription);
86+
}
87+
}
88+
}
89+
}
90+
if ("apiValue".equals(defName) || "apiOperation".equals(defName) || "apiConstructor".equals(defName)) {
91+
Element apiDetailElement = defElement.element(defName + "Detail");
92+
if (apiDetailElement != null) {
93+
Element apiDefElement = apiDetailElement.element(defName + "Def");
94+
if (apiDefElement != null) {
95+
for (Object element : apiDefElement.elements("apiException")) {
96+
Element apiExceptionElement = (Element) element;
97+
Element apiItemNameElement = apiExceptionElement.element("apiItemName");
98+
builder.append("\n * @throws ");
99+
if (apiItemNameElement == null) {
100+
builder.append("_");
101+
} else {
102+
builder.append(apiItemNameElement.getStringValue());
103+
Element paramApiDescElement = apiExceptionElement.element("apiDesc");
104+
if (paramApiDescElement != null) {
105+
String paramDescription = paramApiDescElement.getStringValue();
106+
paramDescription = paramDescription.replaceAll("\n", " ");
107+
builder.append(" ");
108+
builder.append(paramDescription);
109+
}
110+
}
111+
}
112+
}
113+
}
114+
}
115+
if ("apiOperation".equals(defName) || "apiConstructor".equals(defName)) {
116+
Element apiDetailElement = defElement.element(defName + "Detail");
117+
if (apiDetailElement != null) {
118+
Element apiDefElement = apiDetailElement.element(defName + "Def");
119+
if (apiDefElement != null) {
120+
for (Object element : apiDefElement.elements("apiParam")) {
121+
Element apiParamElement = (Element) element;
122+
Element apiItemNameElement = apiParamElement.element("apiItemName");
123+
builder.append("\n * @param ");
124+
if (apiItemNameElement == null) {
125+
builder.append("_");
126+
} else {
127+
builder.append(apiItemNameElement.getStringValue());
128+
Element paramApiDescElement = apiParamElement.element("apiDesc");
129+
if (paramApiDescElement != null) {
130+
String paramDescription = paramApiDescElement.getStringValue();
131+
paramDescription = paramDescription.replaceAll("\n", " ");
132+
builder.append(" ");
133+
builder.append(paramDescription);
134+
}
135+
}
136+
}
137+
Element apiReturnElement = apiDefElement.element("apiReturn");
138+
if (apiReturnElement != null) {
139+
Element returnApiDescElement = apiReturnElement.element("apiDesc");
140+
if (returnApiDescElement != null) {
141+
String returnDescription = returnApiDescElement.getStringValue();
142+
returnDescription = returnDescription.replaceAll("\n", " ");
143+
builder.append("\n * @return ");
144+
builder.append(returnDescription);
145+
}
146+
}
147+
}
148+
}
149+
}
150+
builder.append("\n */");
151+
return new VSCodeASDocComment(builder.toString());
152+
} catch (Exception e) {
153+
return null;
154+
}
155+
}
156+
46157
public VSCodeASDocComment(Token t) {
47158
super((ISourceLocation) t);
48159
token = t;

language-server/src/main/java/com/as3mxml/vscode/asdoc/VSCodePackageDITAParser.java

Lines changed: 1 addition & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
*/
1919
package com.as3mxml.vscode.asdoc;
2020

21-
import java.io.BufferedReader;
2221
import java.io.File;
2322
import java.io.FileInputStream;
2423
import java.io.FileNotFoundException;
2524
import java.io.IOException;
2625
import java.io.InputStream;
27-
import java.io.StringReader;
2826
import java.net.URISyntaxException;
2927
import java.util.ArrayList;
3028
import java.util.List;
@@ -123,110 +121,7 @@ public IASDocComment getComment(IDefinition definition) throws Exception {
123121
if (defElement == null) {
124122
return null;
125123
}
126-
String defName = defElement.getName();
127-
String description = null;
128-
if (definition instanceof IStyleDefinition) {
129-
Element descriptionElement = defElement.element("description");
130-
if (descriptionElement == null) {
131-
return null;
132-
}
133-
description = descriptionElement.asXML();
134-
} else {
135-
Element apiDetailElement = defElement.element(defName + "Detail");
136-
if (apiDetailElement == null) {
137-
return null;
138-
}
139-
Element apiDescElement = apiDetailElement.element("apiDesc");
140-
if (apiDescElement == null) {
141-
return null;
142-
}
143-
description = apiDescElement.asXML();
144-
}
145-
StringBuilder builder = new StringBuilder();
146-
builder.append("/**");
147-
BufferedReader reader = new BufferedReader(new StringReader(description));
148-
String line = null;
149-
while ((line = reader.readLine()) != null) {
150-
builder.append("\n * ");
151-
builder.append(line);
152-
}
153-
if ("apiValue".equals(defName)) {
154-
Element apiDetailElement = defElement.element(defName + "Detail");
155-
if (apiDetailElement != null) {
156-
Element apiDefElement = apiDetailElement.element(defName + "Def");
157-
if (apiDefElement != null) {
158-
Element apiDefaultValueElement = apiDefElement.element("apiDefaultValue");
159-
if (apiDefaultValueElement != null) {
160-
String defaultDescription = apiDefaultValueElement.getStringValue();
161-
defaultDescription = defaultDescription.replaceAll("\n", " ");
162-
builder.append("\n * @default ");
163-
builder.append(defaultDescription);
164-
}
165-
}
166-
}
167-
}
168-
if ("apiValue".equals(defName) || "apiOperation".equals(defName) || "apiConstructor".equals(defName)) {
169-
Element apiDetailElement = defElement.element(defName + "Detail");
170-
if (apiDetailElement != null) {
171-
Element apiDefElement = apiDetailElement.element(defName + "Def");
172-
if (apiDefElement != null) {
173-
for (Object element : apiDefElement.elements("apiException")) {
174-
Element apiExceptionElement = (Element) element;
175-
Element apiItemNameElement = apiExceptionElement.element("apiItemName");
176-
builder.append("\n * @throws ");
177-
if (apiItemNameElement == null) {
178-
builder.append("_");
179-
} else {
180-
builder.append(apiItemNameElement.getStringValue());
181-
Element paramApiDescElement = apiExceptionElement.element("apiDesc");
182-
if (paramApiDescElement != null) {
183-
String paramDescription = paramApiDescElement.getStringValue();
184-
paramDescription = paramDescription.replaceAll("\n", " ");
185-
builder.append(" ");
186-
builder.append(paramDescription);
187-
}
188-
}
189-
}
190-
}
191-
}
192-
}
193-
if ("apiOperation".equals(defName) || "apiConstructor".equals(defName)) {
194-
Element apiDetailElement = defElement.element(defName + "Detail");
195-
if (apiDetailElement != null) {
196-
Element apiDefElement = apiDetailElement.element(defName + "Def");
197-
if (apiDefElement != null) {
198-
for (Object element : apiDefElement.elements("apiParam")) {
199-
Element apiParamElement = (Element) element;
200-
Element apiItemNameElement = apiParamElement.element("apiItemName");
201-
builder.append("\n * @param ");
202-
if (apiItemNameElement == null) {
203-
builder.append("_");
204-
} else {
205-
builder.append(apiItemNameElement.getStringValue());
206-
Element paramApiDescElement = apiParamElement.element("apiDesc");
207-
if (paramApiDescElement != null) {
208-
String paramDescription = paramApiDescElement.getStringValue();
209-
paramDescription = paramDescription.replaceAll("\n", " ");
210-
builder.append(" ");
211-
builder.append(paramDescription);
212-
}
213-
}
214-
}
215-
Element apiReturnElement = apiDefElement.element("apiReturn");
216-
if (apiReturnElement != null) {
217-
Element returnApiDescElement = apiReturnElement.element("apiDesc");
218-
if (returnApiDescElement != null) {
219-
String returnDescription = returnApiDescElement.getStringValue();
220-
returnDescription = returnDescription.replaceAll("\n", " ");
221-
builder.append("\n * @return ");
222-
builder.append(returnDescription);
223-
}
224-
}
225-
}
226-
}
227-
}
228-
builder.append("\n */");
229-
return new VSCodeASDocComment(builder.toString());
124+
return VSCodeASDocComment.getComment(defElement);
230125
}
231126

232127
private Element getDefinitionDITAFromParentTypeDef(IDefinition definition, ITypeDefinition typeDef) {

0 commit comments

Comments
 (0)