Java Code Examples for com.intellij.openapi.vfs.VfsUtil#findRelativeFile()

The following examples show how to use com.intellij.openapi.vfs.VfsUtil#findRelativeFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Symfony2ProjectComponent.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * If plugin is not enabled on first project start/indexing we will never get a filled
 * index until a forced cache rebuild, we check also for vendor path
 */
public static boolean isEnabledForIndex(Project project) {

    if(Settings.getInstance(project).pluginEnabled) {
        return true;
    }

    if(VfsUtil.findRelativeFile(ProjectUtil.getProjectDir(project), "vendor", "symfony") != null) {
        return true;
    }

    // drupal8; this should not really here
    if(VfsUtil.findRelativeFile(ProjectUtil.getProjectDir(project), "core", "vendor", "symfony") != null) {
        return true;
    }

    return false;
}
 
Example 2
Source File: ProjectFileTargetLocator.java    From idea-php-toolbox with MIT License 6 votes vote down vote up
@NotNull
@Override
public Collection<PsiElement> getTargets(@NotNull TargetLocatorParameter parameter) {
    if(!parameter.getTarget().startsWith("file://")) {
        return Collections.emptyList();
    }

    String projectFile = parameter.getTarget().substring("file://".length());
    projectFile = StringUtil.trimStart(projectFile.replaceAll("\\\\+", "/").replaceAll("/+", "/"), "/");

    VirtualFile relativeFile = VfsUtil.findRelativeFile(parameter.getProject().getBaseDir(), projectFile.split("/"));
    if(relativeFile == null) {
        return Collections.emptyList();
    }

    PsiFile file = PsiManager.getInstance(parameter.getProject()).findFile(relativeFile);
    if(file == null) {
        return Collections.emptyList();
    }

    Collection<PsiElement> targets = new ArrayList<>();
    targets.add(file);

    return targets;
}
 
Example 3
Source File: Symfony2ProjectComponent.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void checkProject() {
    if(!this.isEnabled()
        && !Settings.getInstance(project).dismissEnableNotification
        && VfsUtil.findRelativeFile(ProjectUtil.getProjectDir(this.project), "vendor", "symfony") != null
        ) {

        IdeHelper.notifyEnableMessage(project);
        return;
    }

    if(this.getContainerFiles().size() == 0) {
        Symfony2ProjectComponent.getLogger().warn("missing at least one container file");
    }
}
 
Example 4
Source File: ProfilerSettingsDialog.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private TextBrowseFolderListener createBrowseFolderListener(final JTextField textField, final FileChooserDescriptor fileChooserDescriptor) {
    return new TextBrowseFolderListener(fileChooserDescriptor) {
        @Override
        public void actionPerformed(ActionEvent e) {
            VirtualFile projectDirectory = ProjectUtil.getProjectDir(project);

            String text = textField.getText();
            VirtualFile toSelect = VfsUtil.findRelativeFile(text, projectDirectory);
            if(toSelect == null) {
                toSelect = projectDirectory;
            }

            VirtualFile selectedFile = FileChooser.chooseFile(
                FileChooserDescriptorFactory.createSingleFileDescriptor("csv"),
                project,
                toSelect
            );

            if (null == selectedFile) {
                return;
            }

            String path = VfsUtil.getRelativePath(selectedFile, projectDirectory, '/');
            if (null == path) {
                path = selectedFile.getPath();
            }

            textField.setText(path);
        }
    };
}
 
Example 5
Source File: IdeUtil.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@Override
public void run() {
    VirtualFile relativeDirectory = VfsUtil.findRelativeFile(targetPathRelative, project.getBaseDir());
    if(relativeDirectory != null) {
        PsiDirectory directory = PsiManager.getInstance(project).findDirectory(relativeDirectory);

        if(directory != null) {
            PsiFile virtualFile = createFile(directory, fileName, this.content);
            if(virtualFile != null) {
                new OpenFileDescriptor(project, virtualFile.getVirtualFile(), 0).navigate(true);
            }
        }
    }
}
 
