Search in sources :

Example 36 with TextEditor

use of com.intellij.openapi.fileEditor.TextEditor in project oxy-template-support-plugin by mutant-industries.

the class CompiledPreviewController method showCompiledCode.

public boolean showCompiledCode(@NotNull final PsiFile originalFile) {
    PsiFile psiFile;
    Document document = null;
    VirtualFile virtualFile = null;
    EditorWindow editorWindow = null;
    boolean result, newTabCreated = false;
    FileEditorManagerEx fileEditorManager = FileEditorManagerEx.getInstanceEx(myProject);
    // check for already opened editor with compiled source first
    for (FileEditor fileEditor : fileEditorManager.getAllEditors()) {
        if (!(fileEditor instanceof TextEditor)) {
            continue;
        }
        EditorEx editor = (EditorEx) ((TextEditor) fileEditor).getEditor();
        virtualFile = editor.getVirtualFile();
        psiFile = PsiManager.getInstance(myProject).findFile(virtualFile);
        if (psiFile != null && originalFile.isEquivalentTo(psiFile.getUserData(ORIGINAL_FILE_KEY))) {
            document = editor.getDocument();
            // find editor window where the file is opened
            for (EditorWindow window : fileEditorManager.getWindows()) {
                if (findFileIndex(window, virtualFile) != -1) {
                    editorWindow = window;
                    break;
                }
            }
            break;
        }
    }
    // create new editor
    if (document == null) {
        virtualFile = new LightVirtualFile(originalFile.getName() + COMPILED_FILE_SUFFIX);
        ((LightVirtualFile) virtualFile).setLanguage(CompiledPreview.INSTANCE);
        ((LightVirtualFile) virtualFile).setFileType(CompiledPreviewFileType.INSTANCE);
        psiFile = PsiManager.getInstance(myProject).findFile(virtualFile);
        assert psiFile != null;
        document = PsiDocumentManager.getInstance(myProject).getDocument(psiFile);
        assert document != null;
        psiFile.putUserData(ORIGINAL_FILE_KEY, originalFile);
        editorWindow = fileEditorManager.getCurrentWindow();
        newTabCreated = true;
    }
    assert editorWindow != null;
    if (result = showCompiledCode(originalFile, document)) {
        if (newTabCreated) {
            editorWindow.split(SwingConstants.HORIZONTAL, false, virtualFile, false);
        }
        if (findFileIndex(editorWindow, virtualFile) == -1) {
            fileEditorManager.openFile(virtualFile, false);
        }
        // switch to editor with recompiled source
        editorWindow.setEditor(editorWindow.findFileComposite(virtualFile), false);
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) EditorEx(com.intellij.openapi.editor.ex.EditorEx) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) FileEditorManagerEx(com.intellij.openapi.fileEditor.ex.FileEditorManagerEx) EditorWindow(com.intellij.openapi.fileEditor.impl.EditorWindow)

Example 37 with TextEditor

use of com.intellij.openapi.fileEditor.TextEditor in project Perl5-IDEA by Camelcade.

the class PerlUXPerformanceTest method testHighlighting.

public void testHighlighting() {
    initWithPerlTidy();
    final PsiFile file = getFile();
    final Editor editor = getEditor();
    final Project project = getProject();
    CodeInsightTestFixtureImpl.ensureIndexesUpToDate(project);
    final DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl) DaemonCodeAnalyzer.getInstance(project);
    final TextEditor textEditor = TextEditorProvider.getInstance().getTextEditor(editor);
    DaemonCodeAnalyzerEx codeAnalyzerEx = DaemonCodeAnalyzerEx.getInstanceEx(project);
    final int iterations = 30;
    for (int i = 0; i < iterations; i++) {
        codeAnalyzer.restart();
        ((PsiModificationTrackerImpl) getPsiManager().getModificationTracker()).incCounter();
        codeAnalyzer.runPasses(file, editor.getDocument(), Collections.singletonList(textEditor), ArrayUtil.EMPTY_INT_ARRAY, false, null);
        codeAnalyzerEx.getFileLevelHighlights(project, file);
    }
    final int time = 1100;
    PlatformTestUtil.startPerformanceTest("PerlTidy highlighting", iterations * time, () -> {
        long start = System.currentTimeMillis();
        for (int i = 0; i < iterations; i++) {
            codeAnalyzer.restart();
            ((PsiModificationTrackerImpl) getPsiManager().getModificationTracker()).incCounter();
            codeAnalyzer.runPasses(file, editor.getDocument(), Collections.singletonList(textEditor), ArrayUtil.EMPTY_INT_ARRAY, false, null);
            DaemonCodeAnalyzerEx.getInstanceEx(project).getFileLevelHighlights(project, file);
        }
        long length = System.currentTimeMillis() - start;
        System.err.println("Highlighting done in " + length / iterations + " ms per iteration of " + time);
    }).assertTiming();
}
Also used : Project(com.intellij.openapi.project.Project) TextEditor(com.intellij.openapi.fileEditor.TextEditor) PsiModificationTrackerImpl(com.intellij.psi.impl.PsiModificationTrackerImpl) DaemonCodeAnalyzerImpl(com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl) PsiFile(com.intellij.psi.PsiFile) DaemonCodeAnalyzerEx(com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerEx) Editor(com.intellij.openapi.editor.Editor) TextEditor(com.intellij.openapi.fileEditor.TextEditor)

Example 38 with TextEditor

use of com.intellij.openapi.fileEditor.TextEditor in project azure-tools-for-java by Microsoft.

the class IDEHelperImpl method openFileInEditor.

