diff --git a/idea/resources/intentionDescriptions/SpecifyTypeExplicitlyAction/after.kt.template b/idea/resources/intentionDescriptions/SpecifyTypeExplicitlyAction/after.kt.template new file mode 100644 index 00000000000..d59bae419ce --- /dev/null +++ b/idea/resources/intentionDescriptions/SpecifyTypeExplicitlyAction/after.kt.template @@ -0,0 +1,3 @@ +fun main() { + val a : Array = array(1, 2, 3) +} \ No newline at end of file diff --git a/idea/resources/intentionDescriptions/SpecifyTypeExplicitlyAction/before.kt.template b/idea/resources/intentionDescriptions/SpecifyTypeExplicitlyAction/before.kt.template new file mode 100644 index 00000000000..747b9a65230 --- /dev/null +++ b/idea/resources/intentionDescriptions/SpecifyTypeExplicitlyAction/before.kt.template @@ -0,0 +1,3 @@ +fun main() { + val a = array(1, 2, 3) +} \ No newline at end of file diff --git a/idea/resources/intentionDescriptions/SpecifyTypeExplicitlyAction/description.html b/idea/resources/intentionDescriptions/SpecifyTypeExplicitlyAction/description.html new file mode 100644 index 00000000000..60e767f56aa --- /dev/null +++ b/idea/resources/intentionDescriptions/SpecifyTypeExplicitlyAction/description.html @@ -0,0 +1,5 @@ + + +This intention adds or removes explicit type specification for local values, variables and properties. + + \ No newline at end of file diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index ace5239fa46..2abe60e579b 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -174,6 +174,10 @@ icon="/org/jetbrains/jet/plugin/icons/kotlin13x13.png" /> + + org.jetbrains.jet.plugin.intentions.SpecifyTypeExplicitlyAction + Kotlin + diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index cf854a3e94c..d949b7cec59 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -29,6 +29,9 @@ implement.members=Implement members new.kotlin.file.action=Kotlin File import.fix=Import imports.chooser.title=Imports +specify.type.explicitly.action.family.name=Specify Type Explicitly +specify.type.explicitly.add.action.name=Specify Type Explicitly +specify.type.explicitly.remove.action.name=Remove Explicitly Specified Type livetemplate.description.main=main() function livetemplate.description.soutp=Prints function parameter names and values to System.out diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/SpecifyTypeExplicitlyAction.java b/idea/src/org/jetbrains/jet/plugin/intentions/SpecifyTypeExplicitlyAction.java new file mode 100644 index 00000000000..df30eab6263 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/intentions/SpecifyTypeExplicitlyAction.java @@ -0,0 +1,88 @@ +/* + * Copyright 2010-2012 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.intentions; + +import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiElement; +import com.intellij.psi.impl.source.tree.LeafPsiElement; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; +import org.jetbrains.jet.lang.descriptors.VariableDescriptor; +import org.jetbrains.jet.lang.psi.JetFile; +import org.jetbrains.jet.lang.psi.JetProperty; +import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.jet.lang.types.ErrorUtils; +import org.jetbrains.jet.lang.types.JetType; +import org.jetbrains.jet.lexer.JetTokens; +import org.jetbrains.jet.plugin.JetBundle; +import org.jetbrains.jet.plugin.project.AnalyzeSingleFileUtil; +import org.jetbrains.jet.plugin.refactoring.introduceVariable.JetChangePropertyActions; + +/** + * @author Evgeny Gerashchenko + * @since 4/20/12 + */ +public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction { + private JetType targetType; + + @NotNull + @Override + public String getText() { + return targetType == null ? JetBundle.message("specify.type.explicitly.remove.action.name") : JetBundle.message("specify.type.explicitly.add.action.name"); + } + + @NotNull + @Override + public String getFamilyName() { + return JetBundle.message("specify.type.explicitly.action.family.name"); + } + + @Override + public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) { + JetProperty property = (JetProperty)element.getParent(); + if (targetType == null) { + JetChangePropertyActions.removeTypeAnnotation(project, property); + } else { + JetChangePropertyActions.addTypeAnnotation(project, property, targetType); + } + } + + @Override + public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) { + if (!(element instanceof LeafPsiElement) || ((LeafPsiElement)element).getElementType() != JetTokens.IDENTIFIER + || !(element.getParent() instanceof JetProperty)) { + return false; + } + + JetProperty property = (JetProperty)element.getParent(); + boolean hasTypeSpecified = property.getPropertyTypeRef() != null; + if (hasTypeSpecified) { + targetType = null; + } else { + BindingContext bindingContext = AnalyzeSingleFileUtil.getContextForSingleFile((JetFile)element.getContainingFile()); + DeclarationDescriptor descriptor = bindingContext.get(BindingContext.DECLARATION_TO_DESCRIPTOR, property); + assert descriptor instanceof VariableDescriptor; + targetType = ((VariableDescriptor)descriptor).getType(); + if (ErrorUtils.isErrorType(targetType)) { + return false; + } + } + return true; + } +}