Search in sources :

Example 51 with TextEditor

use of com.intellij.openapi.fileEditor.TextEditor in project intellij-community by JetBrains.

the class DaemonRespondToChangesTest method testReactivityPerformance.

public void testReactivityPerformance() throws Throwable {
    @NonNls String filePath = "/psi/resolve/Thinlet.java";
    configureByFile(filePath);
    type(' ');
    CompletionContributor.forLanguage(getFile().getLanguage());
    highlightErrors();
    final DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl) DaemonCodeAnalyzer.getInstance(getProject());
    int N = Math.max(5, Timings.adjustAccordingToMySpeed(80, false));
    System.out.println("N = " + N);
    final long[] interruptTimes = new long[N];
    for (int i = 0; i < N; i++) {
        codeAnalyzer.restart();
        final int finalI = i;
        final long start = System.currentTimeMillis();
        final AtomicLong typingStart = new AtomicLong();
        final AtomicReference<RuntimeException> exception = new AtomicReference<>();
        Thread watcher = new Thread("reactivity watcher") {

            @Override
            public void run() {
                while (true) {
                    final long start1 = typingStart.get();
                    if (start1 == -1)
                        break;
                    if (start1 == 0) {
                        try {
                            Thread.sleep(5);
                        } catch (InterruptedException e1) {
                            throw new RuntimeException(e1);
                        }
                        continue;
                    }
                    long elapsed = System.currentTimeMillis() - start1;
                    if (elapsed > 500) {
                        // too long, see WTF
                        String message = "Too long interrupt: " + elapsed + "; Progress: " + codeAnalyzer.getUpdateProgress() + "\n----------------------------";
                        dumpThreadsToConsole();
                        exception.set(new RuntimeException(message));
                        throw exception.get();
                    }
                }
            }
        };
        try {
            PsiFile file = getFile();
            Editor editor = getEditor();
            Project project = file.getProject();
            CodeInsightTestFixtureImpl.ensureIndexesUpToDate(project);
            TextEditor textEditor = TextEditorProvider.getInstance().getTextEditor(editor);
            PsiDocumentManager.getInstance(myProject).commitAllDocuments();
            watcher.start();
            Runnable interrupt = () -> {
                long now = System.currentTimeMillis();
                if (now - start < 100) {
                    // wait to engage all highlighting threads
                    return;
                }
                typingStart.set(System.currentTimeMillis());
                type(' ');
                long end = System.currentTimeMillis();
                long interruptTime = end - now;
                interruptTimes[finalI] = interruptTime;
                assertTrue(codeAnalyzer.getUpdateProgress().isCanceled());
                System.out.println(interruptTime);
                throw new ProcessCanceledException();
            };
            long hiStart = System.currentTimeMillis();
            codeAnalyzer.runPasses(file, editor.getDocument(), textEditor, ArrayUtil.EMPTY_INT_ARRAY, false, interrupt);
            long hiEnd = System.currentTimeMillis();
            DaemonProgressIndicator progress = codeAnalyzer.getUpdateProgress();
            String message = "Should have been interrupted: " + progress + "; Elapsed: " + (hiEnd - hiStart) + "ms";
            dumpThreadsToConsole();
            throw new RuntimeException(message);
        } catch (ProcessCanceledException ignored) {
        } finally {
            // cancel watcher
            typingStart.set(-1);
            watcher.join();
            if (exception.get() != null) {
                throw exception.get();
            }
        }
    }
    long ave = ArrayUtil.averageAmongMedians(interruptTimes, 3);
    System.out.println("Average among the N/3 median times: " + ave + "ms");
    assertTrue(ave < 300);
}
Also used : NonNls(org.jetbrains.annotations.NonNls) AtomicReference(java.util.concurrent.atomic.AtomicReference) LightweightHint(com.intellij.ui.LightweightHint) Project(com.intellij.openapi.project.Project) AtomicLong(java.util.concurrent.atomic.AtomicLong) TextEditor(com.intellij.openapi.fileEditor.TextEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 52 with TextEditor

use of com.intellij.openapi.fileEditor.TextEditor in project intellij-community by JetBrains.

the class DaemonRespondToChangesTest method testCancelsItSelfOnTypingInAlienProject.

public void testCancelsItSelfOnTypingInAlienProject() throws Throwable {
    String body = StringUtil.repeat("\"String field = null;\"\n", 1000);
    configureByText(StdFileTypes.JAVA, "class X{ void f() {" + body + "<caret>\n} }");
    File temp = createTempDirectory();
    final Project alienProject = createProject(temp + "/alien.ipr", DebugUtil.currentStackTrace());
    boolean succ2 = ProjectManagerEx.getInstanceEx().openProject(alienProject);
    assertTrue(succ2);
    DaemonProgressIndicator.setDebug(true);
    try {
        Module alienModule = doCreateRealModuleIn("x", alienProject, getModuleType());
        final VirtualFile alienRoot = PsiTestUtil.createTestProjectStructure(alienProject, alienModule, myFilesToDelete);
        OpenFileDescriptor alienDescriptor = new WriteAction<OpenFileDescriptor>() {

            @Override
            protected void run(@NotNull Result<OpenFileDescriptor> result) throws Throwable {
                VirtualFile alienFile = alienRoot.createChildData(this, "X.java");
                setFileText(alienFile, "class Alien { }");
                OpenFileDescriptor alienDescriptor = new OpenFileDescriptor(alienProject, alienFile);
                result.setResult(alienDescriptor);
            }
        }.execute().throwException().getResultObject();
        FileEditorManager fe = FileEditorManager.getInstance(alienProject);
        final Editor alienEditor = fe.openTextEditor(alienDescriptor, false);
        ((EditorImpl) alienEditor).setCaretActive();
        PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
        PsiDocumentManager.getInstance(alienProject).commitAllDocuments();
        // start daemon in main project. should check for its cancel when typing in alien
        TextEditor textEditor = TextEditorProvider.getInstance().getTextEditor(getEditor());
        DaemonCodeAnalyzerImpl di = (DaemonCodeAnalyzerImpl) DaemonCodeAnalyzer.getInstance(getProject());
        final boolean[] checked = { false };
        di.runPasses(getFile(), getEditor().getDocument(), textEditor, ArrayUtil.EMPTY_INT_ARRAY, true, () -> {
            if (checked[0])
                return;
            checked[0] = true;
            typeInAlienEditor(alienEditor, 'x');
        });
    } catch (ProcessCanceledException ignored) {
        //System.out.println("indicator = " + indicator[0]);
        return;
    } finally {
        ProjectManagerEx.getInstanceEx().closeAndDispose(alienProject);
    }
    fail("must throw PCE");
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) EditorImpl(com.intellij.openapi.editor.impl.EditorImpl) Project(com.intellij.openapi.project.Project) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) TextEditor(com.intellij.openapi.fileEditor.TextEditor) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Module(com.intellij.openapi.module.Module) TextEditor(com.intellij.openapi.fileEditor.TextEditor) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 53 with TextEditor