private boolean openFileInEditor(final Consumer<? super String> contentSaver, VirtualFile virtualFile, FileEditorManager fileEditorManager) {
    final FileEditor[] editors = fileEditorManager.openFile(virtualFile, true, true);
    if (editors.length == 0) {
        return false;
    }
    for (final FileEditor fileEditor : editors) {
        if (fileEditor instanceof TextEditor) {
            final String originContent = getTextEditorContent((TextEditor) fileEditor);
            final MessageBusConnection messageBusConnection = fileEditorManager.getProject().getMessageBus().connect(fileEditor);
            messageBusConnection.subscribe(FileEditorManagerListener.Before.FILE_EDITOR_MANAGER, new FileEditorManagerListener.Before() {

                @Override
                public void beforeFileClosed(FileEditorManager source, VirtualFile file) {
                    try {
                        final String content = getTextEditorContent((TextEditor) fileEditor);
                        if (file == virtualFile && !StringUtils.equals(content, originContent)) {
                            final boolean result = DefaultLoader.getUIHelper().showYesNoDialog(fileEditor.getComponent(), SAVE_CHANGES, APP_SERVICE_FILE_EDITING, Messages.getQuestionIcon());
                            if (result) {
                                contentSaver.consume(content);
                            }
                        }
                    } catch (final RuntimeException e) {
                        AzureMessager.getMessager().error(e);
                    } finally {
                        messageBusConnection.disconnect();
                    }
                }
            });
        }
    }
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) FileEditor(com.intellij.openapi.fileEditor.FileEditor) MessageBusConnection(com.intellij.util.messages.MessageBusConnection) TextEditor(com.intellij.openapi.fileEditor.TextEditor) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) FileEditorManagerListener(com.intellij.openapi.fileEditor.FileEditorManagerListener)

Example 39 with TextEditor

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

the class ActionScriptStubsTest method doTest.

private void doTest(@Nullable final ThrowableRunnable<Exception> runnable, String... files) throws Exception {
    Runnable r = runnable != null ? () -> {
        try {
            runnable.run();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } : null;
    doTestFor(true, r, files);
    // the first one was parsed during highlighting
    assertNotParsed(myPsiFiles.subList(1, myPsiFiles.size()));
    // we need to go though files open in editors
    assertNotParsed(ContainerUtil.mapNotNull(FileEditorManager.getInstance(myProject).getOpenFiles(), virtualFile -> {
        if (Comparing.equal(virtualFile, myFile.getVirtualFile())) {
            return null;
        }
        Document document = ((TextEditor) FileEditorManager.getInstance(myProject).getSelectedEditor(virtualFile)).getEditor().getDocument();
        return PsiDocumentManager.getInstance(myProject).getPsiFile(document);
    }));
}
Also used : VfsRootAccess(com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) VfsUtilCore.urlToPath(com.intellij.openapi.vfs.VfsUtilCore.urlToPath) JSTestOption(com.intellij.lang.javascript.JSTestOption) FlexModuleType(com.intellij.lang.javascript.flex.FlexModuleType) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) ContainerUtil(com.intellij.util.containers.ContainerUtil) ArrayList(java.util.ArrayList) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) FlexStylesIndexableSetContributor(com.intellij.javascript.flex.css.FlexStylesIndexableSetContributor) Comparing(com.intellij.openapi.util.Comparing) PsiFile(com.intellij.psi.PsiFile) ModuleType(com.intellij.openapi.module.ModuleType) ActionScriptDaemonAnalyzerTestCase(com.intellij.flex.util.ActionScriptDaemonAnalyzerTestCase) FlexTestUtils(com.intellij.flex.util.FlexTestUtils) LightQuickFixTestCase(com.intellij.codeInsight.daemon.quickFix.LightQuickFixTestCase) TextEditor(com.intellij.openapi.fileEditor.TextEditor) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) ThrowableRunnable(com.intellij.util.ThrowableRunnable) PlatformTestUtil(com.intellij.testFramework.PlatformTestUtil) Collection(java.util.Collection) IOException(java.io.IOException) File(java.io.File) CommandProcessor(com.intellij.openapi.command.CommandProcessor) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) VfsUtilCore.convertFromUrl(com.intellij.openapi.vfs.VfsUtilCore.convertFromUrl) JSTestOptions(com.intellij.lang.javascript.JSTestOptions) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) NotNull(org.jetbrains.annotations.NotNull) TextEditor(com.intellij.openapi.fileEditor.TextEditor) ThrowableRunnable(com.intellij.util.ThrowableRunnable) Document(com.intellij.openapi.editor.Document) IOException(java.io.IOException)

Example 40 with TextEditor

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

the class ActionScriptHighlightingTest method setActiveEditor.

private void setActiveEditor(String relativePath) {
    VirtualFile file = myFile.getVirtualFile().findFileByRelativePath(relativePath);
    FileEditor[] editors = FileEditorManager.getInstance(myProject).getEditors(file);
    assertEquals(1, editors.length);
    FileEditor fileEditor = editors[0];
    assertTrue(fileEditor instanceof TextEditor);
    setActiveEditor(((TextEditor) fileEditor).getEditor());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor)

Aggregations

TextEditor (com.intellij.openapi.fileEditor.TextEditor)81 FileEditor (com.intellij.openapi.fileEditor.FileEditor)57 Editor (com.intellij.openapi.editor.Editor)32 VirtualFile (com.intellij.openapi.vfs.VirtualFile)28 Project (com.intellij.openapi.project.Project)21 Document (com.intellij.openapi.editor.Document)14 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)13 Nullable (org.jetbrains.annotations.Nullable)13 PsiFile (com.intellij.psi.PsiFile)11 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)6 NotNull (org.jetbrains.annotations.NotNull)6 Disposable (com.intellij.openapi.Disposable)3 EditorEx (com.intellij.openapi.editor.ex.EditorEx)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