From 2c82e3f86212b93edacb5a1ec775b93b2055cc56 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Tue, 17 Jan 2017 10:11:15 +0700 Subject: [PATCH] StreamToLoop: SourceOperation#wrap: chop parentheses by index (first and last); explanatory comments (IDEA-CR-17009) --- .../streamToLoop/SourceOperation.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInspection/streamToLoop/SourceOperation.java b/java/java-impl/src/com/intellij/codeInspection/streamToLoop/SourceOperation.java index 79c01bb276ab..e7119c9cef95 100644 --- a/java/java-impl/src/com/intellij/codeInspection/streamToLoop/SourceOperation.java +++ b/java/java-impl/src/com/intellij/codeInspection/streamToLoop/SourceOperation.java @@ -163,17 +163,21 @@ abstract class SourceOperation extends Operation { @Override public String wrap(StreamVariable outVar, String code, StreamToLoopReplacementContext context) { String type = outVar.getType(); - String collection; + String iterationParameter; PsiExpressionList argList = myCall.getArgumentList(); if (TypeConversionUtil.isPrimitive(type)) { - collection = StreamEx.of(argList.getChildren()).remove(child -> child.textMatches("(") || child.textMatches(")")) - .map(PsiElement::getText).joining("", "new " + type + "[] {", "}"); + // Not using argList.getExpressions() here as we want to preserve comments and formatting between the expressions + PsiElement[] children = argList.getChildren(); + // first and last children are (parentheses), we need to remove them + iterationParameter = StreamEx.of(children, 1, children.length - 1) + .map(PsiElement::getText) + .joining("", "new " + type + "[] {", "}"); } else { - collection = "java.util.Arrays.<" + type + ">asList" + argList.getText(); + iterationParameter = "java.util.Arrays.<" + type + ">asList" + argList.getText(); } return context.getLoopLabel() + - "for(" + outVar.getDeclaration() + ": " + collection + ") {" + code + "}\n"; + "for(" + outVar.getDeclaration() + ": " + iterationParameter + ") {" + code + "}\n"; } }