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;
}
}
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;
}
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;
}
}
}
}
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);
}
Aggregations