Do not ignore empty value of template expressions

This commit is contained in:
Alexander Zolotov
2015-04-28 20:22:14 +03:00
parent 570bf3ef9e
commit dc3d750079
2 changed files with 77 additions and 10 deletions
@@ -14,7 +14,6 @@
* limitations under the License.
*/
package com.intellij.codeInsight.template
import com.intellij.JavaTestUtil
import com.intellij.codeInsight.CodeInsightSettings
import com.intellij.codeInsight.lookup.Lookup
@@ -38,7 +37,6 @@ import com.intellij.util.ui.UIUtil
import org.jetbrains.annotations.NotNull
import static com.intellij.codeInsight.template.Template.Property.USE_STATIC_IMPORT_IF_POSSIBLE
/**
* @author spleaner
*/
@@ -474,6 +472,8 @@ class Foo {
@Override
protected void invokeTestRunnable(@NotNull final Runnable runnable) throws Exception {
if (name in ["testNavigationActionsDontTerminateTemplate", "testTemplateWithEnd", "testDisappearingVar",
"test do replace macro value with empty result",
"test do not replace macro value with null result",
"test escape string characters in soutv", "test do not replace macro value with empty result"]) {
runnable.run();
return;
@@ -835,7 +835,7 @@ class Foo {
"""
}
public void "test do not replace macro value with empty result"() {
public void "test do not replace macro value with null result"() {
myFixture.configureByText "a.java", """\
class Foo {
{
@@ -868,6 +868,77 @@ class Foo {
}
"""
}
public void "test do replace macro value with empty result"() {
myFixture.configureByText "a.java", """\
class Foo {
{
<caret>
}
}
"""
final TemplateManager manager = TemplateManager.getInstance(getProject());
final Template template = manager.createTemplate("xxx", "user", '$VAR1$ $VAR2$');
template.addVariable("VAR1", "", "", true)
template.addVariable("VAR2", new MacroCallNode(new MyMirrorMacro("VAR1")), null, true)
((TemplateImpl)template).templateContext.setEnabled(contextType(JavaCodeContextType.class), true)
addTemplate(template, testRootDisposable)
writeCommand { startTemplate(template); }
myFixture.checkResult """\
class Foo {
{
<caret>
}
}
"""
writeCommand { myFixture.type '42' }
myFixture.checkResult """\
class Foo {
{
42<caret> 42
}
}
"""
writeCommand { myFixture.type '\b\b' }
myFixture.checkResult """\
class Foo {
{
<caret>
}
}
"""
}
private static class MyMirrorMacro extends Macro {
private final String myVariableName
MyMirrorMacro(String variableName) {
this.myVariableName = variableName
}
@Override
String getName() {
return "mirror"
}
@Override
String getPresentableName() {
return getName();
}
@Override
Result calculateResult(@NotNull Expression[] params, ExpressionContext context) {
def state = TemplateManagerImpl.getTemplateState(context.editor)
return state != null ? state.getVariableValue(myVariableName) : null
}
@Override
Result calculateQuickResult(@NotNull Expression[] params, ExpressionContext context) {
return calculateResult(params, context)
}
}
public void "test multicaret expanding with space"() {
myFixture.configureByText "a.java", """\
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-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.
@@ -742,10 +742,10 @@ public class TemplateState implements Disposable {
ExpressionContext context = createExpressionContext(start);
Result result = isQuick ? expressionNode.calculateQuickResult(context) : expressionNode.calculateResult(context);
if (isQuick && isEmptyResult(result, element) && !oldValue.isEmpty()) {
if (isQuick && result == null && !oldValue.isEmpty()) {
return;
}
if (isEmptyResult(result, element) && defaultValue != null) {
if (defaultValue != null && (result == null || result.equalsToText("", element))) {
result = defaultValue.calculateResult(context);
}
if (element != null) {
@@ -765,10 +765,6 @@ public class TemplateState implements Disposable {
}
}
private static boolean isEmptyResult(Result result, PsiElement context) {
return result == null || result.equalsToText("", context);
}
private void replaceString(String newValue, int start, int end, int segmentNumber) {
String oldText = myDocument.getCharsSequence().subSequence(start, end).toString();