Skip to content

Commit

Permalink
show directories in goto file without slashes after well-matching fil…
Browse files Browse the repository at this point in the history
…es (IDEA-178943)
  • Loading branch information
donnerpeter committed Sep 21, 2017
1 parent 331acd4 commit 9177747
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,31 +196,39 @@ class Intf {

def popup = createPopup(new GotoFileModel(project), fooIndex)

def fooDir = null
def barDir = null
runInEdtAndWait {
fooDir = fooIndex.containingDirectory
barDir = barIndex.containingDirectory
}
def fooDir = fooIndex.containingDirectory
def barDir = barIndex.containingDirectory

assert calcPopupElements(popup, "foo/") == [fooDir]
assert calcPopupElements(popup, "foo\\") == [fooDir]
assert calcPopupElements(popup, "/foo") == [fooDir]
assert calcPopupElements(popup, "\\foo") == [fooDir]
assert calcPopupElements(popup, "foo") == []
assert calcPopupElements(popup, "foo") == [fooDir]
assert calcPopupElements(popup, "/index.html") == [fooIndex]
assert calcPopupElements(popup, "\\index.html") == [fooIndex]
assert calcPopupElements(popup, "index.html/") == [fooIndex]
assert calcPopupElements(popup, "index.html\\") == [fooIndex]
assert calcPopupElements(popup, "index.html/") == []
assert calcPopupElements(popup, "index.html\\") == []

assert calcPopupElements(popup, "bar.txt/") == [barDir]
assert calcPopupElements(popup, "bar.txt\\") == [barDir]
assert calcPopupElements(popup, "/bar.txt") == [barDir]
assert calcPopupElements(popup, "\\bar.txt") == [barDir]
assert calcPopupElements(popup, "bar.txt") == [barIndex]
assert calcPopupElements(popup, "/bar.txt") == [barIndex, barDir]
assert calcPopupElements(popup, "\\bar.txt") == [barIndex, barDir]
assert calcPopupElements(popup, "bar.txt") == [barIndex, barDir]
assert calcPopupElements(popup, "bar") == [barIndex, barDir]
popup.close(false)
}

void "test prefer files to directories even if longer"() {
def fooFile = myFixture.addFileToProject('dir/fooFile.txt', '')
def fooDir = myFixture.addFileToProject('foo/barFile.txt', '').containingDirectory

def popup = createPopup(new GotoFileModel(project))
def popupElements = calcPopupElements(popup, 'foo')

assert popupElements == [fooFile, fooDir]
assert popup.calcSelectedIndex(popupElements.toArray(), 'foo') == 0
}

void "test find method by qualified name"() {
def clazz = myFixture.addClass("package foo.bar; class Goo { void zzzZzz() {} }")
def method = clazz.methods[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ public static PsiFile[] getFilesByName(final Project project, final String name,
}

public static boolean processFilesByName(@NotNull final String name,
boolean includeDirs,
boolean directories,
@NotNull Processor<? super PsiFileSystemItem> processor,
@NotNull GlobalSearchScope scope,
@NotNull Project project,
@Nullable IdFilter idFilter) {
return processFilesByName(name, includeDirs, true, processor, scope, project, idFilter);
return processFilesByName(name, directories, true, processor, scope, project, idFilter);
}

public static boolean processFilesByName(@NotNull final String name,
boolean includeDirs,
boolean directories,
boolean caseSensitively,
@NotNull Processor<? super PsiFileSystemItem> processor,
@NotNull final GlobalSearchScope scope,
Expand All @@ -109,13 +109,13 @@ public static boolean processFilesByName(@NotNull final String name,

for(VirtualFile file: files) {
if (!file.isValid()) continue;
if (!includeDirs && !file.isDirectory()) {
if (!directories && !file.isDirectory()) {
PsiFile psiFile = psiManager.findFile(file);
if (psiFile != null) {
if(!processor.process(psiFile)) return true;
++processedFiles;
}
} else if (includeDirs && file.isDirectory()) {
} else if (directories && file.isDirectory()) {
PsiDirectory dir = psiManager.findDirectory(file);
if (dir != null) {
if(!processor.process(dir)) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFileSystemItem;
import com.intellij.psi.PsiManager;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.FixingLayoutMatcher;
import com.intellij.psi.codeStyle.MinusculeMatcher;
import com.intellij.psi.codeStyle.NameUtil;
Expand Down Expand Up @@ -135,7 +133,8 @@ private List<PsiFileSystemItem> getFilesMatchingPath(@NotNull String pattern,

if (group.size() > 1) {
Collections.sort(group,
Comparator.<PsiFileSystemItem, Integer>comparing(nesting::get).
Comparator.<PsiFileSystemItem, Boolean>comparing(f -> f instanceof PsiDirectory).
thenComparing(nesting::get).
thenComparing(dirCloseness::get).
thenComparing(qualifierMatchingDegrees::get).
thenComparing(getPathProximityComparator()).
Expand Down Expand Up @@ -272,8 +271,9 @@ private List<List<String>> groupByMatchingDegree(boolean preferStartMatches) {
List<List<String>> groups = new ArrayList<>();

Comparator<MatchResult> comparator = (mr1, mr2) -> {
boolean exactPrefix1 = patternSuffix.equalsIgnoreCase(mr1.elementName);
boolean exactPrefix2 = patternSuffix.equalsIgnoreCase(mr2.elementName);
boolean exactPrefix1 = StringUtil.startsWithIgnoreCase(mr1.elementName, patternSuffix);
boolean exactPrefix2 = StringUtil.startsWithIgnoreCase(mr2.elementName, patternSuffix);
if (exactPrefix1 && exactPrefix2) return 0;
if (exactPrefix1 != exactPrefix2) return exactPrefix1 ? -1 : 1;
return mr1.compareDegrees(mr2, preferStartMatches);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,20 @@ public void processElementsWithName(@NotNull String name,
}
return _processor.process(item);
};

String completePattern = parameters.getCompletePattern();
final boolean includeDirs = completePattern.endsWith("/") || completePattern.endsWith("\\") ||
completePattern.startsWith("/") || completePattern.startsWith("\\");
boolean result = FilenameIndex.processFilesByName(
name, includeDirs, processor, parameters.getSearchScope(), parameters.getProject(), parameters.getIdFilter()
);
if (!result && includeDirs) {

if (!isDirectoryOnlyPattern(parameters)) {
FilenameIndex.processFilesByName(
name, false, processor, parameters.getSearchScope(), parameters.getProject(), parameters.getIdFilter()
);
}

FilenameIndex.processFilesByName(
name, true, processor, parameters.getSearchScope(), parameters.getProject(), parameters.getIdFilter()
);
}

private static boolean isDirectoryOnlyPattern(@NotNull FindSymbolParameters parameters) {
String completePattern = parameters.getCompletePattern();
return completePattern.endsWith("/") || completePattern.endsWith("\\");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Comparator;

/**
* Model for "Go to | File" action
*/
public class GotoFileModel extends FilteringGotoByModel<FileType> implements DumbAware {
public class GotoFileModel extends FilteringGotoByModel<FileType> implements DumbAware, Comparator<Object> {
private final int myMaxSize;

public GotoFileModel(@NotNull Project project) {
Expand Down Expand Up @@ -186,4 +187,10 @@ public String removeModelSpecificMarkup(@NotNull String pattern) {
}
return pattern;
}

/** Just to remove smartness from {@link ChooseByNameBase#calcSelectedIndex} */
@Override
public int compare(Object o1, Object o2) {
return 0;
}
}

0 comments on commit 9177747

Please sign in to comment.