From 438a3e28ae1bb353033fba0ac9e73c92cdd26dd8 Mon Sep 17 00:00:00 2001 From: Andrey Starovoyt Date: Fri, 23 May 2014 17:49:50 +0400 Subject: [PATCH] final in "foreach" loop --- .../templates/ForeachPostfixTemplate.java | 5 ++++- .../postfix/templates/for/finalLocals.java | 6 +++++ .../templates/for/finalLocals_after.java | 8 +++++++ .../templates/ForeachTemplateTest.java | 22 +++++++++++++++---- 4 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/for/finalLocals.java create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/for/finalLocals_after.java diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForeachPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForeachPostfixTemplate.java index cbc1cb458bbd..3e702a4493ec 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForeachPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/ForeachPostfixTemplate.java @@ -24,6 +24,7 @@ import com.intellij.codeInsight.template.macro.IterableComponentTypeMacro; import com.intellij.codeInsight.template.macro.SuggestVariableNameMacro; import com.intellij.openapi.editor.Editor; import com.intellij.psi.PsiElement; +import com.intellij.psi.codeStyle.JavaCodeStyleSettingsFacade; import static com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils.IS_ITERABLE_OR_ARRAY; import static com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils.JAVA_PSI_INFO; @@ -35,7 +36,9 @@ public class ForeachPostfixTemplate extends StringBasedPostfixTemplate { @Override public void expandWithTemplateManager(TemplateManager manager, PsiElement expression, Editor editor) { - Template template = manager.createTemplate("", "", "for ($type$ $name$ : $variable$) {\n $END$\n}"); + + String finalPart = JavaCodeStyleSettingsFacade.getInstance(expression.getProject()).isGenerateFinalLocals() ? "final " : ""; + Template template = manager.createTemplate("", "", "for (" + finalPart + "$type$ $name$ : $variable$) {\n $END$\n}"); MacroCallNode type = new MacroCallNode(new IterableComponentTypeMacro()); MacroCallNode name = new MacroCallNode(new SuggestVariableNameMacro()); String variable = "variable"; diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/for/finalLocals.java b/java/java-tests/testData/codeInsight/template/postfix/templates/for/finalLocals.java new file mode 100644 index 000000000000..ee605c4ea7db --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/for/finalLocals.java @@ -0,0 +1,6 @@ +public class Foo { + void m() { + Object[] xs = new Object[]{1, 2, 3}; + xs.for + } +} diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/for/finalLocals_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/for/finalLocals_after.java new file mode 100644 index 000000000000..75ed5681adcf --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/for/finalLocals_after.java @@ -0,0 +1,8 @@ +public class Foo { + void m() { + Object[] xs = new Object[]{1, 2, 3}; + for (final Object x : xs) { + + } + } +} diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/ForeachTemplateTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/ForeachTemplateTest.java index 0e4809d33cb1..bf8c75555618 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/ForeachTemplateTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/postfix/templates/ForeachTemplateTest.java @@ -15,9 +15,17 @@ */ package com.intellij.codeInsight.template.postfix.templates; +import com.intellij.psi.codeStyle.CodeStyleSettings; +import com.intellij.psi.codeStyle.CodeStyleSettingsManager; import org.jetbrains.annotations.NotNull; public class ForeachTemplateTest extends PostfixTemplateTestCase { + @NotNull + @Override + protected String getSuffix() { + return "for"; + } + public void testInts() { doTest(); } @@ -26,9 +34,15 @@ public class ForeachTemplateTest extends PostfixTemplateTestCase { doTest(); } - @NotNull - @Override - protected String getSuffix() { - return "for"; + public void testFinalLocals() { + CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject()); + boolean oldGenerateFinalLocals = settings.GENERATE_FINAL_LOCALS; + try { + settings.GENERATE_FINAL_LOCALS = true; + doTest(); + } + finally { + settings.GENERATE_FINAL_LOCALS = oldGenerateFinalLocals; + } } }