|
34 | 34 | import org.openrewrite.scala.marker.SObject; |
35 | 35 | import org.openrewrite.scala.marker.TypeProjection; |
36 | 36 | import org.openrewrite.scala.marker.ScalaForLoop; |
| 37 | +import org.openrewrite.scala.marker.TypeAscription; |
37 | 38 | import org.openrewrite.scala.marker.UnderscorePlaceholderLambda; |
38 | 39 | import org.openrewrite.scala.tree.S; |
39 | 40 |
|
@@ -191,27 +192,7 @@ public J visitAssignmentOperation(J.AssignmentOperation assignOp, PrintOutputCap |
191 | 192 | afterSyntax(assignOp, p); |
192 | 193 | return assignOp; |
193 | 194 | } |
194 | | - |
195 | | - @Override |
196 | | - public J visitTypeCast(J.TypeCast typeCast, PrintOutputCapture<P> p) { |
197 | | - beforeSyntax(typeCast, Space.Location.TYPE_CAST_PREFIX, p); |
198 | | - // In Scala, type casts are written as expression.asInstanceOf[Type] |
199 | | - visit(typeCast.getExpression(), p); |
200 | | - p.append(".asInstanceOf"); |
201 | | - |
202 | | - // Extract the type from the control parentheses |
203 | | - if (typeCast.getClazz() instanceof J.ControlParentheses) { |
204 | | - J.ControlParentheses<?> controlParens = (J.ControlParentheses<?>) typeCast.getClazz(); |
205 | | - visitSpace(controlParens.getPrefix(), Space.Location.CONTROL_PARENTHESES_PREFIX, p); |
206 | | - p.append('['); |
207 | | - visitRightPadded(controlParens.getPadding().getTree(), JRightPadded.Location.PARENTHESES, "", p); |
208 | | - p.append(']'); |
209 | | - } |
210 | | - |
211 | | - afterSyntax(typeCast, p); |
212 | | - return typeCast; |
213 | | - } |
214 | | - |
| 195 | + |
215 | 196 | @Override |
216 | 197 | public J visitTry(J.Try tryable, PrintOutputCapture<P> p) { |
217 | 198 | beforeSyntax(tryable, Space.Location.TRY_PREFIX, p); |
@@ -829,21 +810,75 @@ public J visitReturn(J.Return return_, PrintOutputCapture<P> p) { |
829 | 810 | } |
830 | 811 |
|
831 | 812 | @Override |
832 | | - public J visitForLoop(J.ForLoop forLoop, PrintOutputCapture<P> p) { |
833 | | - // Check if this is a Scala range-based for loop |
834 | | - ScalaForLoop marker = forLoop.getMarkers().findFirst(ScalaForLoop.class).orElse(null); |
835 | | - if (marker != null && marker.getOriginalSource() != null && !marker.getOriginalSource().isEmpty()) { |
836 | | - // Print the original Scala syntax |
837 | | - beforeSyntax(forLoop, Space.Location.FOR_PREFIX, p); |
838 | | - p.append(marker.getOriginalSource()); |
839 | | - afterSyntax(forLoop, p); |
840 | | - return forLoop; |
| 813 | + public J visitForEachLoop(J.ForEachLoop forEachLoop, PrintOutputCapture<P> p) { |
| 814 | + if (forEachLoop.getMarkers().findFirst(ScalaForLoop.class).isPresent()) { |
| 815 | + // Scala for-comprehension: for (x <- iterable) body |
| 816 | + beforeSyntax(forEachLoop, Space.Location.FOR_EACH_LOOP_PREFIX, p); |
| 817 | + p.append("for"); |
| 818 | + J.ForEachLoop.Control ctrl = forEachLoop.getControl(); |
| 819 | + visitSpace(ctrl.getPrefix(), Space.Location.FOR_EACH_CONTROL_PREFIX, p); |
| 820 | + p.append('('); |
| 821 | + |
| 822 | + // Print the variable (just the name, no type) |
| 823 | + JRightPadded<Statement> variable = ctrl.getPadding().getVariable(); |
| 824 | + Statement varStmt = variable.getElement(); |
| 825 | + if (varStmt instanceof J.VariableDeclarations) { |
| 826 | + J.VariableDeclarations varDecl = (J.VariableDeclarations) varStmt; |
| 827 | + visitSpace(varDecl.getPrefix(), Space.Location.VARIABLE_DECLARATIONS_PREFIX, p); |
| 828 | + if (!varDecl.getVariables().isEmpty()) { |
| 829 | + visit(varDecl.getVariables().get(0).getName(), p); |
| 830 | + } |
| 831 | + } else { |
| 832 | + visit(varStmt, p); |
| 833 | + } |
| 834 | + |
| 835 | + // Print "<-" with spaces from the padding |
| 836 | + visitSpace(variable.getAfter(), JRightPadded.Location.FOREACH_VARIABLE.getAfterLocation(), p); |
| 837 | + p.append("<-"); |
| 838 | + |
| 839 | + // Print the iterable |
| 840 | + JRightPadded<Expression> iterable = ctrl.getPadding().getIterable(); |
| 841 | + visit(iterable.getElement(), p); |
| 842 | + visitSpace(iterable.getAfter(), JRightPadded.Location.FOREACH_ITERABLE.getAfterLocation(), p); |
| 843 | + p.append(')'); |
| 844 | + |
| 845 | + // Print the body |
| 846 | + visitStatement(forEachLoop.getPadding().getBody(), JRightPadded.Location.FOR_BODY, p); |
| 847 | + afterSyntax(forEachLoop, p); |
| 848 | + return forEachLoop; |
841 | 849 | } |
842 | | - // Otherwise use Java syntax |
843 | | - return super.visitForLoop(forLoop, p); |
| 850 | + return super.visitForEachLoop(forEachLoop, p); |
| 851 | + } |
| 852 | + |
| 853 | + @Override |
| 854 | + public J visitTypeCast(J.TypeCast typeCast, PrintOutputCapture<P> p) { |
| 855 | + if (typeCast.getMarkers().findFirst(TypeAscription.class).isPresent()) { |
| 856 | + // Scala type ascription: expr: Type |
| 857 | + beforeSyntax(typeCast, Space.Location.TYPE_CAST_PREFIX, p); |
| 858 | + visit(typeCast.getExpression(), p); |
| 859 | + if (typeCast.getClazz() instanceof J.ControlParentheses) { |
| 860 | + J.ControlParentheses<?> controlParens = (J.ControlParentheses<?>) typeCast.getClazz(); |
| 861 | + visitSpace(controlParens.getPrefix(), Space.Location.CONTROL_PARENTHESES_PREFIX, p); |
| 862 | + p.append(':'); |
| 863 | + visitRightPadded(controlParens.getPadding().getTree(), JRightPadded.Location.PARENTHESES, "", p); |
| 864 | + } |
| 865 | + afterSyntax(typeCast, p); |
| 866 | + return typeCast; |
| 867 | + } |
| 868 | + // Existing asInstanceOf handling |
| 869 | + beforeSyntax(typeCast, Space.Location.TYPE_CAST_PREFIX, p); |
| 870 | + visit(typeCast.getExpression(), p); |
| 871 | + p.append(".asInstanceOf"); |
| 872 | + if (typeCast.getClazz() instanceof J.ControlParentheses) { |
| 873 | + J.ControlParentheses<?> controlParens = (J.ControlParentheses<?>) typeCast.getClazz(); |
| 874 | + visitSpace(controlParens.getPrefix(), Space.Location.CONTROL_PARENTHESES_PREFIX, p); |
| 875 | + p.append('['); |
| 876 | + visitRightPadded(controlParens.getPadding().getTree(), JRightPadded.Location.PARENTHESES, "", p); |
| 877 | + p.append(']'); |
| 878 | + } |
| 879 | + afterSyntax(typeCast, p); |
| 880 | + return typeCast; |
844 | 881 | } |
845 | | - |
846 | | - // Override additional methods here for Scala-specific syntax as needed |
847 | 882 |
|
848 | 883 | @Override |
849 | 884 | public J visitVariableDeclarations(J.VariableDeclarations multiVariable, PrintOutputCapture<P> p) { |
|
0 commit comments