Search in sources :

Example 81 with TextEditor

use of com.intellij.openapi.fileEditor.TextEditor in project checkstyle-idea by jshiell.

the class CheckStyleToolWindowPanel method scrollToError.

/**
 * Scroll to the error specified by the given tree path, or do nothing
 * if no error is specified.
 *
 * @param treePath the tree path to scroll to.
 */
private void scrollToError(final TreePath treePath) {
    final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
    if (treeNode == null || !(treeNode.getUserObject() instanceof ResultTreeNode)) {
        return;
    }
    final ResultTreeNode nodeInfo = (ResultTreeNode) treeNode.getUserObject();
    if (nodeInfo.getFile() == null || nodeInfo.getProblem() == null) {
        // no problem here :-)
        return;
    }
    final VirtualFile virtualFile = nodeInfo.getFile().getVirtualFile();
    if (virtualFile == null || !virtualFile.exists()) {
        return;
    }
    final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
    ApplicationManager.getApplication().invokeLater(() -> {
        final FileEditor[] editor = fileEditorManager.openFile(virtualFile, true);
        if (editor.length > 0 && editor[0] instanceof TextEditor) {
            final LogicalPosition problemPos = new LogicalPosition(Math.max(lineFor(nodeInfo) - 1, 0), Math.max(columnFor(nodeInfo), 0));
            final Editor textEditor = ((TextEditor) editor[0]).getEditor();
            textEditor.getCaretModel().moveToLogicalPosition(problemPos);
            textEditor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
        }
    }, ModalityState.NON_MODAL);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) LogicalPosition(com.intellij.openapi.editor.LogicalPosition) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TextEditor(com.intellij.openapi.fileEditor.TextEditor) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor)

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