/*
 * Copyright 2000-2017 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 com.intellij.codeInsight.daemon.impl.quickfix;

import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.ObjectUtils;
import com.siyeh.ig.psiutils.CommentTracker;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class DeleteElementFix extends LocalQuickFixAndIntentionActionOnPsiElement {
  private final String myText;

  public DeleteElementFix(@NotNull PsiElement element) {
    super(element);
    myText = null;
  }

  public DeleteElementFix(@NotNull PsiElement element, @NotNull @Nls String text) {
    super(element);
    myText = text;
  }

  @Nls
  @NotNull
  @Override
  public String getText() {
    return ObjectUtils.notNull(myText, getFamilyName());
  }

  @Nls
  @NotNull
  @Override
  public String getFamilyName() {
    return QuickFixBundle.message("delete.element.fix.text");
  }

  @Override
  public void invoke(@NotNull Project project,
                     @NotNull PsiFile file,
                     @Nullable Editor editor,
                     @NotNull PsiElement startElement,
                     @NotNull PsiElement endElement) {
    if (FileModificationService.getInstance().preparePsiElementForWrite(file)) {
      WriteAction.run(() -> new CommentTracker().deleteAndRestoreComments(startElement));
    }
  }

  @Override
  public boolean startInWriteAction() {
    return false;
  }
}
@@ -654,65 +654,8 @@
 x;%0A%0A
-import com.intellij.codeInsight.FileModificationService;%0A
 impo
@@ -789,61 +789,8 @@
 nt;%0A
-import com.intellij.openapi.application.WriteAction;%0A
 impo
@@ -2012,254 +2012,67 @@
     
-if (FileModificationService.getInstance().preparePsiElementForWrite(file)) %7B%0A      WriteAction.run(() -%3E new CommentTracker().deleteAndRestoreComments(startElement));%0A    %7D%0A  %7D%0A%0A  @Override%0A  public boolean startInWriteAction() %7B%0A    return false
+new CommentTracker().deleteAndRestoreComments(startElement)
 ;%0A  

delete element: simplify write access processing