Skip to content

Commit

Permalink
Merge pull request #2269 from Haehnchen/feature/route-path-namespace
Browse files Browse the repository at this point in the history
support routing resource registering via path
  • Loading branch information
Haehnchen authored Dec 15, 2023
2 parents 7e953d1 + 6bd13e2 commit 16767bb
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.jetbrains.annotations.Nullable;
import org.jetbrains.yaml.YAMLTokenTypes;
import org.jetbrains.yaml.psi.*;
import org.jetbrains.yaml.psi.impl.YAMLPlainTextImpl;

import java.util.Collection;
import java.util.List;
Expand All @@ -30,7 +31,7 @@ public class YamlLineMarkerProvider implements LineMarkerProvider {

@Override
public void collectSlowLineMarkers(@NotNull List<? extends PsiElement> psiElements, @NotNull Collection<? super LineMarkerInfo<?>> lineMarkerInfos) {
if(psiElements.size() == 0 || !Symfony2ProjectComponent.isEnabled(psiElements.get(0))) {
if(psiElements.isEmpty() || !Symfony2ProjectComponent.isEnabled(psiElements.get(0))) {
return;
}

Expand All @@ -53,6 +54,11 @@ public void collectSlowLineMarkers(@NotNull List<? extends PsiElement> psiElemen
* controllers:
* resource: '../../src/Controller/'
* type: annotation
*
* controllers:
* resource:
* path: ../src/Controller/
* namespace: App\Controller
*/
private void attachRoutingForResources(@NotNull Collection<? super LineMarkerInfo<?>> result, @NotNull PsiElement leafTarget) {
if (leafTarget.getNode().getElementType() != YAMLTokenTypes.SCALAR_KEY) {
Expand All @@ -64,8 +70,8 @@ private void attachRoutingForResources(@NotNull Collection<? super LineMarkerInf
return;
}

String resource = YamlHelper.getYamlKeyValueAsString((YAMLKeyValue) yamlKeyValue, "resource");
if (resource == null) {
String resourcePath = FileResourceUtil.getResourcePath((YAMLKeyValue) yamlKeyValue);
if (resourcePath == null) {
return;
}

Expand All @@ -78,7 +84,7 @@ private void attachRoutingForResources(@NotNull Collection<? super LineMarkerInf
FileResourceUtil.getNavigationGutterForRouteAnnotationResources(
leafTarget.getProject(),
leafTarget.getContainingFile().getVirtualFile(),
resource
resourcePath
).createLineMarkerInfo(leafTarget)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.intellij.util.Consumer;
import fr.adrienbrault.idea.symfony2plugin.stubs.dict.FileResource;
import fr.adrienbrault.idea.symfony2plugin.stubs.dict.FileResourceContextTypeEnum;
import fr.adrienbrault.idea.symfony2plugin.util.resource.FileResourceUtil;
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -68,13 +69,16 @@ private static void visitYamlFile(@NotNull YAMLFile yamlFile, @NotNull Consumer<
private static void visitKeyValue(@NotNull YAMLKeyValue yamlKeyValue, @NotNull Consumer<FileResourceConsumer> consumer) {
// app1:
// resource: "@AcmeOtherBundle/Resources/config/routing1.yml"
YAMLKeyValue resourceKey = YamlHelper.getYamlKeyValue(yamlKeyValue, "resource", true);
if(resourceKey == null) {
// resource:
// path: foo

YAMLKeyValue resourceYamlKeyValue = YamlHelper.getYamlKeyValue(yamlKeyValue, "resource");
if (resourceYamlKeyValue == null) {
return;
}

String resource = PsiElementUtils.trimQuote(resourceKey.getValueText());
if(StringUtils.isBlank(resource)) {
String resource = FileResourceUtil.getResourcePath(yamlKeyValue);
if (resource == null) {
return;
}

Expand All @@ -91,13 +95,14 @@ private static void visitKeyValue(@NotNull YAMLKeyValue yamlKeyValue, @NotNull C
boolean isRouteContext = map.containsKey("type")
|| map.containsKey("prefix")
|| map.containsKey("name_prefix")
|| YamlHelper.getYamlKeyValue(yamlKeyValue, "requirements", true) != null;
|| YamlHelper.getYamlKeyValue(yamlKeyValue, "requirements", true) != null
|| YamlHelper.getYamlKeyValue(resourceYamlKeyValue, "namespace", true) != null;

if (isRouteContext) {
fileResourceContextType = FileResourceContextTypeEnum.ROUTE;
}

consumer.consume(new FileResourceConsumer(resourceKey, normalize(resource), fileResourceContextType, map));
consumer.consume(new FileResourceConsumer(resourceYamlKeyValue, normalize(resource), fileResourceContextType, map));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
import fr.adrienbrault.idea.symfony2plugin.util.SymfonyBundleUtil;
import fr.adrienbrault.idea.symfony2plugin.util.dict.SymfonyBundle;
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.yaml.psi.YAMLKeyValue;
import org.jetbrains.yaml.psi.YAMLMapping;
import org.jetbrains.yaml.psi.YAMLScalar;
import org.jetbrains.yaml.psi.YAMLValue;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -396,6 +401,37 @@ public static Pair<String, String> getGlobalPatternDirectory(@NotNull String res
return new Pair<>(join, null);
}

/**
* controllers:
* resource: '../../src/Controller/'
*
* controllers:
* resource:
* path: ../src/Controller/
*/
@Nullable
public static String getResourcePath(@NotNull YAMLKeyValue yamlKeyValue) {
YAMLKeyValue resourceYamlKeyValue = YamlHelper.getYamlKeyValue(yamlKeyValue, "resource");
if (resourceYamlKeyValue == null) {
return null;
}

YAMLValue value = resourceYamlKeyValue.getValue();

String resource = null;
if (value instanceof YAMLScalar) {
resource = ((YAMLScalar) value).getTextValue();
} else if (value instanceof YAMLMapping) {
resource = YamlHelper.getYamlKeyValueAsString((YAMLMapping) value, "path");
}

if (resource == null || resource.isBlank()) {
return null;
}

return resource;
}

public static @NotNull NavigationGutterIconBuilder<PsiElement> getNavigationGutterForRouteAnnotationResources(@NotNull Project project, @NotNull VirtualFile virtualFile, @NotNull String resource) {
return NavigationGutterIconBuilder.create(AllIcons.Modules.SourceRoot)
.setTargets(NotNullLazyValue.lazy(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ public void setUp() throws Exception {
"when@dev:\n" +
" web_profiler_wdt_2:\n" +
" resource: '@WebProfilerBundle/Resources/config/routing/wdt_2.xml'\n" +
" prefix: /_wdt\n"
" prefix: /_wdt\n" +
"controllers_array:\n" +
" resource:\n" +
" path: ../src/Controller/\n" +
" namespace: foo\n" +
"controllers_array_2:\n" +
" resource:\n" +
" path: '../src/Controller2/'\n" +
" namespace: foo\n"
);

myFixture.configureByText("test1.xml", "" +
Expand Down Expand Up @@ -77,6 +85,9 @@ public void testYamlResourcesImport() {

assertIndexContains(FileResourcesIndex.KEY, "@WebProfilerBundle/Resources/config/routing/wdt_1.xml");
assertIndexContains(FileResourcesIndex.KEY, "@WebProfilerBundle/Resources/config/routing/wdt_2.xml");

assertIndexContains(FileResourcesIndex.KEY, "../src/Controller");
assertIndexContains(FileResourcesIndex.KEY, "../src/Controller2");
}

public void testXmlResourcesImport() {
Expand All @@ -100,5 +111,8 @@ public void testIndexValue() {
assertEquals("/foo", item.getPrefix());
assertEquals(FileResourceContextTypeEnum.ROUTE, item.getContextType());
assertEquals(treeMap, item.getContextValues());

item = ContainerUtil.getFirstItem(FileBasedIndex.getInstance().getValues(FileResourcesIndex.KEY, "../src/Controller", GlobalSearchScope.allScope(getProject())));
assertEquals(FileResourceContextTypeEnum.ROUTE, item.getContextType());
}
}

0 comments on commit 16767bb

Please sign in to comment.