From ff2b90104e21c48e8e91f34b6b0804d7be5c1346 Mon Sep 17 00:00:00 2001 From: Ben Sherman Date: Thu, 12 Dec 2024 16:36:30 -0600 Subject: [PATCH] Preserve Groovy-style type annotations in formatting, remove warning Signed-off-by: Ben Sherman --- .../main/java/script/formatter/Formatter.java | 21 +++++++++++++++ .../formatter/ScriptFormattingVisitor.java | 4 +++ .../java/script/parser/ScriptAstBuilder.java | 27 +++++++++---------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/modules/compiler/src/main/java/script/formatter/Formatter.java b/modules/compiler/src/main/java/script/formatter/Formatter.java index 36da0c9..3d04f8e 100644 --- a/modules/compiler/src/main/java/script/formatter/Formatter.java +++ b/modules/compiler/src/main/java/script/formatter/Formatter.java @@ -347,6 +347,8 @@ public void visitArguments(List args, boolean wrap) { } } + private boolean inVariableDeclaration; + private boolean inWrappedPipeChain; @Override @@ -354,7 +356,9 @@ public void visitBinaryExpression(BinaryExpression node) { if( node instanceof DeclarationExpression ) { if( node.getNodeMetaData(IMPLICIT_DECLARATION) != Boolean.TRUE ) append("def "); + inVariableDeclaration = true; visit(node.getLeftExpression()); + inVariableDeclaration = false; if( !(node.getRightExpression() instanceof EmptyExpression) ) { append(" = "); visit(node.getRightExpression()); @@ -475,6 +479,10 @@ else if( code.getStatements().size() == 1 && code.getStatements().get(0) instanc public void visitParameters(Parameter[] parameters) { for( int i = 0; i < parameters.length; i++ ) { var param = parameters[i]; + if( isLegacyType(param.getType()) ) { + visitTypeName(param.getType()); + append(' '); + } append(param.getName()); if( param.hasInitialExpression() ) { append('='); @@ -618,6 +626,10 @@ public void visitClassExpression(ClassExpression node) { } protected void visitTypeName(ClassNode type) { + if( isLegacyType(type) ) { + append(type.getNodeMetaData(LEGACY_TYPE)); + return; + } if( type.isArray() ) { visitTypeName(type.getComponentType()); return; @@ -644,6 +656,10 @@ protected void visitTypeName(ClassNode type) { @Override public void visitVariableExpression(VariableExpression node) { + if( inVariableDeclaration && isLegacyType(node.getType()) ) { + visitTypeName(node.getType()); + append(' '); + } append(node.getText()); } @@ -697,6 +713,10 @@ public void visit(Expression node) { // helpers + public static boolean isLegacyType(ClassNode cn) { + return cn.getNodeMetaData(LEGACY_TYPE) != null; + } + private boolean shouldWrapExpression(Expression node) { return node.getLineNumber() < node.getLastLineNumber(); } @@ -739,6 +759,7 @@ private boolean shouldWrapPipeExpression(BinaryExpression node) { private static final String IMPLICIT_DECLARATION = "_IMPLICIT_DECLARATION"; private static final String INSIDE_PARENTHESES_LEVEL = "_INSIDE_PARENTHESES_LEVEL"; private static final String LEADING_COMMENTS = "_LEADING_COMMENTS"; + private static final String LEGACY_TYPE = "_LEGACY_TYPE"; private static final String QUOTE_CHAR = "_QUOTE_CHAR"; private static final String TRAILING_COMMENT = "_TRAILING_COMMENT"; private static final String VERBATIM_TEXT = "_VERBATIM_TEXT"; diff --git a/modules/compiler/src/main/java/script/formatter/ScriptFormattingVisitor.java b/modules/compiler/src/main/java/script/formatter/ScriptFormattingVisitor.java index a8ef88d..ea12847 100644 --- a/modules/compiler/src/main/java/script/formatter/ScriptFormattingVisitor.java +++ b/modules/compiler/src/main/java/script/formatter/ScriptFormattingVisitor.java @@ -372,6 +372,10 @@ public void visitProcess(ProcessNode node) { public void visitFunction(FunctionNode node) { fmt.appendLeadingComments(node); fmt.append("def "); + if( Formatter.isLegacyType(node.getReturnType()) ) { + fmt.visitTypeName(node.getReturnType()); + fmt.append(' '); + } fmt.append(node.getName()); fmt.append('('); fmt.visitParameters(node.getParameters()); diff --git a/modules/compiler/src/main/java/script/parser/ScriptAstBuilder.java b/modules/compiler/src/main/java/script/parser/ScriptAstBuilder.java index ffd3d0b..925d91f 100644 --- a/modules/compiler/src/main/java/script/parser/ScriptAstBuilder.java +++ b/modules/compiler/src/main/java/script/parser/ScriptAstBuilder.java @@ -629,13 +629,12 @@ private Statement outputTargetBody(OutputTargetBodyContext ctx) { } private FunctionNode functionDef(FunctionDefContext ctx) { - checkLegacyType(ctx.legacyType()); - var name = identifier(ctx.identifier()); + var returnType = legacyType(ctx.legacyType()); var params = Optional.ofNullable(formalParameterList(ctx.formalParameterList())).orElse(Parameter.EMPTY_ARRAY); var code = blockStatements(ctx.blockStatements()); - var result = ast( new FunctionNode(name, null, params, code), ctx ); + var result = ast( new FunctionNode(name, returnType, params, code), ctx ); checkInvalidVarName(name, result); groovydocManager.handle(result, ctx); return result; @@ -781,9 +780,8 @@ private Statement variableDeclaration(VariableDeclarationContext ctx) { } else { // single assignment - checkLegacyType(ctx.legacyType()); - var target = variableName(ctx.identifier()); + target.setType(legacyType(ctx.legacyType())); var initializer = ctx.initializer != null ? expression(ctx.initializer) : EmptyExpression.INSTANCE; @@ -1510,9 +1508,7 @@ private Parameter[] formalParameterList(FormalParameterListContext ctx) { } private Parameter formalParameter(FormalParameterContext ctx) { - checkLegacyType(ctx.legacyType()); - - var type = ClassHelper.dynamicType(); + var type = legacyType(ctx.legacyType()); var name = identifier(ctx.identifier()); var defaultValue = ctx.expression() != null ? expression(ctx.expression()) @@ -1607,6 +1603,13 @@ private GenericsType genericsType(TypeContext ctx) { return ast( new GenericsType(type(ctx)), ctx ); } + private ClassNode legacyType(ParserRuleContext ctx) { + var result = ClassHelper.dynamicType(); + if( ctx != null ) + result.putNodeMetaData(LEGACY_TYPE, ctx.getText()); + return result; + } + /// COMMENTS private void saveLeadingComments(ASTNode node, ParserRuleContext ctx) { @@ -1708,13 +1711,6 @@ private boolean isInsideParentheses(NodeMetaDataHandler nodeMetaDataHandler) { return insideParenLevel != null && insideParenLevel.intValue() > 0; } - private void checkLegacyType(ParserRuleContext ctx) { - if( ctx != null ) { - var node = ast( new EmptyStatement(), ctx ); - collectWarning("Groovy-style type annotations are ignored", node); - } - } - private CompilationFailedException createParsingFailedException(String msg, ParserRuleContext ctx) { return createParsingFailedException( new SyntaxException(msg, @@ -1826,6 +1822,7 @@ public void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex, private static final String FULLY_QUALIFIED = "_FULLY_QUALIFIED"; private static final String INSIDE_PARENTHESES_LEVEL = "_INSIDE_PARENTHESES_LEVEL"; private static final String LEADING_COMMENTS = "_LEADING_COMMENTS"; + private static final String LEGACY_TYPE = "_LEGACY_TYPE"; private static final String QUOTE_CHAR = "_QUOTE_CHAR"; private static final String TRAILING_COMMENT = "_TRAILING_COMMENT"; private static final String VERBATIM_TEXT = "_VERBATIM_TEXT";