Implement "if null" / "if not null" live templates

This commit is contained in:
Alexey Sedunov
2013-09-25 14:42:52 +04:00
parent a814f6a897
commit e859e02c8b
8 changed files with 105 additions and 8 deletions
+16 -1
View File
@@ -48,7 +48,22 @@
<option name="KOTLIN_STATEMENT" value="true" />
</context>
</template>
<template resource-bundle="org.jetbrains.jet.plugin.JetBundle" key="livetemplate.description.ifn"
name="ifn" toReformat="true" toShortenFQNames="true"
value="if ($VAR$ == null) {&#10;$END$&#10;}">
<variable alwaysStopAt="true" defaultValue="&quot;i&quot;" expression="kotlinSuggestVariableName()" name="VAR" />
<context>
<option name="KOTLIN_EXPRESSION" value="true" />
</context>
</template>
<template resource-bundle="org.jetbrains.jet.plugin.JetBundle" key="livetemplate.description.inn"
name="inn" toReformat="true" toShortenFQNames="true"
value="if ($VAR$ != null) {&#10;$END$&#10;}">
<variable alwaysStopAt="true" defaultValue="&quot;i&quot;" expression="kotlinSuggestVariableName()" name="VAR" />
<context>
<option name="KOTLIN_EXPRESSION" value="true" />
</context>
</template>
<template resource-bundle="org.jetbrains.jet.plugin.JetBundle" key="livetemplate.description.void"
name="void" toReformat="true" toShortenFQNames="true" value="fun $NAME$($PARAMS$) {&#10;$END$&#10;}">
<variable alwaysStopAt="true" defaultValue="" expression="" name="NAME" />
@@ -89,6 +89,8 @@ goto.super.class.chooser.title=Choose super class or interface
livetemplate.description.main=main() function
livetemplate.description.soutp=Prints function parameter names and values to System.out
livetemplate.description.iter=Iterate over elements of iterable
livetemplate.description.ifn=Inserts ''if null'' expression
livetemplate.description.inn=Inserts ''if not null'' expression
livetemplate.description.void=Function returning nothing
livetemplate.description.fun0=Function with no parameters
livetemplate.description.fun1=Function with one parameter
@@ -16,14 +16,14 @@
package org.jetbrains.jet.plugin.liveTemplates.macro;
import com.intellij.codeInsight.template.Expression;
import com.intellij.codeInsight.template.ExpressionContext;
import com.intellij.codeInsight.template.Macro;
import com.intellij.codeInsight.template.Result;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices;
import org.jetbrains.jet.plugin.JetBundle;
public class JetSuggestVariableNameMacro extends Macro {
public class JetSuggestVariableNameMacro extends BaseJetVariableMacro {
@Override
public String getName() {
return "kotlinSuggestVariableName";
@@ -35,7 +35,12 @@ public class JetSuggestVariableNameMacro extends Macro {
}
@Override
public Result calculateResult(@NotNull Expression[] params, ExpressionContext context) {
return null; //TODO
protected boolean isSuitable(
@NotNull VariableDescriptor variableDescriptor,
@NotNull JetScope scope,
@NotNull Project project,
ExpressionTypingServices callResolverContext
) {
return variableDescriptor.getType().isNullable();
}
}
+16
View File
@@ -0,0 +1,16 @@
val a: String = ""
val b: String? = ""
class MyClass {
val x: String = ""
val y: String? = ""
fun foo() {
val s: String = ""
val t: String? = ""
if (b == null) {
<caret>
}
}
}
+14
View File
@@ -0,0 +1,14 @@
val a: String = ""
val b: String? = ""
class MyClass {
val x: String = ""
val y: String? = ""
fun foo() {
val s: String = ""
val t: String? = ""
<caret>
}
}
+16
View File
@@ -0,0 +1,16 @@
val a: String = ""
val b: String? = ""
class MyClass {
val x: String = ""
val y: String? = ""
fun foo() {
val s: String = ""
val t: String? = ""
if (b != null) {
<caret>
}
}
}
+14
View File
@@ -0,0 +1,14 @@
val a: String = ""
val b: String? = ""
class MyClass {
val x: String = ""
val y: String? = ""
fun foo() {
val s: String = ""
val t: String? = ""
<caret>
}
}
@@ -198,7 +198,22 @@ public class LiveTemplatesTest extends LightCodeInsightFixtureTestCase {
checkAfter();
}
private void doTestIfnInn() {
start();
assertStringItems("b", "t", "y");
typeAndNextTab("b");
checkAfter();
}
public void testIfn() {
doTestIfnInn();
}
public void testInn() {
doTestIfnInn();
}
private void paremeterless() {
start();