Skip to content

Commit

Permalink
Components: support for a dash as a subcomponent separator
Browse files Browse the repository at this point in the history
  • Loading branch information
matej21 committed Feb 13, 2016
1 parent 7031cc7 commit ea65014
Show file tree
Hide file tree
Showing 10 changed files with 335 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import cz.juzna.intellij.nette.utils.ComponentSearcher;
import cz.juzna.intellij.nette.utils.ComponentUtil;
import cz.juzna.intellij.nette.utils.PhpIndexUtil;
import cz.juzna.intellij.nette.utils.StringUtil;
import cz.juzna.intellij.nette.utils.PsiUtil;
import org.jetbrains.annotations.NotNull;

public class ComponentCompletionContributor extends CompletionContributor {
Expand All @@ -24,13 +25,24 @@ private class ComponentNameCompletionProvider extends CompletionProvider<Complet

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
PsiElement position = parameters.getPosition().getOriginalElement().getParent();
if (position.getParent() == null || position.getParent().getParent() == null) {
PsiElement position = parameters.getOriginalPosition();
position = PsiUtil.getParentAtLevel(position, 3);
if (position == null) {
return;
}
for (Method method : ComponentUtil.getFactoryMethods(position.getParent().getParent())) {
String componentName = StringUtil.lowerFirst(method.getName().substring("createComponent".length()));
LookupElementBuilder lookupElement = LookupElementBuilder.create(componentName);
ComponentSearcher.ComponentQuery query = ComponentSearcher.createQuery(position);
query.match(ComponentSearcher.Match.PREFIX);
String prefix = result.getPrefixMatcher().getPrefix();
if (prefix.contains("-")) {
prefix = prefix.substring(0, prefix.lastIndexOf("-") + 1);
} else {
prefix = "";
}
for (Method method : ComponentSearcher.findMethods(query)) {
String componentName = ComponentUtil.methodToComponentName(method.getName());

LookupElementBuilder lookupElement = LookupElementBuilder.create(prefix + componentName)
.withPresentableText(componentName);
PhpType returnType = new PhpType();
for (PhpClass typeCls : PhpIndexUtil.getClasses(method, method.getProject())) {
returnType.add(typeCls.getType());
Expand Down
7 changes: 5 additions & 2 deletions src/cz/juzna/intellij/nette/dialogs/ComponentTreePopup.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.intellij.util.ui.tree.TreeUtil;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import cz.juzna.intellij.nette.utils.ComponentSearcher;
import cz.juzna.intellij.nette.utils.ComponentUtil;
import cz.juzna.intellij.nette.utils.PhpIndexUtil;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -114,14 +115,16 @@ public ComponentTreeElement(PsiElement element, boolean expandClass) {
public Collection<StructureViewTreeElement> getChildrenBase() {
Collection<StructureViewTreeElement> children = new ArrayList<StructureViewTreeElement>();
if (expandClass && getElement() instanceof PhpClass) {
for (Method method : ComponentUtil.getFactoryMethods((PhpClass) getElement(), null)) {
ComponentSearcher.ComponentQuery query = new ComponentSearcher.ComponentQuery((PhpClass) getElement());
for (Method method : ComponentSearcher.findMethods(query)) {
children.add(new ComponentTreeElement(method));
}
} else if (getElement() instanceof Method) {
Method method = (Method) getElement();
for (PhpClass cls : PhpIndexUtil.getClasses(method, method.getProject())) {
children.add(new ComponentTreeElement(cls, false));
for (Method m : ComponentUtil.getFactoryMethods(cls, null)) {
ComponentSearcher.ComponentQuery query = new ComponentSearcher.ComponentQuery(cls);
for (Method m : ComponentSearcher.findMethods(query)) {
children.add(new ComponentTreeElement(m));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.jetbrains.php.lang.psi.elements.PhpReturn;
import com.jetbrains.php.lang.psi.elements.PhpTypedElement;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import cz.juzna.intellij.nette.utils.ComponentUtil;
import cz.juzna.intellij.nette.utils.ComponentSearcher;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -49,7 +49,10 @@ public ProblemDescriptor[] checkFile(PsiFile file, InspectionManager manager, bo
Collection<PhpClass> classes = PhpPsiUtil.findAllClasses((PhpFile) file);
Collection<Method> invalidReturnForms = new ArrayList<Method>();
for (PhpClass cls : classes) {
for (final Method method : ComponentUtil.getFactoryMethods(cls, null, true)) {

ComponentSearcher.ComponentQuery query = new ComponentSearcher.ComponentQuery(cls);
query.filterOwn();
for (final Method method : ComponentSearcher.findMethods(query)) {
InvalidCreateComponentMethodVisitor visitor = new InvalidCreateComponentMethodVisitor();
method.accept(visitor);
if (visitor.isInvalid()) {
Expand Down
39 changes: 30 additions & 9 deletions src/cz/juzna/intellij/nette/reference/ComponentReference.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cz.juzna.intellij.nette.reference;

import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReferenceBase;
import com.intellij.psi.ResolveResult;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import cz.juzna.intellij.nette.utils.ComponentSearcher;
import cz.juzna.intellij.nette.utils.ComponentUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -15,24 +16,33 @@

public class ComponentReference extends PsiReferenceBase.Poly<PsiElement> {

public ComponentReference(@NotNull PsiElement element) {
super(element);
private final String path;

public ComponentReference(@NotNull PsiElement element, String path) {
super(element, true);
this.path = path;
}

@NotNull
@Override
public ResolveResult[] multiResolve(boolean b) {
if (getElement().getParent().getParent() == null) {
PsiElement el = getElement().getParent().getParent();
if (el == null) {
return new ResolveResult[0];
}
Method[] factoryMethods = ComponentUtil.getFactoryMethods(getElement().getParent().getParent(), true);
Collection<ResolveResult> results = new ArrayList<ResolveResult>(factoryMethods.length);
for (final Method method : factoryMethods) {
ComponentSearcher.ComponentQuery query = ComponentSearcher.createQuery(el);
query.withPath();
Collection<ComponentSearcher.ComponentSearchResult> searchResults = ComponentSearcher.find(query);
Collection<ResolveResult> results = new ArrayList<ResolveResult>(searchResults.size());
for (final ComponentSearcher.ComponentSearchResult searchResult : searchResults) {
if (!searchResult.getPath().equals(path)) {
continue;
}
results.add(new ResolveResult() {
@Nullable
@Override
public PsiElement getElement() {
return method;
return searchResult.getMethod();
}

@Override
Expand All @@ -46,6 +56,11 @@ public boolean isValidResult() {
return results.toArray(result);
}

@Override
public TextRange getRangeInElement() {
return new TextRange(path.contains("-") ? path.lastIndexOf("-") + 2 : 1, path.length() + 1);
}

@NotNull
@Override
public Object[] getVariants() {
Expand All @@ -56,7 +71,13 @@ public Object[] getVariants() {
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
String componentName = ComponentUtil.methodToComponentName(newElementName);
if (getElement() instanceof StringLiteralExpression && componentName != null) {
((StringLiteralExpression) getElement()).updateText(componentName);
StringLiteralExpression stringLiteral = (StringLiteralExpression) getElement();
TextRange range = getRangeInElement();
String name = stringLiteral.getContents();
name = (range.getStartOffset() > 1 ? name.substring(0, range.getStartOffset() - 1) : "")
+ componentName
+ name.substring(range.getEndOffset() - 1);
stringLiteral.updateText(name);
return getElement();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.intellij.psi.*;
import com.intellij.util.ProcessingContext;
import com.jetbrains.php.lang.parser.PhpElementTypes;
import com.jetbrains.php.lang.psi.elements.Method;
import cz.juzna.intellij.nette.utils.ComponentUtil;
import cz.juzna.intellij.nette.utils.ComponentSearcher;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;

public class ComponentReferenceContributor extends PsiReferenceContributor {

@Override
Expand All @@ -19,11 +21,19 @@ public PsiReference[] getReferencesByElement(PsiElement psiElement, ProcessingCo
if (psiElement.getParent() == null || psiElement.getParent().getParent() == null) {
return new PsiReference[0];
}
Method[] factoryMethods = ComponentUtil.getFactoryMethods(psiElement.getParent().getParent(), true);
if (factoryMethods.length == 0) {
PsiElement el = psiElement.getParent().getParent();
ComponentSearcher.ComponentQuery query = ComponentSearcher.createQuery(el);
query.withPath();
Collection<ComponentSearcher.ComponentSearchResult> result = ComponentSearcher.find(query);
if (result.size() == 0) {
return new PsiReference[0];
}
return new PsiReference[]{new ComponentReference(psiElement)};
Collection<PsiReference> refs = new ArrayList<PsiReference>(result.size());
for (ComponentSearcher.ComponentSearchResult searchResult : result) {
refs.add(new ComponentReference(psiElement, searchResult.getPath()));
}

return refs.toArray(new PsiReference[refs.size()]);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
import com.intellij.util.Processor;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import cz.juzna.intellij.nette.utils.ComponentSearcher;
import cz.juzna.intellij.nette.utils.ComponentUtil;

import java.util.Arrays;


public class ComponentReferenceSearch extends QueryExecutorBase<PsiReference, ReferencesSearch.SearchParameters> {

Expand All @@ -36,9 +35,13 @@ public boolean execute(PsiElement psiElement, int i) {
return true;
}
PsiElement el = psiElement.getParent().getParent();
Method[] methods = ComponentUtil.getFactoryMethods(el);
if (Arrays.asList(methods).contains(method)) {
processor.process(new ComponentReference(psiElement));
ComponentSearcher.ComponentQuery query = ComponentSearcher.createQuery(el);
query.withPath();
for (ComponentSearcher.ComponentSearchResult result : ComponentSearcher.find(query)) {
if (result.getMethod() == method) {
processor.process(new ComponentReference(psiElement, result.getPath()));
}

}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
import com.jetbrains.php.lang.psi.elements.PhpTypedElement;
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider2;
import cz.juzna.intellij.nette.utils.ComponentUtil;
import cz.juzna.intellij.nette.utils.ComponentSearcher;
import cz.juzna.intellij.nette.utils.ElementValueResolver;
import cz.juzna.intellij.nette.utils.PhpIndexUtil;
import cz.juzna.intellij.nette.utils.PhpPsiUtil;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

Expand Down Expand Up @@ -77,14 +76,8 @@ public Collection<? extends PhpNamedElement> getBySignature(String s, Project pr
}
String componentName = parts[0];
Collection<PhpClass> classes = PhpIndexUtil.getByType(parts[1].split(TYPE_SEPARATOR), PhpIndex.getInstance(project));
Collection<PhpNamedElement> result = new ArrayList<PhpNamedElement>();
for (PhpClass cls : classes) {
if (!ComponentUtil.isContainer(cls)) {
continue;
}
result.addAll(ComponentUtil.getFactoryMethodsByName(cls, componentName, false));
}
return result;
return ComponentSearcher.findMethods(new ComponentSearcher.ComponentQuery(componentName, classes));

}

}
Expand Down
Loading

0 comments on commit ea65014

Please sign in to comment.