Example 6
Source File: IdeHelper.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
public static VirtualFile createFile(@NotNull Project project, @NotNull FileType fileType, @Nullable VirtualFile root, @NotNull String fileNameWithPath, @Nullable String content) {

    if(root == null) {
        return null;
    }

    String[] filenameSplit = fileNameWithPath.split("/");
    String pathString = StringUtils.join(Arrays.copyOf(filenameSplit, filenameSplit.length - 1), "/");

    VirtualFile twigDirectory = VfsUtil.findRelativeFile(root, filenameSplit);
    if(twigDirectory != null) {
        return null;
    }

    VirtualFile targetDir;
    try {
        targetDir = VfsUtil.createDirectoryIfMissing(root, pathString);
    } catch (IOException ignored) {
        return null;
    }

    PsiFileFactory factory = PsiFileFactory.getInstance(project);
    final PsiFile file = factory.createFileFromText(filenameSplit[filenameSplit.length - 1], fileType, content != null ? content : "");
    CodeStyleManager.getInstance(project).reformat(file);
    PsiDirectory directory = PsiManager.getInstance(project).findDirectory(targetDir);
    if(directory == null) {
        return null;
    }

    PsiElement add = directory.add(file);
    if(add instanceof PsiFile) {
        return ((PsiFile) add).getVirtualFile();
    }

    return null;
}
 
Example 7
Source File: ThemeUtil.java    From idea-php-shopware-plugin with MIT License 5 votes vote down vote up
public static void visitThemeAssetsFile(@NotNull PhpClass phpClass, final @NotNull ThemeAssetVisitor themeAssetVisitor) {

        PsiDirectory parent = phpClass.getContainingFile().getParent();
        if(parent == null) {
            return;
        }

        final VirtualFile publicFolder = VfsUtil.findRelativeFile(parent.getVirtualFile(), "frontend", "_public");
        if(publicFolder == null) {
            return;
        }

        // collect on project template dir
        VfsUtil.visitChildrenRecursively(publicFolder, new VirtualFileVisitor() {
            @Override
            public boolean visitFile(@NotNull VirtualFile virtualFile) {

                if(!"js".equals(virtualFile.getExtension())) {
                    return true;
                }

                String relative = VfsUtil.getRelativePath(virtualFile, publicFolder, '/');
                if(relative == null) {
                    return true;
                }

                return themeAssetVisitor.visit(virtualFile, relative);
            }
        });


    }
 
Example 8
Source File: LaravelProjectComponent.java    From idea-php-laravel-plugin with MIT License 5 votes vote down vote up
public static boolean isEnabledForIndex(@Nullable Project project) {

        if(project == null) {
            return false;
        }

        if(isEnabled(project)) {
            return true;
        }

        VirtualFile baseDir = project.getBaseDir();
        return VfsUtil.findRelativeFile(baseDir, "vendor", "laravel") != null;
    }
 
Example 9
Source File: NeosProjectService.java    From intellij-neos with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isNeosProject(Project project) {
    VirtualFile projectDir = guessProjectDir(project);
    return (VfsUtil.findRelativeFile(projectDir, "Packages") != null
            && VfsUtil.findRelativeFile(projectDir, "Configuration") != null
            && (VfsUtil.findRelativeFile(projectDir, "Packages", "Application", "TYPO3.Neos") != null
            || VfsUtil.findRelativeFile(projectDir, "Packages", "Application", "Neos.Neos") != null));
}
 
Example 10
Source File: TwigPath.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
public VirtualFile getDirectory(@NotNull Project project) {
    if(!FileUtil.isAbsolute(path)) {
        return VfsUtil.findRelativeFile(path, ProjectUtil.getProjectDir(project));
    } else {
        VirtualFile fileByIoFile = VfsUtil.findFileByIoFile(new File(path), true);
        if(fileByIoFile != null) {
            return fileByIoFile;
        }
    }

    return null;
}
 
Example 11
Source File: AbstractUiFilePath.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public boolean exists(@NotNull Project project) {
    if (!FileUtil.isAbsolute(this.path)) {
        return VfsUtil.findRelativeFile(this.path, ProjectUtil.getProjectDir(project)) != null;
    }

    return new File(this.path).exists();
}
 
Example 12
Source File: LaravelProjectComponent.java    From Thinkphp5-Plugin with MIT License 5 votes vote down vote up
public static boolean isEnabledForIndex(@Nullable Project project) {

        if(project == null) {
            return false;
        }

        if(isEnabled(project)) {
            return true;
        }

        VirtualFile baseDir = project.getBaseDir();
        return VfsUtil.findRelativeFile(baseDir, "vendor", "laravel") != null;
    }
 
