Search in sources :

Example 11 with TextEditor

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

the class InplaceReassignVariableTest method doUndoTest.

private void doUndoTest() {
    String name = getTestName(true);
    configureByFile(getBasePath() + name + getExtension());
    final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled();
    try {
        TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
        getEditor().getSettings().setVariableInplaceRenameEnabled(true);
        invokeRefactoring();
        ReassignVariableUtil.reassign(getEditor());
        assertNull(TemplateManagerImpl.getTemplateState(getEditor()));
        TextEditor textEditor = TextEditorProvider.getInstance().getTextEditor(getEditor());
        assertNotNull(textEditor);
        UndoManager.getInstance(getProject()).undo(textEditor);
        checkResultByFile(getBasePath() + name + getExtension());
    } finally {
        getEditor().getSettings().setVariableInplaceRenameEnabled(enabled);
    }
}
Also used : TextEditor(com.intellij.openapi.fileEditor.TextEditor)

Example 12 with TextEditor

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

the class InheritorChooser method runMethodInAbstractClass.

public boolean runMethodInAbstractClass(final ConfigurationContext context, final Runnable performRunnable, final PsiMethod psiMethod, final PsiClass containingClass, final Condition<PsiClass> acceptAbstractCondition) {
    if (containingClass != null && acceptAbstractCondition.value(containingClass)) {
        final Location location = context.getLocation();
        if (location instanceof MethodLocation) {
            final PsiClass aClass = ((MethodLocation) location).getContainingClass();
            if (aClass != null && !aClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
                return false;
            }
        } else if (location instanceof PsiMemberParameterizedLocation) {
            return false;
        }
        final List<PsiClass> classes = new ArrayList<>();
        if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
            final boolean isJUnit5 = ReadAction.compute(() -> JUnitUtil.isJUnit5(containingClass));
            ClassInheritorsSearch.search(containingClass).forEach(aClass -> {
                if (isJUnit5 && JUnitUtil.isJUnit5TestClass(aClass, true) || PsiClassUtil.isRunnableClass(aClass, true, true)) {
                    classes.add(aClass);
                }
                return true;
            });
        }, "Search for " + containingClass.getQualifiedName() + " inheritors", true, containingClass.getProject())) {
            return true;
        }
        if (classes.size() == 1) {
            runForClass(classes.get(0), psiMethod, context, performRunnable);
            return true;
        }
        if (classes.isEmpty())
            return false;
        final FileEditor fileEditor = PlatformDataKeys.FILE_EDITOR.getData(context.getDataContext());
        if (fileEditor instanceof TextEditor) {
            final Document document = ((TextEditor) fileEditor).getEditor().getDocument();
            final PsiFile containingFile = PsiDocumentManager.getInstance(context.getProject()).getPsiFile(document);
            if (containingFile instanceof PsiClassOwner) {
                final List<PsiClass> psiClasses = new ArrayList<>(Arrays.asList(((PsiClassOwner) containingFile).getClasses()));
                psiClasses.retainAll(classes);
                if (psiClasses.size() == 1) {
                    runForClass(psiClasses.get(0), psiMethod, context, performRunnable);
                    return true;
                }
            }
        }
        final int numberOfInheritors = classes.size();
        final PsiClassListCellRenderer renderer = new PsiClassListCellRenderer() {

            @Override
            protected boolean customizeNonPsiElementLeftRenderer(ColoredListCellRenderer renderer, JList list, Object value, int index, boolean selected, boolean hasFocus) {
                if (value == null) {
                    renderer.append("All (" + numberOfInheritors + ")");
                    return true;
                }
                return super.customizeNonPsiElementLeftRenderer(renderer, list, value, index, selected, hasFocus);
            }
        };
        Collections.sort(classes, renderer.getComparator());
        //suggest to run all inherited tests 
        classes.add(0, null);
        final JBList list = new JBList(classes);
        list.setCellRenderer(renderer);
        JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle("Choose executable classes to run " + (psiMethod != null ? psiMethod.getName() : containingClass.getName())).setMovable(false).setResizable(false).setRequestFocus(true).setItemChoosenCallback(() -> {
            final Object[] values = list.getSelectedValues();
            if (values == null)
                return;
            chooseAndPerform(values, psiMethod, context, performRunnable, classes);
        }).createPopup().showInBestPositionFor(context.getDataContext());
        return true;
    }
    return false;
}
Also used : ProgressManager(com.intellij.openapi.progress.ProgressManager) JBList(com.intellij.ui.components.JBList) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) Arrays(java.util.Arrays) ArrayUtil(com.intellij.util.ArrayUtil) ConfigurationContext(com.intellij.execution.actions.ConfigurationContext) Document(com.intellij.openapi.editor.Document) FileEditor(com.intellij.openapi.fileEditor.FileEditor) ReadAction(com.intellij.openapi.application.ReadAction) ArrayList(java.util.ArrayList) List(java.util.List) PlatformDataKeys(com.intellij.openapi.actionSystem.PlatformDataKeys) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) PsiClassListCellRenderer(com.intellij.ide.util.PsiClassListCellRenderer) PsiClassUtil(com.intellij.psi.util.PsiClassUtil) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) ColoredListCellRenderer(com.intellij.ui.ColoredListCellRenderer) com.intellij.psi(com.intellij.psi) ClassInheritorsSearch(com.intellij.psi.search.searches.ClassInheritorsSearch) Location(com.intellij.execution.Location) Collections(java.util.Collections) TextEditor(com.intellij.openapi.fileEditor.TextEditor) Condition(com.intellij.openapi.util.Condition) javax.swing(javax.swing) FileEditor(com.intellij.openapi.fileEditor.FileEditor) ArrayList(java.util.ArrayList) ColoredListCellRenderer(com.intellij.ui.ColoredListCellRenderer) Document(com.intellij.openapi.editor.Document) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) TextEditor(com.intellij.openapi.fileEditor.TextEditor) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) PsiClassListCellRenderer(com.intellij.ide.util.PsiClassListCellRenderer) JBList(com.intellij.ui.components.JBList) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) Location(com.intellij.execution.Location)

