Skip to content

Commit

Permalink
Merge pull request #2313 from Haehnchen/feature/search-everywhere-rou…
Browse files Browse the repository at this point in the history
…te-matcher

provide a full partial route matcher search for "Search Everywhere" urls
  • Loading branch information
Haehnchen authored Mar 30, 2024
2 parents a30f447 + 279ac28 commit 928a20d
Show file tree
Hide file tree
Showing 7 changed files with 305 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.intellij.openapi.editor.Editor;
import com.intellij.patterns.InitialPatternCondition;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.patterns.PsiElementPattern;
import com.intellij.psi.PsiElement;
import com.intellij.util.ProcessingContext;
import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
Expand Down Expand Up @@ -84,14 +83,6 @@ public static class GotoDeclaration implements GotoDeclarationHandler {
}
}

private static PsiElementPattern.@NotNull Capture<JSLiteralExpression> getPlace() {
return PlatformPatterns.psiElement(JSLiteralExpression.class).withParent(
PlatformPatterns.psiElement(JSArgumentList.class).withParent(
PlatformPatterns.psiElement(JSCallExpression.class)
)
);
}

/**
* insertHandler are after already inserted string, so remove it and replace it :)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ public Icon getIcon(boolean b) {
public static NavigationItemExStateless create(@NotNull PsiElement psiElement, @NotNull String name, @NotNull Icon icon, @NotNull String locationString, boolean appendBundleLocation) {
String locationPathString = locationString;

if(appendBundleLocation) {
if (appendBundleLocation) {
PsiFile psiFile = psiElement.getContainingFile();
if(psiFile != null) {
if (psiFile != null) {
locationPathString = locationString + " " + psiFile.getName();

String bundleName = psiFile.getVirtualFile().getPath();

if(bundleName.contains("Bundle")) {
if (bundleName.contains("Bundle")) {
bundleName = bundleName.substring(0, bundleName.lastIndexOf("Bundle"));
if(bundleName.length() > 1 && bundleName.contains("/")) {
if (bundleName.length() > 1 && bundleName.contains("/")) {
locationPathString = locationPathString + " " + bundleName.substring(bundleName.lastIndexOf("/") + 1) + "::" + psiFile.getName();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package fr.adrienbrault.idea.symfony2plugin.navigation;

import com.intellij.ide.util.PsiNavigationSupport;
import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.NavigationItem;
import com.intellij.openapi.util.NlsSafe;
import com.intellij.pom.Navigatable;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
* @author Daniel Espendiller <[email protected]>
*/
public class NavigationItemPresentableOverwrite implements NavigationItem, ItemPresentation {

@NotNull
private final PsiElement psiElement;

@NotNull
private final String presentableText;

@NotNull
private final Icon icon;

@NotNull
private final String locationString;
private final String name;

private NavigationItemPresentableOverwrite(@NotNull PsiElement psiElement, @NotNull String presentableText, @NotNull Icon icon, @NotNull String locationString, @NotNull String name) {
this.psiElement = psiElement;
this.presentableText = presentableText;
this.name = name;
this.icon = icon;
this.locationString = locationString;
}

@Override
public @NotNull String getName() {
return this.name;
}

@Nullable
@Override
public ItemPresentation getPresentation() {
return new ItemPresentation() {
@Override
public @NlsSafe @NotNull String getPresentableText() {
return NavigationItemPresentableOverwrite.this.presentableText;
}

@Override
public @NlsSafe @NotNull String getLocationString() {
return NavigationItemPresentableOverwrite.this.locationString;
}

@Override
public @Nullable Icon getIcon(boolean unused) {
return NavigationItemPresentableOverwrite.this.getIcon(unused);
}
};
}

@Override
public void navigate(boolean requestFocus) {
final Navigatable descriptor = PsiNavigationSupport.getInstance().getDescriptor(this.psiElement);
if (descriptor != null) {
descriptor.navigate(requestFocus);
}
}

@Override
public boolean canNavigate() {
return PsiNavigationSupport.getInstance().canNavigate(this.psiElement);
}

@Override
public boolean canNavigateToSource() {
return canNavigate();
}

@Override
public String toString() {
return this.presentableText;
}

@Override
public @NotNull String getPresentableText() {
return presentableText;
}

@Override
public @NotNull String getLocationString() {
return this.locationString;
}

@Nullable
@Override
public Icon getIcon(boolean b) {
return icon;
}

public static NavigationItemPresentableOverwrite create(@NotNull PsiElement psiElement, @NotNull String presentableText, @NotNull Icon icon, @NotNull String locationString, boolean appendBundleLocation, @NotNull String name) {
String locationPathString = locationString;

if(appendBundleLocation) {
PsiFile psiFile = psiElement.getContainingFile();
if(psiFile != null) {
locationPathString = locationString + " " + psiFile.getName();

String bundleName = psiFile.getVirtualFile().getPath();

if(bundleName.contains("Bundle")) {
bundleName = bundleName.substring(0, bundleName.lastIndexOf("Bundle"));
if(bundleName.length() > 1 && bundleName.contains("/")) {
locationPathString = locationPathString + " " + bundleName.substring(bundleName.lastIndexOf("/") + 1) + "::" + psiFile.getName();
}
}
}
}

return new NavigationItemPresentableOverwrite(
psiElement,
presentableText,
icon,
locationPathString,
name
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ public void processNames(@NotNull Processor<? super String> processor, @NotNull

for (Route route : routes.values()) {
processor.process(route.getName());

String path = route.getPath();
if (path != null) {
processor.process(path);
}
}
}

Expand All @@ -51,9 +46,5 @@ public void processElementsWithName(@NotNull String name, @NotNull Processor<? s
for (PsiElement psiElement : RouteHelper.getMethods(project, name)) {
processor.process((new NavigationItemEx(psiElement, name, Symfony2Icons.ROUTE, "Symfony Route")));
}

for (PsiElement psiElement : RouteHelper.getMethodsForPathWithPlaceholderMatch(project, name)) {
processor.process((NavigationItemExStateless.create(psiElement, name, Symfony2Icons.ROUTE, "Symfony Route", true)));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package fr.adrienbrault.idea.symfony2plugin.navigation;

import com.intellij.navigation.ChooseByNameContributorEx;
import com.intellij.navigation.ChooseByNameContributorEx2;
import com.intellij.navigation.NavigationItem;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.Processor;
import com.intellij.util.indexing.FindSymbolParameters;
import com.intellij.util.indexing.IdFilter;
import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.routing.Route;
import fr.adrienbrault.idea.symfony2plugin.routing.RouteHelper;
import kotlin.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* @author Daniel Espendiller <[email protected]>
*/
public class RouteUrlMatcherSymbolContributor implements ChooseByNameContributorEx, ChooseByNameContributorEx2 {

@Override
public void processNames(@NotNull Processor<? super String> processor, @NotNull FindSymbolParameters parameters) {
Project project = parameters.getProject();
if (!Symfony2ProjectComponent.isEnabled(project)) {
return;
}

String name = parameters.getLocalPatternName();
if (RouteHelper.hasRoutesForPathWithPlaceholderMatch(project, name)) {
processor.process(name);
}
}

@Override
public void processNames(@NotNull Processor<? super String> processor, @NotNull GlobalSearchScope scope, @Nullable IdFilter filter) {
}

@Override
public void processElementsWithName(@NotNull String name, @NotNull Processor<? super NavigationItem> processor, @NotNull FindSymbolParameters parameters) {
Project project = parameters.getProject();
if (!Symfony2ProjectComponent.isEnabled(project)) {
return;
}

for (Pair<Route, PsiElement> entry : RouteHelper.getMethodsForPathWithPlaceholderMatchRoutes(project, name)) {
Route route = entry.getFirst();

String path = route.getPath();
if (path == null) {
continue;
}

processor.process((NavigationItemPresentableOverwrite.create(
entry.getSecond(),
path,
Symfony2Icons.ROUTE,
"Symfony Route",
true,
name
)));
}
}
}
Loading

0 comments on commit 928a20d

Please sign in to comment.