-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathASTNodeStringUtils.java
More file actions
274 lines (242 loc) · 9.81 KB
/
ASTNodeStringUtils.java
File metadata and controls
274 lines (242 loc) · 9.81 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Copyright 2024, Seqera Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nextflow.lsp.ast;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import groovy.lang.groovydoc.Groovydoc;
import nextflow.lsp.ast.ASTNodeCache;
import nextflow.lsp.ast.ASTUtils;
import nextflow.lsp.services.util.CustomFormattingOptions;
import nextflow.lsp.services.util.Formatter;
import nextflow.script.dsl.Constant;
import nextflow.script.dsl.DslType;
import nextflow.script.dsl.FeatureFlag;
import nextflow.script.dsl.Function;
import nextflow.script.dsl.Operator;
import nextflow.script.dsl.OutputDsl;
import nextflow.script.dsl.ProcessDirectiveDsl;
import nextflow.script.dsl.ProcessInputDsl;
import nextflow.script.dsl.ProcessOutputDsl;
import nextflow.script.v2.FeatureFlagNode;
import nextflow.script.v2.FunctionNode;
import nextflow.script.v2.ProcessNode;
import nextflow.script.v2.WorkflowNode;
import org.codehaus.groovy.ast.AnnotatedNode;
import org.codehaus.groovy.ast.AnnotationNode;
import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.FieldNode;
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.Parameter;
import org.codehaus.groovy.ast.Variable;
import org.codehaus.groovy.runtime.StringGroovyMethods;
import static nextflow.script.v2.ASTHelpers.*;
/**
* Utility methods for retreiving text information for ast nodes.
*
* @author Ben Sherman <bentshermann@gmail.com>
*/
public class ASTNodeStringUtils {
public static String getLabel(ASTNode node, ASTNodeCache ast) {
if( node instanceof ClassNode cn )
return toString(cn, ast);
if( node instanceof FeatureFlagNode ffn )
return toString(ffn);
if( node instanceof MethodNode mn )
return toString(mn, ast);
if( node instanceof Variable var )
return toString(var, ast);
return null;
}
public static String toString(ClassNode classNode, ASTNodeCache ast) {
var builder = new StringBuilder();
if( classNode.isEnum() )
builder.append("enum ");
else
builder.append("class ");
builder.append(classNode.toString(false));
return builder.toString();
}
public static String toString(FeatureFlagNode node) {
var builder = new StringBuilder();
builder.append("(feature flag) ");
builder.append(node.name);
return builder.toString();
}
public static String toString(MethodNode node, ASTNodeCache ast) {
if( node instanceof WorkflowNode wn ) {
var builder = new StringBuilder();
builder.append("workflow ");
builder.append(wn.isEntry() ? "<entry>" : wn.getName());
builder.append("(");
builder.append(
asBlockStatements(wn.takes).stream()
.map(take -> asVarX(take).getName())
.collect(Collectors.joining(", "))
);
builder.append(")");
return builder.toString();
}
if( node instanceof ProcessNode pn ) {
var builder = new StringBuilder();
builder.append("process ");
builder.append(pn.getName());
builder.append("\n\ninput:\n");
asDirectives(pn.inputs).forEach((call) -> {
var fmt = new Formatter(new CustomFormattingOptions(0, false, false));
fmt.append(call.getMethodAsString());
fmt.append(' ');
fmt.visitArguments(asMethodCallArguments(call), hasNamedArgs(call), false);
builder.append(fmt.toString());
builder.append('\n');
});
return builder.toString();
}
var label = getMethodTypeLabel(node);
if( label != null ) {
var builder = new StringBuilder();
builder.append("(");
builder.append(label);
builder.append(") ");
builder.append(node.getName());
return builder.toString();
}
var builder = new StringBuilder();
builder.append("def ");
builder.append(node.getName());
builder.append("(");
builder.append(toString(node.getParameters(), ast));
builder.append(")");
var returnType = node.getReturnType();
if( !ClassHelper.OBJECT_TYPE.equals(returnType) ) {
builder.append(" -> ");
builder.append(returnType.toString(false));
}
return builder.toString();
}
private static String getMethodTypeLabel(MethodNode mn) {
if( mn instanceof FunctionNode )
return null;
if( findAnnotation(mn, Operator.class).isPresent() )
return "operator";
var type = mn.getDeclaringClass().getTypeClass();
if( type == ProcessDirectiveDsl.class )
return "process directive";
if( type == ProcessInputDsl.class )
return "process input";
if( type == ProcessOutputDsl.class )
return "process output";
if( type == OutputDsl.class )
return "output directive";
return null;
}
public static String toString(Parameter[] params, ASTNodeCache ast) {
return Stream.of(params)
.map(param -> toString(param, ast))
.collect(Collectors.joining(", "));
}
public static String toString(Variable variable, ASTNodeCache ast) {
var builder = new StringBuilder();
builder.append(variable.getName());
var type = variable instanceof ASTNode
? ASTUtils.getTypeOfNode((ASTNode) variable, ast)
: variable.getType();
if( !ClassHelper.OBJECT_TYPE.equals(type) ) {
builder.append(": ");
builder.append(type.getNameWithoutPackage());
}
return builder.toString();
}
public static String getDocumentation(ASTNode node) {
if( node instanceof FeatureFlagNode ffn ) {
if( ffn.accessedVariable instanceof AnnotatedNode an )
return annotationValueToMarkdown(an, FeatureFlag.class, "description");
}
if( node instanceof FunctionNode fn )
return groovydocToMarkdown(fn.getGroovydoc());
if( node instanceof ProcessNode pn )
return groovydocToMarkdown(pn.getGroovydoc());
if( node instanceof WorkflowNode wn )
return groovydocToMarkdown(wn.getGroovydoc());
if( node instanceof ClassNode cn )
return annotationValueToMarkdown(cn, DslType.class);
if( node instanceof FieldNode fn )
return annotationValueToMarkdown(fn, Constant.class);
if( node instanceof MethodNode mn )
return annotationValueToMarkdown(mn, Function.class);
return null;
}
private static String annotationValueToMarkdown(AnnotatedNode node, Class type, String member) {
return findAnnotation(node, type)
.map((an) -> {
var description = an.getMember(member).getText();
return StringGroovyMethods.stripIndent(description, true).trim();
})
.orElse(null);
}
private static String annotationValueToMarkdown(AnnotatedNode node, Class type) {
return annotationValueToMarkdown(node, type, "value");
}
private static String groovydocToMarkdown(Groovydoc groovydoc) {
if( groovydoc == null || !groovydoc.isPresent() )
return null;
var content = groovydoc.getContent();
var lines = content.split("\n");
var builder = new StringBuilder();
var n = lines.length;
// strip end of groovydoc comment
if( n == 1 ) {
var c = lines[0].indexOf("*/");
if( c != -1 )
lines[0] = lines[0].substring(0, c);
}
// strip start of groovydoc coment
var first = lines[0];
var lengthToRemove = Math.min(first.length(), 3);
appendLine(builder, first.substring(lengthToRemove));
// append lines that start with an asterisk (*)
for( int i = 1; i < n - 1; i++ ) {
var line = lines[i];
var star = line.indexOf('*');
var at = line.indexOf('@');
if( at == -1 && star > -1 )
appendLine(builder, line.substring(star + 1));
}
return builder.toString().trim();
}
private static void appendLine(StringBuilder builder, String line) {
line = reformatLine(line);
if( line.length() == 0 )
return;
builder.append(line);
builder.append("\n");
}
private static String reformatLine(String line) {
line = line.replaceAll("<(\\w+)(?:\\s+\\w+(?::\\w+)?=(\"|\')[^\"\']*\\2)*\\s*(\\/{0,1})>", "<$1$3>");
line = line.replaceAll("<pre>", "\n\n```\n");
line = line.replaceAll("</pre>", "\n```\n");
line = line.replaceAll("</?(em|i)>", "_");
line = line.replaceAll("</?(strong|b)>", "**");
line = line.replaceAll("</?code>", "`");
line = line.replaceAll("<hr ?\\/>", "\n\n---\n\n");
line = line.replaceAll("<(p|ul|ol|dl|li|dt|table|tr|div|blockquote)>", "\n\n");
line = line.replaceAll("<br\\s*/?>\\s*", " \n");
line = line.replaceAll("<\\/{0,1}\\w+\\/{0,1}>", "");
return line;
}
}