-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure that plugin works with a disabled Kotlin plugin
- Loading branch information
Showing
13 changed files
with
303 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 0 additions & 62 deletions
62
src/main/java/org/mapstruct/intellij/util/KtMapstructElementUtil.kt
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
src/main/java/org/mapstruct/intellij/util/MapstructKotlinElementUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package org.mapstruct.intellij.util; | ||
|
||
import com.intellij.patterns.StandardPatterns; | ||
import com.intellij.psi.PsiElement; | ||
import org.mapstruct.intellij.util.patterns.KotlinElementPattern; | ||
|
||
import static org.mapstruct.intellij.util.patterns.MapStructKotlinPatterns.psiElement; | ||
|
||
/** | ||
* Utils for working with MapStruct kotlin elements. | ||
* | ||
* @author Filip Hrisafov | ||
*/ | ||
public final class MapstructKotlinElementUtils { | ||
|
||
/** | ||
* Hide default constructor. | ||
*/ | ||
private MapstructKotlinElementUtils() { | ||
} | ||
|
||
/** | ||
* @param parameterName the name of the parameter in the {@code @ValueMapping} annotation | ||
* | ||
* @return an element pattern for a parameter in the {@code @ValueMapping} annotation | ||
*/ | ||
public static KotlinElementPattern.Capture<? extends PsiElement> valueMappingElementPattern(String parameterName) { | ||
return elementPattern( | ||
parameterName, | ||
MapstructUtil.VALUE_MAPPING_ANNOTATION_FQN, | ||
MapstructUtil.VALUE_MAPPINGS_ANNOTATION_FQN | ||
); | ||
} | ||
|
||
/** | ||
* @param parameterName the name of the parameter in the {@code @Mapping} annotation | ||
* | ||
* @return an element pattern for a parameter in the {@code @Mapping} annotation | ||
*/ | ||
public static KotlinElementPattern.Capture<? extends PsiElement> mappingElementPattern(String parameterName) { | ||
return elementPattern( | ||
parameterName, | ||
MapstructUtil.MAPPING_ANNOTATION_FQN, | ||
MapstructUtil.MAPPINGS_ANNOTATION_FQN | ||
); | ||
} | ||
|
||
private static KotlinElementPattern.Capture<? extends PsiElement> elementPattern(String parameterName, | ||
String annotationFQN, | ||
String annotationHolderFQN | ||
) { | ||
return psiElement() | ||
.insideRepeatableAnnotationParam( | ||
StandardPatterns.string().equalTo( annotationFQN ), | ||
StandardPatterns.string().equalTo( annotationHolderFQN ), | ||
parameterName | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/org/mapstruct/intellij/util/patterns/KotlinElementPattern.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package org.mapstruct.intellij.util.patterns; | ||
|
||
import com.intellij.patterns.ElementPattern; | ||
import com.intellij.patterns.PsiElementPattern; | ||
import com.intellij.psi.PsiElement; | ||
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes; | ||
|
||
import static org.mapstruct.intellij.util.patterns.MapStructKotlinPatterns.ktAnnotation; | ||
import static org.mapstruct.intellij.util.patterns.MapStructKotlinPatterns.ktValueArgument; | ||
|
||
/** | ||
* @author Filip Hrisafov | ||
*/ | ||
public class KotlinElementPattern<T extends PsiElement, Self extends KotlinElementPattern<T, Self>> | ||
extends PsiElementPattern<T, Self> { | ||
|
||
public KotlinElementPattern(final Class<T> aClass) { | ||
super( aClass ); | ||
} | ||
|
||
public Self insideRepeatableAnnotationParam( | ||
ElementPattern<String> annotationQualifiedName, | ||
ElementPattern<String> annotationHolderQualifiedName, | ||
String parameterName) { | ||
// A repeatable annotation in kotlin has 2 possible ways of PSI structure: | ||
// 1. Part of the repeatable holder | ||
// @Mappings( | ||
// Mapping(target = "name") | ||
// ) | ||
// 2. Just the annotation | ||
// @Mapping(target = "name") | ||
|
||
KtValueArgumentPattern ktValueArgumentPattern = ktValueArgument().withName( parameterName ); | ||
return withElementType( KtStubElementTypes.STRING_TEMPLATE ).andOr( | ||
withParent( | ||
ktValueArgumentPattern | ||
.withAncestor( 5, ktAnnotation().qName( annotationHolderQualifiedName ) ) | ||
), | ||
|
||
withParent( | ||
ktValueArgumentPattern | ||
.withSuperParent( 2, ktAnnotation().qName( annotationQualifiedName ) ) | ||
) | ||
); | ||
} | ||
|
||
public static class Capture<T extends PsiElement> extends KotlinElementPattern<T, KotlinElementPattern.Capture<T>> { | ||
public Capture(Class<T> aClass) { | ||
super( aClass ); | ||
} | ||
|
||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/org/mapstruct/intellij/util/patterns/KtAnnotationEntryPattern.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package org.mapstruct.intellij.util.patterns; | ||
|
||
import com.intellij.patterns.ElementPattern; | ||
import com.intellij.patterns.PatternCondition; | ||
import com.intellij.patterns.PsiElementPattern; | ||
import com.intellij.util.ProcessingContext; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor; | ||
import org.jetbrains.kotlin.idea.caches.resolve.ResolutionUtils; | ||
import org.jetbrains.kotlin.name.FqName; | ||
import org.jetbrains.kotlin.psi.KtAnnotationEntry; | ||
import org.jetbrains.kotlin.resolve.BindingContext; | ||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode; | ||
|
||
/** | ||
* @author Filip Hrisafov | ||
*/ | ||
public class KtAnnotationEntryPattern extends PsiElementPattern<KtAnnotationEntry, KtAnnotationEntryPattern> { | ||
|
||
static final KtAnnotationEntryPattern KT_ANNOTATION_ENTRY_PATTERN = new KtAnnotationEntryPattern(); | ||
|
||
private KtAnnotationEntryPattern() { | ||
super( KtAnnotationEntry.class ); | ||
} | ||
|
||
public KtAnnotationEntryPattern qName(ElementPattern<String> pattern) { | ||
return with( new PatternCondition<KtAnnotationEntry>( "qName" ) { | ||
@Override | ||
public boolean accepts(@NotNull KtAnnotationEntry ktAnnotation, ProcessingContext context) { | ||
AnnotationDescriptor descriptor = ResolutionUtils.analyze( | ||
ktAnnotation, | ||
BodyResolveMode.PARTIAL_FOR_COMPLETION | ||
).get( BindingContext.ANNOTATION, ktAnnotation ); | ||
|
||
if ( descriptor == null ) { | ||
return false; | ||
} | ||
|
||
FqName fqName = descriptor.getFqName(); | ||
if ( fqName == null ) { | ||
return false; | ||
} | ||
return pattern.accepts( fqName.asString(), context ); | ||
} | ||
} ); | ||
} | ||
|
||
} |
Oops, something went wrong.