Search in sources :

Example 36 with PsiDocumentManager

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

the class CodeStyleAbstractPanel method collectChangesBeforeCurrentSettingsAppliance.

/**
   * Reformats {@link #myTextToReformat target text} with the {@link #mySettings current code style settings} and returns
   * list of changes applied to the target text during that.
   *
   * @param project   project to use
   * @return          list of changes applied to the {@link #myTextToReformat target text} during reformatting. It is sorted
   *                  by change start offset in ascending order
   */
@Nullable
private Document collectChangesBeforeCurrentSettingsAppliance(Project project) {
    PsiFile psiFile = createFileFromText(project, myTextToReformat);
    prepareForReformat(psiFile);
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    if (documentManager != null) {
        Document document = documentManager.getDocument(psiFile);
        if (document != null) {
            CodeStyleSettings clone = mySettings.clone();
            clone.setRightMargin(getDefaultLanguage(), getAdjustedRightMargin());
            CodeStyleSettingsManager.getInstance(project).setTemporarySettings(clone);
            try {
                CodeStyleManager.getInstance(project).reformat(psiFile);
            } finally {
                CodeStyleSettingsManager.getInstance(project).dropTemporarySettings();
            }
            return document;
        }
    }
    return null;
}
Also used : PsiFile(com.intellij.psi.PsiFile) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) Nullable(org.jetbrains.annotations.Nullable)

Example 37 with PsiDocumentManager

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

the class FileContentUtil method setFileText.

/**
   * @deprecated to be removed after IDEA 15. Use {@link VfsUtil#saveText(VirtualFile, String)} instead.
   */
public static void setFileText(@Nullable Project project, final VirtualFile virtualFile, final String text) throws IOException {
    if (project == null) {
        project = ProjectUtil.guessProjectForFile(virtualFile);
    }
    if (project != null) {
        final PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
        final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
        final Document document = psiFile == null ? null : psiDocumentManager.getDocument(psiFile);
        if (document != null) {
            document.setText(text != null ? text : "");
            psiDocumentManager.commitDocument(document);
            FileDocumentManager.getInstance().saveDocument(document);
            return;
        }
    }
    VfsUtil.saveText(virtualFile, text != null ? text : "");
    virtualFile.refresh(false, false);
}
Also used : PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 38 with PsiDocumentManager

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

the class BaseMoveHandler method executeWriteAction.

@Override
public void executeWriteAction(Editor editor, Caret caret, DataContext dataContext) {
    final Project project = editor.getProject();
    assert project != null;
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    final Document document = editor.getDocument();
    documentManager.commitDocument(document);
    PsiFile file = getRoot(documentManager.getPsiFile(document), editor);
    if (file != null) {
        final MoverWrapper mover = getSuitableMover(editor, file);
        if (mover != null && mover.getInfo().toMove2 != null) {
            LineRange range = mover.getInfo().toMove;
            if ((range.startLine > 0 || isDown) && (range.endLine < document.getLineCount() || !isDown)) {
                mover.move(editor, file);
            }
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 39 with PsiDocumentManager

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

the class ImageOrColorPreviewManager method getPsiElementsAt.

@NotNull
private static Collection<PsiElement> getPsiElementsAt(Point point, Editor editor) {
    if (editor.isDisposed()) {
        return Collections.emptySet();
    }
    Project project = editor.getProject();
    if (project == null || project.isDisposed()) {
        return Collections.emptySet();
    }
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    final Document document = editor.getDocument();
    PsiFile psiFile = documentManager.getPsiFile(document);
    if (psiFile == null || psiFile instanceof PsiCompiledElement || !psiFile.isValid()) {
        return Collections.emptySet();
    }
    final Set<PsiElement> elements = Collections.newSetFromMap(new WeakHashMap<PsiElement, Boolean>());
    final int offset = editor.logicalPositionToOffset(editor.xyToLogicalPosition(point));
    if (documentManager.isCommitted(document)) {
        ContainerUtil.addIfNotNull(elements, InjectedLanguageUtil.findElementAtNoCommit(psiFile, offset));
    }
    for (PsiFile file : psiFile.getViewProvider().getAllFiles()) {
        ContainerUtil.addIfNotNull(elements, file.findElementAt(offset));
    }
    return elements;
}
Also used : Project(com.intellij.openapi.project.Project) PsiCompiledElement(com.intellij.psi.PsiCompiledElement) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) NotNull(org.jetbrains.annotations.NotNull)

Example 40 with PsiDocumentManager

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

the class FormatterImpl method validateModel.

private static void validateModel(FormattingModel model) throws FormattingModelInconsistencyException {
    FormattingDocumentModel documentModel = model.getDocumentModel();
    Document document = documentModel.getDocument();
    Block rootBlock = model.getRootBlock();
    if (rootBlock instanceof ASTBlock) {
        PsiElement rootElement = ((ASTBlock) rootBlock).getNode().getPsi();
        if (!rootElement.isValid()) {
            throw new FormattingModelInconsistencyException("Invalid root block PSI element");
        }
        PsiFile file = rootElement.getContainingFile();
        Project project = file.getProject();
        PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
        if (documentManager.isUncommited(document)) {
            throw new FormattingModelInconsistencyException("Uncommitted document");
        }
        if (document.getTextLength() != file.getTextLength()) {
            throw new FormattingModelInconsistencyException("Document length " + document.getTextLength() + " doesn't match PSI file length " + file.getTextLength() + ", language: " + file.getLanguage());
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

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