Search in sources :

Example 1 with GotoFileModel

use of com.intellij.ide.util.gotoByName.GotoFileModel in project intellij-community by JetBrains.

the class GotoFileAction method gotoActionPerformed.

@Override
public void gotoActionPerformed(AnActionEvent e) {
    final Project project = e.getData(CommonDataKeys.PROJECT);
    if (project == null)
        return;
    FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.popup.file");
    final GotoFileModel gotoFileModel = new GotoFileModel(project);
    GotoActionCallback<FileType> callback = new GotoActionCallback<FileType>() {

        @Override
        protected ChooseByNameFilter<FileType> createFilter(@NotNull ChooseByNamePopup popup) {
            return new GotoFileFilter(popup, gotoFileModel, project);
        }

        @Override
        public void elementChosen(final ChooseByNamePopup popup, final Object element) {
            if (element == null)
                return;
            ApplicationManager.getApplication().assertIsDispatchThread();
            Navigatable n = (Navigatable) element;
            //this is for better cursor position
            if (element instanceof PsiFile) {
                VirtualFile file = ((PsiFile) element).getVirtualFile();
                if (file == null)
                    return;
                OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, popup.getLinePosition(), popup.getColumnPosition());
                n = descriptor.setUseCurrentWindow(popup.isOpenInCurrentWindowRequested());
            }
            if (n.canNavigate()) {
                n.navigate(true);
            }
        }
    };
    GotoFileItemProvider provider = new GotoFileItemProvider(project, getPsiContext(e), gotoFileModel);
    showNavigationPopup(e, gotoFileModel, callback, IdeBundle.message("go.to.file.toolwindow.title"), true, true, provider);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) NotNull(org.jetbrains.annotations.NotNull) Navigatable(com.intellij.pom.Navigatable) Project(com.intellij.openapi.project.Project) FileType(com.intellij.openapi.fileTypes.FileType) ChooseByNamePopup(com.intellij.ide.util.gotoByName.ChooseByNamePopup) GotoFileModel(com.intellij.ide.util.gotoByName.GotoFileModel) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

Example 2 with GotoFileModel

use of com.intellij.ide.util.gotoByName.GotoFileModel in project intellij-community by JetBrains.

the class TestDataGuessByExistingFilesUtil method buildDescriptor.

@NotNull
private static TestDataDescriptor buildDescriptor(@NotNull String test, @NotNull PsiClass psiClass) {
    ProjectFileIndex fileIndex = ProjectRootManager.getInstance(psiClass.getProject()).getFileIndex();
    GotoFileModel gotoModel = new GotoFileModel(psiClass.getProject());
    Set<TestLocationDescriptor> descriptors = new HashSet<>();
    // PhpStorm has tests that use '$' symbol as a file path separator, e.g. 'test$while_stmt$declaration' test 
    // stands for '/while_smt/declaration.php' file somewhere in a test data.
    final String possibleFileName = ContainerUtil.getLastItem(StringUtil.split(test, "$"), test);
    assert possibleFileName != null;
    final String possibleFilePath = test.replace('$', '/');
    final Collection<String> fileNames = getAllFileNames(possibleFileName, gotoModel);
    for (String name : fileNames) {
        ProgressManager.checkCanceled();
        boolean currentNameProcessed = false;
        final Object[] elements = gotoModel.getElementsByName(name, false, name);
        for (Object element : elements) {
            if (!(element instanceof PsiFile)) {
                continue;
            }
            final VirtualFile file = ((PsiFile) element).getVirtualFile();
            if (file == null || fileIndex.isInSource(file) && !fileIndex.isUnderSourceRootOfType(file, JavaModuleSourceRootTypes.RESOURCES)) {
                continue;
            }
            final String filePath = file.getPath();
            if (!StringUtil.containsIgnoreCase(filePath, possibleFilePath) && !StringUtil.containsIgnoreCase(filePath, test)) {
                continue;
            }
            final String fileName = PathUtil.getFileName(filePath).toLowerCase();
            int i = fileName.indexOf(possibleFileName.toLowerCase());
            // to be matched to the test 'testEnter()'.
            if (i < 0 || (i + possibleFileName.length() < fileName.length()) && Character.isDigit(fileName.charAt(i + possibleFileName.length()))) {
                continue;
            }
            TestLocationDescriptor current = new TestLocationDescriptor();
            current.populate(possibleFileName, file);
            if (!current.isComplete()) {
                continue;
            }
            currentNameProcessed = true;
            if (descriptors.isEmpty() || (descriptors.iterator().next().dir.equals(current.dir) && !descriptors.contains(current))) {
                descriptors.add(current);
                continue;
            }
            if (moreRelevantPath(current, descriptors, psiClass)) {
                descriptors.clear();
                descriptors.add(current);
            }
        }
        if (currentNameProcessed) {
            break;
        }
    }
    return new TestDataDescriptor(descriptors, possibleFileName);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) GotoFileModel(com.intellij.ide.util.gotoByName.GotoFileModel) PsiFile(com.intellij.psi.PsiFile) HashSet(com.intellij.util.containers.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GotoFileModel (com.intellij.ide.util.gotoByName.GotoFileModel)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiFile (com.intellij.psi.PsiFile)2 NotNull (org.jetbrains.annotations.NotNull)2 ChooseByNamePopup (com.intellij.ide.util.gotoByName.ChooseByNamePopup)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 FileType (com.intellij.openapi.fileTypes.FileType)1 Project (com.intellij.openapi.project.Project)1 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)1 Navigatable (com.intellij.pom.Navigatable)1 HashSet (com.intellij.util.containers.HashSet)1