Example 13 with TextEditor

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

the class EditorWindow method syncCaretIfPossible.

/**
   * Tries to setup caret and viewport for the given editor from the selected one.
   *
   * @param toSync    editor to setup caret and viewport for
   */
private void syncCaretIfPossible(@Nullable FileEditor[] toSync) {
    if (toSync == null) {
        return;
    }
    final EditorWithProviderComposite from = getSelectedEditor();
    if (from == null) {
        return;
    }
    final FileEditor caretSource = from.getSelectedEditor();
    if (!(caretSource instanceof TextEditor)) {
        return;
    }
    final Editor editorFrom = ((TextEditor) caretSource).getEditor();
    final int offset = editorFrom.getCaretModel().getOffset();
    if (offset <= 0) {
        return;
    }
    final int scrollOffset = editorFrom.getScrollingModel().getVerticalScrollOffset();
    for (FileEditor fileEditor : toSync) {
        if (!(fileEditor instanceof TextEditor)) {
            continue;
        }
        final Editor editor = ((TextEditor) fileEditor).getEditor();
        if (editorFrom.getDocument() == editor.getDocument()) {
            editor.getCaretModel().moveToOffset(offset);
            final ScrollingModel scrollingModel = editor.getScrollingModel();
            scrollingModel.scrollVertically(scrollOffset);
            SwingUtilities.invokeLater(() -> {
                if (!editor.isDisposed()) {
                    scrollingModel.scrollToCaret(ScrollType.MAKE_VISIBLE);
                }
            });
        }
    }
}
Also used : FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) ScrollingModel(com.intellij.openapi.editor.ScrollingModel) TextEditor(com.intellij.openapi.fileEditor.TextEditor) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor)

Example 14 with TextEditor

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

the class EditorPlaceHolder method setContent.

public void setContent(final DiffContent content) {
    runRegisteredDisposables();
    myContent = content;
    if (myContent != null) {
        Document document = myContent.getDocument();
        if (myContent.isBinary() || document == null || myContent.getContentType() instanceof UIBasedFileType) {
            final VirtualFile file = myContent.getFile();
            if (file != null) {
                final FileEditorProvider[] providers = FileEditorProviderManager.getInstance().getProviders(getProject(), file);
                if (providers.length > 0) {
                    myFileEditor = providers[0].createEditor(getProject(), file);
                    if (myFileEditor instanceof TextEditor) {
                        myEditor = (EditorEx) ((TextEditor) myFileEditor).getEditor();
                        ContentDocumentListener.install(myContent, this);
                    }
                    myFileEditorProvider = providers[0];
                    addDisposable(new Disposable() {

                        @Override
                        public void dispose() {
                            myFileEditorProvider.disposeEditor(myFileEditor);
                            myFileEditor = null;
                            myFileEditorProvider = null;
                            myEditor = null;
                        }
                    });
                } else {
                    document = new DocumentImpl("Can not show", true);
                    final EditorFactory editorFactory = EditorFactory.getInstance();
                    myEditor = DiffUtil.createEditor(document, getProject(), true, content.getContentType());
                    addDisposable(new Disposable() {

                        public void dispose() {
                            editorFactory.releaseEditor(myEditor);
                            myEditor = null;
                        }
                    });
                }
            }
        } else {
            final EditorFactory editorFactory = EditorFactory.getInstance();
            myEditor = DiffUtil.createEditor(document, getProject(), false, content.getContentType());
            addDisposable(new Disposable() {

                public void dispose() {
                    editorFactory.releaseEditor(myEditor);
                    myEditor = null;
                }
            });
            ContentDocumentListener.install(myContent, this);
        }
    }
    fireContentChanged();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Disposable(com.intellij.openapi.Disposable) UIBasedFileType(com.intellij.openapi.fileTypes.UIBasedFileType) EditorFactory(com.intellij.openapi.editor.EditorFactory) TextEditor(com.intellij.openapi.fileEditor.TextEditor) FileEditorProvider(com.intellij.openapi.fileEditor.FileEditorProvider) Document(com.intellij.openapi.editor.Document) DocumentImpl(com.intellij.openapi.editor.impl.DocumentImpl)

Example 15 with TextEditor

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

the class RemoteFilePanel method switchEditor.

private void switchEditor() {
    LOG.debug("Switching editor...");
    AppUIUtil.invokeOnEdt(() -> {
        TextEditor textEditor = (TextEditor) TextEditorProvider.getInstance().createEditor(myProject, myVirtualFile);
        textEditor.addPropertyChangeListener(myPropertyChangeListener);
        myEditorPanel.removeAll();
        myEditorPanel.add(textEditor.getComponent(), BorderLayout.CENTER);
        myFileEditor = textEditor;
        showCard(EDITOR_CARD);
        LOG.debug("Editor for downloaded file opened.");
    }, myProject.getDisposed());
}
Also used : 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