Search in sources :

Example 61 with TextEditor

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

the class DetectedIndentOptionsNotificationProvider method createNotificationPanel.

@Nullable
@Override
public EditorNotificationPanel createNotificationPanel(@NotNull final VirtualFile file, @NotNull FileEditor fileEditor) {
    Boolean notifiedFlag = fileEditor.getUserData(NOTIFIED_FLAG);
    if (fileEditor instanceof TextEditor && notifiedFlag != null) {
        final Editor editor = ((TextEditor) fileEditor).getEditor();
        final Project project = editor.getProject();
        if (project != null) {
            Document document = editor.getDocument();
            PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
            PsiFile psiFile = documentManager.getPsiFile(document);
            final Ref<FileIndentOptionsProvider> indentOptionsProviderRef = new Ref<>();
            if (psiFile != null) {
                CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project);
                CommonCodeStyleSettings.IndentOptions userOptions = settings.getIndentOptions(psiFile.getFileType());
                CommonCodeStyleSettings.IndentOptions detectedOptions = CodeStyleSettingsManager.getSettings(project).getIndentOptionsByFile(psiFile, null, false, provider -> {
                    indentOptionsProviderRef.set(provider);
                    return false;
                });
                final FileIndentOptionsProvider provider = indentOptionsProviderRef.get();
                EditorNotificationInfo info = provider != null && !provider.isAcceptedWithoutWarning(project, file) && !userOptions.equals(detectedOptions) ? provider.getNotificationInfo(project, file, fileEditor, userOptions, detectedOptions) : null;
                if (info != null) {
                    EditorNotificationPanel panel = new EditorNotificationPanel().text(info.getTitle());
                    if (info.getIcon() != null) {
                        panel.icon(info.getIcon());
                    }
                    for (final ActionLabelData actionLabelData : info.getLabelAndActions()) {
                        Runnable onClickAction = () -> {
                            actionLabelData.action.run();
                            EditorNotifications.getInstance(project).updateAllNotifications();
                        };
                        panel.createActionLabel(actionLabelData.label, onClickAction);
                    }
                    if (ApplicationManager.getApplication().isUnitTestMode()) {
                        file.putUserData(DETECT_INDENT_NOTIFICATION_SHOWN_KEY, Boolean.TRUE);
                    }
                    return panel;
                }
            }
        }
    }
    return null;
}
Also used : ActionLabelData(com.intellij.psi.codeStyle.EditorNotificationInfo.ActionLabelData) Document(com.intellij.openapi.editor.Document) Project(com.intellij.openapi.project.Project) Ref(com.intellij.openapi.util.Ref) TextEditor(com.intellij.openapi.fileEditor.TextEditor) EditorNotificationPanel(com.intellij.ui.EditorNotificationPanel) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) Nullable(org.jetbrains.annotations.Nullable)

Example 62 with TextEditor

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

the class BaseRemoteFileEditor method contentLoaded.

protected final void contentLoaded() {
    ApplicationManager.getApplication().assertIsDispatchThread();
    Navigatable navigatable = myPendingNavigatable;
    if (navigatable != null) {
        myPendingNavigatable = null;
        TextEditor editor = getTextEditor();
        assert editor != null;
        editor.navigateTo(navigatable);
    }
    if (myMockTextEditor != null) {
        if (!myMockTextEditor.isDisposed()) {
            EditorFactory.getInstance().releaseEditor(myMockTextEditor);
        }
        myMockTextEditor = null;
    }
}
Also used : TextEditor(com.intellij.openapi.fileEditor.TextEditor) Navigatable(com.intellij.pom.Navigatable)

Example 63 with TextEditor

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

the class InplaceRefactoring method navigateToStarted.

