From f73c4b296ebe48b3944ecab1ef75dd6458ede94c Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 5 May 2015 17:15:56 +0300 Subject: [PATCH] Converted to Kotlin --- .../intentions/JetTypeLookupExpression.java | 100 ------------------ .../intentions/JetTypeLookupExpression.kt | 64 +++++++++++ .../intentions/DoubleBangToIfThenIntention.kt | 4 +- 3 files changed, 66 insertions(+), 102 deletions(-) delete mode 100644 idea/src/org/jetbrains/kotlin/idea/intentions/JetTypeLookupExpression.java create mode 100644 idea/src/org/jetbrains/kotlin/idea/intentions/JetTypeLookupExpression.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/JetTypeLookupExpression.java b/idea/src/org/jetbrains/kotlin/idea/intentions/JetTypeLookupExpression.java deleted file mode 100644 index f678eaa1f7a..00000000000 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/JetTypeLookupExpression.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * 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.intentions; - -import com.intellij.codeInsight.completion.InsertHandler; -import com.intellij.codeInsight.completion.InsertionContext; -import com.intellij.codeInsight.lookup.LookupElement; -import com.intellij.codeInsight.lookup.LookupElementBuilder; -import com.intellij.codeInsight.template.Expression; -import com.intellij.codeInsight.template.ExpressionContext; -import com.intellij.codeInsight.template.Result; -import com.intellij.codeInsight.template.TextResult; -import com.intellij.codeInsight.template.impl.TemplateManagerImpl; -import com.intellij.codeInsight.template.impl.TemplateState; -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.util.TextRange; -import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; - -import java.util.Iterator; -import java.util.List; - - -public abstract class JetTypeLookupExpression extends Expression { - - protected final LookupElement[] lookupItems; - - protected final T defaultItem; - - private final String advertisementText; - - public JetTypeLookupExpression( - List lookupItems, - T defaultItem, - String advertisement - ) { - this.advertisementText = advertisement; - this.defaultItem = defaultItem; - this.lookupItems = initLookupItems(lookupItems); - - } - - private LookupElement[] initLookupItems(List lookupItems) { - LookupElement[] lookupElements = new LookupElement[lookupItems.size()]; - Iterator iterator = lookupItems.iterator(); - for (int i = 0; i < lookupElements.length; i++) { - T suggestion = iterator.next(); - lookupElements[i] = LookupElementBuilder.create(suggestion, getLookupString(suggestion)).withInsertHandler( - new InsertHandler() { - @Override - public void handleInsert(InsertionContext context, LookupElement item) { - Editor topLevelEditor = InjectedLanguageUtil.getTopLevelEditor(context.getEditor()); - TemplateState templateState = TemplateManagerImpl.getTemplateState(topLevelEditor); - if (templateState != null) { - TextRange range = templateState.getCurrentVariableRange(); - if (range != null) { - topLevelEditor.getDocument() - .replaceString(range.getStartOffset(), range.getEndOffset(), getResult((T) item.getObject())); - } - } - } - }); - } - return lookupElements; - } - - public LookupElement[] calculateLookupItems(ExpressionContext context) { - return lookupItems.length > 1 ? lookupItems : null; - } - - public Result calculateQuickResult(ExpressionContext context) { - return calculateResult(context); - } - - public Result calculateResult(ExpressionContext context) { - return new TextResult(getLookupString(defaultItem)); - } - - @Override - public String getAdvertisingText() { - return advertisementText; - } - - protected abstract String getLookupString(T element); - - protected abstract String getResult(T element); -} diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/JetTypeLookupExpression.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/JetTypeLookupExpression.kt new file mode 100644 index 00000000000..96a741b413f --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/JetTypeLookupExpression.kt @@ -0,0 +1,64 @@ +/* + * 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.intentions + +import com.intellij.codeInsight.completion.InsertHandler +import com.intellij.codeInsight.completion.InsertionContext +import com.intellij.codeInsight.lookup.LookupElement +import com.intellij.codeInsight.lookup.LookupElementBuilder +import com.intellij.codeInsight.template.Expression +import com.intellij.codeInsight.template.ExpressionContext +import com.intellij.codeInsight.template.Result +import com.intellij.codeInsight.template.TextResult +import com.intellij.codeInsight.template.impl.TemplateManagerImpl +import com.intellij.codeInsight.template.impl.TemplateState +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.util.TextRange +import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil + +public abstract class JetTypeLookupExpression( + lookupItems: List, + protected val defaultItem: T, + private val advertisementText: String +) : Expression() { + + protected abstract fun getLookupString(element: T): String + protected abstract fun getResult(element: T): String + + protected val lookupItems: Array = lookupItems.map { suggestion -> + LookupElementBuilder.create(suggestion, getLookupString(suggestion)).withInsertHandler(object : InsertHandler { + override fun handleInsert(context: InsertionContext, item: LookupElement) { + val topLevelEditor = InjectedLanguageUtil.getTopLevelEditor(context.getEditor()) + val templateState = TemplateManagerImpl.getTemplateState(topLevelEditor) + if (templateState != null) { + val range = templateState.getCurrentVariableRange() + if (range != null) { + topLevelEditor.getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), getResult(item.getObject() as T)) + } + } + } + }) + }.toTypedArray() + + override fun calculateLookupItems(context: ExpressionContext) = if (lookupItems.size() > 1) lookupItems else null + + override fun calculateQuickResult(context: ExpressionContext) = calculateResult(context) + + override fun calculateResult(context: ExpressionContext) = TextResult(getLookupString(defaultItem)) + + override fun getAdvertisingText() = advertisementText +} diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/DoubleBangToIfThenIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/DoubleBangToIfThenIntention.kt index 7e52c6b3dd1..916e3040630 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/DoubleBangToIfThenIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/DoubleBangToIfThenIntention.kt @@ -62,8 +62,8 @@ public class DoubleBangToIfThenIntention : JetSelfTargetingOffsetIndependentInte object: JetTypeLookupExpression(listOf(nullPtrExceptionText, kotlinNullPtrExceptionText), nullPtrExceptionText, JetBundle.message("double.bang.to.if.then.choose.exception")) { - override fun getLookupString(element: String?) = element - override fun getResult(element: String?) = element + override fun getLookupString(element: String) = element + override fun getResult(element: String) = element } val project = element.getProject()