/*
 * Copyright 2000-2016 JetBrains s.r.o.
 *
 * 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 org.jetbrains.plugins.gradle.codeInspection;

import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.codeInspection.GroovyFix;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression;
import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Vladislav.Soroka
 * @since 11/21/13
 */
public class MultipleRepositoryUrlsFix extends GroovyFix {
  private final GrClosableBlock myClosure;
  private final String myRepoType;

  public MultipleRepositoryUrlsFix(@NotNull GrClosableBlock closure, @NotNull String repoType) {
    myClosure = closure;
    myRepoType = repoType;
  }

  @Override
  protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
    List<GrCallExpression> statements = MultipleRepositoryUrlsInspection.findUrlCallExpressions(myClosure);
    if (statements.size() <= 1) return;
    statements.remove(0);

    List<PsiElement> elements = new ArrayList<>(statements);
    for (GrCallExpression statement : statements) {
      PsiElement newLineCandidate = statement.getNextSibling();
      if (PsiUtil.isNewLine(newLineCandidate)) {
        elements.add(newLineCandidate);
      }
    }

    myClosure.removeElements(elements.toArray(PsiElement.EMPTY_ARRAY));
    GrClosableBlock closableBlock = PsiTreeUtil.getParentOfType(myClosure, GrClosableBlock.class);
    if (closableBlock == null) return;

    GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(project);
    for (GrCallExpression statement : statements) {
      closableBlock.addStatementBefore(elementFactory.createStatementFromText(myRepoType + '{' + statement.getText() + '}'), null);
    }
  }

  @NotNull
  @Override
  public String getFamilyName() {
    return GradleInspectionBundle.message("multiple.repository.urls.fix.name");
  }
}

@@ -1,13 +1,10 @@
 /
-*%0A *
+/
  Copyrig
@@ -18,9 +18,9 @@
 -201
-6
+8
  Jet
@@ -36,566 +36,109 @@
 r.o.
-%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */
+ Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
 %0Apac
@@ -323,16 +323,109 @@
 lement;%0A
+import com.intellij.psi.SmartPointerManager;%0Aimport com.intellij.psi.SmartPsiElementPointer;%0A
 import c
@@ -1107,16 +1107,39 @@
 e final 
+SmartPsiElementPointer%3C
 GrClosab
@@ -1145,16 +1145,17 @@
 bleBlock
+%3E
  myClosu
@@ -1307,23 +1307,107 @@
 osure = 
-closure
+SmartPointerManager.getInstance(closure.getProject()).createSmartPsiElementPointer(closure)
 ;%0A    my
@@ -1569,16 +1569,103 @@
 ption %7B%0A
+    GrClosableBlock closure = myClosure.getElement();%0A    if (closure == null) return;%0A
     List
@@ -1752,19 +1752,17 @@
 essions(
-myC
+c
 losure);
@@ -2110,27 +2110,25 @@
 %0A    %7D%0A%0A    
-myC
+c
 losure.remov
@@ -2244,19 +2244,17 @@
 tOfType(
-myC
+c
 losure, 

remove some potential memory leaks in quick fixes