Search in sources :

Example 16 with OpenFileDescriptor

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

the class FormatterTestCase method checkDocument.

protected void checkDocument(final PsiFile file, final String text, String textAfter) {
    final Document document = PsiDocumentManager.getInstance(getProject()).getDocument(file);
    final EditorImpl editor;
    if (doCheckDocumentUpdate()) {
        editor = (EditorImpl) FileEditorManager.getInstance(getProject()).openTextEditor(new OpenFileDescriptor(getProject(), file.getVirtualFile(), 0), false);
        editor.putUserData(EditorImpl.DO_DOCUMENT_UPDATE_TEST, Boolean.TRUE);
        if (myFile != null) {
            FileEditorManager.getInstance(getProject()).closeFile(myFile.getVirtualFile());
        }
        myEditor = editor;
        myFile = file;
    } else {
        editor = null;
    }
    WriteCommandAction.runWriteCommandAction(getProject(), () -> {
        document.replaceString(0, document.getTextLength(), text);
        PsiDocumentManager.getInstance(getProject()).commitDocument(document);
        assertEquals(file.getText(), document.getText());
        try {
            if (doReformatRangeTest) {
                CodeStyleManager.getInstance(getProject()).reformatRange(file, file.getTextRange().getStartOffset(), file.getTextRange().getEndOffset());
            } else if (myTextRange != null) {
                CodeStyleManager.getInstance(getProject()).reformatText(file, myTextRange.getStartOffset(), myTextRange.getEndOffset());
            } else {
                CodeStyleManager.getInstance(getProject()).reformatText(file, file.getTextRange().getStartOffset(), file.getTextRange().getEndOffset());
            }
        } catch (IncorrectOperationException e) {
            fail();
        }
    });
    assertEquals(textAfter, document.getText());
    PsiDocumentManager.getInstance(getProject()).commitDocument(document);
    assertEquals(textAfter, file.getText());
}
Also used : EditorImpl(com.intellij.openapi.editor.impl.EditorImpl) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Document(com.intellij.openapi.editor.Document)

Example 17 with OpenFileDescriptor

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

the class AnnotateRevisionActionBase method annotate.

public static void annotate(@NotNull VirtualFile file, @NotNull VcsFileRevision fileRevision, @NotNull AbstractVcs vcs, @Nullable Editor editor, int annotatedLine) {
    final CharSequence oldContent = editor == null ? null : editor.getDocument().getImmutableCharSequence();
    final AnnotationProvider annotationProvider = vcs.getAnnotationProvider();
    assert annotationProvider != null;
    final Ref<FileAnnotation> fileAnnotationRef = new Ref<>();
    final Ref<Integer> newLineRef = new Ref<>();
    final Ref<VcsException> exceptionRef = new Ref<>();
    VcsAnnotateUtil.getBackgroundableLock(vcs.getProject(), file).lock();
    Semaphore semaphore = new Semaphore(0);
    AtomicBoolean shouldOpenEditorInSync = new AtomicBoolean(true);
    ProgressManager.getInstance().run(new Task.Backgroundable(vcs.getProject(), VcsBundle.message("retrieving.annotations"), true) {

        public void run(@NotNull ProgressIndicator indicator) {
            try {
                FileAnnotation fileAnnotation = annotationProvider.annotate(file, fileRevision);
                int newLine = translateLine(oldContent, fileAnnotation.getAnnotatedContent(), annotatedLine);
                fileAnnotationRef.set(fileAnnotation);
                newLineRef.set(newLine);
                shouldOpenEditorInSync.set(false);
                semaphore.release();
            } catch (VcsException e) {
                exceptionRef.set(e);
            }
        }

        @Override
        public void onFinished() {
            VcsAnnotateUtil.getBackgroundableLock(vcs.getProject(), file).unlock();
        }

        @Override
        public void onSuccess() {
            if (!exceptionRef.isNull()) {
                AbstractVcsHelper.getInstance(myProject).showError(exceptionRef.get(), VcsBundle.message("operation.name.annotate"));
            }
            if (fileAnnotationRef.isNull())
                return;
            AbstractVcsHelper.getInstance(myProject).showAnnotation(fileAnnotationRef.get(), file, vcs, newLineRef.get());
        }
    });
    try {
        semaphore.tryAcquire(ProgressWindow.DEFAULT_PROGRESS_DIALOG_POSTPONE_TIME_MILLIS, TimeUnit.MILLISECONDS);
        // This will remove blinking on editor opening (step 1 - editor opens, step 2 - annotations are shown).
        if (shouldOpenEditorInSync.get()) {
            CharSequence content = LoadTextUtil.loadText(file);
            int newLine = translateLine(oldContent, content, annotatedLine);
            OpenFileDescriptor openFileDescriptor = new OpenFileDescriptor(vcs.getProject(), file, newLine, 0);
            FileEditorManager.getInstance(vcs.getProject()).openTextEditor(openFileDescriptor, true);
        }
    } catch (InterruptedException ignore) {
    }
}
Also used : Task(com.intellij.openapi.progress.Task) AnnotationProvider(com.intellij.openapi.vcs.annotate.AnnotationProvider) Semaphore(java.util.concurrent.Semaphore) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Ref(com.intellij.openapi.util.Ref) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) VcsException(com.intellij.openapi.vcs.VcsException) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) FileAnnotation(com.intellij.openapi.vcs.annotate.FileAnnotation)