Example 13
Source File: SymfonyBundle.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
public VirtualFile getRelative(@NotNull String path) {
    PsiDirectory virtualDirectory =  this.getDirectory();
    if(virtualDirectory == null) {
        return null;
    }

   return VfsUtil.findRelativeFile(virtualDirectory.getVirtualFile(), path.split("/"));
}
 
Example 14
Source File: BasicFormatFilter.java    From CppTools with Apache License 2.0 5 votes vote down vote up
public Result applyFilter(String line, int entireLength) {
  if (fileNameAndSizeExractor == null) return null;
  final Matcher matcher = fileNameAndSizeExractor.matcher(line);

  if (matcher.find()) {
    String fileName = matcher.group(1).trim();
    if (fileName.length() == 0) return null;
    final boolean hasLineNumber = matcher.group(2) != null;
    final boolean hasColumnNumber = matcher.groupCount() > 2 && matcher.group(3) != null;
    final int lineNumber = hasLineNumber ? Integer.parseInt(matcher.group(2)):0;
    final int columnNumber = hasColumnNumber ? Integer.parseInt(matcher.group(3)):0;

    VirtualFile child = resolveFilename(fileName);
    if (child == null) child = VfsUtil.findRelativeFile(fileName, null);
    if (child == null) return null;

    return new BasicFormatResult(
      child,
      entireLength + matcher.start(1) - line.length(),
      entireLength + matcher.end(hasLineNumber ? (hasColumnNumber ? 3:2):1) - line.length(),
      lineNumber,
      columnNumber,
      line.indexOf("error") != -1
    );
  }
  return null;
}
 
Example 15
Source File: AssetDirectoryReader.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@Nullable
private static VirtualFile getProjectAssetRoot(@NotNull Project project) {
    String webDirectoryName = Settings.getInstance(project).directoryToWeb;
    return VfsUtil.findRelativeFile(ProjectUtil.getProjectDir(project), webDirectoryName.split("/"));
}
 
Example 16
Source File: ViewCollector.java    From Thinkphp5-Plugin with MIT License 4 votes vote down vote up
@Nullable
@Override
public Result<Collection<TemplatePath>> compute() {

    Collection<TemplatePath> twigPaths = new ArrayList<>();

    String text = psiFile.getText();
    JsonTemplatePaths configJson = null;
    try {
        configJson = new Gson().fromJson(text, JsonTemplatePaths.class);
    } catch (JsonSyntaxException | JsonIOException | IllegalStateException ignored) {
    }

    if (configJson == null) {
        return Result.create(twigPaths, psiFile, psiFile.getVirtualFile());
    }

    Collection<JsonTemplatePaths.Path> namespaces = configJson.getNamespaces();
    if (namespaces == null || namespaces.size() == 0) {
        return Result.create(twigPaths, psiFile, psiFile.getVirtualFile());
    }

    for (JsonTemplatePaths.Path jsonPath : namespaces) {
        String path = jsonPath.getPath();
        if (path == null) {
            path = "";
        }

        path = StringUtils.stripStart(path.replace("\\", "/"), "/");
        PsiDirectory parent = psiFile.getParent();
        if (parent == null) {
            continue;
        }

        // current directory check and subfolder
        VirtualFile twigRoot;
        if (path.length() > 0) {
            twigRoot = VfsUtil.findRelativeFile(parent.getVirtualFile(), path.split("/"));
        } else {
            twigRoot = psiFile.getParent().getVirtualFile();
        }

        if (twigRoot == null) {
            continue;
        }

        String relativePath = VfsExUtil.getRelativeProjectPath(psiFile.getProject(), twigRoot);
        if (relativePath == null) {
            continue;
        }

        String namespace = jsonPath.getNamespace();

        String namespacePath = StringUtils.stripStart(relativePath, "/");

        if (StringUtils.isNotBlank(namespace)) {
            twigPaths.add(new TemplatePath(namespacePath, namespace, true));
        } else {
            twigPaths.add(new TemplatePath(namespacePath, true));
        }
    }

    return Result.create(twigPaths, psiFile, psiFile.getVirtualFile());
}
 
