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

The following examples show how to use com.intellij.openapi.vfs.VfsUtil#getRelativePath() . 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: TranslationKeyIntentionAction.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public String getText() {
    String filename = psiFile.getName();

    // try to find suitable presentable filename
    VirtualFile virtualFile = psiFile.getVirtualFile();
    if(virtualFile != null) {
        filename = virtualFile.getPath();
        String relativePath = VfsUtil.getRelativePath(virtualFile, ProjectUtil.getProjectDir(psiFile), '/');
        if(relativePath != null) {
            filename =  relativePath;
        }
    }

    return "Create translation key: " + filename;
}
 
Example 2
Source File: UiSettingsUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
public static String getPathDialog(@NotNull Project project, @NotNull FileType fileType, @Nullable String current) {
    VirtualFile projectDirectory = ProjectUtil.getProjectDir(project);

    VirtualFile selectedFileBefore = null;
    if(current != null) {
        selectedFileBefore = VfsUtil.findRelativeFile(current, projectDirectory);
    }

    VirtualFile selectedFile = FileChooser.chooseFile(
            FileChooserDescriptorFactory.createSingleFileDescriptor(fileType),
            project,
            selectedFileBefore
    );

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

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

    return path;
}
 
Example 3
Source File: TwigNamespaceDialog.java    From idea-php-symfony2-plugin with MIT License 6 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);
            VirtualFile selectedFile = FileChooser.chooseFile(
                    fileChooserDescriptor,
                    project,
                    VfsUtil.findRelativeFile(textField.getText(), projectDirectory)
            );

            if (null == selectedFile) {
                return; // Ignore but keep the previous path
            }

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

            textField.setText(path);
        }
    };
}
 
Example 4
Source File: BladeTemplateUtil.java    From idea-php-laravel-plugin with MIT License 6 votes vote down vote up
@NotNull
public static Collection<String> resolveTemplateName(@NotNull Project project, @NotNull VirtualFile virtualFile) {
    Set<String> templateNames = new HashSet<>();

    for(TemplatePath templatePath : ViewCollector.getPaths(project)) {
        VirtualFile viewDir = templatePath.getRelativePath(project);
        if(viewDir == null) {
            continue;
        }

        String relativePath = VfsUtil.getRelativePath(virtualFile, viewDir);
        if(relativePath != null) {
            relativePath = stripTemplateExtensions(relativePath);

            if(templatePath.getNamespace() != null && StringUtils.isNotBlank(templatePath.getNamespace())) {
                templateNames.add(templatePath.getNamespace() + "::" + relativePath.replace("/", "."));
            } else {
                templateNames.add(relativePath.replace("/", "."));
            }
        }
    }

    return templateNames;
}
 
Example 5
Source File: JavascriptServiceNameStrategy.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
public static Object run(@NotNull Project project, @NotNull String className, @NotNull String serviceJsNameStrategy) throws ScriptException {

    JsonObject jsonObject = new JsonObject();

    jsonObject.addProperty("className", className);
    jsonObject.addProperty("projectName", project.getName());
    jsonObject.addProperty("projectBasePath", project.getBasePath());
    jsonObject.addProperty("defaultNaming", new DefaultServiceNameStrategy().getServiceName(new ServiceNameStrategyParameter(project, className)));

    PhpClass aClass = PhpElementsUtil.getClass(project, className);
    if(aClass != null) {
        String relativePath = VfsUtil.getRelativePath(aClass.getContainingFile().getVirtualFile(), ProjectUtil.getProjectDir(aClass), '/');
        if(relativePath != null) {
            jsonObject.addProperty("relativePath", relativePath);
        }
        jsonObject.addProperty("absolutePath", aClass.getContainingFile().getVirtualFile().toString());
    }

    ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
    if(engine == null) {
        return null;
    }

    return engine.eval("var __p = eval(" + jsonObject.toString() + "); result = function(args) { " + serviceJsNameStrategy + " }(__p)");
}
 
