Skip to content

Commit

Permalink
add testing for doctrine types in yaml and provide navigation #555
Browse files Browse the repository at this point in the history
  • Loading branch information
Haehnchen committed Aug 9, 2015
1 parent 84386fd commit df846be
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
import fr.adrienbrault.idea.symfony2plugin.util.completion.annotations.AnnotationMethodInsertHandler;
import fr.adrienbrault.idea.symfony2plugin.util.completion.annotations.AnnotationTagInsertHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

Expand All @@ -22,22 +24,62 @@ public class DoctrineStaticTypeLookupBuilder {

public static Collection<LookupElement> getTypes(@NotNull Project project) {

Map<String, LookupElement> lookupElements = new HashMap<String, LookupElement>();
final Collection<LookupElement> lookupElements = new ArrayList<LookupElement>();

visitCustomTypes(project, new ColumnTypeVisitor() {
@Override
public void visit(@NotNull String name, @Nullable PhpClass phpClass, @Nullable PsiElement psiElement) {
LookupElementBuilder lookupElementBuilder = LookupElementBuilder.create(name).withIcon(Symfony2Icons.DOCTRINE);

if(phpClass != null) {
lookupElementBuilder = lookupElementBuilder.withTypeText(phpClass.getName(), true);
}

lookupElements.add(lookupElementBuilder);
}
});

return lookupElements;
}

public static void visitCustomTypes(@NotNull Project project, @NotNull ColumnTypeVisitor visitor) {

Set<String> found = new HashSet<String>();

for (PhpClass phpClass : PhpIndex.getInstance(project).getAllSubclasses("\\Doctrine\\DBAL\\Types\\Type")) {
String getName = PhpElementsUtil.getMethodReturnAsString(phpClass, "getName");
if(getName != null) {
lookupElements.put(getName, LookupElementBuilder.create(getName).withIcon(Symfony2Icons.DOCTRINE).withTypeText(phpClass.getName(), true));
String name = PhpElementsUtil.getMethodReturnAsString(phpClass, "getName");
if(name != null) {
found.add(name);
visitor.visit(name, phpClass, phpClass.findMethodByName("getName"));
}
}

for (String s : Arrays.asList("id", "string", "integer", "smallint", "bigint", "boolean", "decimal", "date", "time", "datetime", "text", "array", "float")) {
if(!lookupElements.containsKey(s)) {
lookupElements.put(s, LookupElementBuilder.create(s).withIcon(Symfony2Icons.DOCTRINE));
if(!found.contains(s)) {
visitor.visit(s, null, null);
}
}

return lookupElements.values();
}

private interface ColumnTypeVisitor {
void visit(@NotNull String name, @Nullable PhpClass phpClass, @Nullable PsiElement psiElement);
}

public static Collection<PsiElement> getColumnTypesTargets(@NotNull Project project, final @NotNull String contents) {

final Collection<PsiElement> targets = new ArrayList<PsiElement>();

visitCustomTypes(project, new ColumnTypeVisitor() {
@Override
public void visit(@NotNull String name, @Nullable PhpClass phpClass, @Nullable PsiElement psiElement) {
if(name.equals(contents) && phpClass != null) {
targets.add(phpClass);
}
}
});

return targets;
}

public ArrayList<LookupElement> getNullAble() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,26 @@
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.completion.PhpLookupElement;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.resolve.PhpResolveResult;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.config.EventDispatcherSubscriberUtil;
import fr.adrienbrault.idea.symfony2plugin.dic.XmlTagParser;
import fr.adrienbrault.idea.symfony2plugin.config.doctrine.DoctrineStaticTypeLookupBuilder;
import fr.adrienbrault.idea.symfony2plugin.routing.RouteHelper;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
import fr.adrienbrault.idea.symfony2plugin.util.SymfonyBundleUtil;
import fr.adrienbrault.idea.symfony2plugin.util.controller.ControllerIndex;
import fr.adrienbrault.idea.symfony2plugin.util.dict.ServiceUtil;
import fr.adrienbrault.idea.symfony2plugin.util.dict.SymfonyBundle;
import fr.adrienbrault.idea.symfony2plugin.util.service.ServiceXmlParserFactory;
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.yaml.psi.YAMLCompoundValue;
import org.jetbrains.yaml.psi.YAMLKeyValue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -94,9 +90,23 @@ public PsiElement[] getGotoDeclarationTargets(PsiElement psiElement, int i, Edit
this.getArrayMethodGoto(psiElement, results);
}

if(YamlElementPatternHelper.getOrmSingleLineScalarKey("type").accepts(psiElement)) {
this.getOrmTypesNavigation(psiElement, results);
}

return results.toArray(new PsiElement[results.size()]);
}

private void getOrmTypesNavigation(@NotNull PsiElement psiElement, @NotNull List<PsiElement> results) {

String text = PsiElementUtils.trimQuote(psiElement.getText());
if(StringUtils.isBlank(text)) {
return;
}

results.addAll(DoctrineStaticTypeLookupBuilder.getColumnTypesTargets(psiElement.getProject(), text));
}

private void getArrayMethodGoto(PsiElement psiElement, List<PsiElement> results) {

String text = PsiElementUtils.trimQuote(psiElement.getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,18 @@ public void assertNavigationMatchWithParent(LanguageFileType languageFileType, S
assertNavigationMatch(languageFileType, configureByText, PlatformPatterns.psiElement().withParent(PlatformPatterns.psiElement(iElementType)));
}

public void assertNavigationMatch(LanguageFileType languageFileType, String configureByText, ElementPattern<?> pattern) {
public void assertNavigationMatch(String filename, String configureByText, ElementPattern<?> pattern) {
myFixture.configureByText(filename, configureByText);
assertNavigationMatch(pattern);
}

public void assertNavigationMatch(LanguageFileType languageFileType, String configureByText, ElementPattern<?> pattern) {
myFixture.configureByText(languageFileType, configureByText);
assertNavigationMatch(pattern);
}

private void assertNavigationMatch(ElementPattern<?> pattern) {

PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());

Set<String> targetStrings = new HashSet<String>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package fr.adrienbrault.idea.symfony2plugin.tests.config.yaml.doctrine;

import com.intellij.patterns.PlatformPatterns;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;

import java.io.File;

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

public void setUp() throws Exception {
super.setUp();
myFixture.configureFromExistingVirtualFile(myFixture.copyFileToProject("DoctrineTypes.php"));
}

public String getTestDataPath() {
return new File(this.getClass().getResource("fixtures").getFile()).getAbsolutePath();
}

/**
* @see fr.adrienbrault.idea.symfony2plugin.config.yaml.YamlCompletionContributor
*/
public void testDoctrineOrmFieldCompletion() {

assertCompletionContains("foo.orm.yml", "foo:\n" +
" fields:\n" +
" field_1:\n" +
" type: <caret>",
"BAR", "foo_const", "id"
);

assertCompletionContains("foo.orm.yml", "foo:\n" +
" id:\n" +
" field_1:\n" +
" type: <caret>",
"BAR", "foo_const", "id"
);
}

/**
* @see fr.adrienbrault.idea.symfony2plugin.config.yaml.YamlGoToKnownDeclarationHandler
*/
public void testDoctrineOrmFieldNavigation() {

assertNavigationMatch("foo.orm.yml", "foo:\n" +
" fields:\n" +
" field_1:\n" +
" type: BA<caret>R",
PlatformPatterns.psiElement(PhpClass.class)
);

assertNavigationMatch("foo.orm.yml", "foo:\n" +
" fields:\n" +
" field_1:\n" +
" type: foo_c<caret>onst",
PlatformPatterns.psiElement(PhpClass.class)
);

assertNavigationMatch("foo.orm.yml", "foo:\n" +
" id:\n" +
" field_1:\n" +
" type: BA<caret>R",
PlatformPatterns.psiElement(PhpClass.class)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Doctrine\DBAL\Types {
interface Type {
const FOO = "foo_const";
public function getName();
}
}

namespace {

use Doctrine\DBAL\Types\Type;

class Foo implements Type {
public function getName() {
return Doctrine\DBAL\Types\Type::FOO;
}
}

class Bar implements Type {
public function getName() {
return "BAR";
}
}
}

0 comments on commit df846be

Please sign in to comment.