Skip to content
This repository has been archived by the owner on Aug 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #23 from rjuang/bugfixes
Browse files Browse the repository at this point in the history
Updates behavior of plugin in several ways:
  • Loading branch information
Alec Strong authored Nov 22, 2016
2 parents 5e12eb2 + 4a8ff74 commit 8409edd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
25 changes: 11 additions & 14 deletions src/com/squareup/ideaplugin/dagger/Decider.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.intellij.usages.Usage;
import com.intellij.usages.UsageInfo2UsageAdapter;
import com.intellij.usages.UsageTarget;

import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -44,9 +43,9 @@ public ProvidesMethodDecider(PsiMethod psiMethod) {
}

PsiMethod method = PsiConsultantImpl.findMethod(element);
if (method != null && PsiConsultantImpl.hasAnnotation(method, CLASS_INJECT)) {
PsiParameter[] parameters = method.getParameterList().getParameters();
for (PsiParameter parameter : parameters) {
if (method != null && (PsiConsultantImpl.hasAnnotation(method, CLASS_INJECT)
|| PsiConsultantImpl.hasAnnotation(method, CLASS_PROVIDES))) {
for (PsiParameter parameter : method.getParameterList().getParameters()) {
PsiClass parameterClass = PsiConsultantImpl.checkForLazyOrProvider(parameter);
if (parameterClass.equals(returnType) && PsiConsultantImpl.hasQuailifierAnnotations(
parameter, qualifierAnnotations)
Expand All @@ -66,8 +65,7 @@ public ProvidesMethodDecider(PsiMethod psiMethod) {
*/
public class ConstructorParameterInjectDecider extends IsAProviderDecider {
public ConstructorParameterInjectDecider(PsiParameter psiParameter) {
super(PsiConsultantImpl.getQualifierAnnotations(psiParameter),
PsiConsultantImpl.getTypeParameters(psiParameter));
super(psiParameter);
}
}

Expand All @@ -77,28 +75,27 @@ public ConstructorParameterInjectDecider(PsiParameter psiParameter) {
*/
public class FieldInjectDecider extends IsAProviderDecider {
public FieldInjectDecider(PsiField psiField) {
super(PsiConsultantImpl.getQualifierAnnotations(psiField),
PsiConsultantImpl.getTypeParameters(psiField));
super(psiField);
}
}

class IsAProviderDecider implements Decider {
private final Set<String> qualifierAnnotations;
private final List<PsiType> typeParameters;

public IsAProviderDecider(Set<String> qualifierAnnotations, List<PsiType> typeParameters) {
this.qualifierAnnotations = qualifierAnnotations;
this.typeParameters = typeParameters;
public IsAProviderDecider(PsiElement element) {
this.qualifierAnnotations = PsiConsultantImpl.getQualifierAnnotations(element);
this.typeParameters = PsiConsultantImpl.getTypeParameters(element);
}

@Override public boolean shouldShow(UsageTarget target, Usage usage) {
PsiElement element = ((UsageInfo2UsageAdapter) usage).getElement();

// Is it a constructor annotated w/ @Inject?
// I don't even know how to get to the constructor!
PsiMethod psimethod = PsiConsultantImpl.findMethod(element);

// For constructors annotated w/ @Inject, this is searched first before committing to the usage search.

// Is it a @Provides method?
PsiMethod psimethod = PsiConsultantImpl.findMethod(element);
return psimethod != null
// Ensure it has an @Provides.
&& PsiConsultantImpl.hasAnnotation(psimethod, CLASS_PROVIDES)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.intellij.codeHighlighting.Pass.UPDATE_ALL;
import static com.intellij.openapi.editor.markup.GutterIconRenderer.Alignment.LEFT;
import static com.squareup.ideaplugin.dagger.DaggerConstants.CLASS_INJECT;
import static com.squareup.ideaplugin.dagger.DaggerConstants.CLASS_PROVIDES;

public class InjectionLineMarkerProvider implements LineMarkerProvider {
private static final Icon ICON = IconLoader.getIcon("/icons/inject.png");
Expand All @@ -39,14 +40,25 @@ public LineMarkerInfo getLineMarkerInfo(@NotNull final PsiElement element) {
PsiMethod methodElement = (PsiMethod) element;

// Constructor injection.
if (methodElement.isConstructor() && PsiConsultantImpl.hasAnnotation(element, CLASS_INJECT)) {
if (methodElement.isConstructor() && PsiConsultantImpl.hasAnnotation(element, CLASS_INJECT) &&
methodElement.getParameterList().getParametersCount() > 0) {
PsiIdentifier nameIdentifier = methodElement.getNameIdentifier();
if (nameIdentifier != null) {
return new LineMarkerInfo<PsiElement>(element, nameIdentifier.getTextRange(), ICON,
UPDATE_ALL, null, new ConstructorInjectToProvidesHandler(), LEFT);
}
}

// Method annotated with @Provides and has at least one argument
if (!methodElement.isConstructor() && PsiConsultantImpl.hasAnnotation(element, CLASS_PROVIDES) &&
methodElement.getParameterList().getParametersCount() > 0) {
PsiIdentifier nameIdentifier = methodElement.getNameIdentifier();
if (nameIdentifier != null) {
return new LineMarkerInfo<PsiElement>(element, nameIdentifier.getTextRange(), ICON,
UPDATE_ALL, null, new ConstructorInjectToProvidesHandler(), LEFT);
}
}

// Not a method, is it a Field?
} else if (element instanceof PsiField) {
// Field injection.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package com.squareup.ideaplugin.dagger.handler;

import com.intellij.codeInsight.daemon.GutterIconNavigationHandler;
import com.intellij.pom.Navigatable;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiParameter;
import com.intellij.psi.PsiTypeElement;
import com.intellij.psi.util.PsiUtilBase;
import com.intellij.ui.awt.RelativePoint;
import com.squareup.ideaplugin.dagger.Decider;
import com.squareup.ideaplugin.dagger.PickTypeAction;
import com.squareup.ideaplugin.dagger.PsiConsultantImpl;
import com.squareup.ideaplugin.dagger.ShowUsagesAction;

import java.awt.event.MouseEvent;

import static com.squareup.ideaplugin.dagger.DaggerConstants.CLASS_INJECT;
import static com.squareup.ideaplugin.dagger.DaggerConstants.MAX_USAGES;

/**
Expand All @@ -27,10 +33,6 @@ public class ConstructorInjectToProvidesHandler implements GutterIconNavigationH
}

PsiMethod psiMethod = (PsiMethod) psiElement;
if (!psiMethod.isConstructor()) {
throw new IllegalStateException("Called with non-constructor: " + psiElement);
}

PsiParameter[] parameters = psiMethod.getParameterList().getParameters();
if (parameters.length == 1) {
showUsages(mouseEvent, parameters[0]);
Expand All @@ -45,8 +47,40 @@ public class ConstructorInjectToProvidesHandler implements GutterIconNavigationH
}

private void showUsages(MouseEvent mouseEvent, PsiParameter psiParameter) {
// Check to see if class type of psiParameter has constructor with @Inject. Otherwise, proceed.
if (navigateToConstructorIfProvider(psiParameter)) {
return;
}
new ShowUsagesAction(new Decider.ConstructorParameterInjectDecider(psiParameter)).startFindUsages(
PsiConsultantImpl.checkForLazyOrProvider(psiParameter), new RelativePoint(mouseEvent),
PsiUtilBase.findEditor(psiParameter), MAX_USAGES);
}

private boolean navigateToConstructorIfProvider(PsiParameter psiParameter) {
PsiTypeElement declaringTypeElement = psiParameter.getTypeElement();
PsiClass classElement = JavaPsiFacade.getInstance(psiParameter.getProject()).findClass(
declaringTypeElement.getType().getCanonicalText(),
declaringTypeElement.getResolveScope());

if (classElement == null) {
return false;
}

for (PsiMethod method : classElement.getConstructors()) {
if (PsiConsultantImpl.hasAnnotation(method, CLASS_INJECT) && navigateToElement(method)) {
return true;
}
}
return false;
}

private boolean navigateToElement(PsiElement element) {
PsiElement navigationElement = element.getNavigationElement();
if (navigationElement != null && navigationElement instanceof Navigatable &&
((Navigatable) navigationElement).canNavigate()) {
((Navigatable) navigationElement).navigate(true);
return true;
}
return false;
}
}

0 comments on commit 8409edd

Please sign in to comment.