private static void navigateToStarted(final Document oldDocument, final Project project, @Messages.YesNoResult final int exitCode) {
    final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(oldDocument);
    if (file != null) {
        final VirtualFile virtualFile = file.getVirtualFile();
        if (virtualFile != null) {
            final FileEditor[] editors = FileEditorManager.getInstance(project).getEditors(virtualFile);
            for (FileEditor editor : editors) {
                if (editor instanceof TextEditor) {
                    final Editor textEditor = ((TextEditor) editor).getEditor();
                    final TemplateState templateState = TemplateManagerImpl.getTemplateState(textEditor);
                    if (templateState != null) {
                        if (exitCode == Messages.YES) {
                            final TextRange range = templateState.getVariableRange(PRIMARY_VARIABLE_NAME);
                            if (range != null) {
                                new OpenFileDescriptor(project, virtualFile, range.getStartOffset()).navigate(true);
                                return;
                            }
                        } else if (exitCode > 0) {
                            templateState.gotoEnd();
                            return;
                        }
                    }
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 64 with TextEditor

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

the class CCHideFromStudent method hideFromStudent.

public static void hideFromStudent(VirtualFile file, Project project, Map<String, TaskFile> taskFiles, TaskFile taskFile) {
    if (!taskFile.getAnswerPlaceholders().isEmpty() && FileEditorManager.getInstance(project).isFileOpen(file)) {
        for (FileEditor fileEditor : FileEditorManager.getInstance(project).getEditors(file)) {
            if (fileEditor instanceof TextEditor) {
                Editor editor = ((TextEditor) fileEditor).getEditor();
                editor.getMarkupModel().removeAllHighlighters();
            }
        }
    }
    String taskRelativePath = StudyUtils.pathRelativeToTask(file);
    VirtualFile patternFile = StudyUtils.getPatternFile(taskFile, taskRelativePath);
    ApplicationManager.getApplication().runWriteAction(() -> {
        if (patternFile != null) {
            try {
                patternFile.delete(CCHideFromStudent.class);
            } catch (IOException e) {
                LOG.info(e);
            }
        }
    });
    taskFiles.remove(taskRelativePath);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) IOException(java.io.IOException) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor)

Example 65 with TextEditor

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

the class SourceCodeChecker method check.

private static ThreeState check(Location location, SourcePosition position, Project project) {
    Method method = DebuggerUtilsEx.getMethod(location);
    // for now skip constructors, bridges, lambdas etc.
    if (method == null || method.isConstructor() || method.isSynthetic() || method.isBridge() || method.isStaticInitializer() || (method.declaringType() instanceof ClassType && ((ClassType) method.declaringType()).isEnum()) || DebuggerUtilsEx.isLambda(method)) {
        return ThreeState.UNSURE;
    }
    List<Location> locations = DebuggerUtilsEx.allLineLocations(method);
    if (ContainerUtil.isEmpty(locations)) {
        return ThreeState.UNSURE;
    }
    if (position != null) {
        return ReadAction.compute(() -> {
            PsiFile psiFile = position.getFile();
            if (!psiFile.getLanguage().isKindOf(JavaLanguage.INSTANCE)) {
                // only for java for now
                return ThreeState.UNSURE;
            }
            Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
            if (document == null) {
                return ThreeState.UNSURE;
            }
            boolean res = false;
            PsiElement psiMethod = DebuggerUtilsEx.getContainingMethod(position);
            if (psiMethod != null) {
                TextRange range = psiMethod.getTextRange();
                if (psiMethod instanceof PsiDocCommentOwner) {
                    PsiDocComment comment = ((PsiDocCommentOwner) psiMethod).getDocComment();
                    if (comment != null) {
                        range = new TextRange(comment.getTextRange().getEndOffset() + 1, range.getEndOffset());
                    }
                }
                int startLine = document.getLineNumber(range.getStartOffset()) + 1;
                int endLine = document.getLineNumber(range.getEndOffset()) + 1;
                res = getLinesStream(locations, psiFile).allMatch(line -> startLine <= line && line <= endLine);
                if (!res) {
                    LOG.debug("Source check failed: Method " + method.name() + ", source: " + ((NavigationItem) psiMethod).getName() + "\nLines: " + getLinesStream(locations, psiFile).joining(", ") + "\nExpected range: " + startLine + "-" + endLine);
                }
            } else {
                LOG.debug("Source check failed: method " + method.name() + " not found in sources");
            }
            if (!res) {
                FileEditor editor = FileEditorManager.getInstance(project).getSelectedEditor(position.getFile().getVirtualFile());
                if (editor instanceof TextEditor) {
                    AppUIUtil.invokeOnEdt(() -> HintManager.getInstance().showErrorHint(((TextEditor) editor).getEditor(), DebuggerBundle.message("warning.source.code.not.match")));
                } else {
                    XDebugSessionImpl.NOTIFICATION_GROUP.createNotification(DebuggerBundle.message("warning.source.code.not.match"), NotificationType.WARNING).notify(project);
                }
                return ThreeState.NO;
            }
            return ThreeState.YES;
        });
    }
    return ThreeState.YES;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) NavigationItem(com.intellij.navigation.NavigationItem) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) ContainerUtil(com.intellij.util.containers.ContainerUtil) XDebugSessionImpl(com.intellij.xdebugger.impl.XDebugSessionImpl) ReadAction(com.intellij.openapi.application.ReadAction) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) PositionManagerImpl(com.intellij.debugger.engine.PositionManagerImpl) SuspendContextImpl(com.intellij.debugger.engine.SuspendContextImpl) Project(com.intellij.openapi.project.Project) DebuggerBundle(com.intellij.debugger.DebuggerBundle) Logger(com.intellij.openapi.diagnostic.Logger) JavaLanguage(com.intellij.lang.java.JavaLanguage) TextEditor(com.intellij.openapi.fileEditor.TextEditor) SuspendContextCommandImpl(com.intellij.debugger.engine.events.SuspendContextCommandImpl) StackFrameProxyImpl(com.intellij.debugger.jdi.StackFrameProxyImpl) EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) NoDataException(com.intellij.debugger.NoDataException) AppUIUtil(com.intellij.ui.AppUIUtil) ThreeState(com.intellij.util.ThreeState) DebugProcessImpl(com.intellij.debugger.engine.DebugProcessImpl) TextRange(com.intellij.openapi.util.TextRange) FileEditor(com.intellij.openapi.fileEditor.FileEditor) NotificationType(com.intellij.notification.NotificationType) TestOnly(org.jetbrains.annotations.TestOnly) PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) List(java.util.List) LineNumbersMapping(com.intellij.execution.filters.LineNumbersMapping) StreamEx(one.util.streamex.StreamEx) Registry(com.intellij.openapi.util.registry.Registry) com.sun.jdi(com.sun.jdi) IntStreamEx(one.util.streamex.IntStreamEx) com.intellij.psi(com.intellij.psi) HintManager(com.intellij.codeInsight.hint.HintManager) SourcePosition(com.intellij.debugger.SourcePosition) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) NavigationItem(com.intellij.navigation.NavigationItem) TextEditor(com.intellij.openapi.fileEditor.TextEditor)

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