Converted AutoImportFix to Kotlin

This commit is contained in:
Valentin Kipyatkov
2014-08-13 13:48:37 +04:00
parent 7c61539516
commit ced2abb860
15 changed files with 222 additions and 342 deletions
@@ -0,0 +1,5 @@
<root>
<item name='com.intellij.codeInsight.daemon.impl.ShowAutoImportPass java.lang.String getMessage(boolean, java.lang.String)'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
</root>
@@ -0,0 +1,6 @@
<root>
<item
name='com.intellij.psi.search.PsiShortNamesCache com.intellij.psi.search.PsiShortNamesCache getInstance(com.intellij.openapi.project.Project)'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
</root>
@@ -25,5 +25,5 @@ import java.util.List;
public interface JetIntentionActionsFactory {
@NotNull
List<IntentionAction> createActions(Diagnostic diagnostic);
List<IntentionAction> createActions(@NotNull Diagnostic diagnostic);
}
@@ -82,7 +82,7 @@ public class AddWhenElseBranchFix extends JetIntentionAction<JetWhenExpression>
return new JetSingleIntentionActionFactory() {
@Nullable
@Override
public JetIntentionAction createAction(Diagnostic diagnostic) {
public JetIntentionAction createAction(@NotNull Diagnostic diagnostic) {
PsiElement element = diagnostic.getPsiElement();
JetWhenExpression whenExpression = PsiTreeUtil.getParentOfType(element, JetWhenExpression.class, false);
if (whenExpression == null) return null;
@@ -1,320 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.plugin.quickfix;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.intellij.codeInsight.daemon.impl.ShowAutoImportPass;
import com.intellij.codeInsight.hint.HintManager;
import com.intellij.codeInsight.intention.HighPriorityAction;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.PsiShortNamesCache;
import com.intellij.util.IncorrectOperationException;
import kotlin.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetPsiUtil;
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.ImportPath;
import org.jetbrains.jet.lang.resolve.lazy.KotlinCodeAnalyzer;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.plugin.JetBundle;
import org.jetbrains.jet.plugin.actions.JetAddImportAction;
import org.jetbrains.jet.plugin.caches.JetShortNamesCache;
import org.jetbrains.jet.plugin.caches.KotlinIndicesHelper;
import org.jetbrains.jet.plugin.caches.resolve.ResolvePackage;
import org.jetbrains.jet.plugin.project.ProjectStructureUtil;
import org.jetbrains.jet.plugin.project.ResolveSessionForBodies;
import org.jetbrains.jet.plugin.util.JetPsiHeuristicsUtil;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
/**
* Check possibility and perform fix for unresolved references.
*/
public class AutoImportFix extends JetHintAction<JetSimpleNameExpression> implements HighPriorityAction {
@NotNull
private final Collection<FqName> suggestions;
public AutoImportFix(@NotNull JetSimpleNameExpression element) {
super(element);
suggestions = computeSuggestions(element);
}
private static Collection<FqName> computeSuggestions(@NotNull JetSimpleNameExpression element) {
final PsiFile file = element.getContainingFile();
if (!(file instanceof JetFile)) {
return Collections.emptyList();
}
String referenceName = element.getReferencedName();
if (element.getIdentifier() == null) {
Name conventionName = JetPsiUtil.getConventionName(element);
if (conventionName != null) {
referenceName = conventionName.asString();
}
}
if (referenceName.isEmpty()) {
return Collections.emptyList();
}
ResolveSessionForBodies resolveSessionForBodies = ResolvePackage.getLazyResolveSession(element);
Module module = ModuleUtilCore.findModuleForPsiElement(file);
if (module == null) return Collections.emptyList();
GlobalSearchScope searchScope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module);
List<FqName> result = Lists.newArrayList();
if (!isSuppressedTopLevelImportInPosition(element)) {
result.addAll(getClassNames(referenceName, (JetFile) file, searchScope, resolveSessionForBodies));
result.addAll(getJetTopLevelFunctions(referenceName, element, searchScope, resolveSessionForBodies, file.getProject()));
}
result.addAll(getJetExtensionFunctions(referenceName, element, searchScope, resolveSessionForBodies, file.getProject()));
return Collections2.filter(result, new Predicate<FqName>() {
@Override
public boolean apply(@Nullable FqName fqName) {
assert fqName != null;
return ImportInsertHelper.needImport(new ImportPath(fqName, false), (JetFile) file);
}
});
}
private static boolean isSuppressedTopLevelImportInPosition(@NotNull JetSimpleNameExpression element) {
return PsiUtilPackage.isImportDirectiveExpression(element) || JetPsiUtil.isSelectorInQualified(element);
}
private static Collection<FqName> getJetTopLevelFunctions(
@NotNull String referenceName,
@NotNull JetExpression context,
@NotNull GlobalSearchScope searchScope,
@NotNull ResolveSessionForBodies resolveSession,
@NotNull Project project
) {
Collection<FunctionDescriptor> topLevelFunctions = new KotlinIndicesHelper(project).getTopLevelFunctionDescriptorsByName(
referenceName, context, resolveSession, searchScope);
return Sets.newHashSet(Collections2.transform(topLevelFunctions, new Function<DeclarationDescriptor, FqName>() {
@Override
public FqName apply(@Nullable DeclarationDescriptor declarationDescriptor) {
assert declarationDescriptor != null;
return DescriptorUtils.getFqNameSafe(declarationDescriptor);
}
}));
}
private static Collection<FqName> getJetExtensionFunctions(
@NotNull final String referenceName,
@NotNull JetSimpleNameExpression expression,
@NotNull GlobalSearchScope searchScope,
@NotNull ResolveSessionForBodies resolveSession,
@NotNull Project project
) {
Collection<DeclarationDescriptor> jetCallableExtensions = new KotlinIndicesHelper(project).getCallableExtensions(
new Function1<String, Boolean>() {
@Override
public Boolean invoke(String callableExtensionName) {
return callableExtensionName.equals(referenceName);
}
},
expression,
resolveSession,
searchScope);
return Sets.newHashSet(Collections2.transform(jetCallableExtensions, new Function<DeclarationDescriptor, FqName>() {
@Override
public FqName apply(@Nullable DeclarationDescriptor declarationDescriptor) {
assert declarationDescriptor != null;
return DescriptorUtils.getFqNameSafe(declarationDescriptor);
}
}));
}
/*
* Searches for possible class names in kotlin context and java facade.
*/
public static Collection<FqName> getClassNames(@NotNull String referenceName, @NotNull JetFile file, @NotNull GlobalSearchScope searchScope, @NotNull KotlinCodeAnalyzer analyzer) {
Set<FqName> possibleResolveNames = Sets.newHashSet();
if (!ProjectStructureUtil.isJsKotlinModule(file)) {
possibleResolveNames.addAll(getClassesFromCache(referenceName, searchScope, file));
}
else {
possibleResolveNames.addAll(getJetClasses(referenceName, searchScope, file.getProject(), analyzer));
}
// TODO: Do appropriate sorting
return Lists.newArrayList(possibleResolveNames);
}
private static Collection<FqName> getClassesFromCache(@NotNull String typeName, @NotNull GlobalSearchScope searchScope, @NotNull final JetFile file) {
PsiShortNamesCache cache = getShortNamesCache(file);
PsiClass[] classes = cache.getClassesByName(typeName, searchScope);
Collection<PsiClass> accessibleClasses = Collections2.filter(Lists.newArrayList(classes), new Predicate<PsiClass>() {
@Override
public boolean apply(PsiClass psiClass) {
assert psiClass != null;
return JetPsiHeuristicsUtil.isAccessible(psiClass, file);
}
});
return Collections2.transform(accessibleClasses, new Function<PsiClass, FqName>() {
@Nullable
@Override
public FqName apply(@Nullable PsiClass javaClass) {
assert javaClass != null;
String qualifiedName = javaClass.getQualifiedName();
assert qualifiedName != null;
return new FqName(qualifiedName);
}
});
}
private static PsiShortNamesCache getShortNamesCache(@NotNull JetFile jetFile) {
if (ProjectStructureUtil.isJsKotlinModule(jetFile)) {
return JetShortNamesCache.OBJECT$.getKotlinInstance(jetFile.getProject());
}
return PsiShortNamesCache.getInstance(jetFile.getProject());
}
private static Collection<FqName> getJetClasses(@NotNull final String typeName, @NotNull GlobalSearchScope searchScope, @NotNull Project project, @NotNull KotlinCodeAnalyzer resolveSession) {
Collection<ClassDescriptor> descriptors = new KotlinIndicesHelper(project).getClassDescriptorsByName(typeName, resolveSession, searchScope);
return Collections2.transform(descriptors, new Function<ClassDescriptor, FqName>() {
@Override
public FqName apply(ClassDescriptor descriptor) {
return DescriptorUtils.getFqNameSafe(descriptor);
}
});
}
@Override
public boolean showHint(@NotNull Editor editor) {
if (suggestions.isEmpty()) {
return false;
}
Project project = editor.getProject();
if (project == null) {
return false;
}
if (HintManager.getInstance().hasShownHintsThatWillHideByOtherHint(true)) {
return false;
}
if (!ApplicationManager.getApplication().isUnitTestMode()) {
String hintText = ShowAutoImportPass.getMessage(suggestions.size() > 1, suggestions.iterator().next().asString());
HintManager.getInstance().showQuestionHint(
editor, hintText,
element.getTextOffset(), element.getTextRange().getEndOffset(),
createAction(project, editor));
}
return true;
}
@Override
@NotNull
public String getText() {
return JetBundle.message("import.fix");
}
@Override
@NotNull
public String getFamilyName() {
return JetBundle.message("import.fix");
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return super.isAvailable(project, editor, file) && !suggestions.isEmpty();
}
@Override
public void invoke(@NotNull final Project project, @NotNull final Editor editor, JetFile file)
throws IncorrectOperationException {
CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {
@Override
public void run() {
createAction(project, editor).execute();
}
});
}
@Override
public boolean startInWriteAction() {
return true;
}
@NotNull
private JetAddImportAction createAction(@NotNull Project project, @NotNull Editor editor) {
return new JetAddImportAction(project, editor, element, suggestions);
}
@Nullable
public static JetSingleIntentionActionFactory createFactory() {
return new JetSingleIntentionActionFactory() {
@Nullable
@Override
public JetIntentionAction<JetSimpleNameExpression> createAction(@NotNull Diagnostic diagnostic) {
// There could be different psi elements (i.e. JetArrayAccessExpression), but we can fix only JetSimpleNameExpression case
if (diagnostic.getPsiElement() instanceof JetSimpleNameExpression) {
JetSimpleNameExpression psiElement = (JetSimpleNameExpression) diagnostic.getPsiElement();
return new AutoImportFix(psiElement);
}
return null;
}
@Override
public boolean isApplicableForCodeFragment() {
return true;
}
};
}
}
@@ -0,0 +1,189 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.plugin.quickfix
import com.google.common.collect.Lists
import com.intellij.codeInsight.daemon.impl.ShowAutoImportPass
import com.intellij.codeInsight.hint.HintManager
import com.intellij.codeInsight.intention.HighPriorityAction
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.command.CommandProcessor
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.module.ModuleUtilCore
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.search.PsiShortNamesCache
import org.jetbrains.jet.lang.diagnostics.Diagnostic
import org.jetbrains.jet.lang.psi.JetExpression
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.lang.psi.JetPsiUtil
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression
import org.jetbrains.jet.lang.psi.psiUtil.*
import org.jetbrains.jet.lang.resolve.DescriptorUtils
import org.jetbrains.jet.lang.resolve.ImportPath
import org.jetbrains.jet.lang.resolve.lazy.KotlinCodeAnalyzer
import org.jetbrains.jet.lang.resolve.name.FqName
import org.jetbrains.jet.plugin.JetBundle
import org.jetbrains.jet.plugin.actions.JetAddImportAction
import org.jetbrains.jet.plugin.caches.JetShortNamesCache
import org.jetbrains.jet.plugin.caches.KotlinIndicesHelper
import org.jetbrains.jet.plugin.caches.resolve.*
import org.jetbrains.jet.plugin.project.ProjectStructureUtil
import org.jetbrains.jet.plugin.project.ResolveSessionForBodies
import org.jetbrains.jet.plugin.util.JetPsiHeuristicsUtil
import java.util.ArrayList
import com.intellij.codeInsight.intention.IntentionAction
/**
* Check possibility and perform fix for unresolved references.
*/
public class AutoImportFix(element: JetSimpleNameExpression) : JetHintAction<JetSimpleNameExpression>(element), HighPriorityAction {
private val suggestions: Collection<FqName> = computeSuggestions(element)
override fun showHint(editor: Editor): Boolean {
if (suggestions.isEmpty()) return false
val project = editor.getProject() ?: return false
if (HintManager.getInstance().hasShownHintsThatWillHideByOtherHint(true)) return false
if (!ApplicationManager.getApplication()!!.isUnitTestMode()) {
val hintText = ShowAutoImportPass.getMessage(suggestions.size() > 1, suggestions.first().asString())
HintManager.getInstance().showQuestionHint(editor, hintText, element.getTextOffset(), element.getTextRange()!!.getEndOffset(), createAction(project, editor))
}
return true
}
override fun getText()
= JetBundle.message("import.fix")
override fun getFamilyName()
= JetBundle.message("import.fix")
override fun isAvailable(project: Project, editor: Editor, file: PsiFile)
= super< JetHintAction>.isAvailable(project, editor, file) && !suggestions.isEmpty()
override fun invoke(project: Project, editor: Editor?, file: JetFile?) {
CommandProcessor.getInstance().runUndoTransparentAction {
createAction(project, editor!!).execute()
}
}
override fun startInWriteAction()
= true
private fun createAction(project: Project, editor: Editor)
= JetAddImportAction(project, editor, element, suggestions)
private fun computeSuggestions(element: JetSimpleNameExpression): Collection<FqName> {
val file = element.getContainingFile() as? JetFile ?: return listOf()
var referenceName = element.getReferencedName()
if (element.getIdentifier() == null) {
val conventionName = JetPsiUtil.getConventionName(element)
if (conventionName != null) {
referenceName = conventionName.asString()
}
}
if (referenceName.isEmpty()) return listOf()
val resolveSessionForBodies = element.getLazyResolveSession()
val module = ModuleUtilCore.findModuleForPsiElement(file) ?: return listOf()
val searchScope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module)
val result = ArrayList<FqName>()
if (!isSuppressedTopLevelImportInPosition(element)) {
result.addAll(getClassNames(referenceName, file, searchScope, resolveSessionForBodies))
result.addAll(getJetTopLevelFunctions(referenceName, element, searchScope, resolveSessionForBodies, file.getProject()))
}
result.addAll(getJetExtensionFunctions(referenceName, element, searchScope, resolveSessionForBodies, file.getProject()))
return result.filter { ImportInsertHelper.needImport(ImportPath(it, false), file) }
}
private fun isSuppressedTopLevelImportInPosition(element: JetSimpleNameExpression)
= element.isImportDirectiveExpression() || JetPsiUtil.isSelectorInQualified(element)
private fun getJetTopLevelFunctions(referenceName: String, context: JetExpression, searchScope: GlobalSearchScope, resolveSession: ResolveSessionForBodies, project: Project): Collection<FqName>
= KotlinIndicesHelper(project).getTopLevelFunctionDescriptorsByName(referenceName, context, resolveSession, searchScope)
.map { DescriptorUtils.getFqNameSafe(it) }
.toSet()
private fun getJetExtensionFunctions(referenceName: String, expression: JetSimpleNameExpression, searchScope: GlobalSearchScope, resolveSession: ResolveSessionForBodies, project: Project): Collection<FqName>
= KotlinIndicesHelper(project).getCallableExtensions({ it == referenceName}, expression, resolveSession, searchScope)
.map { DescriptorUtils.getFqNameSafe(it) }
.toSet()
/*
* Searches for possible class names in kotlin context and java facade.
*/
private fun getClassNames(referenceName: String, file: JetFile, searchScope: GlobalSearchScope, analyzer: KotlinCodeAnalyzer): Collection<FqName> {
val possibleResolveNames = if (!ProjectStructureUtil.isJsKotlinModule(file)) {
getClassesFromCache(referenceName, searchScope, file)
}
else {
getJetClasses(referenceName, searchScope, file.getProject(), analyzer)
}
// TODO: Do appropriate sorting
return Lists.newArrayList<FqName>(possibleResolveNames)
}
private fun getClassesFromCache(typeName: String, searchScope: GlobalSearchScope, file: JetFile): Collection<FqName>
= getShortNamesCache(file).getClassesByName(typeName, searchScope)
.filter { JetPsiHeuristicsUtil.isAccessible(it, file) }
.map { FqName(it.getQualifiedName()!!) }
.toSet()
private fun getShortNamesCache(jetFile: JetFile): PsiShortNamesCache {
if (ProjectStructureUtil.isJsKotlinModule(jetFile)) {
return JetShortNamesCache.getKotlinInstance(jetFile.getProject())
}
return PsiShortNamesCache.getInstance(jetFile.getProject())
}
private fun getJetClasses(typeName: String, searchScope: GlobalSearchScope, project: Project, resolveSession: KotlinCodeAnalyzer): Collection<FqName>
= KotlinIndicesHelper(project).getClassDescriptorsByName(typeName, resolveSession, searchScope)
.map { DescriptorUtils.getFqNameSafe(it) }
.toSet()
class object {
public fun createFactory(): JetSingleIntentionActionFactory {
return object : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): JetIntentionAction<JetSimpleNameExpression>? {
// There could be different psi elements (i.e. JetArrayAccessExpression), but we can fix only JetSimpleNameExpression case
val psiElement = diagnostic.getPsiElement()
if (psiElement is JetSimpleNameExpression) {
return AutoImportFix(psiElement)
}
return null
}
override fun isApplicableForCodeFragment()
= true
}
}
}
}
@@ -202,7 +202,7 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction<JetFunction>
return new JetIntentionActionsFactory() {
@NotNull
@Override
public List<IntentionAction> createActions(Diagnostic diagnostic) {
public List<IntentionAction> createActions(@NotNull Diagnostic diagnostic) {
List<IntentionAction> actions = new LinkedList<IntentionAction>();
JetFunction function = QuickFixUtil.getParentElementOfType(diagnostic, JetFunction.class);
@@ -142,7 +142,7 @@ public abstract class ChangeFunctionSignatureFix extends JetIntentionAction<PsiE
public static JetSingleIntentionActionFactory createFactory() {
return new JetSingleIntentionActionFactory() {
@Override
public ChangeFunctionSignatureFix createAction(Diagnostic diagnostic) {
public ChangeFunctionSignatureFix createAction(@NotNull Diagnostic diagnostic) {
JetCallElement callElement = PsiTreeUtil.getParentOfType(diagnostic.getPsiElement(), JetCallElement.class);
//noinspection unchecked
CallableDescriptor descriptor = DiagnosticFactory.cast(diagnostic, Errors.TOO_MANY_ARGUMENTS, Errors.NO_VALUE_FOR_PARAMETER).getA();
@@ -181,7 +181,7 @@ public abstract class ChangeFunctionSignatureFix extends JetIntentionAction<PsiE
public static JetSingleIntentionActionFactory createFactoryForUnusedParameter() {
return new JetSingleIntentionActionFactory() {
@Override
public ChangeFunctionSignatureFix createAction(Diagnostic diagnostic) {
public ChangeFunctionSignatureFix createAction(@NotNull Diagnostic diagnostic) {
@SuppressWarnings("unchecked")
Object descriptor = UNUSED_PARAMETER.cast(diagnostic).getA();
@@ -129,7 +129,7 @@ public class ChangeVariableTypeFix extends JetIntentionAction<JetVariableDeclara
return new JetIntentionActionsFactory() {
@NotNull
@Override
public List<IntentionAction> createActions(Diagnostic diagnostic) {
public List<IntentionAction> createActions(@NotNull Diagnostic diagnostic) {
List<IntentionAction> actions = new LinkedList<IntentionAction>();
JetProperty property = QuickFixUtil.getParentElementOfType(diagnostic, JetProperty.class);
@@ -872,7 +872,7 @@ public class CreateFunctionFromUsageFix internal (
class object {
public fun createCreateGetFunctionFromUsageFactory(): JetSingleIntentionActionFactory {
return object : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic?): IntentionAction? {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val accessExpr = QuickFixUtil.getParentElementOfType(diagnostic, javaClass<JetArrayAccessExpression>()) ?: return null
val arrayExpr = accessExpr.getArrayExpression() ?: return null
val arrayType = TypeOrExpressionThereof(arrayExpr, Variance.IN_VARIANCE)
@@ -889,7 +889,7 @@ public class CreateFunctionFromUsageFix internal (
public fun createCreateSetFunctionFromUsageFactory(): JetSingleIntentionActionFactory {
return object : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic?): IntentionAction? {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val accessExpr = QuickFixUtil.getParentElementOfType(diagnostic, javaClass<JetArrayAccessExpression>()) ?: return null
val arrayExpr = accessExpr.getArrayExpression() ?: return null
val arrayType = TypeOrExpressionThereof(arrayExpr, Variance.IN_VARIANCE)
@@ -911,8 +911,8 @@ public class CreateFunctionFromUsageFix internal (
public fun createCreateHasNextFunctionFromUsageFactory(): JetSingleIntentionActionFactory {
return object : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic?): IntentionAction? {
val diagnosticWithParameters = DiagnosticFactory.cast(diagnostic!!, Errors.HAS_NEXT_MISSING, Errors.HAS_NEXT_FUNCTION_NONE_APPLICABLE)
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val diagnosticWithParameters = DiagnosticFactory.cast(diagnostic, Errors.HAS_NEXT_MISSING, Errors.HAS_NEXT_FUNCTION_NONE_APPLICABLE)
val ownerType = TypeOrExpressionThereof(diagnosticWithParameters.getA(), Variance.IN_VARIANCE)
val forExpr = QuickFixUtil.getParentElementOfType(diagnostic, javaClass<JetForExpression>()) ?: return null
@@ -924,8 +924,8 @@ public class CreateFunctionFromUsageFix internal (
public fun createCreateNextFunctionFromUsageFactory(): JetSingleIntentionActionFactory {
return object : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic?): IntentionAction? {
val diagnosticWithParameters = DiagnosticFactory.cast(diagnostic!!, Errors.NEXT_MISSING, Errors.NEXT_NONE_APPLICABLE)
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val diagnosticWithParameters = DiagnosticFactory.cast(diagnostic, Errors.NEXT_MISSING, Errors.NEXT_NONE_APPLICABLE)
val ownerType = TypeOrExpressionThereof(diagnosticWithParameters.getA(), Variance.IN_VARIANCE)
val forExpr = QuickFixUtil.getParentElementOfType(diagnostic, javaClass<JetForExpression>()) ?: return null
@@ -938,8 +938,8 @@ public class CreateFunctionFromUsageFix internal (
public fun createCreateIteratorFunctionFromUsageFactory(): JetSingleIntentionActionFactory {
return object : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic?): IntentionAction? {
val file = diagnostic!!.getPsiFile() as? JetFile ?: return null
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val file = diagnostic.getPsiFile() as? JetFile ?: return null
val forExpr = QuickFixUtil.getParentElementOfType(diagnostic, javaClass<JetForExpression>()) ?: return null
val iterableExpr = forExpr.getLoopRange() ?: return null
val variableExpr: JetExpression = ((forExpr.getLoopParameter() ?: forExpr.getMultiParameter()) ?: return null) as JetExpression
@@ -961,8 +961,8 @@ public class CreateFunctionFromUsageFix internal (
public fun createCreateComponentFunctionFromUsageFactory(): JetSingleIntentionActionFactory {
return object : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic?): IntentionAction? {
val diagnosticWithParameters = Errors.COMPONENT_FUNCTION_MISSING.cast(diagnostic!!)
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val diagnosticWithParameters = Errors.COMPONENT_FUNCTION_MISSING.cast(diagnostic)
val name = diagnosticWithParameters.getA()
val componentNumberMatcher = COMPONENT_FUNCTION_PATTERN.matcher(name.getIdentifier())
if (!componentNumberMatcher.matches()) return null
@@ -28,11 +28,11 @@ import java.util.List;
public abstract class JetSingleIntentionActionFactory implements JetIntentionActionsFactory {
@Nullable
public abstract IntentionAction createAction(Diagnostic diagnostic);
public abstract IntentionAction createAction(@NotNull Diagnostic diagnostic);
@NotNull
@Override
public final List<IntentionAction> createActions(Diagnostic diagnostic) {
public final List<IntentionAction> createActions(@NotNull Diagnostic diagnostic) {
List<IntentionAction> intentionActionList = new LinkedList<IntentionAction>();
if (diagnostic.getPsiElement().getContainingFile() instanceof JetCodeFragment && !isApplicableForCodeFragment()) {
@@ -152,7 +152,7 @@ public class MakeOverriddenMemberOpenFix extends JetIntentionAction<JetDeclarati
return new JetSingleIntentionActionFactory() {
@Nullable
@Override
public IntentionAction createAction(Diagnostic diagnostic) {
public IntentionAction createAction(@NotNull Diagnostic diagnostic) {
JetDeclaration declaration = QuickFixUtil.getParentElementOfType(diagnostic, JetDeclaration.class);
assert declaration != null;
return new MakeOverriddenMemberOpenFix(declaration);
@@ -39,7 +39,7 @@ import java.util.List;
public class QuickFixFactoryForTypeMismatchError implements JetIntentionActionsFactory {
@NotNull
@Override
public List<IntentionAction> createActions(Diagnostic diagnostic) {
public List<IntentionAction> createActions(@NotNull Diagnostic diagnostic) {
List<IntentionAction> actions = new LinkedList<IntentionAction>();
DiagnosticWithParameters2<JetExpression, JetType, JetType> diagnosticWithParameters = Errors.TYPE_MISMATCH.cast(diagnostic);
@@ -111,7 +111,7 @@ public class QuickFixRegistrar {
QuickFixes.factories.put(NO_BACKING_FIELD_CUSTOM_ACCESSORS, changeToPropertyNameFactory);
QuickFixes.factories.put(INACCESSIBLE_BACKING_FIELD, changeToPropertyNameFactory);
JetSingleIntentionActionFactory unresolvedReferenceFactory = AutoImportFix.createFactory();
JetSingleIntentionActionFactory unresolvedReferenceFactory = AutoImportFix.OBJECT$.createFactory();
QuickFixes.factories.put(UNRESOLVED_REFERENCE, unresolvedReferenceFactory);
QuickFixes.factories.put(UNRESOLVED_REFERENCE_WRONG_RECEIVER, unresolvedReferenceFactory);
@@ -123,7 +123,7 @@ public class RemoveModifierFix extends JetIntentionAction<JetModifierListOwner>
return new JetSingleIntentionActionFactory() {
@Nullable
@Override
public JetIntentionAction<JetModifierListOwner> createAction(Diagnostic diagnostic) {
public JetIntentionAction<JetModifierListOwner> createAction(@NotNull Diagnostic diagnostic) {
JetModifierListOwner modifierListOwner = QuickFixUtil.getParentElementOfType(diagnostic, JetModifierListOwner.class);
if (modifierListOwner == null) return null;
PsiElement psiElement = diagnostic.getPsiElement();