Search in sources :

Example 66 with FileDocumentManager

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

the class CodeSmellDetectorImpl method showCodeSmellErrors.

@Override
public void showCodeSmellErrors(@NotNull final List<CodeSmellInfo> smellList) {
    Collections.sort(smellList, new Comparator<CodeSmellInfo>() {

        @Override
        public int compare(final CodeSmellInfo o1, final CodeSmellInfo o2) {
            return o1.getTextRange().getStartOffset() - o2.getTextRange().getStartOffset();
        }
    });
    ApplicationManager.getApplication().invokeLater(new Runnable() {

        @Override
        public void run() {
            if (myProject.isDisposed())
                return;
            if (smellList.isEmpty()) {
                return;
            }
            final VcsErrorViewPanel errorTreeView = new VcsErrorViewPanel(myProject);
            AbstractVcsHelperImpl helper = (AbstractVcsHelperImpl) AbstractVcsHelper.getInstance(myProject);
            helper.openMessagesView(errorTreeView, VcsBundle.message("code.smells.error.messages.tab.name"));
            FileDocumentManager fileManager = FileDocumentManager.getInstance();
            for (CodeSmellInfo smellInfo : smellList) {
                final VirtualFile file = fileManager.getFile(smellInfo.getDocument());
                final OpenFileDescriptor navigatable = new OpenFileDescriptor(myProject, file, smellInfo.getStartLine(), smellInfo.getStartColumn());
                final String exportPrefix = NewErrorTreeViewPanel.createExportPrefix(smellInfo.getStartLine() + 1);
                final String rendererPrefix = NewErrorTreeViewPanel.createRendererPrefix(smellInfo.getStartLine() + 1, smellInfo.getStartColumn() + 1);
                if (smellInfo.getSeverity() == HighlightSeverity.ERROR) {
                    errorTreeView.addMessage(MessageCategory.ERROR, new String[] { smellInfo.getDescription() }, file.getPresentableUrl(), navigatable, exportPrefix, rendererPrefix, null);
                } else {
                    //if (smellInfo.getSeverity() == HighlightSeverity.WARNING) {
                    errorTreeView.addMessage(MessageCategory.WARNING, new String[] { smellInfo.getDescription() }, file.getPresentableUrl(), navigatable, exportPrefix, rendererPrefix, null);
                }
            }
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) CodeSmellInfo(com.intellij.codeInsight.CodeSmellInfo) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

Example 67 with FileDocumentManager

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

the class ActionsTest method saveDocument.

private static void saveDocument(VirtualFile f) {
    FileDocumentManager dm = FileDocumentManager.getInstance();
    Document document = dm.getDocument(f);
    assertNotNull(f.getPath(), document);
    dm.saveDocument(document);
}
Also used : FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document)

Example 68 with FileDocumentManager

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

the class FileEncodingTest method testUndoChangeEncoding.

public void testUndoChangeEncoding() throws IOException {
    VirtualFile file = createTempFile("txt", null, "xxx", CharsetToolkit.UTF8_CHARSET);
    file.setCharset(CharsetToolkit.UTF8_CHARSET);
    UIUtil.dispatchAllInvocationEvents();
    assertEquals(CharsetToolkit.UTF8_CHARSET, file.getCharset());
    FileDocumentManager documentManager = FileDocumentManager.getInstance();
    assertNull(documentManager.getCachedDocument(file));
    change(file, WINDOWS_1251);
    assertEquals(WINDOWS_1251, file.getCharset());
    Document document = documentManager.getDocument(file);
    change(file, CharsetToolkit.UTF8_CHARSET);
    assertEquals(CharsetToolkit.UTF8_CHARSET, file.getCharset());
    globalUndo();
    UIUtil.dispatchAllInvocationEvents();
    assertEquals(WINDOWS_1251, file.getCharset());
    document.getText();
}
Also used : FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document)

Example 69 with FileDocumentManager

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

the class EditorImpl method processKeyTyped.

private boolean processKeyTyped(char c) {
    // [vova] This is patch for Mac OS X. Under Mac "input methods"
    // is handled before our EventQueue consume upcoming KeyEvents.
    IdeEventQueue queue = IdeEventQueue.getInstance();
    if (queue.shouldNotTypeInEditor() || ProgressManager.getInstance().hasModalProgressIndicator()) {
        return false;
    }
    FileDocumentManager manager = FileDocumentManager.getInstance();
    final VirtualFile file = manager.getFile(myDocument);
    if (file != null && !file.isValid()) {
        return false;
    }
    DataContext context = getDataContext();
    Graphics graphics = GraphicsUtil.safelyGetGraphics(myEditorComponent);
    if (graphics != null) {
        // editor component is not showing
        processKeyTypedImmediately(c, graphics, context);
        graphics.dispose();
    }
    ActionManagerEx.getInstanceEx().fireBeforeEditorTyping(c, context);
    EditorUIUtil.hideCursorInEditor(this);
    processKeyTypedNormally(c, context);
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager)

Example 70 with FileDocumentManager

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

the class TrailingSpacesStripper method strip.

private void strip(@NotNull final Document document) {
    if (!document.isWritable())
        return;
    FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
    VirtualFile file = fileDocumentManager.getFile(document);
    if (file == null || !file.isValid() || Boolean.TRUE.equals(DISABLE_FOR_FILE_KEY.get(file)))
        return;
    final EditorSettingsExternalizable settings = EditorSettingsExternalizable.getInstance();
    if (settings == null)
        return;
    final String overrideStripTrailingSpacesData = file.getUserData(OVERRIDE_STRIP_TRAILING_SPACES_KEY);
    final Boolean overrideEnsureNewlineData = file.getUserData(OVERRIDE_ENSURE_NEWLINE_KEY);
    @EditorSettingsExternalizable.StripTrailingSpaces String stripTrailingSpaces = overrideStripTrailingSpacesData != null ? overrideStripTrailingSpacesData : settings.getStripTrailingSpaces();
    final boolean doStrip = !stripTrailingSpaces.equals(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_NONE);
    final boolean ensureEOL = overrideEnsureNewlineData != null ? overrideEnsureNewlineData.booleanValue() : settings.isEnsureNewLineAtEOF();
    if (doStrip) {
        final boolean inChangedLinesOnly = !stripTrailingSpaces.equals(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_WHOLE);
        boolean success = strip(document, inChangedLinesOnly, settings.isKeepTrailingSpacesOnCaretLine());
        if (!success) {
            myDocumentsToStripLater.add(document);
        }
    }
    final int lines = document.getLineCount();
    if (ensureEOL && lines > 0) {
        final int start = document.getLineStartOffset(lines - 1);
        final int end = document.getLineEndOffset(lines - 1);
        if (start != end) {
            final CharSequence content = document.getCharsSequence();
            ApplicationManager.getApplication().runWriteAction(new DocumentRunnable(document, null) {

                @Override
                public void run() {
                    CommandProcessor.getInstance().runUndoTransparentAction(() -> {
                        if (CharArrayUtil.containsOnlyWhiteSpaces(content.subSequence(start, end)) && doStrip) {
                            document.deleteString(start, end);
                        } else {
                            document.insertString(end, "\n");
                        }
                    });
                }
            });
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) EditorSettingsExternalizable(com.intellij.openapi.editor.ex.EditorSettingsExternalizable)

Aggregations

FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)98 Document (com.intellij.openapi.editor.Document)64 VirtualFile (com.intellij.openapi.vfs.VirtualFile)54 IOException (java.io.IOException)24 Editor (com.intellij.openapi.editor.Editor)15 Project (com.intellij.openapi.project.Project)15 File (java.io.File)13 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)11 PsiFile (com.intellij.psi.PsiFile)9 MockVirtualFile (com.intellij.mock.MockVirtualFile)8 Nullable (org.jetbrains.annotations.Nullable)8 IncorrectOperationException (com.intellij.util.IncorrectOperationException)7 DiscordIntegrationProjectComponent (com.almightyalpaca.jetbrains.plugins.discord.components.DiscordIntegrationProjectComponent)5 FileDocumentManagerImpl (com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl)4 Module (com.intellij.openapi.module.Module)4 TaskFile (com.jetbrains.edu.learning.courseFormat.TaskFile)4 ArrayList (java.util.ArrayList)4 NotNull (org.jetbrains.annotations.NotNull)4 Application (com.intellij.openapi.application.Application)3 Result (com.intellij.openapi.application.Result)3