Search in sources :

Example 46 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.

the class GotoCustomRegionAction method getCustomFoldingDescriptors.

@NotNull
private static Collection<FoldingDescriptor> getCustomFoldingDescriptors(@NotNull Editor editor, @NotNull Project project) {
    Set<FoldingDescriptor> foldingDescriptors = new HashSet<>();
    final Document document = editor.getDocument();
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    PsiFile file = documentManager != null ? documentManager.getPsiFile(document) : null;
    if (file != null) {
        final FileViewProvider viewProvider = file.getViewProvider();
        for (final Language language : viewProvider.getLanguages()) {
            final PsiFile psi = viewProvider.getPsi(language);
            final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(language);
            if (psi != null) {
                for (FoldingDescriptor descriptor : LanguageFolding.buildFoldingDescriptors(foldingBuilder, psi, document, false)) {
                    CustomFoldingBuilder customFoldingBuilder = getCustomFoldingBuilder(foldingBuilder, descriptor);
                    if (customFoldingBuilder != null) {
                        if (customFoldingBuilder.isCustomRegionStart(descriptor.getElement())) {
                            foldingDescriptors.add(descriptor);
                        }
                    }
                }
            }
        }
    }
    return foldingDescriptors;
}
Also used : FileViewProvider(com.intellij.psi.FileViewProvider) Language(com.intellij.lang.Language) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) HashSet(com.intellij.util.containers.HashSet) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) NotNull(org.jetbrains.annotations.NotNull)

Example 47 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.

the class PsiFileGistImpl method shouldUseMemoryStorage.

private static boolean shouldUseMemoryStorage(PsiFile file) {
    if (!(file.getVirtualFile() instanceof NewVirtualFile))
        return true;
    PsiDocumentManager pdm = PsiDocumentManager.getInstance(file.getProject());
    Document document = pdm.getCachedDocument(file);
    return document != null && (pdm.isUncommited(document) || FileDocumentManager.getInstance().isDocumentUnsaved(document));
}
Also used : NewVirtualFile(com.intellij.openapi.vfs.newvfs.NewVirtualFile) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 48 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.

the class AbstractJavaFormatterTest method formatEveryoneAndCheckIfResultEqual.

public void formatEveryoneAndCheckIfResultEqual(@NotNull final String... before) {
    assert before.length > 1;
    final PsiFile file = createFile("A.java", "");
    final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
    final Document document = manager.getDocument(file);
    String afterFirst = replaceAndProcessDocument(REFORMAT, before[0], file, document);
    for (String nextBefore : before) {
        assertEquals(afterFirst, replaceAndProcessDocument(REFORMAT, nextBefore, file, document));
    }
}
Also used : PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 49 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.

the class AbstractJavaFormatterTest method replaceAndProcessDocument.

@NotNull
private String replaceAndProcessDocument(@NotNull final Action action, @NotNull final String text, @NotNull final PsiFile file, @Nullable final Document document) throws IncorrectOperationException {
    if (document == null) {
        fail("Don't expect the document to be null");
        return null;
    }
    if (myLineRange != null) {
        final DocumentImpl doc = new DocumentImpl(text);
        myTextRange = new TextRange(doc.getLineStartOffset(myLineRange.getStartOffset()), doc.getLineEndOffset(myLineRange.getEndOffset()));
    }
    final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
    CommandProcessor.getInstance().executeCommand(getProject(), () -> ApplicationManager.getApplication().runWriteAction(() -> {
        document.replaceString(0, document.getTextLength(), text);
        manager.commitDocument(document);
        try {
            TextRange rangeToUse = myTextRange;
            if (rangeToUse == null) {
                rangeToUse = file.getTextRange();
            }
            ACTIONS.get(action).run(file, rangeToUse.getStartOffset(), rangeToUse.getEndOffset());
        } catch (IncorrectOperationException e) {
            assertTrue(e.getLocalizedMessage(), false);
        }
    }), action == REFORMAT ? ReformatCodeProcessor.COMMAND_NAME : "", "");
    return document.getText();
}
Also used : TextRange(com.intellij.openapi.util.TextRange) IncorrectOperationException(com.intellij.util.IncorrectOperationException) DocumentImpl(com.intellij.openapi.editor.impl.DocumentImpl) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) NotNull(org.jetbrains.annotations.NotNull)

Example 50 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project intellij-community by JetBrains.

the class AbstractTreeStructure method asyncCommitDocuments.

@NotNull
public static ActionCallback asyncCommitDocuments(@NotNull Project project) {
    if (project.isDisposed())
        return ActionCallback.DONE;
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    if (!documentManager.hasUncommitedDocuments()) {
        return ActionCallback.DONE;
    }
    final ActionCallback callback = new ActionCallback();
    documentManager.performWhenAllCommitted(callback.createSetDoneRunnable());
    return callback;
}
Also used : ActionCallback(com.intellij.openapi.util.ActionCallback) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiDocumentManager (com.intellij.psi.PsiDocumentManager)140 Document (com.intellij.openapi.editor.Document)111 PsiFile (com.intellij.psi.PsiFile)100 VirtualFile (com.intellij.openapi.vfs.VirtualFile)51 ResourceItem (com.android.ide.common.res2.ResourceItem)26 PsiElement (com.intellij.psi.PsiElement)24 Project (com.intellij.openapi.project.Project)22 TextRange (com.intellij.openapi.util.TextRange)13 NotNull (org.jetbrains.annotations.NotNull)12 Nullable (org.jetbrains.annotations.Nullable)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 Editor (com.intellij.openapi.editor.Editor)7 XmlFile (com.intellij.psi.xml.XmlFile)6 FileType (com.intellij.openapi.fileTypes.FileType)5 XmlTag (com.intellij.psi.xml.XmlTag)5 ASTNode (com.intellij.lang.ASTNode)3 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 DocumentEx (com.intellij.openapi.editor.ex.DocumentEx)2 EditorEx (com.intellij.openapi.editor.ex.EditorEx)2