diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/AddModifierFix.java b/idea/src/org/jetbrains/kotlin/idea/quickfix/AddModifierFix.java deleted file mode 100644 index 2e188a53488..00000000000 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/AddModifierFix.java +++ /dev/null @@ -1,106 +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.quickfix; - -import com.intellij.codeInsight.intention.IntentionAction; -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiNameIdentifierOwner; -import com.intellij.util.IncorrectOperationException; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.diagnostics.Diagnostic; -import org.jetbrains.kotlin.idea.JetBundle; -import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil; -import org.jetbrains.kotlin.lexer.KtModifierKeywordToken; -import org.jetbrains.kotlin.lexer.KtTokens; -import org.jetbrains.kotlin.psi.KtFile; -import org.jetbrains.kotlin.psi.KtModifierListOwner; -import org.jetbrains.kotlin.psi.KtObjectDeclaration; -import org.jetbrains.kotlin.psi.KtPropertyAccessor; - -import static org.jetbrains.kotlin.lexer.KtTokens.ABSTRACT_KEYWORD; - -public class AddModifierFix extends KotlinQuickFixAction { - @NotNull private final KtModifierKeywordToken modifier; - - public AddModifierFix(@NotNull KtModifierListOwner element, @NotNull KtModifierKeywordToken modifier) { - super(element); - this.modifier = modifier; - } - - @NotNull - /*package*/ static String getElementName(@NotNull KtModifierListOwner modifierListOwner) { - String name = null; - if (modifierListOwner instanceof PsiNameIdentifierOwner) { - PsiElement nameIdentifier = ((PsiNameIdentifierOwner) modifierListOwner).getNameIdentifier(); - if (nameIdentifier != null) { - name = nameIdentifier.getText(); - } - } - else if (modifierListOwner instanceof KtPropertyAccessor) { - name = ((KtPropertyAccessor) modifierListOwner).getNamePlaceholder().getText(); - } - if (name == null) { - name = modifierListOwner.getText(); - } - return "'" + name + "'"; - } - - @NotNull - @Override - public String getText() { - if (modifier == ABSTRACT_KEYWORD || modifier == KtTokens.OPEN_KEYWORD) { - return JetBundle.message("make.element.modifier", getElementName(getElement()), modifier.getValue()); - } - return JetBundle.message("add.modifier", modifier.getValue()); - } - - @NotNull - @Override - public String getFamilyName() { - return JetBundle.message("add.modifier.family"); - } - - @Override - public void invoke(@NotNull Project project, Editor editor, @NotNull KtFile file) throws IncorrectOperationException { - getElement().addModifier(modifier); - } - - @Override - public boolean startInWriteAction() { - return true; - } - - public static JetSingleIntentionActionFactory createFactory(final KtModifierKeywordToken modifier, final Class modifierOwnerClass) { - return new JetSingleIntentionActionFactory() { - @Override - public IntentionAction createAction(@NotNull Diagnostic diagnostic) { - KtModifierListOwner modifierListOwner = QuickFixUtil.getParentElementOfType(diagnostic, modifierOwnerClass); - if (modifierListOwner == null) return null; - - if (modifier == KtTokens.ABSTRACT_KEYWORD && modifierListOwner instanceof KtObjectDeclaration) return null; - - return new AddModifierFix(modifierListOwner, modifier); - } - }; - } - - public static JetSingleIntentionActionFactory createFactory(KtModifierKeywordToken modifier) { - return createFactory(modifier, KtModifierListOwner.class); - } -} diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/AddModifierFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/AddModifierFix.kt new file mode 100644 index 00000000000..a7eca1ad4ec --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/AddModifierFix.kt @@ -0,0 +1,89 @@ +/* + * 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.intention.IntentionAction +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import com.intellij.psi.PsiNameIdentifierOwner +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.idea.JetBundle +import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil +import org.jetbrains.kotlin.lexer.KtModifierKeywordToken +import org.jetbrains.kotlin.lexer.KtTokens +import org.jetbrains.kotlin.lexer.KtTokens.ABSTRACT_KEYWORD +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.psi.KtModifierListOwner +import org.jetbrains.kotlin.psi.KtObjectDeclaration +import org.jetbrains.kotlin.psi.KtPropertyAccessor + +open class AddModifierFix( + element: KtModifierListOwner, + private val modifier: KtModifierKeywordToken +) : KotlinQuickFixAction(element) { + + override fun getText(): String { + if (modifier == ABSTRACT_KEYWORD || modifier == KtTokens.OPEN_KEYWORD) { + return JetBundle.message("make.element.modifier", getElementName(element), modifier.value) + } + return JetBundle.message("add.modifier", modifier.value) + } + + override fun getFamilyName() = JetBundle.message("add.modifier.family") + + public override operator fun invoke(project: Project, editor: Editor?, file: KtFile) { + element.addModifier(modifier) + } + + override fun startInWriteAction() = true + + companion object { + fun getElementName(modifierListOwner: KtModifierListOwner): String { + var name: String? = null + if (modifierListOwner is PsiNameIdentifierOwner) { + val nameIdentifier = modifierListOwner.nameIdentifier + if (nameIdentifier != null) { + name = nameIdentifier.text + } + } + else if (modifierListOwner is KtPropertyAccessor) { + name = modifierListOwner.namePlaceholder.text + } + if (name == null) { + name = modifierListOwner.text + } + return "'$name'" + } + + fun createFactory(modifier: KtModifierKeywordToken): JetSingleIntentionActionFactory { + return createFactory(modifier, KtModifierListOwner::class.java) + } + + fun createFactory(modifier: KtModifierKeywordToken, modifierOwnerClass: Class): JetSingleIntentionActionFactory { + return object : JetSingleIntentionActionFactory() { + public override fun createAction(diagnostic: Diagnostic): IntentionAction? { + val modifierListOwner = QuickFixUtil.getParentElementOfType(diagnostic, modifierOwnerClass) ?: return null + + if (modifier == KtTokens.ABSTRACT_KEYWORD && modifierListOwner is KtObjectDeclaration && modifierListOwner.isObjectLiteral()) + return null + + return AddModifierFix(modifierListOwner, modifier) + } + } + } + } +}