Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add kotlin step impl support to Gauge plugin #873

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion gauge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ intellij {
localPath = ideaPath
}

plugins = ['java', 'properties', 'maven', 'gradle']
plugins = ['java', 'properties', 'maven', 'gradle', 'Kotlin']
}
patchPluginXml {
}
Expand Down
1 change: 1 addition & 0 deletions gauge/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<depends optional="true" config-file="with-gradle.xml">org.jetbrains.plugins.gradle</depends>
<depends optional="true" config-file="with-maven.xml">org.jetbrains.idea.maven</depends>
<depends optional="true" config-file="with-kotlin.xml">org.jetbrains.kotlin</depends>

<description><![CDATA[
Plugin for writing <a href="https://gauge.org">Gauge tests</a> (originally developed by Thoughtworks Inc.).
Expand Down
2 changes: 2 additions & 0 deletions gauge/resources/META-INF/with-kotlin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<idea-plugin>
</idea-plugin>
7 changes: 3 additions & 4 deletions gauge/src/com/thoughtworks/gauge/GaugeBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@

package com.thoughtworks.gauge;

import java.util.function.Supplier;

import com.intellij.DynamicBundle;
import com.intellij.openapi.util.NlsSafe;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.PropertyKey;

import java.util.function.Supplier;

public final class GaugeBundle extends DynamicBundle {
public static final @NlsSafe String GAUGE = "Gauge";

Expand All @@ -41,7 +40,7 @@ private GaugeBundle() {
}

@NotNull
public static Supplier<@Nls String> messagePointer(@NotNull @PropertyKey(resourceBundle = BUNDLE) String key,
public static Supplier<String> messagePointer(@NotNull @PropertyKey(resourceBundle = BUNDLE) String key,
Object @NotNull ... params) {
return INSTANCE.getLazyMessage(key, params);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.intellij.psi.PsiMethod;
import com.thoughtworks.gauge.language.psi.impl.ConceptStepImpl;
import com.thoughtworks.gauge.language.psi.impl.SpecStepImpl;
import com.thoughtworks.gauge.util.KtUtil;
import com.thoughtworks.gauge.util.StepUtil;
import org.jetbrains.annotations.NotNull;

Expand All @@ -30,6 +31,8 @@ public final class CustomFindUsagesHandlerFactory extends FindUsagesHandlerFacto
public boolean canFindUsages(@NotNull PsiElement psiElement) {
if (psiElement instanceof PsiMethod) {
return StepUtil.getGaugeStepAnnotationValues((PsiMethod)psiElement).size() > 0;
} else if (KtUtil.isKtFunction(psiElement)) {
return KtUtil.getGaugeStepAnnotationValues(psiElement).size() > 0;
}
return psiElement instanceof SpecStepImpl || psiElement instanceof ConceptStepImpl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.thoughtworks.gauge.helper.ModuleHelper;
import com.thoughtworks.gauge.util.GaugeUtil;
import com.thoughtworks.gauge.util.HookUtil;
import com.thoughtworks.gauge.util.KtUtil;
import com.thoughtworks.gauge.util.StepUtil;
import org.jetbrains.annotations.NotNull;

Expand All @@ -41,6 +42,7 @@ public ImplUsageProvider() {
public boolean isImplicitUsage(@NotNull PsiElement element) {
if (!moduleHelper.isGaugeModule(element)) return false;
if (element instanceof PsiClassImpl) return isClassUsed((PsiClassImpl)element);
if (KtUtil.isKtClass(element)) return KtUtil.isClassUsed(element);
if (element instanceof PsiParameterImpl) return isParameterUsed((PsiParameterImpl)element);
return isElementUsed(element);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.thoughtworks.gauge.findUsages.helper;

import java.util.ArrayList;
import java.util.List;

import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.impl.source.PsiMethodImpl;
Expand All @@ -27,12 +30,10 @@
import com.thoughtworks.gauge.language.psi.impl.ConceptStepImpl;
import com.thoughtworks.gauge.language.psi.impl.SpecStepImpl;
import com.thoughtworks.gauge.util.GaugeUtil;
import com.thoughtworks.gauge.util.KtUtil;
import com.thoughtworks.gauge.util.StepUtil;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class ReferenceSearchHelper {

public static final String UNKNOWN_SCOPE = "<unknown scope>";
Expand Down Expand Up @@ -68,6 +69,11 @@ else if (element instanceof PsiMethodImpl) {
elements.addAll(collector.get(getStepText(alias, element)));
}
}
else if (KtUtil.isKtFunction(element) || KtUtil.isKtMethod(element)) {
for (String alias : KtUtil.getGaugeStepAnnotationValues(element)) {
elements.addAll(collector.get(getStepText(alias, element)));
}
}
else if (element instanceof SpecStepImpl) {
elements = collector.get(getStepText((SpecStepImpl)element));
elements.addAll(collector.get(((SpecStepImpl)element).getName()));
Expand Down
11 changes: 8 additions & 3 deletions gauge/src/com/thoughtworks/gauge/util/GaugeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,14 @@ public static boolean isGradleModule(Module module) {
}

public static boolean isGaugeElement(PsiElement element) {
return StepUtil.isMethod(element)
? StepUtil.getGaugeStepAnnotationValues((PsiMethod)element).size() > 0
: (StepUtil.isConcept(element) || StepUtil.isStep(element));
if (StepUtil.isMethod(element)) {
return StepUtil.getGaugeStepAnnotationValues((PsiMethod) element)
.size() > 0;
} else if (KtUtil.isKtFunction(element) || KtUtil.isKtMethod(element)) {
return KtUtil.getGaugeStepAnnotationValues(element).size() > 0;
} else {
return StepUtil.isConcept(element) || StepUtil.isStep(element);
}
}

@Nullable
Expand Down
89 changes: 89 additions & 0 deletions gauge/src/com/thoughtworks/gauge/util/KtUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.thoughtworks.gauge.util;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import org.jetbrains.kotlin.asJava.LightClassUtil;
import org.jetbrains.kotlin.asJava.classes.KtUltraLightClass;
import org.jetbrains.kotlin.asJava.elements.KtLightMethodImpl;
import org.jetbrains.kotlin.psi.KtNamedFunction;

public final class KtUtil {

public static boolean isKtClass(PsiElement element) {
try {
return Class.forName("org.jetbrains.kotlin.asJava.classes.KtUltraLightClass").isInstance(element);
} catch (ClassNotFoundException e) {
return false;
}
}

public static boolean isKtFunction(PsiElement element) {
try {
return Class.forName("org.jetbrains.kotlin.psi.KtNamedFunction").isInstance(element);
} catch (ClassNotFoundException e) {
return false;
}
}

public static boolean isKtMethod(PsiElement element) {
try {
return Class.forName("org.jetbrains.kotlin.asJava.classes.KtUltraLightMethod")
.isInstance(element);
} catch (ClassNotFoundException e) {
return false;
}
}

public static boolean isClassUsed(final PsiElement element) {
if (!isKtClass(element)) {
return false;
}
return Inner.isClassUsed(element);
}

public static List<String> getGaugeStepAnnotationValues(PsiElement element) {
return Inner.getGaugeStepAnnotationValues(element);
}

public static final class Inner {

private Inner() {
}

private static boolean isClassUsed(final PsiElement element) {
if (!isKtClass(element)) {
return false;
}
return Arrays.stream(((KtUltraLightClass) element).getAllMethods())
.anyMatch(function ->
StepUtil.getGaugeStepAnnotationValues(function).size() > 0 || HookUtil.isHook(function));
}


private static List<String> getGaugeStepAnnotationValues(PsiElement element) {
final PsiAnnotation[] annotations;
if (isKtFunction(element)) {
annotations = toPsiMethod((KtNamedFunction) element).getAnnotations();
} else if (isKtMethod(element)) {
annotations = ((KtLightMethodImpl) element).getAnnotations();
} else {
annotations = PsiAnnotation.EMPTY_ARRAY;
}
return Arrays.stream(annotations)
.map(StepUtil::getGaugeStepAnnotationValues)
.flatMap(Collection::stream)
.collect(Collectors.toList());
}

private static PsiMethod toPsiMethod(KtNamedFunction function) {
return LightClassUtil.INSTANCE.getLightClassMethod(function);
}

}
}
2 changes: 1 addition & 1 deletion gauge/src/com/thoughtworks/gauge/util/StepUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public static List<String> getGaugeStepAnnotationValues(PsiMethod stepMethod) {
return values;
}

private static List<String> getGaugeStepAnnotationValues(PsiAnnotation annotation) {
static List<String> getGaugeStepAnnotationValues(PsiAnnotation annotation) {
List<String> values = new ArrayList<>();
if (!isGaugeAnnotation(annotation)) {
return values;
Expand Down