Search in sources :

Example 6 with PsiManagerImpl

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

the class CreateFileFix method invoke.

private void invoke(@NotNull Project project, PsiDirectory myDirectory) throws IncorrectOperationException {
    // to revalidate applicability
    myIsAvailableTimeStamp = 0;
    try {
        if (myIsDirectory) {
            myDirectory.createSubdirectory(myNewFileName);
        } else {
            String newFileName = myNewFileName;
            String newDirectories = null;
            if (myNewFileName.contains("/")) {
                int pos = myNewFileName.lastIndexOf("/");
                newFileName = myNewFileName.substring(pos + 1);
                newDirectories = myNewFileName.substring(0, pos);
            }
            PsiDirectory directory = myDirectory;
            if (newDirectories != null) {
                try {
                    VfsUtil.createDirectoryIfMissing(myDirectory.getVirtualFile(), newDirectories);
                    VirtualFile vfsDir = VfsUtil.findRelativeFile(myDirectory.getVirtualFile(), ArrayUtil.toStringArray(StringUtil.split(newDirectories, "/")));
                    directory = new PsiDirectoryImpl((PsiManagerImpl) myDirectory.getManager(), vfsDir);
                } catch (IOException e) {
                    throw new IncorrectOperationException(e.getMessage());
                }
            }
            final PsiFile newFile = directory.createFile(newFileName);
            String text = getFileText();
            if (text != null) {
                final FileType type = FileTypeRegistry.getInstance().getFileTypeByFileName(newFileName);
                final PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText("_" + newFileName, type, text);
                final PsiElement psiElement = CodeStyleManager.getInstance(project).reformat(psiFile);
                text = psiElement.getText();
            }
            openFile(project, directory, newFile, text);
        }
    } catch (IncorrectOperationException e) {
        myIsAvailable = false;
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiManagerImpl(com.intellij.psi.impl.PsiManagerImpl) FileType(com.intellij.openapi.fileTypes.FileType) PsiDirectoryImpl(com.intellij.psi.impl.file.PsiDirectoryImpl) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IOException(java.io.IOException) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)

Example 7 with PsiManagerImpl

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

the class PsiFileImplUtil method setName.

public static PsiFile setName(@NotNull PsiFile file, @NotNull String newName) throws IncorrectOperationException {
    VirtualFile vFile = file.getViewProvider().getVirtualFile();
    PsiManagerImpl manager = (PsiManagerImpl) file.getManager();
    try {
        final FileType newFileType = FileTypeRegistry.getInstance().getFileTypeByFileName(newName);
        if (UnknownFileType.INSTANCE.equals(newFileType) || newFileType.isBinary()) {
            // before the file becomes unknown or a binary (thus, not openable in the editor), save it to prevent data loss
            final FileDocumentManager fdm = FileDocumentManager.getInstance();
            final Document doc = fdm.getCachedDocument(vFile);
            if (doc != null) {
                fdm.saveDocumentAsIs(doc);
            }
        }
        vFile.rename(manager, newName);
    } catch (IOException e) {
        throw new IncorrectOperationException(e);
    }
    return file.getViewProvider().isPhysical() ? manager.findFile(vFile) : file;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiManagerImpl(com.intellij.psi.impl.PsiManagerImpl) UnknownFileType(com.intellij.openapi.fileTypes.UnknownFileType) FileType(com.intellij.openapi.fileTypes.FileType) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document)

Example 8 with PsiManagerImpl

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

the class PsiEventWrapperAspect method sendAfterEvents.

private static void sendAfterEvents(TreeChangeEvent changeSet) {
    ASTNode rootElement = changeSet.getRootElement();
    final PsiFile file = (PsiFile) SourceTreeToPsiMap.treeElementToPsi(rootElement);
    final PsiManagerImpl manager = (PsiManagerImpl) file.getManager();
    if (manager == null)
        return;
    if (!file.isPhysical()) {
        manager.afterChange(false);
        return;
    }
    final ASTNode[] changedElements = changeSet.getChangedElements();
    for (ASTNode changedElement : changedElements) {
        TreeChange changesByElement = changeSet.getChangesByElement(changedElement);
        PsiElement psiParent = null;
        while (changedElement != null && ((psiParent = changedElement.getPsi()) == null || !checkPsiForChildren(changesByElement.getAffectedChildren()))) {
            final ASTNode parent = changedElement.getTreeParent();
            final ChangeInfoImpl changeInfo = ChangeInfoImpl.create(ChangeInfo.CONTENTS_CHANGED, changedElement);
            changeInfo.compactChange(changesByElement);
            changesByElement = new TreeChangeImpl(parent);
            changesByElement.addChange(changedElement, changeInfo);
            changedElement = parent;
        }
        if (changedElement == null)
            continue;
        final ASTNode[] affectedChildren = changesByElement.getAffectedChildren();
        for (final ASTNode treeElement : affectedChildren) {
            PsiTreeChangeEventImpl psiEvent = new PsiTreeChangeEventImpl(manager);
            psiEvent.setParent(psiParent);
            psiEvent.setFile(file);
            final PsiElement psiChild = treeElement.getPsi();
            psiEvent.setChild(psiChild);
            final ChangeInfo changeByChild = changesByElement.getChangeByChild(treeElement);
            switch(changeByChild.getChangeType()) {
                case ChangeInfo.ADD:
                    psiEvent.setOffset(treeElement.getStartOffset());
                    psiEvent.setOldLength(0);
                    manager.childAdded(psiEvent);
                    break;
                case ChangeInfo.REPLACE:
                    final ReplaceChangeInfo change = (ReplaceChangeInfo) changeByChild;
                    psiEvent.setOffset(treeElement.getStartOffset());
                    final ASTNode replaced = change.getReplaced();
                    psiEvent.setOldChild(replaced.getPsi());
                    psiEvent.setNewChild(psiChild);
                    psiEvent.setOldLength(replaced.getTextLength());
                    manager.childReplaced(psiEvent);
                    break;
                case ChangeInfo.CONTENTS_CHANGED:
                    psiEvent.setOffset(treeElement.getStartOffset());
                    psiEvent.setParent(psiChild);
                    psiEvent.setOldLength(changeByChild.getOldLength());
                    manager.childrenChanged(psiEvent);
                    break;
                case ChangeInfo.REMOVED:
                    psiEvent.setOffset(changesByElement.getChildOffsetInNewTree(treeElement));
                    psiEvent.setOldParent(psiParent);
                    psiEvent.setOldChild(psiChild);
                    psiEvent.setOldLength(changeByChild.getOldLength());
                    manager.childRemoved(psiEvent);
                    break;
            }
        }
    }
}
Also used : ReplaceChangeInfo(com.intellij.pom.tree.events.ReplaceChangeInfo) PsiManagerImpl(com.intellij.psi.impl.PsiManagerImpl) TreeChange(com.intellij.pom.tree.events.TreeChange) TreeChangeImpl(com.intellij.pom.tree.events.impl.TreeChangeImpl) ReplaceChangeInfo(com.intellij.pom.tree.events.ReplaceChangeInfo) ChangeInfo(com.intellij.pom.tree.events.ChangeInfo) ASTNode(com.intellij.lang.ASTNode) ChangeInfoImpl(com.intellij.pom.tree.events.impl.ChangeInfoImpl) PsiFile(com.intellij.psi.PsiFile) PsiTreeChangeEventImpl(com.intellij.psi.impl.PsiTreeChangeEventImpl) PsiElement(com.intellij.psi.PsiElement)

Example 9 with PsiManagerImpl

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

the class PsiModificationTrackerTreeChangesUpdatesTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    // since we want to test PsiModificationTrackerImpl in isolation, we create a separate instance:
    // The existing PsiModificationTrackerImpl is affected by various components.
    myTracker = new PsiModificationTrackerImpl(getProject());
    ((PsiManagerImpl) PsiManager.getInstance(getProject())).addTreeChangePreprocessor(myTracker);
}
Also used : PsiManagerImpl(com.intellij.psi.impl.PsiManagerImpl) PsiModificationTrackerImpl(com.intellij.psi.impl.PsiModificationTrackerImpl)

Aggregations

PsiManagerImpl (com.intellij.psi.impl.PsiManagerImpl)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 IOException (java.io.IOException)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 Document (com.intellij.openapi.editor.Document)2 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)2 FileType (com.intellij.openapi.fileTypes.FileType)2 PsiFile (com.intellij.psi.PsiFile)2 PsiTreeChangeEventImpl (com.intellij.psi.impl.PsiTreeChangeEventImpl)2 LocalQuickFixAndIntentionActionOnPsiElement (com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)1 ASTNode (com.intellij.lang.ASTNode)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 DocumentReferenceManagerImpl (com.intellij.openapi.command.impl.DocumentReferenceManagerImpl)1 UndoManagerImpl (com.intellij.openapi.command.impl.UndoManagerImpl)1 FileTypeManager (com.intellij.openapi.fileTypes.FileTypeManager)1 UnknownFileType (com.intellij.openapi.fileTypes.UnknownFileType)1 FileTypeManagerEx (com.intellij.openapi.fileTypes.ex.FileTypeManagerEx)1 Project (com.intellij.openapi.project.Project)1 ProjectManager (com.intellij.openapi.project.ProjectManager)1