Skip to content

Commit 17618f5

Browse files
committed
Use destructuring syntax for tuple/record inputs
Signed-off-by: Ben Sherman <bentshermann@gmail.com>
1 parent ed8a2d2 commit 17618f5

3 files changed

Lines changed: 41 additions & 37 deletions

File tree

src/main/java/nextflow/lsp/ast/ASTNodeStringUtils.java

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,7 @@ private static String workflowToLabel(WorkflowNode node) {
128128
}
129129
for( var take : takes ) {
130130
fmt.appendIndent();
131-
fmt.append(take.getName());
132-
if( fmt.hasType(take) ) {
133-
fmt.append(": ");
134-
fmt.visitTypeAnnotation(take.getType());
135-
}
131+
typedInput(take, fmt);
136132
fmt.appendNewLine();
137133
}
138134
fmt.appendNewLine();
@@ -183,25 +179,7 @@ private static String processToLabel(ProcessNodeV2 node) {
183179
}
184180
for( var input : node.inputs ) {
185181
fmt.appendIndent();
186-
if( input instanceof TupleParameter tp ) {
187-
var components = Arrays.stream(tp.components)
188-
.map(p -> p.getName())
189-
.collect(Collectors.joining(", "));
190-
fmt.append('(');
191-
fmt.append(components);
192-
fmt.append(')');
193-
}
194-
else {
195-
fmt.append(input.getName());
196-
}
197-
if( fmt.hasType(input) ) {
198-
fmt.append(": ");
199-
var type = input.getType();
200-
if( type.getNameWithoutPackage().startsWith("__Record") )
201-
processRecordToLabel(type.redirect(), fmt);
202-
else
203-
fmt.visitTypeAnnotation(type);
204-
}
182+
typedInput(input, fmt);
205183
fmt.appendNewLine();
206184
}
207185
fmt.appendNewLine();
@@ -223,16 +201,32 @@ private static String processToLabel(ProcessNodeV2 node) {
223201
return fmt.toString();
224202
}
225203

226-
private static void processRecordToLabel(ClassNode type, Formatter fmt) {
204+
private static void typedInput(Parameter input, Formatter fmt) {
205+
if( input instanceof TupleParameter tp ) {
206+
if( "Record".equals(tp.getType().getNameWithoutPackage()) )
207+
processRecordInput(tp, fmt);
208+
else
209+
processTupleInput(tp, fmt);
210+
}
211+
else {
212+
fmt.append(input.getName());
213+
if( fmt.hasType(input) ) {
214+
fmt.append(": ");
215+
fmt.visitTypeAnnotation(input.getType());
216+
}
217+
}
218+
}
219+
220+
private static void processRecordInput(TupleParameter tp, Formatter fmt) {
227221
fmt.append("Record {");
228222
fmt.appendNewLine();
229223
fmt.incIndent();
230-
for( var fn : type.getFields() ) {
224+
for( var p : tp.components ) {
231225
fmt.appendIndent();
232-
fmt.append(fn.getName());
233-
if( fmt.hasType(fn) ) {
226+
fmt.append(p.getName());
227+
if( fmt.hasType(p) ) {
234228
fmt.append(": ");
235-
fmt.append(TypesEx.getName(fn.getType()));
229+
fmt.append(TypesEx.getName(p.getType()));
236230
}
237231
fmt.appendNewLine();
238232
}
@@ -241,6 +235,21 @@ private static void processRecordToLabel(ClassNode type, Formatter fmt) {
241235
fmt.append('}');
242236
}
243237

238+
private static void processTupleInput(TupleParameter tp, Formatter fmt) {
239+
fmt.append("Tuple<");
240+
for( int i = 0; i < tp.components.length; i++ ) {
241+
var p = tp.components[i];
242+
fmt.append(p.getName());
243+
if( fmt.hasType(p) ) {
244+
fmt.append(": ");
245+
fmt.append(TypesEx.getName(p.getType()));
246+
}
247+
if( i < tp.components.length - 1 )
248+
fmt.append(", ");
249+
}
250+
fmt.append(">");
251+
}
252+
244253
private static String processToLabel(ProcessNodeV1 node) {
245254
var fmt = new Formatter(new FormattingOptions(2, true));
246255
fmt.append("process ");

src/main/java/nextflow/lsp/services/script/ProcessConverter.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.nio.file.Files;
2121
import java.nio.file.Path;
2222
import java.util.ArrayList;
23-
import java.util.Arrays;
2423
import java.util.Collections;
2524
import java.util.HashMap;
2625
import java.util.List;
@@ -172,10 +171,6 @@ private Parameter typedInput(MethodCallExpression call, List<Statement> stagers)
172171
.map(mce -> typedInput(mce, stagers))
173172
.toArray(Parameter[]::new);
174173
var type = new ClassNode(Tuple.class);
175-
var genericsTypes = Arrays.stream(components)
176-
.map(p -> new GenericsType(p.getType()))
177-
.toArray(GenericsType[]::new);
178-
type.setGenericsTypes(genericsTypes);
179174
return new TupleParameter(type, components);
180175
}
181176

src/test/groovy/nextflow/lsp/services/script/ConvertScriptStaticTypesTest.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ class ConvertScriptStaticTypesTest extends Specification {
151151
checkInputs(service, INPUT, TYPED_INPUT, STAGE)
152152

153153
where:
154-
INPUT | TYPED_INPUT | STAGE
155-
'tuple val(id), path(fastq)' | '(id, fastq): Tuple<?, Path>' | null
156-
"tuple val(id), path('file.txt')" | '(id, $in1): Tuple<?, Path>' | "stageAs \$in1, 'file.txt'"
154+
INPUT | TYPED_INPUT | STAGE
155+
'tuple val(id), path(fastq)' | 'tuple(id, fastq: Path)' | null
156+
"tuple val(id), path('file.txt')" | 'tuple(id, $in1: Path)' | "stageAs \$in1, 'file.txt'"
157157
}
158158

159159
def 'should convert each inputs as val or path' () {

0 commit comments

Comments
 (0)