Example 18 with OpenFileDescriptor

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

the class LoadProjectTest method testLoadProject.

public void testLoadProject() throws Exception {
    VirtualFile src = ProjectRootManager.getInstance(getProject()).getContentSourceRoots()[0];
    VirtualFile a = src.findFileByRelativePath("/x/AClass.java");
    assertNotNull(a);
    PsiFile fileA = getPsiManager().findFile(a);
    assertNotNull(fileA);
    fileA.navigate(true);
    Editor editorA = FileEditorManager.getInstance(getProject()).openTextEditor(new OpenFileDescriptor(getProject(), a), true);
    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
    assertNotNull(editorA);
    CodeInsightTestFixtureImpl.instantiateAndRun(fileA, editorA, new int[] { Pass.EXTERNAL_TOOLS }, false);
    VirtualFile b = src.findFileByRelativePath("/x/BClass.java");
    assertNotNull(b);
    PsiFile fileB = getPsiManager().findFile(b);
    assertNotNull(fileB);
    fileB.navigate(true);
    Editor editorB = FileEditorManager.getInstance(getProject()).openTextEditor(new OpenFileDescriptor(getProject(), b), true);
    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
    assertNotNull(editorB);
    CodeInsightTestFixtureImpl.instantiateAndRun(fileB, editorB, new int[] { Pass.EXTERNAL_TOOLS }, false);
    FileEditor[] allEditors = FileEditorManager.getInstance(getProject()).getAllEditors();
    assertEquals(2, allEditors.length);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditor(com.intellij.openapi.fileEditor.FileEditor) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor)

Example 19 with OpenFileDescriptor

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

the class ExternalSystemNotificationManager method addMessage.