Example 17
Source File: HookSubscriberUtil.java    From idea-php-shopware-plugin with MIT License 4 votes vote down vote up
public static void collectHooks(Project project, HookVisitor hookVisitor) {

        Collection<PhpClass> phpClasses = new ArrayList<>();

        // directly use core classes
        PhpIndex phpIndex = PhpIndex.getInstance(project);
        for(String coreClass: CORE_CLASSES) {
            phpClasses.addAll(phpIndex.getClassesByName(coreClass));
        }

        // fallback: search on directory
        VirtualFile virtualFile = VfsUtil.findRelativeFile(project.getBaseDir(), "engine", "core", "class");
        if(virtualFile != null) {
            for(VirtualFile coreClassFile: VfsUtil.getChildren(virtualFile)) {
                String name = coreClassFile.getName();
                if(name.contains(".")) name = name.substring(0, name.lastIndexOf('.'));
                if(!CORE_CLASSES.contains(name)) {
                    phpClasses.addAll(phpIndex.getClassesByName(name));
                }
            }
        }

        phpClasses.addAll(phpIndex.getAllSubclasses("\\Enlight_Hook"));

        for(PhpClass phpClass: phpClasses) {

            // dont use proxy classes
            String presentableFQN = phpClass.getPresentableFQN();
            if((presentableFQN.endsWith("Proxy") && PhpElementsUtil.isInstanceOf(phpClass, "\\Enlight_Hook_Proxy"))) {
                continue;
            }

            for(Method method: phpClass.getMethods()) {
                if(!method.getAccess().isPrivate() && !method.isStatic() && !method.isAbstract() && !method.getName().startsWith("_")) {
                    boolean returnValue = hookVisitor.visitHook(phpClass, method);
                    if(!returnValue) {
                        return;
                    }
                }
            }
        }

    }
 
Example 18
Source File: EnvironmentFacade.java    From CppTools with Apache License 2.0 4 votes vote down vote up
public static VirtualFile getSdkHomeDirectory(Sdk projectJdk) {
  return VfsUtil.findRelativeFile(projectJdk.getHomePath(), null); // TODO
}
 
Example 19
Source File: ViewCollector.java    From idea-php-laravel-plugin with MIT License 4 votes vote down vote up
@Nullable
@Override
public Result<Collection<TemplatePath>> compute() {

    Collection<TemplatePath> twigPaths = new ArrayList<>();

    String text = psiFile.getText();
    JsonTemplatePaths configJson = null;
    try {
        configJson = new Gson().fromJson(text, JsonTemplatePaths.class);
    } catch (JsonSyntaxException | JsonIOException | IllegalStateException ignored) {
    }

    if(configJson == null) {
        return Result.create(twigPaths, psiFile, psiFile.getVirtualFile());
    }

    Collection<JsonTemplatePaths.Path> namespaces = configJson.getNamespaces();
    if(namespaces == null || namespaces.size() == 0) {
        return Result.create(twigPaths, psiFile, psiFile.getVirtualFile());
    }

    for(JsonTemplatePaths.Path jsonPath : namespaces) {
        String path = jsonPath.getPath();
        if(path == null) {
            path = "";
        }

        path = StringUtils.stripStart(path.replace("\\", "/"), "/");
        PsiDirectory parent = psiFile.getParent();
        if(parent == null) {
            continue;
        }

        // current directory check and subfolder
        VirtualFile twigRoot;
        if(path.length() > 0) {
            twigRoot = VfsUtil.findRelativeFile(parent.getVirtualFile(), path.split("/"));
        } else {
            twigRoot = psiFile.getParent().getVirtualFile();
        }

        if(twigRoot == null) {
            continue;
        }

        String relativePath = VfsExUtil.getRelativeProjectPath(psiFile.getProject(), twigRoot);
        if(relativePath == null) {
            continue;
        }

        String namespace = jsonPath.getNamespace();

        String namespacePath = StringUtils.stripStart(relativePath, "/");

        if(StringUtils.isNotBlank(namespace)) {
            twigPaths.add(new TemplatePath(namespacePath, namespace, true));
        } else {
            twigPaths.add(new TemplatePath(namespacePath, true));
        }
    }

    return Result.create(twigPaths, psiFile, psiFile.getVirtualFile());
}
 
Example 20
Source File: TemplatePath.java    From Thinkphp5-Plugin with MIT License 4 votes vote down vote up
@Nullable
public VirtualFile getRelativePath(Project project) {
    return VfsUtil.findRelativeFile(project.getBaseDir(), this.getPath().split("/"));
}