From 001c5d6e3e6410790386f777e3f37b48411da175 Mon Sep 17 00:00:00 2001 From: Nicolay Mitropolsky Date: Fri, 6 Apr 2018 20:35:16 +0300 Subject: [PATCH] 182: `PsiReferenceBase` nullability change fixes --- .../KotlinAddOrderEntryActionFactory.kt.182 | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 idea/src/org/jetbrains/kotlin/idea/quickfix/KotlinAddOrderEntryActionFactory.kt.182 diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/KotlinAddOrderEntryActionFactory.kt.182 b/idea/src/org/jetbrains/kotlin/idea/quickfix/KotlinAddOrderEntryActionFactory.kt.182 new file mode 100644 index 00000000000..922f93ea6ba --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/KotlinAddOrderEntryActionFactory.kt.182 @@ -0,0 +1,67 @@ +/* + * Copyright 2010-2015 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.kotlin.idea.quickfix + +import com.intellij.codeInsight.daemon.impl.quickfix.OrderEntryFix +import com.intellij.codeInsight.daemon.impl.quickfix.QuickFixActionRegistrarImpl +import com.intellij.codeInsight.intention.IntentionAction +import com.intellij.openapi.util.TextRange +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiReference +import com.intellij.psi.PsiReferenceBase +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.idea.references.KtSimpleNameReference.ShorteningMode +import org.jetbrains.kotlin.idea.references.mainReference +import org.jetbrains.kotlin.idea.util.application.executeWriteCommand +import org.jetbrains.kotlin.idea.util.runWhenSmart +import org.jetbrains.kotlin.psi.KtElement +import org.jetbrains.kotlin.psi.KtSimpleNameExpression +import org.jetbrains.kotlin.psi.psiUtil.getQualifiedElement +import org.jetbrains.kotlin.psi.psiUtil.startOffset + +object KotlinAddOrderEntryActionFactory : KotlinIntentionActionsFactory() { + override fun doCreateActions(diagnostic: Diagnostic): List { + val simpleExpression = diagnostic.psiElement as? KtSimpleNameExpression ?: return emptyList() + val refElement = simpleExpression.getQualifiedElement() + + val reference = object: PsiReferenceBase(refElement) { + override fun resolve() = null + + override fun getVariants() = PsiReference.EMPTY_ARRAY + + override fun getRangeInElement(): TextRange { + val offset = simpleExpression.startOffset - refElement.startOffset + return TextRange(offset, offset + simpleExpression.textLength) + } + + override fun getCanonicalText() = refElement.text + + override fun bindToElement(element: PsiElement): PsiElement { + val project = element.project + project.runWhenSmart { + project.executeWriteCommand("") { + simpleExpression.mainReference.bindToElement(element, ShorteningMode.FORCED_SHORTENING) + } + } + return element + } + } + + @Suppress("UNCHECKED_CAST") + return OrderEntryFix.registerFixes(QuickFixActionRegistrarImpl(null), reference) as List? ?: emptyList() + } +}