Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<php.typeProvider implementation="fr.adrienbrault.idea.symfony2plugin.dic.SymfonyContainerTypeProvider"/>
<psi.referenceContributor implementation="fr.adrienbrault.idea.symfony2plugin.dic.ServiceReferenceContributor"/>
<psi.referenceContributor implementation="fr.adrienbrault.idea.symfony2plugin.templating.PhpTemplateReferenceContributor"/>
<psi.referenceContributor implementation="fr.adrienbrault.idea.symfony2plugin.doctrine.DoctrineEntityReferenceContributor"/>
<!--<psi.referenceContributor implementation="fr.adrienbrault.idea.symfony2plugin.templating.TwigTemplateReferenceContributor"/>-->
<completion.contributor language="Twig" implementationClass="fr.adrienbrault.idea.symfony2plugin.templating.TwigCompletionContributor"/>
<projectConfigurable instance="fr.adrienbrault.idea.symfony2plugin.SettingsForm"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public static boolean isTemplatingRenderCall(PsiElement e) {
});
}

public static boolean isRepositoryCall(PsiElement e) {
// EntityManager is needed for symfony 2.0?
return isCallTo(e, new String[] {
"\\Doctrine\\ORM\\EntityManager.getRepository",
"\\Doctrine\\Common\\Persistence\\ObjectManager.getRepository"
});
}

