protected void doTest(final String extension) throws Exception {
    CommandProcessor.getInstance()
        .executeCommand(
            getProject(),
            () ->
                WriteCommandAction.runWriteCommandAction(
                    null,
                    () -> {
                      String fileName = getTestName(false) + extension;
                      try {
                        String text = loadFile(fileName);
                        PsiFile file = createFile(fileName, text);

                        JavaCodeStyleManager.getInstance(myProject).optimizeImports(file);
                        PostprocessReformattingAspect.getInstance(getProject())
                            .doPostponedFormatting();
                        PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
                        String textAfter = loadFile(getTestName(false) + "_after" + extension);
                        String fileText = file.getText();
                        assertEquals(textAfter, fileText);
                      } catch (Exception e) {
                        LOG.error(e);
                      }
                    }),
            "",
            "");
  }
  @Override
  protected void runTest() throws Throwable {
    if (getTestName(false).contains("NoVerify")) {
      super.runTest();
      return;
    }
    boolean oldVerify = RedBlackTree.VERIFY;
    RedBlackTree.VERIFY = !isPerformanceTest();
    final Throwable[] ex = {null};
    try {
      if (getTestName(false).contains("NoCommand")) {
        super.runTest();
        return;
      }
      WriteCommandAction.runWriteCommandAction(
          getProject(),
          new ThrowableComputable<Void, Throwable>() {
            @Override
            public Void compute() throws Throwable {
              RangeMarkerTest.super.runTest();
              return null;
            }
          });
    } finally {
      RedBlackTree.VERIFY = oldVerify;
    }

    if (ex[0] != null) throw ex[0];
  }
  public void testSoftWrapCacheReset() throws IOException {
    // Inspired by IDEA-76537 - the point is to drop cached document info on complete soft wraps
    // recalculation
    String text = "\t first line\n" + "\t second line\n" + "\t third line";

    // Make soft wraps to build a document info cache.
    init(40, text);

    // Modify document while soft wraps processing is off.
    final EditorSettings settings = getEditor().getSettings();
    settings.setUseSoftWraps(false);
    int startOffset = text.indexOf("\t third") - 1;
    WriteCommandAction.runWriteCommandAction(
        getProject(), () -> getEditor().getDocument().deleteString(startOffset, text.length()));

    // Enable soft wraps and ensure that the cache is correctly re-built.
    settings.setUseSoftWraps(true);

    getEditor().getCaretModel().moveToOffset(getEditor().getDocument().getTextLength());
    type("\n test");

    final int offset = getEditor().getDocument().getTextLength() - 1;
    final LogicalPosition logicalPosition = getEditor().offsetToLogicalPosition(offset);
    assertEquals(offset, getEditor().logicalPositionToOffset(logicalPosition));

    final VisualPosition visualPosition = getEditor().offsetToVisualPosition(offset);
    assertEquals(visualPosition, getEditor().logicalToVisualPosition(logicalPosition));
    assertEquals(logicalPosition, getEditor().visualToLogicalPosition(visualPosition));
  }
  public void testFoldRegionsUpdate() throws IOException {
    String text =
        "import java.util.List;\n"
            + "import java.util.ArrayList;\n"
            + "\n"
            + "class Test {\n"
            + "}";
    init(40, text);

    final int foldStartOffset = "import".length() + 1;
    int foldEndOffset = text.indexOf("class") - 2;
    addCollapsedFoldRegion(foldStartOffset, foldEndOffset, "...");

    // Simulate addition of the new import that modifies existing fold region.
    WriteCommandAction.runWriteCommandAction(
        getProject(),
        () -> myEditor.getDocument().insertString(foldEndOffset, "\nimport java.util.Date;\n"));

    final FoldingModel foldingModel = myEditor.getFoldingModel();
    foldingModel.runBatchFoldingOperation(
        () -> {
          FoldRegion oldFoldRegion = getFoldRegion(foldStartOffset);
          assertNotNull(oldFoldRegion);
          foldingModel.removeFoldRegion(oldFoldRegion);

          int newFoldEndOffset = myEditor.getDocument().getText().indexOf("class") - 2;
          FoldRegion newFoldRegion =
              foldingModel.addFoldRegion(foldStartOffset, newFoldEndOffset, "...");
          assertNotNull(newFoldRegion);
          newFoldRegion.setExpanded(false);
        });
    CodeFoldingManager.getInstance(getProject()).updateFoldRegions(myEditor);
    assertEquals(
        new VisualPosition(2, 0), myEditor.logicalToVisualPosition(new LogicalPosition(5, 0)));
  }
  private void performAction(
      String from,
      String to,
      final int docCommentPolicy,
      String[] expectedConflicts,
      final String[] toPullUp) {
    final JSClassResolver resolver =
        JSDialectSpecificHandlersFactory.forLanguage(JavaScriptSupportLoader.ECMA_SCRIPT_L4)
            .getClassResolver();
    final JSClass sourceClass =
        (JSClass) resolver.findClassByQName(from, GlobalSearchScope.projectScope(getProject()));
    assertNotNull("source class not found: " + sourceClass, sourceClass);

    final JSClass targetClass =
        (JSClass) resolver.findClassByQName(to, GlobalSearchScope.projectScope(getProject()));
    assertNotNull("target class not found: " + targetClass, targetClass);

    assertTrue(
        "Source should be a subclass of target",
        JSInheritanceUtil.isParentClass(sourceClass, targetClass));

    final List<JSMemberInfo> memberInfos = getMemberInfos(toPullUp, sourceClass, false);

    final JSMemberInfo[] infosArray =
        JSMemberInfo.getSelected(memberInfos, sourceClass, Conditions.<JSMemberInfo>alwaysTrue());
    MultiMap<PsiElement, String> conflicts =
        JSPullUpConflictsUtil.checkConflicts(
            infosArray,
            sourceClass,
            targetClass,
            new JSInterfaceContainmentVerifier() {
              @Override
              public boolean checkedInterfacesContain(JSFunction psiMethod) {
                return JSPullUpHelper.checkedInterfacesContain(memberInfos, psiMethod);
              }
            },
            JSVisibilityUtil.DEFAULT_OPTIONS);

    ArrayList<String> messages = new ArrayList<String>(conflicts.values());
    for (int i = 0; i < messages.size(); i++) {
      messages.set(i, messages.get(i).replaceAll("<[^>]+>", ""));
    }
    assertSameElements(messages, expectedConflicts);
    if (conflicts.isEmpty()) {
      WriteCommandAction.runWriteCommandAction(
          null,
          new Runnable() {
            public void run() {
              new JSPullUpHelper(sourceClass, targetClass, infosArray, docCommentPolicy)
                  .moveMembersToBase();
              myProject.getComponent(PostprocessReformattingAspect.class).doPostponedFormatting();
            }
          });

      FileDocumentManager.getInstance().saveAllDocuments();
    }
  }
  public void testMoveWithFoldRegionInside() throws Exception {
    initText("abc\ndef\nghi\n");
    configureSoftWraps(100);
    addCollapsedFoldRegion(0, 4, "...");

    WriteCommandAction.runWriteCommandAction(
        getProject(), () -> ((DocumentEx) myEditor.getDocument()).moveText(0, 4, 12));

    assertEquals(
        new LogicalPosition(2, 0), myEditor.visualToLogicalPosition(new VisualPosition(2, 1)));
  }
 private void checkOperatorPrecedence(
     @NotNull final String firstLine, @NotNull String resultPrefix) throws Exception {
   myFixture.configureByFile("/refactoring/inlinelocal/operatorPrecedence/template.py");
   WriteCommandAction.runWriteCommandAction(
       myFixture.getProject(),
       () -> myFixture.getEditor().getDocument().insertString(0, firstLine + "\n"));
   PsiDocumentManager.getInstance(myFixture.getProject()).commitAllDocuments();
   performRefactoring(null);
   myFixture.checkResultByFile(
       "/refactoring/inlinelocal/operatorPrecedence/" + resultPrefix + ".after.py");
   FileDocumentManager.getInstance().reloadFromDisk(myFixture.getDocument(myFixture.getFile()));
 }
  public void testFoldRegionEndingAtLineStart() throws IOException {
    init(100, "aaa\nbbb\nccc\nddd");
    addCollapsedFoldRegion(4, 8, "...");
    addCollapsedFoldRegion(13, 15, "...");

    WriteCommandAction.runWriteCommandAction(
        getProject(), () -> myEditor.getDocument().insertString(10, "C"));

    // verify that cached layout data is intact after document change and position recalculation is
    // done correctly
    assertEquals(
        new LogicalPosition(0, 0), myEditor.visualToLogicalPosition(new VisualPosition(0, 0)));
  }
  public void testInsertStringAtCaretNotMovingCaret() throws Exception {
    configureFromFileText(getTestName(false) + ".txt", "text <caret>");
    WriteCommandAction.runWriteCommandAction(
        getProject(),
        new Runnable() {
          @Override
          public void run() {
            EditorModificationUtil.insertStringAtCaret(myEditor, " ", false, false);
          }
        });

    checkResultByText("text <caret> ");
  }
    private void doGenerate(
        final Editor editor,
        final PsiFile file,
        final PsiClass targetClass,
        final TestFramework framework) {
      if (framework instanceof JavaTestFramework
          && ((JavaTestFramework) framework).isSingleConfig()) {
        PsiElement alreadyExist = null;
        switch (myMethodKind) {
          case SET_UP:
            alreadyExist = framework.findSetUpMethod(targetClass);
            break;
          case TEAR_DOWN:
            alreadyExist = framework.findTearDownMethod(targetClass);
            break;
          default:
            break;
        }

        if (alreadyExist instanceof PsiMethod) {
          editor.getCaretModel().moveToOffset(alreadyExist.getNavigationElement().getTextOffset());
          HintManager.getInstance()
              .showErrorHint(
                  editor, "Method " + ((PsiMethod) alreadyExist).getName() + " already exists");
          return;
        }
      }

      if (!CommonRefactoringUtil.checkReadOnlyStatus(file)) return;

      WriteCommandAction.runWriteCommandAction(
          file.getProject(),
          new Runnable() {
            @Override
            public void run() {
              try {
                PsiDocumentManager.getInstance(file.getProject()).commitAllDocuments();
                PsiMethod method = generateDummyMethod(editor, file);
                if (method == null) return;

                TestIntegrationUtils.runTestMethodTemplate(
                    myMethodKind, framework, editor, targetClass, method, "name", false, null);
              } catch (IncorrectOperationException e) {
                HintManager.getInstance()
                    .showErrorHint(editor, "Cannot generate method: " + e.getMessage());
                LOG.warn(e);
              }
            }
          });
    }
 protected static void delete(@NotNull final VirtualFile file) {
   WriteCommandAction.runWriteCommandAction(
       null,
       new Runnable() {
         @Override
         public void run() {
           try {
             file.delete(null);
           } catch (IOException e) {
             throw new RuntimeException(e);
           }
         }
       });
 }
  @SuppressWarnings("ConstantConditions")
  public void testCustomTagCompletion0() throws Throwable {
    final VirtualFile labelViewJava =
        copyFileToProject("LabelView.java", "src/p1/p2/LabelView.java");

    VirtualFile lf1 =
        myFixture.copyFileToProject(testFolder + '/' + "ctn0.xml", "res/layout/layout1.xml");
    myFixture.configureFromExistingVirtualFile(lf1);
    myFixture.complete(CompletionType.BASIC);
    List<String> variants = myFixture.getLookupElementStrings();
    assertTrue(variants.contains("p1.p2.LabelView"));

    final PsiFile psiLabelViewFile = PsiManager.getInstance(getProject()).findFile(labelViewJava);
    assertInstanceOf(psiLabelViewFile, PsiJavaFile.class);
    final PsiClass labelViewClass = ((PsiJavaFile) psiLabelViewFile).getClasses()[0];
    assertNotNull(labelViewClass);
    myFixture.renameElement(labelViewClass, "LabelView1");

    VirtualFile lf2 =
        myFixture.copyFileToProject(testFolder + '/' + "ctn0.xml", "res/layout/layout2.xml");
    myFixture.configureFromExistingVirtualFile(lf2);
    myFixture.complete(CompletionType.BASIC);
    variants = myFixture.getLookupElementStrings();
    assertFalse(variants.contains("p1.p2.LabelView"));
    assertTrue(variants.contains("p1.p2.LabelView1"));

    WriteCommandAction.runWriteCommandAction(
        null,
        new Runnable() {
          @Override
          public void run() {
            try {
              labelViewJava.delete(null);
            } catch (IOException e) {
              throw new RuntimeException(e);
            }
          }
        });

    VirtualFile lf3 =
        myFixture.copyFileToProject(testFolder + '/' + "ctn0.xml", "res/layout/layout3.xml");
    myFixture.configureFromExistingVirtualFile(lf3);
    myFixture.complete(CompletionType.BASIC);
    variants = myFixture.getLookupElementStrings();
    assertFalse(variants.contains("p1.p2.LabelView"));
    assertFalse(variants.contains("p1.p2.LabelView1"));
  }
  public void testSavedUncommittedDocument() throws IOException {
    VirtualFile dir = getVirtualFile(createTempDirectory());
    PsiTestUtil.addSourceContentToRoots(myModule, dir);

    final VirtualFile vFile = createChildData(dir, "Foo.java");
    VfsUtil.saveText(vFile, "");

    final GlobalSearchScope scope = GlobalSearchScope.allScope(getProject());
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
    assertNull(facade.findClass("Foo", scope));
    WriteCommandAction.runWriteCommandAction(
        null,
        new Runnable() {
          @Override
          public void run() {
            PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(vFile);
            assertNotNull(psiFile);

            long count =
                PsiManager.getInstance(myProject).getModificationTracker().getModificationCount();

            Document document = FileDocumentManager.getInstance().getDocument(vFile);
            document.insertString(0, "class Foo {}");
            FileDocumentManager.getInstance().saveDocument(document);

            assertTrue(
                count
                    == PsiManager.getInstance(myProject)
                        .getModificationTracker()
                        .getModificationCount());
            assertNull(facade.findClass("Foo", scope));

            PsiDocumentManager.getInstance(myProject).commitAllDocuments();
            assertNotNull(facade.findClass("Foo", scope));
            assertNotNull(facade.findClass("Foo", scope).getText());
            // if Foo exists now, mod count should be different
            assertTrue(
                count
                    != PsiManager.getInstance(myProject)
                        .getModificationTracker()
                        .getModificationCount());
          }
        });
  }
 public void testOnClickIntention() throws Throwable {
   myFixture.copyFileToProject(testFolder + "/OnClickActivity.java", "src/p1/p2/Activity1.java");
   final VirtualFile file = copyFileToProject("onClickIntention.xml");
   myFixture.configureFromExistingVirtualFile(file);
   final AndroidCreateOnClickHandlerAction action = new AndroidCreateOnClickHandlerAction();
   assertTrue(
       action.isAvailable(myFixture.getProject(), myFixture.getEditor(), myFixture.getFile()));
   WriteCommandAction.runWriteCommandAction(
       null,
       new Runnable() {
         @Override
         public void run() {
           action.invoke(myFixture.getProject(), myFixture.getEditor(), myFixture.getFile());
         }
       });
   myFixture.checkResultByFile(testFolder + "/onClickIntention.xml");
   myFixture.checkResultByFile(
       "src/p1/p2/Activity1.java", testFolder + "/OnClickActivity_after.java", false);
 }
  public void testOnClickQuickFix2() throws Throwable {
    myFixture.enableInspections(AndroidMissingOnClickHandlerInspection.class);
    myFixture.copyFileToProject(testFolder + "/OnClickActivity1.java", "src/p1/p2/Activity1.java");
    final VirtualFile file = copyFileToProject("onClickIntention.xml");
    myFixture.configureFromExistingVirtualFile(file);
    final List<IntentionAction> actions =
        highlightAndFindQuickFixes(AndroidMissingOnClickHandlerInspection.MyQuickFix.class);
    assertEquals(1, actions.size());
    WriteCommandAction.runWriteCommandAction(
        null,
        new Runnable() {
          @Override
          public void run() {
            actions.get(0).invoke(getProject(), myFixture.getEditor(), myFixture.getFile());
          }
        });

    myFixture.checkResultByFile(testFolder + "/onClickIntention.xml");
    myFixture.checkResultByFile(
        "src/p1/p2/Activity1.java", testFolder + "/OnClickActivity1_after.java", false);
  }
 @Override
 protected void runTest() throws Throwable {
   if (isRunInWriteAction()) {
     WriteCommandAction.runWriteCommandAction(
         getProject(),
         new ThrowableComputable<Void, Throwable>() {
           @Override
           public Void compute() throws Throwable {
             doRunTest();
             return null;
           }
         });
   } else {
     new WriteCommandAction.Simple(getProject()) {
       @Override
       protected void run() throws Throwable {
         doRunTest();
       }
     }.performCommand();
   }
 }
  public void testCollectedPsiWithChangedDocument() throws IOException {
    VirtualFile dir = getVirtualFile(createTempDirectory());
    PsiTestUtil.addSourceContentToRoots(myModule, dir);

    final VirtualFile vFile = createChildData(dir, "Foo.java");
    VfsUtil.saveText(vFile, "class Foo {}");

    final GlobalSearchScope scope = GlobalSearchScope.allScope(getProject());
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
    assertNotNull(facade.findClass("Foo", scope));
    WriteCommandAction.runWriteCommandAction(
        null,
        new Runnable() {
          @Override
          public void run() {
            PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(vFile);
            assertNotNull(psiFile);

            Document document = FileDocumentManager.getInstance().getDocument(vFile);
            document.deleteString(0, document.getTextLength());
            assertNotNull(facade.findClass("Foo", scope));

            psiFile = null;
            PlatformTestUtil.tryGcSoftlyReachableObjects();
            assertNull(
                ((PsiManagerEx) PsiManager.getInstance(getProject()))
                    .getFileManager()
                    .getCachedPsiFile(vFile));

            PsiClass foo = facade.findClass("Foo", scope);
            assertNotNull(foo);
            assertTrue(foo.isValid());
            assertEquals("class Foo {}", foo.getText());
            assertTrue(foo.isValid());

            PsiDocumentManager.getInstance(myProject).commitAllDocuments();
            assertNull(facade.findClass("Foo", scope));
          }
        });
  }
  public void testModificationOfSoftWrappedFoldRegion() throws IOException {
    String text =
        "import java.util.List;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;\n"
            + "import java.util.LinkedList;\n"
            + "import java.util.Set;\n"
            + "\n"
            + "class Test {\n"
            + "}";
    init(40, text);

    final int foldStartOffset = text.indexOf("java.util.Collections");
    final int foldEndOffset = text.indexOf("class") - 2;
    addCollapsedFoldRegion(foldStartOffset, foldEndOffset, "...");

    int modificationOffset = text.indexOf("java.util.Set");
    WriteCommandAction.runWriteCommandAction(
        getProject(),
        () ->
            myEditor.getDocument().insertString(modificationOffset, "import java.util.HashSet;\n"));

    // Used to get StackOverflowError here, hence, no additional checking is performed.
  }
 private List<PsiFile> createPropertiesFiles() {
   final Set<String> fileNames = getFileNamesToCreate();
   final List<PsiFile> createdFiles =
       WriteCommandAction.runWriteCommandAction(
           myProject,
           new Computable<List<PsiFile>>() {
             @Override
             public List<PsiFile> compute() {
               return ReadAction.compute(
                   () ->
                       ContainerUtil.map(
                           fileNames,
                           n -> {
                             final boolean isXml =
                                 myResourceBundle == null
                                     ? myUseXMLBasedPropertiesCheckBox.isSelected()
                                     : myResourceBundle.getDefaultPropertiesFile()
                                         instanceof XmlPropertiesFile;
                             if (isXml) {
                               FileTemplate template =
                                   FileTemplateManager.getInstance(myProject)
                                       .getInternalTemplate("XML Properties File.xml");
                               LOG.assertTrue(template != null);
                               try {
                                 return (PsiFile)
                                     FileTemplateUtil.createFromTemplate(
                                         template, n, null, myDirectory);
                               } catch (Exception e) {
                                 throw new RuntimeException(e);
                               }
                             } else {
                               return myDirectory.createFile(n);
                             }
                           }));
             }
           });
   combineToResourceBundleIfNeed(createdFiles);
   return createdFiles;
 }