forked from mapstruct/mapstruct-idea
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mapstruct#163 Reference support for @InheritConfiguration and @Inheri…
…tInverseConfiguration
- Loading branch information
1 parent
4b48a62
commit 88ebd38
Showing
13 changed files
with
512 additions
and
47 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
75 changes: 75 additions & 0 deletions
75
...struct/intellij/codeinsight/references/MapstructMappingInheritConfigurationReference.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,75 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package org.mapstruct.intellij.codeinsight.references; | ||
|
||
import java.util.Objects; | ||
|
||
import com.intellij.openapi.util.TextRange; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiMethod; | ||
import com.intellij.psi.PsiReference; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.mapstruct.InheritConfiguration; | ||
import org.mapstruct.intellij.util.MapStructVersion; | ||
import org.mapstruct.intellij.util.MapstructUtil; | ||
|
||
import static org.mapstruct.intellij.inspection.inheritance.InheritConfigurationUtils.findInheritConfigurationMethods; | ||
|
||
/** | ||
* Reference for {@link InheritConfiguration#name()}. | ||
* | ||
* @author Oliver Erhart | ||
*/ | ||
class MapstructMappingInheritConfigurationReference extends MapstructNonNestedBaseReference { | ||
|
||
private final MapStructVersion mapStructVersion; | ||
|
||
/** | ||
* Create a new {@link MapstructMappingInheritConfigurationReference} with the provided parameters | ||
* | ||
* @param element the element that the reference belongs to | ||
* @param previousReference the previous reference if there is one (in nested properties for example) | ||
* @param rangeInElement the range that the reference represent in the {@code element} | ||
* @param value the matched value (useful when {@code rangeInElement} is empty) | ||
*/ | ||
private MapstructMappingInheritConfigurationReference( | ||
PsiElement element, | ||
MapstructMappingInheritConfigurationReference previousReference, | ||
TextRange rangeInElement, String value | ||
) { | ||
super( element, previousReference, rangeInElement, value ); | ||
mapStructVersion = MapstructUtil.resolveMapStructProjectVersion( element.getContainingFile() | ||
.getOriginalFile() ); | ||
} | ||
|
||
@Override | ||
PsiElement resolveInternal(@NotNull String value, @NotNull PsiMethod mappingMethod) { | ||
|
||
return findInheritConfigurationMethods( mappingMethod, mapStructVersion ) | ||
.filter( a -> Objects.equals( a.getName(), value ) ) | ||
.findAny() | ||
.orElse( null ); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
Object[] getVariantsInternal(@NotNull PsiMethod mappingMethod) { | ||
|
||
return findInheritConfigurationMethods( mappingMethod, mapStructVersion ) | ||
.map( method -> MapstructUtil.asLookup( method, method.getName(), method.getName() ) ) | ||
.filter( Objects::nonNull ) | ||
.toArray(); | ||
} | ||
|
||
/** | ||
* @param psiElement the literal for which references need to be created | ||
* @return the references for the given {@code psiLiteral} | ||
*/ | ||
static PsiReference[] create(PsiElement psiElement) { | ||
return MapstructBaseReference.create( psiElement, MapstructMappingInheritConfigurationReference::new, false ); | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
...intellij/codeinsight/references/MapstructMappingInheritInverseConfigurationReference.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,81 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package org.mapstruct.intellij.codeinsight.references; | ||
|
||
import java.util.Objects; | ||
|
||
import com.intellij.openapi.util.TextRange; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiMethod; | ||
import com.intellij.psi.PsiReference; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.mapstruct.InheritInverseConfiguration; | ||
import org.mapstruct.intellij.util.MapStructVersion; | ||
import org.mapstruct.intellij.util.MapstructUtil; | ||
|
||
import static org.mapstruct.intellij.inspection.inheritance.InheritConfigurationUtils.findInheritInverseConfigurationMethods; | ||
|
||
/** | ||
* Reference for {@link InheritInverseConfiguration#name()}. | ||
* | ||
* @author Oliver Erhart | ||
*/ | ||
class MapstructMappingInheritInverseConfigurationReference extends MapstructNonNestedBaseReference { | ||
|
||
private final MapStructVersion mapStructVersion; | ||
|
||
/** | ||
* Create a new {@link MapstructMappingInheritInverseConfigurationReference} with the provided parameters | ||
* | ||
* @param element the element that the reference belongs to | ||
* @param previousReference the previous reference if there is one (in nested properties for example) | ||
* @param rangeInElement the range that the reference represent in the {@code element} | ||
* @param value the matched value (useful when {@code rangeInElement} is empty) | ||
*/ | ||
private MapstructMappingInheritInverseConfigurationReference( | ||
PsiElement element, | ||
MapstructMappingInheritInverseConfigurationReference previousReference, | ||
TextRange rangeInElement, | ||
String value | ||
) { | ||
|
||
super( element, previousReference, rangeInElement, value ); | ||
mapStructVersion = MapstructUtil.resolveMapStructProjectVersion( element.getContainingFile() | ||
.getOriginalFile() ); | ||
} | ||
|
||
@Override | ||
PsiElement resolveInternal(@NotNull String value, @NotNull PsiMethod mappingMethod) { | ||
|
||
return findInheritInverseConfigurationMethods( mappingMethod, mapStructVersion ) | ||
.filter( a -> Objects.equals( a.getName(), value ) ) | ||
.findAny() | ||
.orElse( null ); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
Object[] getVariantsInternal(@NotNull PsiMethod mappingMethod) { | ||
|
||
return findInheritInverseConfigurationMethods( mappingMethod, mapStructVersion ) | ||
.map( method -> MapstructUtil.asLookup( method, method.getName(), method.getName() ) ) | ||
.filter( Objects::nonNull ) | ||
.toArray(); | ||
} | ||
|
||
/** | ||
* @param psiElement the literal for which references need to be created | ||
* @return the references for the given {@code psiLiteral} | ||
*/ | ||
static PsiReference[] create(PsiElement psiElement) { | ||
return MapstructBaseReference.create( | ||
psiElement, | ||
MapstructMappingInheritInverseConfigurationReference::new, | ||
false | ||
); | ||
} | ||
|
||
} |
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
51 changes: 51 additions & 0 deletions
51
...n/java/org/mapstruct/intellij/codeinsight/references/MapstructNonNestedBaseReference.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,51 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package org.mapstruct.intellij.codeinsight.references; | ||
|
||
import com.intellij.codeInsight.lookup.LookupElement; | ||
import com.intellij.openapi.util.TextRange; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiType; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* A base reference to mapstruct annotations without nested types. | ||
* | ||
* @author Oliver Erhart | ||
*/ | ||
public abstract class MapstructNonNestedBaseReference extends MapstructBaseReference { | ||
|
||
/** | ||
* Create a reference. | ||
* | ||
* @param element the literal where the text is | ||
* @param previous the previous reference ({@code null} if there is no previous reference) | ||
* @param rangeInElement the range in the {@code element} for which this reference is valid | ||
*/ | ||
MapstructNonNestedBaseReference(@NotNull PsiElement element, | ||
@Nullable MapstructBaseReference previous, | ||
TextRange rangeInElement, String value) { | ||
super( element, previous, rangeInElement, value ); | ||
} | ||
|
||
@Override | ||
final PsiElement resolveInternal(@NotNull String value, @NotNull PsiType psiType) { | ||
return null; // not needed | ||
} | ||
|
||
@NotNull | ||
@Override | ||
final Object[] getVariantsInternal(@NotNull PsiType psiType) { | ||
return LookupElement.EMPTY_ARRAY; // not needed | ||
} | ||
|
||
@Override | ||
@Nullable | ||
final PsiType resolvedType() { | ||
return null; // not needed | ||
} | ||
} |
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
Oops, something went wrong.