private static boolean isCallTo(PsiElement e, String expectedMethodFQN) {
return isCallTo(e, new String[] { expectedMethodFQN }, 1);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package fr.adrienbrault.idea.symfony2plugin.doctrine;

import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.*;
import com.intellij.util.ProcessingContext;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.ParameterList;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import fr.adrienbrault.idea.symfony2plugin.SymfonyInterfacesHelper;
import org.jetbrains.annotations.NotNull;

/**
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class DoctrineEntityReferenceContributor extends PsiReferenceContributor {

@Override
public void registerReferenceProviders(PsiReferenceRegistrar psiReferenceRegistrar) {
psiReferenceRegistrar.registerReferenceProvider(
PlatformPatterns.psiElement(StringLiteralExpression.class),
new PsiReferenceProvider() {
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext processingContext) {
if (!(psiElement.getContext() instanceof ParameterList)) {
return new PsiReference[0];
}
ParameterList parameterList = (ParameterList) psiElement.getContext();

if (!(parameterList.getContext() instanceof MethodReference)) {
return new PsiReference[0];
}
MethodReference method = (MethodReference) parameterList.getContext();

if (!SymfonyInterfacesHelper.isRepositoryCall(method)) {
return new PsiReference[0];
}

return new PsiReference[]{ new EntityReference((StringLiteralExpression) psiElement) };
}
}
);
}

}
65 changes: 65 additions & 0 deletions src/fr/adrienbrault/idea/symfony2plugin/doctrine/EntityHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package fr.adrienbrault.idea.symfony2plugin.doctrine;

import com.intellij.openapi.project.Project;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.psi.elements.PhpClass;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class EntityHelper {

/**
*
* @param project PHPStorm projects
* @param shortcutName name as MyBundle\Entity\Model or MyBundle:Model
* @return null|PhpClass
*/
public static PhpClass resolveShortcutName(Project project, String shortcutName) {

PhpIndex phpIndex = PhpIndex.getInstance(project);
Collection<PhpClass> phpClasses = phpIndex.getAllSubclasses("\\Symfony\\Component\\HttpKernel\\Bundle\\Bundle");

Map<String, String> bundlesDirectories = new HashMap<String, String>();
for (PhpClass phpClass : phpClasses) {
bundlesDirectories.put(phpClass.getName(), phpClass.getNamespaceName());
}

String entity_name = null;

// resolve:
// MyBundle:Model -> MyBundle\Entity\Model
// MyBundle:Folder\Model -> MyBundle\Entity\Folder\Model
if (shortcutName.contains(":")) {

int firstDirectorySeparatorIndex = shortcutName.indexOf(":");

String bundlename = shortcutName.substring(0, firstDirectorySeparatorIndex);
String entityName = shortcutName.substring(firstDirectorySeparatorIndex + 1);

String namespace = bundlesDirectories.get(bundlename);

if(namespace == null) {
return null;
}

entity_name = namespace + "Entity\\" + entityName;

} else {
entity_name = shortcutName;
}

// dont we have any unique class getting method here?
Collection<PhpClass> entity_classes = phpIndex.getClassesByFQN(entity_name);
if(!entity_classes.isEmpty()){
return entity_classes.iterator().next();
}

return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package fr.adrienbrault.idea.symfony2plugin.doctrine;

import com.intellij.psi.*;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.psi.PsiElement;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.PhpNamespace;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import fr.adrienbrault.idea.symfony2plugin.doctrine.dict.DoctrineEntityLookupElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

/**
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class EntityReference extends PsiReferenceBase<PsiElement> implements PsiReference {
private String entityName;

public EntityReference(@NotNull StringLiteralExpression element) {
super(element);

entityName = element.getText().substring(
element.getValueRange().getStartOffset(),
element.getValueRange().getEndOffset()
);
}

@Nullable
@Override
public PsiElement resolve() {

PhpClass entity = EntityHelper.resolveShortcutName(getElement().getProject(), this.entityName);
if(entity != null) {
return new PsiElementResolveResult(entity).getElement();
}

return null;
}

@NotNull
@Override
public Object[] getVariants() {

PhpIndex phpIndex = PhpIndex.getInstance(getElement().getProject());
Collection<PhpClass> phpClasses = phpIndex.getAllSubclasses("\\Symfony\\Component\\HttpKernel\\Bundle\\Bundle");

List<LookupElement> results = new ArrayList<LookupElement>();
for (PhpClass phpClass : phpClasses) {

// search for classes that match the symfony2 namings
String ns = phpClass.getNamespaceName() + "Entity";
Collection<PhpNamespace> entities = phpIndex.getNamespacesByName(ns);

// @TODO: it looks like PhpIndex cant search for classes like \ns\Path\*\...
// temporary only use flat entities and dont support "MyBundle:Folder\Entity"
for (PhpNamespace entity_files : entities) {

// build our symfony2 shortcut
System.out.println(entity_files.getContainingFile().getContainingDirectory());
String filename = entity_files.getContainingFile().getName();
String className = filename.substring(0, filename.lastIndexOf('.'));
String repoName = phpClass.getName() + ':' + className;

for (PhpClass entity_phpclass : phpIndex.getClassesByFQN(ns + "\\" + className)) {
results.add(new DoctrineEntityLookupElement(repoName, entity_phpclass));
}

}

}

return results.toArray();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package fr.adrienbrault.idea.symfony2plugin.doctrine.dict;

import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementPresentation;
import com.jetbrains.php.PhpIcons;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import org.jetbrains.annotations.NotNull;

/**
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class DoctrineEntityLookupElement extends LookupElement {

private String entityName;
private PhpClass className;

public DoctrineEntityLookupElement(String entityName, PhpClass className) {
this.entityName = entityName;
this.className = className;
}

@NotNull
@Override
public String getLookupString() {
return entityName;
}

public void renderElement(LookupElementPresentation presentation) {
presentation.setItemText(getLookupString());
presentation.setTypeText(className.getPresentableFQN());
presentation.setTypeGrayed(true);
presentation.setIcon(PhpIcons.CLASS);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ public static Map<String, TwigFile> getTwigFilesByName(Project project) {
String templateDirectory = null; // xxx:XXX:xxx
String templateFile = null; // xxx:xxx:XXX
if (templatePath.contains("/")) {
int lastDirectorySeparatorIndex = templatePath.lastIndexOf("/");
templateDirectory = templatePath.substring(0, lastDirectorySeparatorIndex);
templateFile = templatePath.substring(lastDirectorySeparatorIndex + 1);

// remap twig folder shortcut:
// Folder/Subfolder/file.html.twig -> BundleName:Folder/Subfolder:file.html.twig

int firstDirectorySeparatorIndex = templatePath.indexOf("/");
templateDirectory = templatePath.substring(0, firstDirectorySeparatorIndex);
templateFile = templatePath.substring(firstDirectorySeparatorIndex + 1);
} else {
templateDirectory = "";
templateFile = templatePath;
Expand Down