Skip to content

Commit 4b1cf20

Browse files
committed
Fix missing types for parameter and value mapping lookups in newer IntelliJ versions
Add IntelliJ 2020.2 and 2020.3 to the build matrix Fixes #71
1 parent ce0d808 commit 4b1cf20

File tree

6 files changed

+41
-11
lines changed

6 files changed

+41
-11
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
strategy:
88
fail-fast: false
99
matrix:
10-
ideaVersion: [2019.1, 2019.2, 2019.3, 2020.1, LATEST-EAP-SNAPSHOT]
10+
ideaVersion: [2019.1, 2019.2, 2019.3, 2020.1, 2020.2, 2020.3, LATEST-EAP-SNAPSHOT]
1111
name: 'IDEA ${{ matrix.ideaVersion }}'
1212
env:
1313
IDEA_VERSION: ${{ matrix.ideaVersion }}

src/main/java/org/mapstruct/intellij/codeinsight/references/MapstructSourceReference.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ Object[] getVariantsInternal(@NotNull PsiMethod mappingMethod) {
106106
return parameterType == null ? LookupElement.EMPTY_ARRAY : getVariantsInternal( parameterType );
107107
}
108108

109-
return sourceParameters;
109+
return Stream.of( sourceParameters )
110+
.map( MapstructUtil::asLookup )
111+
.toArray( LookupElement[]::new );
110112
}
111113

112114
@Nullable

src/main/java/org/mapstruct/intellij/codeinsight/references/ValueMappingSourceReference.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.jetbrains.annotations.NotNull;
1818
import org.jetbrains.annotations.Nullable;
1919

20+
import static org.mapstruct.intellij.util.MapstructUtil.asLookup;
2021
import static org.mapstruct.intellij.util.SourceUtils.getParameterClass;
2122

