Search in sources :

Example 61 with PsiDirectory

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

the class CCShowPreview method actionPerformed.

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
    final Project project = e.getProject();
    Module module = LangDataKeys.MODULE.getData(e.getDataContext());
    if (project == null || module == null) {
        return;
    }
    final PsiFile file = CommonDataKeys.PSI_FILE.getData(e.getDataContext());
    if (file == null) {
        return;
    }
    Course course = StudyTaskManager.getInstance(project).getCourse();
    if (course == null) {
        return;
    }
    VirtualFile virtualFile = file.getVirtualFile();
    TaskFile taskFile = StudyUtils.getTaskFile(project, virtualFile);
    if (taskFile == null) {
        return;
    }
    final PsiDirectory taskDir = file.getContainingDirectory();
    if (taskDir == null) {
        return;
    }
    PsiDirectory lessonDir = taskDir.getParentDirectory();
    if (lessonDir == null) {
        return;
    }
    if (taskFile.getActivePlaceholders().isEmpty()) {
        Messages.showInfoMessage("Preview is available for task files with answer placeholders only", "No Preview for This File");
        return;
    }
    VirtualFile generatedFilesFolder = CCUtils.getGeneratedFilesFolder(project, module);
    if (generatedFilesFolder == null) {
        return;
    }
    ApplicationManager.getApplication().runWriteAction(new Runnable() {

        @Override
        public void run() {
            Pair<VirtualFile, TaskFile> pair = EduUtils.createStudentFile(this, project, virtualFile, generatedFilesFolder, null, taskFile.getTask().getActiveSubtaskIndex());
            if (pair != null) {
                showPreviewDialog(project, pair.getFirst(), pair.getSecond());
            }
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) Project(com.intellij.openapi.project.Project) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) Course(com.jetbrains.edu.learning.courseFormat.Course) Pair(com.intellij.openapi.util.Pair)

Example 62 with PsiDirectory

use of com.intellij.psi.PsiDirectory in project WebStormRequireJsPlugin by Fedott.

the class RequirejsProjectComponent method getCompletion.

public List<String> getCompletion(PsiElement element) {
    List<String> completions = new ArrayList<String>();
    String value = element.getText().replace("'", "").replace("\"", "").replace("IntellijIdeaRulezzz ", "");
    String valuePath = value;
    boolean exclamationMark = value.contains("!");
    String plugin = "";
    int doubleDotCount = 0;
    boolean notEndSlash = false;
    String pathOnDots = "";
    String dotString = "";
    VirtualFile elementFile = element.getContainingFile().getOriginalFile().getVirtualFile();
    if (exclamationMark) {
        String[] exclamationMarkSplit = valuePath.split("!");
        plugin = exclamationMarkSplit[0];
        if (exclamationMarkSplit.length == 2) {
            valuePath = exclamationMarkSplit[1];
        } else {
            valuePath = "";
        }
    }
    if (exclamationMark) {
        for (String moduleName : getModulesNames()) {
            completions.add(plugin + '!' + moduleName);
        }
    } else {
        completions.addAll(getModulesNames());
    // expand current package
    }
    PsiDirectory fileDirectory = element.getContainingFile().getOriginalFile().getContainingDirectory();
    if (null == fileDirectory) {
        return completions;
    }
    String filePath = fileDirectory.getVirtualFile().getPath().replace(getWebDir(elementFile).getPath(), "");
    if (filePath.startsWith("/")) {
        filePath = filePath.substring(1);
    }
    boolean startSlash = valuePath.startsWith("/");
    if (startSlash) {
        valuePath = valuePath.substring(1);
    }
    boolean oneDot = valuePath.startsWith("./");
    if (oneDot) {
        if (filePath.isEmpty()) {
            valuePath = valuePath.substring(2);
        } else {
            valuePath = valuePath.replaceFirst(".", filePath);
        }
    }
    if (valuePath.startsWith("..")) {
        doubleDotCount = FileUtils.getDoubleDotCount(valuePath);
        String[] pathsOfPath = filePath.split("/");
        if (pathsOfPath.length > 0) {
            if (doubleDotCount > 0) {
                if (doubleDotCount > pathsOfPath.length || filePath.isEmpty()) {
                    return new ArrayList<String>();
                }
                pathOnDots = FileUtils.getNormalizedPath(doubleDotCount, pathsOfPath);
                dotString = StringUtil.repeat("../", doubleDotCount);
                if (valuePath.endsWith("..")) {
                    notEndSlash = true;
                }
                if (valuePath.endsWith("..") || !StringUtil.isEmpty(pathOnDots)) {
                    dotString = dotString.substring(0, dotString.length() - 1);
                }
                valuePath = valuePath.replaceFirst(dotString, pathOnDots);
            }
        }
    }
    List<String> allFiles = FileUtils.getAllFilesInDirectory(getWebDir(elementFile), getWebDir(elementFile).getPath() + '/', "");
    List<String> aliasFiles = requirePaths.getAllFilesOnPaths();
    aliasFiles.addAll(packageConfig.getAllFilesOnPackages());
    String requireMapModule = FileUtils.removeExt(element.getContainingFile().getOriginalFile().getVirtualFile().getPath().replace(getWebDir(elementFile).getPath() + '/', ""), ".js");
    completions.addAll(requireMap.getCompletionByModule(requireMapModule));
    String valuePathForAlias = valuePath;
    if (!oneDot && 0 == doubleDotCount && !startSlash && !getBaseUrl().isEmpty()) {
        valuePath = FileUtils.join(getBaseUrl(), valuePath);
    }
    for (String file : allFiles) {
        if (file.startsWith(valuePath)) {
            // Prepare file path
            if (oneDot) {
                if (filePath.isEmpty()) {
                    file = "./" + file;
                } else {
                    file = file.replaceFirst(filePath, ".");
                }
            }
            if (doubleDotCount > 0) {
                if (!StringUtil.isEmpty(valuePath)) {
                    file = file.replace(pathOnDots, "");
                }
                if (notEndSlash) {
                    file = '/' + file;
                }
                file = dotString + file;
            }
            if (!oneDot && 0 == doubleDotCount && !startSlash && !getBaseUrl().isEmpty()) {
                file = file.substring(getBaseUrl().length() + 1);
            }
            if (startSlash) {
                file = '/' + file;
            }
            addToCompletion(completions, file, exclamationMark, plugin);
        }
    }
    for (String file : aliasFiles) {
        if (file.startsWith(valuePathForAlias)) {
            addToCompletion(completions, file, exclamationMark, plugin);
        }
    }
    return completions;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiDirectory(com.intellij.psi.PsiDirectory) ArrayList(java.util.ArrayList)

Example 63 with PsiDirectory

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

the class CompilerIconLayerProvider method getLayerIcon.

@Override
public Icon getLayerIcon(@NotNull Iconable element, boolean isLocked) {
    VirtualFile vFile = null;
    Project project = null;
    if (element instanceof PsiModifierListOwner) {
        project = ((PsiModifierListOwner) element).getProject();
        final PsiFile containingFile = ((PsiModifierListOwner) element).getContainingFile();
        vFile = containingFile == null ? null : containingFile.getVirtualFile();
    } else if (element instanceof PsiDirectory) {
        project = ((PsiDirectory) element).getProject();
        vFile = ((PsiDirectory) element).getVirtualFile();
    }
    if (vFile != null && isExcluded(vFile, project)) {
        return PlatformIcons.EXCLUDED_FROM_COMPILE_ICON;
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) PsiModifierListOwner(com.intellij.psi.PsiModifierListOwner)

Example 64 with PsiDirectory

use of com.intellij.psi.PsiDirectory in project intellij-elixir by KronicDeth.

the class CreateElixirModuleAction method buildDialog.

/*
     * Protected Instance Methods
     */
/**
     * todo: the Application-template, Supervisor-template, GenServer-template, GenEvent-template should be improved
     */
@Override
protected void buildDialog(@NotNull Project project, @NotNull final PsiDirectory directory, @NotNull CreateFileFromTemplateDialog.Builder builder) {
    builder.setTitle(NEW_ELIXIR_MODULE).addKind("Empty module", ElixirIcons.FILE, "Elixir Module").addKind("Elixir Application", ElixirIcons.ELIXIR_APPLICATION, "Elixir Application").addKind("Elixir Supervisor", ElixirIcons.ELIXIR_SUPERVISOR, "Elixir Supervisor").addKind("Elixir GenServer", ElixirIcons.ELIXIR_GEN_SERVER, "Elixir GenServer").addKind("Elixir GenEvent", ElixirIcons.ELIXIR_GEN_EVENT, "Elixir GenEvent").setValidator(new InputValidatorEx() {

        /*
                     * Public Instance Methods
                     */
        @Override
        public boolean canClose(String inputString) {
            return !StringUtil.isEmptyOrSpaces(inputString) && getErrorText(inputString) == null;
        }

        @Override
        public boolean checkInput(String inputString) {
            return checkFormat(inputString) && checkDoesNotExist(inputString);
        }

        @Nullable
        @Override
        public String getErrorText(String inputString) {
            String errorText = null;
            if (!StringUtil.isEmpty(inputString)) {
                if (!checkFormat(inputString)) {
                    errorText = String.format(INVALID_MODULE_MESSAGE_FMT, inputString);
                } else if (!checkDoesNotExist(inputString)) {
                    String fullPath = fullPath(directory, ancestorDirectoryNamesBaseNamePair(inputString));
                    errorText = String.format(EXISTING_MODULE_MESSAGE_FMT, fullPath);
                }
            }
            return errorText;
        }

        /*
                     * Private Instance Methods
                     */
        private boolean checkDoesNotExist(@NotNull String moduleName) {
            Pair<List<String>, String> ancestorDirectoryNamesBaseNamePair = ancestorDirectoryNamesBaseNamePair(moduleName);
            List<String> ancestorDirectoryNames = ancestorDirectoryNamesBaseNamePair.first;
            PsiDirectory currentDirectory = directory;
            boolean doesNotExists = false;
            for (String ancestorDirectoryName : ancestorDirectoryNames) {
                PsiDirectory subdirectory = currentDirectory.findSubdirectory(ancestorDirectoryName);
                if (subdirectory == null) {
                    doesNotExists = true;
                    break;
                }
                currentDirectory = subdirectory;
            }
            // if all the directories exist
            if (!doesNotExists) {
                String baseName = ancestorDirectoryNamesBaseNamePair.second;
                doesNotExists = currentDirectory.findFile(baseName) == null;
            }
            return doesNotExists;
        }

        private boolean checkFormat(@NotNull String inputString) {
            Matcher matcher = MODULE_NAME_PATTERN.matcher(inputString);
            return matcher.matches();
        }
    });
}
Also used : Matcher(java.util.regex.Matcher) PsiDirectory(com.intellij.psi.PsiDirectory) ArrayList(java.util.ArrayList) List(java.util.List) InputValidatorEx(com.intellij.openapi.ui.InputValidatorEx) Nullable(org.jetbrains.annotations.Nullable) Pair(com.intellij.openapi.util.Pair)

Example 65 with PsiDirectory

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

the class JarRootsRefreshTest method checkMove.

private void checkMove(File jar, VirtualFile vFile, PsiFile file) {
    VirtualFile jarRoot;
    File libDir = new File(jar.getParent(), "lib");
    assertTrue(libDir.mkdir());
    VirtualFile vLibDir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(libDir);
    assertNotNull(vLibDir);
    jarRoot = JarFileSystem.getInstance().getRootByLocal(vFile);
    assertNotNull(jarRoot);
    assertTrue(jarRoot.isValid());
    PsiDirectory directory = getPsiManager().findDirectory(vLibDir);
    DataContext psiDataContext = SimpleDataContext.getSimpleContext(LangDataKeys.TARGET_PSI_ELEMENT.getName(), directory);
    new MoveHandler().invoke(myProject, new PsiElement[] { file }, psiDataContext);
    assertFalse(jarRoot.isValid());
    jarRoot = JarFileSystem.getInstance().getRootByLocal(vFile);
    assertNotNull(jarRoot);
    assertTrue(jarRoot.isValid());
    rename(directory, "lib2");
    assertFalse(jarRoot.isValid());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DataContext(com.intellij.openapi.actionSystem.DataContext) SimpleDataContext(com.intellij.openapi.actionSystem.impl.SimpleDataContext) PsiDirectory(com.intellij.psi.PsiDirectory) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) PsiFile(com.intellij.psi.PsiFile) MoveHandler(com.intellij.refactoring.move.MoveHandler)

Aggregations

PsiDirectory (com.intellij.psi.PsiDirectory)321 VirtualFile (com.intellij.openapi.vfs.VirtualFile)122 PsiFile (com.intellij.psi.PsiFile)111 PsiElement (com.intellij.psi.PsiElement)79 Project (com.intellij.openapi.project.Project)73 Module (com.intellij.openapi.module.Module)68 Nullable (org.jetbrains.annotations.Nullable)62 NotNull (org.jetbrains.annotations.NotNull)50 IdeView (com.intellij.ide.IdeView)31 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)24 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)24 PsiManager (com.intellij.psi.PsiManager)24 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)20 ArrayList (java.util.ArrayList)20 PsiPackage (com.intellij.psi.PsiPackage)17 File (java.io.File)15 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)11 IncorrectOperationException (com.intellij.util.IncorrectOperationException)11 Course (com.jetbrains.edu.learning.courseFormat.Course)11 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)10