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

Fixes #2316 #2317

Merged
merged 1 commit into from
Jun 20, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,13 @@ class SpongeInjectionInspection : AbstractBaseJavaLocalInspectionTool() {
)
}
}
"ninja.leaping.configurate.loader.ConfigurationLoader" -> {
"ninja.leaping.configurate.loader.ConfigurationLoader",
"org.spongepowered.configurate.reference.ConfigurationReference",
"org.spongepowered.configurate.loader.ConfigurationLoader" -> {
if (defaultConfig == null) {
holder.registerProblem(
variable.nameIdentifier ?: variable,
"Injected ConfigurationLoader must be annotated with @DefaultConfig.",
"Injected ${classType.name} must be annotated with @DefaultConfig.",
ProblemHighlightType.GENERIC_ERROR,
AddAnnotationFix(SpongeConstants.DEFAULT_CONFIG_ANNOTATION, annotationsOwner),
)
Expand All @@ -257,7 +259,7 @@ class SpongeInjectionInspection : AbstractBaseJavaLocalInspectionTool() {
if (configDir != null) {
holder.registerProblem(
configDir,
"Injected ConfigurationLoader cannot be annotated with @ConfigDir.",
"Injected ${classType.name} cannot be annotated with @ConfigDir.",
ProblemHighlightType.GENERIC_ERROR,
QuickFixFactory.getInstance().createDeleteFix(configDir, "Remove @ConfigDir"),
)
Expand All @@ -267,22 +269,25 @@ class SpongeInjectionInspection : AbstractBaseJavaLocalInspectionTool() {
val ref = classType.reference
holder.registerProblem(
ref,
"Injected ConfigurationLoader must have a generic parameter.",
"Injected ${classType.name} must have a generic parameter.",
ProblemHighlightType.GENERIC_ERROR,
MissingConfLoaderTypeParamFix(ref),
)
} else {
classType.parameters.firstOrNull()?.let { param ->
val paramType = param as? PsiClassReferenceType ?: return@let
val paramTypeFQName = paramType.fullQualifiedName ?: return@let
if (paramTypeFQName != "ninja.leaping.configurate.commented.CommentedConfigurationNode") {
if (
paramTypeFQName != "ninja.leaping.configurate.commented.CommentedConfigurationNode" &&
paramTypeFQName != "org.spongepowered.configurate.CommentedConfigurationNode"
) {
val ref = param.reference
holder.registerProblem(
ref,
"Injected ConfigurationLoader generic parameter must be " +
"CommentedConfigurationNode.",
ProblemHighlightType.GENERIC_ERROR,
WrongConfLoaderTypeParamFix(ref),
WrongConfLoaderTypeParamFix(classType.className, ref),
)
}
}
Expand Down Expand Up @@ -371,15 +376,20 @@ class SpongeInjectionInspection : AbstractBaseJavaLocalInspectionTool() {
}
}

class WrongConfLoaderTypeParamFix(ref: PsiJavaCodeReferenceElement) : LocalQuickFixOnPsiElement(ref) {
class WrongConfLoaderTypeParamFix(private val clazzName: String, param: PsiJavaCodeReferenceElement) :
LocalQuickFixOnPsiElement(param) {

override fun getFamilyName(): String = name

override fun getText(): String = "Set to CommentedConfigurationNode"

override fun invoke(project: Project, file: PsiFile, startElement: PsiElement, endElement: PsiElement) {
val newRef = JavaPsiFacade.getElementFactory(project).createReferenceFromText(
"ninja.leaping.configurate.commented.CommentedConfigurationNode",
when (clazzName) {
"ninja.leaping.configurate.loader.ConfigurationLoader" ->
"ninja.leaping.configurate.commented.CommentedConfigurationNode"
else -> { "org.spongepowered.configurate.CommentedConfigurationNode" }
},
startElement,
)
startElement.replace(newRef)
Expand All @@ -393,11 +403,23 @@ class SpongeInjectionInspection : AbstractBaseJavaLocalInspectionTool() {
override fun getText(): String = "Insert generic parameter"

override fun invoke(project: Project, file: PsiFile, startElement: PsiElement, endElement: PsiElement) {
val newRef = JavaPsiFacade.getElementFactory(project).createReferenceFromText(
"ninja.leaping.configurate.loader.ConfigurationLoader" +
"<ninja.leaping.configurate.commented.CommentedConfigurationNode>",
startElement,
)
val newRef: PsiElement = if (
JavaPsiFacade.getInstance(project)
.findPackage("ninja.leaping.configurate") != null
) {
JavaPsiFacade.getElementFactory(project).createReferenceFromText(
"ninja.leaping.configurate.loader.ConfigurationLoader" +
"<ninja.leaping.configurate.commented.CommentedConfigurationNode>",
startElement
)
} else {
JavaPsiFacade.getElementFactory(project).createReferenceFromText(
"org.spongepowered.configurate.loader.ConfigurationLoader" +
"<org.spongepowered.configurate.CommentedConfigurationNode>",
startElement
)
}

startElement.replace(newRef)
}
}
Expand Down
Loading