/*
 * 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.groovy.annotator.intentions;

import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiType;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.GroovyBundle;
import org.jetbrains.plugins.groovy.codeInspection.GroovyFix;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;
import org.jetbrains.plugins.groovy.lang.psi.expectedTypes.SupertypeConstraint;
import org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint;

/**
 * @author Maxim.Medvedev
 */
public class CreateFieldFromConstructorLabelFix extends GroovyFix {
  private final CreateFieldFix myFix;
  private final GrNamedArgument myNamedArgument;

  public CreateFieldFromConstructorLabelFix(GrTypeDefinition targetClass, GrNamedArgument namedArgument) {
    myFix = new CreateFieldFix(targetClass);
    myNamedArgument = namedArgument;
  }

  @Nullable
  private String getFieldName() {
    final GrArgumentLabel label = myNamedArgument.getLabel();
    assert label != null;
    return label.getName();
  }

  private TypeConstraint[] calculateTypeConstrains() {
    final GrExpression expression = myNamedArgument.getExpression();
    PsiType type = null;
    if (expression != null) {
      type = expression.getType();
    }
    if (type != null) {
      return new TypeConstraint[]{SupertypeConstraint.create(type, type)};
    }
    else {
      return TypeConstraint.EMPTY_ARRAY;
    }
  }

  @NotNull
  @Override
  public String getName() {
    return GroovyBundle.message("create.field.from.usage", getFieldName());
  }

  @Nls
  @NotNull
  @Override
  public String getFamilyName() {
    return "Create field";
  }

  @Override
  protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
    myFix.doFix(project, ArrayUtil.EMPTY_STRING_ARRAY, getFieldName(), calculateTypeConstrains(), myNamedArgument);
  }
}

@@ -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
@@ -326,16 +326,64 @@
 siType;%0A
+import com.intellij.psi.SmartPsiElementPointer;%0A
 import c
@@ -1201,16 +1201,84 @@
 raint;%0A%0A
+import static com.intellij.psi.util.PointersKt.createSmartPointer;%0A%0A
 /**%0A * @
@@ -1425,16 +1425,39 @@
 e final 
+SmartPsiElementPointer%3C
 GrNamedA
@@ -1463,16 +1463,17 @@
 Argument
+%3E
  myNamed
@@ -1480,16 +1480,23 @@
 Argument
+Pointer
 ;%0A%0A  pub
@@ -1534,16 +1534,25 @@
 abelFix(
+@NotNull 
 GrTypeDe
@@ -1572,16 +1572,25 @@
 etClass,
+ @NotNull
  GrNamed
@@ -1679,19 +1679,45 @@
 Argument
- = 
+Pointer = createSmartPointer(
 namedArg
@@ -1721,16 +1721,17 @@
 Argument
+)
 ;%0A  %7D%0A%0A 
@@ -1771,24 +1771,141 @@
 eldName() %7B%0A
+    GrNamedArgument namedArgument = myNamedArgumentPointer.getElement();%0A    if (namedArgument == null) return null;%0A
     final Gr
@@ -1926,19 +1926,17 @@
 label = 
-myN
+n
 amedArgu
@@ -2021,16 +2021,23 @@
 private 
+static 
 TypeCons
@@ -2069,16 +2069,54 @@
 strains(
+@NotNull GrNamedArgument namedArgument
 ) %7B%0A    
@@ -2147,19 +2147,17 @@
 ssion = 
-myN
+n
 amedArgu
@@ -2807,16 +2807,202 @@
 ption %7B%0A
+    GrNamedArgument namedArgument = myNamedArgumentPointer.getElement();%0A    if (namedArgument == null) return;%0A    String fieldName = getFieldName();%0A    if (fieldName == null) return;%0A
     myFi
@@ -3048,28 +3048,25 @@
 _ARRAY, 
-getF
+f
 ieldName
 (), calc
@@ -3057,18 +3057,16 @@
 ieldName
-()
 , calcul
@@ -3087,14 +3087,25 @@
 ins(
-), myN
+namedArgument), n
 amed

fix psi class leak and CreateFieldFromConstructorLabelFix