Example 6
Source File: IndexUtil.java    From idea-php-drupal-symfony2-bridge with MIT License 6 votes vote down vote up
public static boolean isValidForIndex(@NotNull FileContent inputData, @NotNull PsiFile psiFile) {

        String fileName = psiFile.getName();
        if(fileName.startsWith(".") || fileName.endsWith("Test")) {
            return false;
        }

        VirtualFile baseDir = inputData.getProject().getBaseDir();
        if(baseDir == null) {
            return false;
        }

        // is Test file in path name
        String relativePath = VfsUtil.getRelativePath(inputData.getFile(), baseDir, '/');
        if(relativePath != null && (relativePath.contains("/Test/") || relativePath.contains("/Fixtures/"))) {
            return false;
        }

        return true;
    }
 
Example 7
Source File: AnnotationUtil.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
public static boolean isValidForIndex(@NotNull FileContent inputData) {
    String fileName = inputData.getPsiFile().getName();
    if(fileName.startsWith(".") || fileName.contains("Test")) {
        return false;
    }

    // we check for project path, on no match we are properly inside external library paths
    VirtualFile baseDir = inputData.getProject().getBaseDir();
    if (baseDir == null) {
        return true;
    }

    String relativePath = VfsUtil.getRelativePath(inputData.getFile(), baseDir, '/');
    if(relativePath == null) {
        return true;
    }

    // is Test file in path name
    return !(relativePath.contains("\\Test\\") || relativePath.contains("\\Fixtures\\"));
}
 
Example 8
Source File: ContainerBuilderStubIndex.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private static boolean isValidForIndex(FileContent inputData, PsiFile psiFile) {

        String fileName = psiFile.getName();
        if(fileName.startsWith(".") || fileName.endsWith("Test")) {
            return false;
        }

        // is Test file in path name
        String relativePath = VfsUtil.getRelativePath(inputData.getFile(), ProjectUtil.getProjectDir(inputData.getProject()), '/');
        if(relativePath != null && (relativePath.contains("/Test/") || relativePath.contains("/Tests/") || relativePath.contains("/Fixture/") || relativePath.contains("/Fixtures/"))) {
            return false;
        }

        // dont index files larger then files; use 5 MB here
        if(inputData.getFile().getLength() > MAX_FILE_BYTE_SIZE) {
            return false;
        }

        return true;
    }
 
Example 9
Source File: SettingsForm.java    From idea-php-symfony2-plugin with MIT License 6 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);
            VirtualFile selectedFile = FileChooser.chooseFile(
                fileChooserDescriptor,
                project,
                VfsUtil.findRelativeFile(textField.getText(), projectDirectory)
            );

            if (null == selectedFile) {
                return; // Ignore but keep the previous path
            }

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

            textField.setText(path);
        }
    };
}
 
Example 10
Source File: RoutesStubIndex.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private static boolean isValidForIndex(FileContent inputData, PsiFile psiFile) {

        String fileName = psiFile.getName();
        if(fileName.startsWith(".") || fileName.endsWith("Test")) {
            return false;
        }

        VirtualFile baseDir = ProjectUtil.getProjectDir(inputData.getProject());
        if(baseDir == null) {
            return false;
        }

        // is Test file in path name
        String relativePath = VfsUtil.getRelativePath(inputData.getFile(), baseDir, '/');
        if(relativePath != null && (relativePath.contains("/Test/") || relativePath.contains("/Fixtures/"))) {
            return false;
        }

        return true;
    }
 
Example 11
Source File: TwigUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@NotNull
public static String getPresentableTemplateName(@NotNull PsiElement psiElement, boolean shortMode) {
    VirtualFile currentFile = psiElement.getContainingFile().getVirtualFile();

    List<String> templateNames = new ArrayList<>(
        getTemplateNamesForFile(psiElement.getProject(), currentFile)
    );

    if(templateNames.size() > 0) {

        // bundle names wins
        if(templateNames.size() > 1) {
            templateNames.sort(new TemplateStringComparator());
        }

        String templateName = templateNames.iterator().next();
        if(shortMode) {
            String shortName = getFoldingTemplateName(templateName);
            if(shortName != null) {
                return shortName;
            }
        }

        return templateName;
    }

    String relativePath = VfsUtil.getRelativePath(currentFile, ProjectUtil.getProjectDir(psiElement), '/');
    return relativePath != null ? relativePath : currentFile.getPath();

}
 
