-
-
Notifications
You must be signed in to change notification settings - Fork 41
#163 Reference support for @InheritConfiguration and @InheritInverseConfiguration #199
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 ); | ||
} | ||
|
||
} |
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 | ||
); | ||
} | ||
|
||
} |
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 { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to provide this kind of base reference on the level of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure that I understand what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a reference that does not support nesting, see:
This is called with Then it is a reference that supports the nested, dotted notation like It is false, when nesting is not supported, so in Got a better naming idea? I also called it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It feels to me like we need a type like |
||||||
|
||||||
/** | ||||||
* 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 | ||||||
} | ||||||
} |
Uh oh!
There was an error while loading. Please reload this page.