Added intention which adds or removes explicit type specification for property or variable.

#KT-1427 fixed
This commit is contained in:
Evgeny Gerashchenko
2012-04-21 01:09:05 +04:00
parent 8c5c9311fb
commit 32605248c5
6 changed files with 106 additions and 0 deletions
@@ -0,0 +1,3 @@
fun main() {
val a <spot>: Array<Int></spot> = array(1, 2, 3)
}
@@ -0,0 +1,3 @@
fun main() {
val <spot>a</spot> = array(1, 2, 3)
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention adds or removes explicit type specification for local values, variables and properties.
</body>
</html>
+4
View File
@@ -174,6 +174,10 @@
icon="/org/jetbrains/jet/plugin/icons/kotlin13x13.png"
/>
<intentionAction>
<className>org.jetbrains.jet.plugin.intentions.SpecifyTypeExplicitlyAction</className>
<category>Kotlin</category>
</intentionAction>
</extensions>
</idea-plugin>
@@ -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
@@ -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;
}
}