2223
/**
@@ -60,7 +61,8 @@ Object[] getVariantsInternal(@NotNull PsiMethod mappingMethod) {
6061

6162
return Stream.of( sourceClass.getFields() )
6263
.filter( psiField -> psiField instanceof PsiEnumConstant )
63-
.toArray( PsiField[]::new );
64+
.map( psiEnumConstant -> asLookup( (PsiEnumConstant) psiEnumConstant ) )
65+
.toArray( LookupElement[]::new );
6466
}
6567

6668
/**

src/main/java/org/mapstruct/intellij/codeinsight/references/ValueMappingTargetReference.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import org.jetbrains.annotations.NotNull;
1919
import org.jetbrains.annotations.Nullable;
2020

21+
import static org.mapstruct.intellij.util.MapstructUtil.asLookup;
22+
2123
/**
2224
* Reference for {@link org.mapstruct.ValueMapping#target()}.
2325
*
@@ -59,7 +61,8 @@ Object[] getVariantsInternal(@NotNull PsiMethod mappingMethod) {
5961

6062
return Stream.of( targetClass.getFields() )
6163
.filter( psiField -> psiField instanceof PsiEnumConstant )
62-
.toArray( PsiField[]::new );
64+
.map( psiEnumConstant -> asLookup( (PsiEnumConstant) psiEnumConstant ) )
65+
.toArray( LookupElement[]::new );
6366
}
6467

6568
/**

src/main/java/org/mapstruct/intellij/util/MapstructUtil.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Map;
1313
import java.util.function.Function;
1414
import java.util.stream.Stream;
15+
import javax.swing.Icon;
1516

1617
import com.intellij.codeInsight.AnnotationUtil;
1718
import com.intellij.codeInsight.lookup.LookupElement;
@@ -21,11 +22,13 @@
2122
import com.intellij.openapi.roots.ProjectRootManager;
2223
import com.intellij.openapi.util.Pair;
2324
import com.intellij.psi.CommonClassNames;
25+
import com.intellij.psi.EmptySubstitutor;
2426
import com.intellij.psi.JavaPsiFacade;
2527
import com.intellij.psi.PsiArrayType;
2628
import com.intellij.psi.PsiClass;
2729
import com.intellij.psi.PsiClassType;
2830
import com.intellij.psi.PsiElement;
31+
import com.intellij.psi.PsiEnumConstant;
2932
import com.intellij.psi.PsiField;
3033
import com.intellij.psi.PsiFile;
3134
import com.intellij.psi.PsiMethod;
@@ -109,7 +112,8 @@ public static LookupElement[] asLookup(Map<String, Pair<? extends PsiElement, Ps
109112
lookupElements[index++] = asLookup(
110113
propertyName,
111114
pair,
112-
typeMapper
115+
typeMapper,
116+
PlatformIcons.VARIABLE_ICON
113117
);
114118
}
115119
return lookupElements;
@@ -120,13 +124,29 @@ public static LookupElement[] asLookup(Map<String, Pair<? extends PsiElement, Ps
120124

121125
}
122126

127+
public static LookupElement asLookup(PsiParameter parameter) {
128+
return asLookup( parameter.getName(), parameter, PsiParameter::getType, PlatformIcons.PARAMETER_ICON );
129+
}
130+
131+
public static LookupElement asLookup(PsiEnumConstant enumConstant) {
132+
return asLookup( enumConstant.getName(), enumConstant, PsiField::getType, PlatformIcons.FIELD_ICON );
133+
}
134+
135+
public static <T extends PsiElement> LookupElement asLookup(String propertyName, @NotNull T psiElement,
136+
Function<T, PsiType> typeMapper, Icon icon) {
137+
//noinspection unchecked
138+
return asLookup( propertyName, Pair.pair( psiElement, EmptySubstitutor.getInstance() ),
139+
(Function<PsiElement, PsiType>) typeMapper, icon
140+
);
141+
}
142+
123143
public static LookupElement asLookup(String propertyName, @NotNull Pair<? extends PsiElement, PsiSubstitutor> pair,
124-
Function<PsiElement, PsiType> typeMapper) {
144+
Function<PsiElement, PsiType> typeMapper, Icon icon) {
125145
PsiElement member = pair.getFirst();
126146
PsiSubstitutor substitutor = pair.getSecond();
127147

128148
LookupElementBuilder builder = LookupElementBuilder.create( member, propertyName )
129-
.withIcon( PlatformIcons.VARIABLE_ICON )
149+
.withIcon( icon )
130150
.withPresentableText( propertyName )
131151
.withTailText( formatTailText( member, substitutor ) );
132152
final PsiType type = typeMapper.apply( member );
@@ -138,20 +158,23 @@ public static LookupElement asLookup(String propertyName, @NotNull Pair<? extend
138158
}
139159

140160
private static String formatTailText(PsiElement member, PsiSubstitutor substitutor) {
161+
String tailText;
141162
if ( member instanceof PsiMethod ) {
142-
return PsiFormatUtil.formatMethod(
163+
tailText = PsiFormatUtil.formatMethod(
143164
(PsiMethod) member,
144165
substitutor,
145166
0,
146167
PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_TYPE
147168
);
148169
}
149170
else if ( member instanceof PsiVariable ) {
150-
return PsiFormatUtil.formatVariable( (PsiVariable) member, 0, substitutor );
171+
tailText = PsiFormatUtil.formatVariable( (PsiVariable) member, 0, substitutor );
151172
}
152173
else {
153-
return "";
174+
tailText = "";
154175
}
176+
177+
return !tailText.isEmpty() ? tailText : null;
155178
}
156179

157180
public static boolean isPublic(@NotNull PsiMethod method) {

src/test/java/org/mapstruct/intellij/testutil/TestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static LookupElementPresentation createField(String lookupString, String
2424
}
2525

2626
public static LookupElementPresentation createVariable(String lookupString, String typeText) {
27-
return create( lookupString, typeText, PlatformIcons.VARIABLE_ICON, "" );
27+
return create( lookupString, typeText, PlatformIcons.VARIABLE_ICON, null );
2828
}
2929

3030
public static LookupElementPresentation createParameter(String lookupString, String typeText) {

0 commit comments

Comments
 (0)