[javafx] [jvm] create field from fxml works for kotlin (IDEA-194806)

GitOrigin-RevId: 527a354b90f49c22202f582d1c433ffbea84ea07
This commit is contained in:
Anna Kozlova
2021-10-06 13:04:16 +02:00
committed by intellij-monorepo-bot
parent 7450e40b4e
commit da574201f1
2 changed files with 43 additions and 3 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.javaFX.fxml.codeInsight.inspections;
import com.intellij.codeInsight.ExpectedTypeInfo;
@@ -10,6 +10,9 @@ import com.intellij.codeInsight.daemon.impl.quickfix.CreateFieldFromUsageFix;
import com.intellij.codeInsight.daemon.impl.quickfix.CreateFieldFromUsageHelper;
import com.intellij.codeInspection.*;
import com.intellij.lang.LanguageNamesValidation;
import com.intellij.lang.jvm.JvmModifier;
import com.intellij.lang.jvm.actions.*;
import com.intellij.lang.jvm.util.JvmUtil;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
@@ -30,6 +33,10 @@ import org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxBuiltInTagDescriptor;
import org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxClassTagDescriptorBase;
import org.jetbrains.plugins.javaFX.fxml.refs.JavaFxFieldIdReferenceProvider;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class JavaFxUnresolvedFxIdReferenceInspection extends XmlSuppressableInspectionTool {
@NotNull
@Override
@@ -55,7 +62,8 @@ public class JavaFxUnresolvedFxIdReferenceInspection extends XmlSuppressableInsp
final String text = reference.getCanonicalText();
boolean validName = LanguageNamesValidation.isIdentifier(fieldClass.getLanguage(), text, fieldClass.getProject());
holder.registerProblem(reference.getElement(), reference.getRangeInElement(), JavaFXBundle.message("inspection.javafx.unresolved.fx.id.reference.problem"),
isOnTheFly && validName ? new LocalQuickFix[]{new CreateFieldFromUsageQuickFix(text)} : LocalQuickFix.EMPTY_ARRAY);
isOnTheFly && validName ?
createFixes((JavaFxFieldIdReferenceProvider.JavaFxControllerFieldRef)reference, holder.getFile()) : LocalQuickFix.EMPTY_ARRAY);
}
}
}
@@ -65,6 +73,29 @@ public class JavaFxUnresolvedFxIdReferenceInspection extends XmlSuppressableInsp
};
}
private static LocalQuickFix @NotNull [] createFixes(JavaFxFieldIdReferenceProvider.JavaFxControllerFieldRef reference, PsiFile file) {
@PsiModifier.ModifierConstant
String visibility = JavaCodeStyleSettings.getInstance(file).VISIBILITY;
Collection<AnnotationRequest> annotations;
if (!PsiModifier.PUBLIC.equals(visibility)) {
annotations = Collections.singletonList(AnnotationRequestsKt.annotationRequest(JavaFxCommonNames.JAVAFX_FXML_ANNOTATION));
}
else {
annotations = Collections.emptyList();
}
JvmModifier modifier = JvmUtil.getAccessModifier(VisibilityUtil.getAccessLevel(visibility));
List<ExpectedType> expectedTypes = ExpectedTypesKt.expectedTypes(JavaPsiFacade.getElementFactory(file.getProject()).createType(checkContext(reference.getXmlAttributeValue())), ExpectedType.Kind.SUBTYPE);
CreateFieldRequest request = FieldRequestsKt.fieldRequest(reference.getCanonicalText(),
annotations,
Collections.singletonList(modifier),
expectedTypes,
new PsiJvmSubstitutor(file.getProject(), PsiSubstitutor.EMPTY), null, false);
return IntentionWrapper.wrapToQuickFixes(JvmElementActionFactories.createAddFieldActions(reference.getAClass(), request), file).toArray(LocalQuickFix.EMPTY_ARRAY);
}
protected static PsiClass checkContext(final XmlAttributeValue attributeValue) {
if (attributeValue == null) return null;
final PsiElement parent = attributeValue.getParent();