Search in sources :

Example 1 with UndoManagerImpl

use of com.intellij.openapi.command.impl.UndoManagerImpl in project intellij-community by JetBrains.

the class ImportHelperTest method testAutoImportCaretLocation2.

public void testAutoImportCaretLocation2() throws Throwable {
    boolean old = CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY;
    try {
        CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = true;
        configureByText(StdFileTypes.JAVA, "class X { <caret>ArrayList c = new ArrayList(); }");
        ((UndoManagerImpl) UndoManager.getInstance(getProject())).flushCurrentCommandMerger();
        ((UndoManagerImpl) UndoManager.getInstance(getProject())).clearUndoRedoQueueInTests(getFile().getVirtualFile());
        type(" ");
        backspace();
        assertEquals(2, highlightErrors().size());
        UIUtil.dispatchAllInvocationEvents();
        int offset = myEditor.getCaretModel().getOffset();
        PsiReference ref = myFile.findReferenceAt(offset);
        assertTrue(ref instanceof PsiJavaCodeReferenceElement);
        ImportClassFixBase.Result result = new ImportClassFix((PsiJavaCodeReferenceElement) ref).doFix(getEditor(), true, false);
        assertEquals(ImportClassFixBase.Result.CLASS_AUTO_IMPORTED, result);
        UIUtil.dispatchAllInvocationEvents();
        assertEmpty(highlightErrors());
    } finally {
        CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = old;
    }
}
Also used : UndoManagerImpl(com.intellij.openapi.command.impl.UndoManagerImpl) ImportClassFixBase(com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFixBase) ImportClassFix(com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFix)

Example 2 with UndoManagerImpl

use of com.intellij.openapi.command.impl.UndoManagerImpl in project intellij-community by JetBrains.

the class RangeMarkerTest method testRandomEdit_NoCommand.

public void testRandomEdit_NoCommand() {
    final int N = 100;
    final Random gen = new Random();
    int N_TRIES = Timings.adjustAccordingToMySpeed(7000, false);
    System.out.println("N_TRIES = " + N_TRIES);
    DocumentEx document = null;
    for (int tryn = 0; tryn < N_TRIES; tryn++) {
        ((UndoManagerImpl) UndoManager.getInstance(getProject())).flushCurrentCommandMerger();
        ((UndoManagerImpl) UndoManager.getGlobalInstance()).flushCurrentCommandMerger();
        if (document != null) {
            ((UndoManagerImpl) UndoManager.getInstance(getProject())).clearUndoRedoQueueInTests(document);
            ((UndoManagerImpl) UndoManager.getGlobalInstance()).clearUndoRedoQueueInTests(document);
        }
        if (tryn % 10000 == 0) {
            System.out.println(tryn);
        }
        document = (DocumentEx) EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', N));
        final DocumentEx finalDocument = document;
        new WriteCommandAction(getProject()) {

            @Override
            protected void run(@NotNull Result result) throws Exception {
                List<Pair<RangeMarker, TextRange>> adds = new ArrayList<>();
                List<Pair<RangeMarker, TextRange>> dels = new ArrayList<>();
                List<Trinity<Integer, Integer, Integer>> edits = new ArrayList<>();
                try {
                    for (int i = 0; i < 30; i++) {
                        int x = gen.nextInt(N);
                        int y = x + gen.nextInt(N - x);
                        RangeMarkerEx r = (RangeMarkerEx) finalDocument.createRangeMarker(x, y);
                        adds.add(Pair.create(r, TextRange.create(r)));
                    }
                    for (int i = 0; i < 10; i++) {
                        int offset = gen.nextInt(finalDocument.getTextLength());
                        if (gen.nextBoolean()) {
                            int length = gen.nextInt(5);
                            edits.add(Trinity.create(offset, 0, length));
                            finalDocument.insertString(offset, StringUtil.repeatSymbol(' ', length));
                        } else {
                            int length = gen.nextInt(finalDocument.getTextLength() - offset);
                            edits.add(Trinity.create(offset, length, 0));
                            finalDocument.deleteString(offset, offset + length);
                        }
                    }
                    List<Pair<RangeMarker, TextRange>> candidates = new ArrayList<>(adds);
                    while (!candidates.isEmpty()) {
                        int size = candidates.size();
                        int x = gen.nextInt(size);
                        Pair<RangeMarker, TextRange> c = candidates.remove(x);
                        RangeMarkerEx r = (RangeMarkerEx) c.first;
                        assertEquals(size - 1, candidates.size());
                        dels.add(c);
                        r.dispose();
                    }
                } catch (AssertionError e) {
                    String s = "adds: ";
                    for (Pair<RangeMarker, TextRange> c : adds) {
                        TextRange t = c.second;
                        s += t.getStartOffset() + "," + t.getEndOffset() + ", ";
                    }
                    s += "\nedits: ";
                    for (Trinity<Integer, Integer, Integer> edit : edits) {
                        s += edit.first + "," + edit.second + "," + edit.third + ",  ";
                    }
                    s += "\ndels: ";
                    for (Pair<RangeMarker, TextRange> c : dels) {
                        int index = adds.indexOf(c);
                        assertSame(c, adds.get(index));
                        s += index + ", ";
                    }
                    System.err.println(s);
                    throw e;
                }
            }
        }.execute();
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) RangeMarkerEx(com.intellij.openapi.editor.ex.RangeMarkerEx) TextRange(com.intellij.openapi.util.TextRange) Result(com.intellij.openapi.application.Result) DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) UndoManagerImpl(com.intellij.openapi.command.impl.UndoManagerImpl) WeakList(com.intellij.util.containers.WeakList) Pair(com.intellij.openapi.util.Pair)

