From dc3d750079ba90af1a8b224cf641a1d19d4add84 Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Tue, 28 Apr 2015 19:13:33 +0300 Subject: [PATCH] Do not ignore empty value of template expressions --- .../template/LiveTemplateTest.groovy | 77 ++++++++++++++++++- .../template/impl/TemplateState.java | 10 +-- 2 files changed, 77 insertions(+), 10 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy index 80ccbebd5fc8..04b2612b1279 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy @@ -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 { + { + + } +} +""" + 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 { + { + + } +} +""" + writeCommand { myFixture.type '42' } + myFixture.checkResult """\ +class Foo { + { + 42 42 + } +} +""" + + writeCommand { myFixture.type '\b\b' } + myFixture.checkResult """\ +class Foo { + { + + } +} +""" + } + + 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", """\ diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.java index 99a064368e30..cf45fa82c311 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.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();