Example 12
Source File: SymfonyBundle.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
public String getRelativePath(@NotNull VirtualFile file) {
    PsiDirectory currentDir = this.getDirectory();

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

    return VfsUtil.getRelativePath(file, currentDir.getVirtualFile(), '/');
}
 
Example 13
Source File: TranslationKeyIntentionAndQuickFixAction.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@NotNull
private String getPresentableName(@NotNull Project project, @NotNull VirtualFile virtualFile) {
    // try to find suitable presentable filename
    String filename = virtualFile.getPath();

    String relativePath = VfsUtil.getRelativePath(virtualFile, ProjectUtil.getProjectDir(project), '/');
    if(relativePath != null) {
        filename =  relativePath;
    }

    return StringUtils.abbreviate(filename, 180);
}
 
Example 14
Source File: SymfonyBundle.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
public String getRelative(@NotNull VirtualFile virtualFile) {
    PsiDirectory virtualDirectory =  this.getDirectory();
    if(virtualDirectory == null) {
        return null;
    }

    return VfsUtil.getRelativePath(virtualFile, virtualDirectory.getVirtualFile(), '/');
}
 
Example 15
Source File: VfsExUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public static String getRelativeProjectPath(@NotNull Project project, @NotNull VirtualFile virtualFile) {
    // hacking around project as temp file
    if(ApplicationManager.getApplication().isUnitTestMode()) {
        return virtualFile.getPath();
    }
    return VfsUtil.getRelativePath(virtualFile, ProjectUtil.getProjectDir(project));
}
 
Example 16
Source File: VfsExUtil.java    From Thinkphp5-Plugin with MIT License 5 votes vote down vote up
public static String getRelativeProjectPath(@NotNull Project project, @NotNull VirtualFile virtualFile) {
    // hacking around project as temp file
    if(ApplicationManager.getApplication().isUnitTestMode()) {
        return virtualFile.getPath();
    }
    return VfsUtil.getRelativePath(virtualFile, project.getBaseDir());
}
 
Example 17
Source File: TwigUtil.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@Nullable
static String getTemplateNameForTwigPath(@NotNull Project project, @NotNull TwigPath twigPath, @NotNull VirtualFile virtualFile) {
    VirtualFile directory = twigPath.getDirectory(project);
    if(directory == null) {
        return null;
    }

    String templatePath = VfsUtil.getRelativePath(virtualFile, directory, '/');
    if(templatePath == null) {
        return null;
    }

    String templateDirectory; // xxx:XXX:xxx
    String templateFile; // xxx:xxx:XXX

    if (templatePath.contains("/")) {
        int lastDirectorySeparatorIndex = templatePath.lastIndexOf("/");
        templateDirectory = templatePath.substring(0, lastDirectorySeparatorIndex);
        templateFile = templatePath.substring(lastDirectorySeparatorIndex + 1);
    } else {
        templateDirectory = "";
        templateFile = templatePath;
    }

    String namespace = twigPath.getNamespace().equals(MAIN) ? "" : twigPath.getNamespace();

    String templateFinalName;
    if(twigPath.getNamespaceType() == NamespaceType.BUNDLE) {
        templateFinalName = namespace + ":" + templateDirectory + ":" + templateFile;
    } else {
        templateFinalName = namespace + "/" + templateDirectory + "/" + templateFile;

        // remove empty path and check for root (global namespace)
        templateFinalName = templateFinalName.replace("//", "/");
        if(templateFinalName.startsWith("/")) {
            templateFinalName = templateFinalName.substring(1);
        } else {
            templateFinalName = "@" + templateFinalName;
        }
    }

    return templateFinalName;
}
 
Example 18
Source File: TranslationFileModel.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@Nullable
public String getRelativePath() {
    return VfsUtil.getRelativePath(psiFile.getVirtualFile(), ProjectUtil.getProjectDir(psiFile), '/');
}
 
Example 19
Source File: AssetFile.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public String toString() {
    return this.prefix + VfsUtil.getRelativePath(assetFile, relativeFolder, '/');
}
 
Example 20
Source File: FileUtils.java    From vue-for-idea with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static String makeRelative(VirtualFile root, VirtualFile absolutePath) {
    //FileUtil.getRelativePath(path, file.getPath().replace('/', File.separatorChar), File.separatorChar)
    return VfsUtil.getRelativePath(absolutePath, root, File.separatorChar);
}