Example 3 with UndoManagerImpl

use of com.intellij.openapi.command.impl.UndoManagerImpl in project intellij-community by JetBrains.

the class PlatformTestCase method cleanupApplicationCaches.

public static void cleanupApplicationCaches(Project project) {
    if (project != null && !project.isDisposed()) {
        UndoManagerImpl globalInstance = (UndoManagerImpl) UndoManager.getGlobalInstance();
        if (globalInstance != null) {
            globalInstance.dropHistoryInTests();
        }
        ((UndoManagerImpl) UndoManager.getInstance(project)).dropHistoryInTests();
        ((DocumentReferenceManagerImpl) DocumentReferenceManager.getInstance()).cleanupForNextTest();
        ((PsiManagerImpl) PsiManager.getInstance(project)).cleanupForNextTest();
    }
    final ProjectManager projectManager = ProjectManager.getInstance();
    assert projectManager != null : "The ProjectManager is not initialized yet";
    ProjectManagerImpl projectManagerImpl = (ProjectManagerImpl) projectManager;
    if (projectManagerImpl.isDefaultProjectInitialized()) {
        Project defaultProject = projectManager.getDefaultProject();
        ((PsiManagerImpl) PsiManager.getInstance(defaultProject)).cleanupForNextTest();
    }
    AsyncHighlighterUpdater.completeAsyncTasks();
    ((FileBasedIndexImpl) FileBasedIndex.getInstance()).cleanupForNextTest();
    LocalFileSystemImpl localFileSystem = (LocalFileSystemImpl) LocalFileSystem.getInstance();
    if (localFileSystem != null) {
        localFileSystem.cleanupForNextTest();
    }
}
Also used : UndoManagerImpl(com.intellij.openapi.command.impl.UndoManagerImpl) Project(com.intellij.openapi.project.Project) DocumentReferenceManagerImpl(com.intellij.openapi.command.impl.DocumentReferenceManagerImpl) PsiManagerImpl(com.intellij.psi.impl.PsiManagerImpl) FileBasedIndexImpl(com.intellij.util.indexing.FileBasedIndexImpl) LocalFileSystemImpl(com.intellij.openapi.vfs.impl.local.LocalFileSystemImpl) ProjectManager(com.intellij.openapi.project.ProjectManager) ProjectManagerImpl(com.intellij.openapi.project.impl.ProjectManagerImpl)

Example 4 with UndoManagerImpl

use of com.intellij.openapi.command.impl.UndoManagerImpl in project intellij-community by JetBrains.

the class ImportHelperTest method testAutoImportCaretLocation.