use of com.intellij.openapi.fileEditor.TextEditor in project intellij-community by JetBrains.

the class DaemonRespondToChangesTest method _testCaretMovementDoesNotRestartHighlighting.

public void _testCaretMovementDoesNotRestartHighlighting() throws Exception {
    configureByText(JavaFileType.INSTANCE, "class X { int f = \"error\"; int f() { int gg<caret> = 11; return 0;} }");
    TextEditor textEditor = TextEditorProvider.getInstance().getTextEditor(getEditor());
    final DaemonCodeAnalyzerImpl di = (DaemonCodeAnalyzerImpl) DaemonCodeAnalyzer.getInstance(getProject());
    final AtomicReference<ProgressIndicator> indicator = new AtomicReference<>();
    final List<HighlightInfo> errors = filter(di.runPasses(getFile(), getEditor().getDocument(), textEditor, ArrayUtil.EMPTY_INT_ARRAY, false, () -> {
        if (indicator.get() == null) {
            indicator.set(di.getUpdateProgress());
        }
        assertSame(indicator.get(), di.getUpdateProgress());
        caretRight();
        if (getEditor().getCaretModel().getOffset() == getEditor().getDocument().getTextLength() - 1) {
            getEditor().getCaretModel().moveToOffset(0);
        }
    }), HighlightSeverity.ERROR);
    assertEquals(1, errors.size());
    assertEquals("Incompatible types. Found: 'java.lang.String', required: 'int'", errors.get(0).getDescription());
}
Also used : TextEditor(com.intellij.openapi.fileEditor.TextEditor) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 54 with TextEditor

use of com.intellij.openapi.fileEditor.TextEditor in project intellij-community by JetBrains.

the class DaemonRespondToChangesTest method testTypingLatencyPerformance.

