package jetbrains.mps.baseLanguage.util.plugin.refactorings;

/*Generated by MPS */

import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.language.SConcept;

public class InlineFieldAssignmentRefactoring extends InlineFieldRefactoring {
  private SNode myVariable;

  public InlineFieldAssignmentRefactoring(SNode node) {
    this.myVariable = node;

  }
  @Override
  public SNode doRefactoring() {
    for (SNode reference : this.findAllReferences(this.myVariable)) {
      SNode expr = SNodeOperations.copyNode(SLinkOperations.getTarget(this.myVariable, LINKS.initializer$2twD));
      SNodeOperations.replaceWithAnother(reference, expr);
      InlinePrecedenceUtil.parenthesiseIfNecessary(expr);
    }
    for (SNode reference : this.findAllReferenceOperations(this.myVariable)) {
      SNode expr = SNodeOperations.copyNode(SLinkOperations.getTarget(this.myVariable, LINKS.initializer$2twD));
      SNodeOperations.replaceWithAnother(SNodeOperations.getNodeAncestor(reference, CONCEPTS.DotExpression$yW, false, false), expr);
      InlinePrecedenceUtil.parenthesiseIfNecessary(expr);
    }
    this.optimizeDeclaration(this.myVariable);
    return null;
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink initializer$2twD = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, 0xf8c37f506eL, "initializer");
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept DotExpression$yW = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x116b46a08c4L, "jetbrains.mps.baseLanguage.structure.DotExpression");
  }
}