public void testAutoImportCaretLocation() throws Throwable {
    boolean old = CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY;
    try {
        CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = true;
        configureByText(StdFileTypes.JAVA, "class X { ArrayList<caret> c; }");
        ((UndoManagerImpl) UndoManager.getInstance(getProject())).flushCurrentCommandMerger();
        ((UndoManagerImpl) UndoManager.getInstance(getProject())).clearUndoRedoQueueInTests(getFile().getVirtualFile());
        type(" ");
        backspace();
        assertOneElement(highlightErrors());
        int offset = myEditor.getCaretModel().getOffset();
        PsiReference ref = myFile.findReferenceAt(offset - 1);
        assertTrue(ref instanceof PsiJavaCodeReferenceElement);
        ImportClassFixBase.Result result = new ImportClassFix((PsiJavaCodeReferenceElement) ref).doFix(getEditor(), true, false);
        assertEquals(ImportClassFixBase.Result.POPUP_NOT_SHOWN, result);
        UIUtil.dispatchAllInvocationEvents();
        myEditor.getCaretModel().moveToOffset(offset - 1);
        result = new ImportClassFix((PsiJavaCodeReferenceElement) ref).doFix(getEditor(), true, false);
        assertEquals(ImportClassFixBase.Result.CLASS_AUTO_IMPORTED, result);
        UIUtil.dispatchAllInvocationEvents();
        assertEmpty(highlightErrors());
    } finally {
        CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = old;
    }
}
Also used : UndoManagerImpl(com.intellij.openapi.command.impl.UndoManagerImpl) ImportClassFixBase(com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFixBase) ImportClassFix(com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFix)

Example 5 with UndoManagerImpl

use of com.intellij.openapi.command.impl.UndoManagerImpl in project intellij-community by JetBrains.

the class FormatterTestCase method doSanityTestForFile.

private void doSanityTestForFile(final File subFile, final List<File> failedFiles, final boolean formatWithPsi) throws IOException, IncorrectOperationException {
    if (subFile.isFile() && subFile.getName().endsWith(getFileExtension())) {
        final byte[] bytes = FileUtil.loadFileBytes(subFile);
        final String text = new String(bytes);
        final String fileName = "before." + getFileExtension();
        final PsiFile file = PsiFileFactory.getInstance(getProject()).createFileFromText(fileName, getFileType(fileName), StringUtil.convertLineSeparators(text), LocalTimeCounter.currentTime(), true);
        try {
            CommandProcessor.getInstance().executeCommand(getProject(), () -> ApplicationManager.getApplication().runWriteAction(() -> {
                try {
                    if (formatWithPsi) {
                        performFormatting(file);
                    } else {
                        performFormattingWithDocument(file);
                    }
                } catch (Throwable e) {
                    //noinspection CallToPrintStackTrace
                    e.printStackTrace();
                    failedFiles.add(subFile);
                }
                //noinspection UseOfSystemOutOrSystemErr
                System.out.println(subFile.getPath() + ": finished");
            }), "", null);
        } finally {
            final VirtualFile virtualFile = file.getVirtualFile();
            if (virtualFile != null) {
                ((UndoManagerImpl) UndoManager.getInstance(getProject())).clearUndoRedoQueueInTests(virtualFile);
                ((UndoManagerImpl) UndoManager.getGlobalInstance()).clearUndoRedoQueueInTests(virtualFile);
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) UndoManagerImpl(com.intellij.openapi.command.impl.UndoManagerImpl) PsiFile(com.intellij.psi.PsiFile)

Aggregations

UndoManagerImpl (com.intellij.openapi.command.impl.UndoManagerImpl)6 ImportClassFix (com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFix)2 ImportClassFixBase (com.intellij.codeInsight.daemon.impl.quickfix.ImportClassFixBase)2 IProperty (com.intellij.lang.properties.IProperty)1 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)1 XmlPropertiesFile (com.intellij.lang.properties.xml.XmlPropertiesFile)1 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 DocumentReferenceManagerImpl (com.intellij.openapi.command.impl.DocumentReferenceManagerImpl)1 DocumentEx (com.intellij.openapi.editor.ex.DocumentEx)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1 RangeMarkerEx (com.intellij.openapi.editor.ex.RangeMarkerEx)1 Project (com.intellij.openapi.project.Project)1 ProjectManager (com.intellij.openapi.project.ProjectManager)1 ProjectManagerImpl (com.intellij.openapi.project.impl.ProjectManagerImpl)1 Pair (com.intellij.openapi.util.Pair)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 LocalFileSystemImpl (com.intellij.openapi.vfs.impl.local.LocalFileSystemImpl)1 PsiFile (com.intellij.psi.PsiFile)1