mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-104327 Navigate > File... > prioritize based on the current file location
This commit is contained in:
@@ -106,6 +106,21 @@ class Intf {
|
||||
assert elements == [ooIndex, fooIndex, fooBarIndex]
|
||||
}
|
||||
|
||||
public void "test prefer files from current directory"() {
|
||||
def fooIndex = myFixture.addFileToProject("foo/index.html", "foo")
|
||||
def barIndex = myFixture.addFileToProject("bar/index.html", "bar")
|
||||
def fooContext = myFixture.addFileToProject("foo/context.html", "")
|
||||
def barContext = myFixture.addFileToProject("bar/context.html", "")
|
||||
|
||||
def popup = createPopup(new GotoFileModel(project), fooContext)
|
||||
assert getPopupElements(popup, "index") == [fooIndex, barIndex]
|
||||
popup.close(false)
|
||||
|
||||
popup = createPopup(new GotoFileModel(project), barContext)
|
||||
assert getPopupElements(popup, "index") == [barIndex, fooIndex]
|
||||
|
||||
}
|
||||
|
||||
private List<Object> getPopupElements(ChooseByNameModel model, String text) {
|
||||
return getPopupElements(createPopup(model), text)
|
||||
}
|
||||
@@ -122,8 +137,8 @@ class Intf {
|
||||
return elements
|
||||
}
|
||||
|
||||
private ChooseByNamePopup createPopup(ChooseByNameModel model) {
|
||||
def popup = ChooseByNamePopup.createPopup(project, model, (PsiElement)null, "")
|
||||
private ChooseByNamePopup createPopup(ChooseByNameModel model, PsiElement context = null) {
|
||||
def popup = ChooseByNamePopup.createPopup(project, model, (PsiElement)context, "")
|
||||
Disposer.register(testRootDisposable, { popup.close(false) } as Disposable)
|
||||
popup
|
||||
}
|
||||
|
||||
+13
-37
@@ -18,7 +18,6 @@ package com.intellij.ide.util.gotoByName;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.progress.ProgressIndicator;
|
||||
import com.intellij.openapi.progress.ProgressManager;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.PsiCompiledElement;
|
||||
@@ -54,7 +53,7 @@ public class DefaultChooseByNameItemProvider implements ChooseByNameItemProvider
|
||||
|
||||
if (removeModelSpecificMarkup(base, pattern).isEmpty() && !base.canShowListForEmptyPattern()) return true;
|
||||
|
||||
ChooseByNameModel model = base.getModel();
|
||||
final ChooseByNameModel model = base.getModel();
|
||||
String matchingPattern = convertToMatchingPattern(base, namePattern);
|
||||
List<MatchResult> namesList = new ArrayList<MatchResult>();
|
||||
processNamesByPattern(base, base.getNames(everywhere), matchingPattern, new CollectConsumer<MatchResult>(namesList));
|
||||
@@ -64,10 +63,14 @@ public class DefaultChooseByNameItemProvider implements ChooseByNameItemProvider
|
||||
|
||||
List<Object> sameNameElements = new SmartList<Object>();
|
||||
final Map<Object, MatchResult> sameNameWeights = new THashMap<Object, MatchResult>();
|
||||
|
||||
Comparator<Object> weightComparator = new Comparator<Object>() {
|
||||
Comparator<Object> modelComparator = model instanceof Comparator ? (Comparator<Object>)model : new PathProximityComparator(model, myContext.get());
|
||||
|
||||
@Override
|
||||
public int compare(Object o1, Object o2) {
|
||||
return sameNameWeights.get(o1).compareTo(sameNameWeights.get(o2));
|
||||
int result = modelComparator.compare(o1, o2);
|
||||
return result != 0 ? result : sameNameWeights.get(o1).compareTo(sameNameWeights.get(o2));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -99,7 +102,6 @@ public class DefaultChooseByNameItemProvider implements ChooseByNameItemProvider
|
||||
sameNameWeights.put(element, qualifierResult);
|
||||
}
|
||||
}
|
||||
sortByProximity(base, sameNameElements);
|
||||
Collections.sort(sameNameElements, weightComparator);
|
||||
for (Object element : sameNameElements) {
|
||||
if (!sameNameWeights.get(element).startMatch) {
|
||||
@@ -133,17 +135,6 @@ public class DefaultChooseByNameItemProvider implements ChooseByNameItemProvider
|
||||
Collections.sort(namesList);
|
||||
}
|
||||
|
||||
private void sortByProximity(@NotNull ChooseByNameBase base, @NotNull List<Object> sameNameElements) {
|
||||
final ChooseByNameModel model = base.getModel();
|
||||
if (model instanceof Comparator) {
|
||||
//noinspection unchecked
|
||||
Collections.sort(sameNameElements, (Comparator)model);
|
||||
}
|
||||
else {
|
||||
Collections.sort(sameNameElements, new PathProximityComparator(model, myContext.get()));
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String getQualifierPattern(@NotNull ChooseByNameBase base, @NotNull String pattern) {
|
||||
final String[] separators = base.getModel().getSeparators();
|
||||
@@ -314,33 +305,18 @@ public class DefaultChooseByNameItemProvider implements ChooseByNameItemProvider
|
||||
myProximityComparator = new PsiProximityComparator(context);
|
||||
}
|
||||
|
||||
private static boolean isCompiledWithoutSource(Object o) {
|
||||
return o instanceof PsiCompiledElement && ((PsiCompiledElement)o).getNavigationElement() == o;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(final Object o1, final Object o2) {
|
||||
int rc = myProximityComparator.compare(o1, o2);
|
||||
if (rc != 0) return rc;
|
||||
|
||||
int compare = Comparing.compare(myModel.getFullName(o1), myModel.getFullName(o2));
|
||||
if (compare == 0) {
|
||||
int o1Weight;
|
||||
int o2Weight;
|
||||
|
||||
if (o1 instanceof PsiCompiledElement) {
|
||||
PsiElement navElement = ((PsiCompiledElement)o1).getNavigationElement();
|
||||
o1Weight = navElement != o1 ? 0 : 1;
|
||||
} else {
|
||||
o1Weight = 0;
|
||||
}
|
||||
|
||||
if (o2 instanceof PsiCompiledElement) {
|
||||
PsiElement navElement = ((PsiCompiledElement)o2).getNavigationElement();
|
||||
o2Weight = navElement != o2 ? 0 : 1;
|
||||
} else {
|
||||
o2Weight = 0;
|
||||
}
|
||||
|
||||
compare = o1Weight - o2Weight;
|
||||
}
|
||||
return compare;
|
||||
int o1Weight = isCompiledWithoutSource(o1) ? 1 : 0;
|
||||
int o2Weight = isCompiledWithoutSource(o2) ? 1 : 0;
|
||||
return o1Weight - o2Weight;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,13 +19,10 @@ import com.intellij.openapi.util.NullableLazyKey;
|
||||
import com.intellij.psi.PsiDirectory;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.util.ProximityLocation;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.NullableFunction;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* NOTE: This class is only registered in platform-based IDEs. In IDEA, SamePackageWeigher is used instead.
|
||||
*
|
||||
* @author yole
|
||||
*/
|
||||
public class SameDirectoryWeigher extends ProximityWeigher {
|
||||
@@ -33,20 +30,23 @@ public class SameDirectoryWeigher extends ProximityWeigher {
|
||||
PLACE_DIRECTORY = NullableLazyKey.create("placeDirectory", new NullableFunction<ProximityLocation, PsiDirectory>() {
|
||||
@Override
|
||||
public PsiDirectory fun(ProximityLocation location) {
|
||||
return PsiTreeUtil.getParentOfType(location.getPosition(), PsiDirectory.class, false);
|
||||
return getParentDirectory(location.getPosition());
|
||||
}
|
||||
});
|
||||
|
||||
private static PsiDirectory getParentDirectory(PsiElement element) {
|
||||
while (element != null && !(element instanceof PsiDirectory)) {
|
||||
element = element.getParent();
|
||||
}
|
||||
return (PsiDirectory)element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Comparable weigh(@NotNull final PsiElement element, @NotNull final ProximityLocation location) {
|
||||
if (location.getPosition() == null){
|
||||
return null;
|
||||
public Boolean weigh(@NotNull final PsiElement element, @NotNull final ProximityLocation location) {
|
||||
if (location.getPosition() == null) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
final PsiDirectory placeDirectory = PLACE_DIRECTORY.getValue(location);
|
||||
if (placeDirectory == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return placeDirectory.equals(PsiTreeUtil.getParentOfType(element, PsiDirectory.class, false));
|
||||
return placeDirectory != null && placeDirectory.equals(getParentDirectory(element));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -464,8 +464,10 @@
|
||||
<statistician key="completion" implementationClass="com.intellij.codeInsight.completion.DefaultCompletionStatistician" order="last"/>
|
||||
|
||||
<weigher key="proximity" implementationClass="com.intellij.psi.util.proximity.OpenedInEditorWeigher" id="openedInEditor"/>
|
||||
<weigher key="proximity" implementationClass="com.intellij.psi.util.proximity.SameLogicalRootWeigher" id="sameLogicalRoot"
|
||||
<weigher key="proximity" implementationClass="com.intellij.psi.util.proximity.SameDirectoryWeigher" id="sameDirectory"
|
||||
order="after openedInEditor"/>
|
||||
<weigher key="proximity" implementationClass="com.intellij.psi.util.proximity.SameLogicalRootWeigher" id="sameLogicalRoot"
|
||||
order="after sameDirectory"/>
|
||||
<weigher key="proximity" implementationClass="com.intellij.psi.util.proximity.SameModuleWeigher" id="sameModule"
|
||||
order="after sameLogicalRoot"/>
|
||||
<weigher key="proximity" implementationClass="com.intellij.psi.util.proximity.SdkOrLibraryWeigher" id="sdkOrLibrary"
|
||||
|
||||
@@ -85,9 +85,6 @@
|
||||
|
||||
<checkoutCompletedListener implementation="com.intellij.openapi.vcs.checkout.PlatformProjectCheckoutListener" id="PlatformProjectCheckoutListener"/>
|
||||
|
||||
<weigher key="proximity" implementationClass="com.intellij.psi.util.proximity.SameDirectoryWeigher" id="sameDirectory"
|
||||
order="after openedInEditor"/>
|
||||
|
||||
</extensions>
|
||||
|
||||
<xi:include href="xdebugger.xml" xpointer="xpointer(/idea-plugin/*)"/>
|
||||
|
||||
Reference in New Issue
Block a user