public void testTypingLatencyPerformance() throws Throwable {
    @NonNls String filePath = "/psi/resolve/ThinletBig.java";
    configureByFile(filePath);
    type(' ');
    CompletionContributor.forLanguage(getFile().getLanguage());
    long s = System.currentTimeMillis();
    highlightErrors();
    long e = System.currentTimeMillis();
    //System.out.println("Hi elapsed: "+(e-s));
    final DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl) DaemonCodeAnalyzer.getInstance(getProject());
    int N = Math.max(5, Timings.adjustAccordingToMySpeed(80, false));
    System.out.println("N = " + N);
    final long[] interruptTimes = new long[N];
    for (int i = 0; i < N; i++) {
        codeAnalyzer.restart();
        final int finalI = i;
        final long start = System.currentTimeMillis();
        Runnable interrupt = () -> {
            long now = System.currentTimeMillis();
            if (now - start < 100) {
                // wait to engage all highlighting threads
                return;
            }
            type(' ');
            long end = System.currentTimeMillis();
            long interruptTime = end - now;
            interruptTimes[finalI] = interruptTime;
            assertTrue(codeAnalyzer.getUpdateProgress().isCanceled());
            System.out.println(interruptTime);
            throw new ProcessCanceledException();
        };
        try {
            PsiFile file = getFile();
            Editor editor = getEditor();
            Project project = file.getProject();
            CodeInsightTestFixtureImpl.ensureIndexesUpToDate(project);
            TextEditor textEditor = TextEditorProvider.getInstance().getTextEditor(editor);
            PsiDocumentManager.getInstance(myProject).commitAllDocuments();
            codeAnalyzer.runPasses(file, editor.getDocument(), textEditor, ArrayUtil.EMPTY_INT_ARRAY, false, interrupt);
            throw new RuntimeException("should have been interrupted");
        } catch (ProcessCanceledException ignored) {
        }
        backspace();
    //highlightErrors();
    }
    long mean = ArrayUtil.averageAmongMedians(interruptTimes, 3);
    long avg = Arrays.stream(interruptTimes).sum() / interruptTimes.length;
    long max = Arrays.stream(interruptTimes).max().getAsLong();
    long min = Arrays.stream(interruptTimes).min().getAsLong();
    System.out.println("Average among the N/3 median times: " + mean + "ms; max: " + max + "; min:" + min + "; avg: " + avg);
    assertTrue(String.valueOf(mean), mean < 10);
}
Also used : NonNls(org.jetbrains.annotations.NonNls) LightweightHint(com.intellij.ui.LightweightHint) Project(com.intellij.openapi.project.Project) TextEditor(com.intellij.openapi.fileEditor.TextEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 55 with TextEditor

use of com.intellij.openapi.fileEditor.TextEditor in project intellij-community by JetBrains.

the class DaemonRespondToChangesTest method testAllPassesFinishAfterInterruptOnTyping_Performance.

public void testAllPassesFinishAfterInterruptOnTyping_Performance() throws Throwable {
    @NonNls String filePath = "/psi/resolve/Thinlet.java";
    configureByFile(filePath);
    highlightErrors();
    final DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl) DaemonCodeAnalyzer.getInstance(getProject());
    type(' ');
    for (int i = 0; i < 100; i++) {
        backspace();
        codeAnalyzer.restart();
        try {
            PsiDocumentManager.getInstance(myProject).commitAllDocuments();
            PsiFile file = getFile();
            Editor editor = getEditor();
            Project project = file.getProject();
            CodeInsightTestFixtureImpl.ensureIndexesUpToDate(project);
            TextEditor textEditor = TextEditorProvider.getInstance().getTextEditor(editor);
            codeAnalyzer.runPasses(file, editor.getDocument(), textEditor, ArrayUtil.EMPTY_INT_ARRAY, true, () -> type(' '));
        } catch (ProcessCanceledException ignored) {
            codeAnalyzer.waitForTermination();
            continue;
        }
        fail("PCE must have been thrown");
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) Project(com.intellij.openapi.project.Project) TextEditor(com.intellij.openapi.fileEditor.TextEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) LightweightHint(com.intellij.ui.LightweightHint) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Aggregations

TextEditor (com.intellij.openapi.fileEditor.TextEditor)75 FileEditor (com.intellij.openapi.fileEditor.FileEditor)52 Editor (com.intellij.openapi.editor.Editor)29 VirtualFile (com.intellij.openapi.vfs.VirtualFile)23 Project (com.intellij.openapi.project.Project)20 Document (com.intellij.openapi.editor.Document)13 Nullable (org.jetbrains.annotations.Nullable)12 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)11 PsiFile (com.intellij.psi.PsiFile)8 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)6 NotNull (org.jetbrains.annotations.NotNull)6 Disposable (com.intellij.openapi.Disposable)3 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)3 EditorNotificationPanel (com.intellij.ui.EditorNotificationPanel)3 LightweightHint (com.intellij.ui.LightweightHint)3 NonNls (org.jetbrains.annotations.NonNls)3 HighlightingPass (com.intellij.codeHighlighting.HighlightingPass)2 TextEditorHighlightingPass (com.intellij.codeHighlighting.TextEditorHighlightingPass)2 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)2 UndoManager (com.intellij.openapi.command.undo.UndoManager)2