Search in sources :

Example 1 with PsiDirectoryImpl

use of com.intellij.psi.impl.file.PsiDirectoryImpl 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 2 with PsiDirectoryImpl

use of com.intellij.psi.impl.file.PsiDirectoryImpl in project intellij-plugins by JetBrains.

the class DartServerResolverTest method getPresentableElementPosition.

@NotNull
private static String getPresentableElementPosition(@NotNull final CodeInsightTestFixture fixture, @Nullable final PsiElement element) {
    if (element == null)
        return "";
    final StringBuilder buf = new StringBuilder(element.getText());
    DartComponent component = PsiTreeUtil.getParentOfType(element, DartComponent.class);
    while (component != null) {
        final DartComponentName componentName = component.getComponentName();
        if (componentName != null && componentName != element) {
            buf.insert(0, component.getName() + " -> ");
        }
        component = PsiTreeUtil.getParentOfType(component, DartComponent.class);
    }
    String path = element instanceof PsiDirectoryImpl ? ((PsiDirectoryImpl) element).getVirtualFile().getPath() : element.getContainingFile().getVirtualFile().getPath();
    final String contentRoot = ModuleRootManager.getInstance(fixture.getModule()).getContentRoots()[0].getPath();
    if (path.equals(contentRoot))
        path = "[content root]";
    final String contentRootWithSlash = contentRoot + "/";
    path = StringUtil.trimStart(path, contentRootWithSlash);
    final DartSdk sdk = DartSdk.getDartSdk(element.getProject());
    if (sdk != null && path.startsWith(sdk.getHomePath()))
        path = "[Dart SDK]" + path.substring(sdk.getHomePath().length());
    if (buf.length() > 0)
        buf.insert(0, " -> ");
    buf.insert(0, path);
    return buf.toString();
}
Also used : DartSdk(com.jetbrains.lang.dart.sdk.DartSdk) DartComponentName(com.jetbrains.lang.dart.psi.DartComponentName) DartComponent(com.jetbrains.lang.dart.psi.DartComponent) PsiDirectoryImpl(com.intellij.psi.impl.file.PsiDirectoryImpl) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiDirectoryImpl (com.intellij.psi.impl.file.PsiDirectoryImpl)2 LocalQuickFixAndIntentionActionOnPsiElement (com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)1 FileType (com.intellij.openapi.fileTypes.FileType)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiManagerImpl (com.intellij.psi.impl.PsiManagerImpl)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 DartComponent (com.jetbrains.lang.dart.psi.DartComponent)1 DartComponentName (com.jetbrains.lang.dart.psi.DartComponentName)1 DartSdk (com.jetbrains.lang.dart.sdk.DartSdk)1 IOException (java.io.IOException)1 NotNull (org.jetbrains.annotations.NotNull)1