private void addMessage(@NotNull final Notification notification, @NotNull final ProjectSystemId externalSystemId, @NotNull final NotificationData notificationData) {
    final VirtualFile virtualFile = notificationData.getFilePath() != null ? ExternalSystemUtil.findLocalFileByPath(notificationData.getFilePath()) : null;
    final String groupName = virtualFile != null ? virtualFile.getPresentableUrl() : notificationData.getTitle();
    myMessageCounter.increment(groupName, notificationData.getNotificationSource(), notificationData.getNotificationCategory(), externalSystemId);
    int line = notificationData.getLine() - 1;
    int column = notificationData.getColumn() - 1;
    if (virtualFile == null)
        line = column = -1;
    final int guiLine = line < 0 ? -1 : line + 1;
    final int guiColumn = column < 0 ? 0 : column + 1;
    final Navigatable navigatable = notificationData.getNavigatable() != null ? notificationData.getNavigatable() : virtualFile != null ? new OpenFileDescriptor(myProject, virtualFile, line, column) : NonNavigatable.INSTANCE;
    final ErrorTreeElementKind kind = ErrorTreeElementKind.convertMessageFromCompilerErrorType(notificationData.getNotificationCategory().getMessageCategory());
    final String[] message = notificationData.getMessage().split("\n");
    final String exportPrefix = NewErrorTreeViewPanel.createExportPrefix(guiLine);
    final String rendererPrefix = NewErrorTreeViewPanel.createRendererPrefix(guiLine, guiColumn);
    UIUtil.invokeLaterIfNeeded(() -> {
        boolean activate = notificationData.getNotificationCategory() == NotificationCategory.ERROR || notificationData.getNotificationCategory() == NotificationCategory.WARNING;
        final NewErrorTreeViewPanel errorTreeView = prepareMessagesView(externalSystemId, notificationData.getNotificationSource(), activate);
        final GroupingElement groupingElement = errorTreeView.getErrorViewStructure().getGroupingElement(groupName, null, virtualFile);
        final NavigatableMessageElement navigatableMessageElement;
        if (notificationData.hasLinks()) {
            navigatableMessageElement = new EditableNotificationMessageElement(notification, kind, groupingElement, message, navigatable, exportPrefix, rendererPrefix);
        } else {
            navigatableMessageElement = new NotificationMessageElement(kind, groupingElement, message, navigatable, exportPrefix, rendererPrefix);
        }
        errorTreeView.getErrorViewStructure().addNavigatableMessage(groupName, navigatableMessageElement);
        errorTreeView.updateTree();
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) NonNavigatable(com.intellij.pom.NonNavigatable) Navigatable(com.intellij.pom.Navigatable) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

Example 20 with OpenFileDescriptor

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

the class TreeView method createPanel.

private JPanel createPanel() {
    createModel();
    myTree = new MyTree();
    myTree.setLineStyleAngled();
    myTree.setRootVisible(false);
    myTree.setShowsRootHandles(true);
    myTree.updateUI();
    myTree.setLargeModel(true);
    myTree.addKeyListener(new KeyAdapter() {

        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                OpenSourceUtil.openSourcesFrom(DataManager.getInstance().getDataContext(myTree), false);
            }
        }
    });
    myTree.addMouseListener(new PopupHandler() {

        public void invokePopup(Component comp, int x, int y) {
            popupInvoked(comp, x, y);
        }
    });
    EditSourceOnDoubleClickHandler.install(myTree);
    myAutoScrollToSourceHandler.install(myTree);
    myOccurenceNavigatorSupport = new OccurenceNavigatorSupport(myTree) {

        protected Navigatable createDescriptorForNode(DefaultMutableTreeNode node) {
            if (!(node instanceof MessageNode)) {
                return null;
            }
            MessageNode messageNode = (MessageNode) node;
            AntBuildMessageView.MessageType type = messageNode.getType();
            if (type != AntBuildMessageView.MessageType.MESSAGE && type != AntBuildMessageView.MessageType.ERROR) {
                return null;
            }
            if (!isValid(messageNode.getFile())) {
                return null;
            }
            return new OpenFileDescriptor(myProject, messageNode.getFile(), messageNode.getOffset());
        }

        @Nullable
        public String getNextOccurenceActionName() {
            return AntBundle.message("ant.execution.next.error.warning.action.name");
        }

        @Nullable
        public String getPreviousOccurenceActionName() {
            return AntBundle.message("ant.execution.previous.error.warning.action.name");
        }
    };
    return JBUI.Panels.simplePanel(MessageTreeRenderer.install(myTree));
}
Also used : PopupHandler(com.intellij.ui.PopupHandler) KeyAdapter(java.awt.event.KeyAdapter) Navigatable(com.intellij.pom.Navigatable) KeyEvent(java.awt.event.KeyEvent) OccurenceNavigatorSupport(com.intellij.ide.OccurenceNavigatorSupport) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)155 VirtualFile (com.intellij.openapi.vfs.VirtualFile)97 Project (com.intellij.openapi.project.Project)40 Editor (com.intellij.openapi.editor.Editor)30 PsiElement (com.intellij.psi.PsiElement)27 PsiFile (com.intellij.psi.PsiFile)21 Navigatable (com.intellij.pom.Navigatable)19 Nullable (org.jetbrains.annotations.Nullable)18 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)10 Document (com.intellij.openapi.editor.Document)9 FileEditor (com.intellij.openapi.fileEditor.FileEditor)9 TextRange (com.intellij.openapi.util.TextRange)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)9 File (java.io.File)9 Result (com.intellij.openapi.application.Result)8 EditorImpl (com.intellij.openapi.editor.impl.EditorImpl)8 NotNull (org.jetbrains.annotations.NotNull)6 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)5 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)5 XmlTag (com.intellij.psi.xml.XmlTag)5