diff --git a/java/java-impl/src/com/intellij/codeInsight/template/macro/ExpressionTypeMacro.java b/java/java-impl/src/com/intellij/codeInsight/template/macro/ExpressionTypeMacro.java new file mode 100644 index 000000000000..586420da1077 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/template/macro/ExpressionTypeMacro.java @@ -0,0 +1,54 @@ +/* + * 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. + * 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 com.intellij.codeInsight.template.macro; + +import com.intellij.codeInsight.CodeInsightBundle; +import com.intellij.codeInsight.template.*; +import com.intellij.psi.PsiExpression; +import com.intellij.psi.PsiType; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class ExpressionTypeMacro extends Macro { + @Override + public String getName() { + return "expressionType"; + } + + @Override + public String getPresentableName() { + return CodeInsightBundle.message("macro.expression.type"); + } + + @Nullable + @Override + public Result calculateResult(@NotNull Expression[] params, ExpressionContext context) { + if (params.length == 1) { + Result result = params[0].calculateResult(context); + if (result != null) { + PsiExpression expression = MacroUtil.resultToPsiExpression(result, context); + if (expression != null) { + PsiType type = expression.getType(); + if (type != null) { + return new PsiTypeResult(type, context.getProject()); + } + } + } + } + + return null; + } +} diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/template/SurroundWithTemplateTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/template/SurroundWithTemplateTest.groovy new file mode 100644 index 000000000000..e8c51544388e --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/SurroundWithTemplateTest.groovy @@ -0,0 +1,56 @@ +/* + * 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. + * 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 com.intellij.codeInsight.template + +import com.intellij.codeInsight.template.impl.InvokeTemplateAction +import com.intellij.codeInsight.template.impl.SurroundWithTemplateHandler +import com.intellij.openapi.actionSystem.DefaultActionGroup +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase + +class SurroundWithTemplateTest extends LightCodeInsightFixtureTestCase { + public void testSurroundWithTryWithResources() { + myFixture.configureByText "C.java", """\ + import java.io.*; + class C { + void m() { + new FileReader("/dev/null") + } + }""".stripIndent() + + invokeTemplate("TR") + + myFixture.checkResult """\ + import java.io.*; + class C { + void m() { + try (FileReader fileReader = new FileReader("/dev/null")) { + + } + + } + }""".stripIndent() + } + + private void invokeTemplate(String template) { + DefaultActionGroup group = SurroundWithTemplateHandler.createActionGroup(project, editor, file) + assertNotNull(group) + InvokeTemplateAction action = group.childActionsOrStubs.find { + it instanceof InvokeTemplateAction && (it as InvokeTemplateAction).template.key == template + } as InvokeTemplateAction + assertNotNull(action) + action.perform() + } +} diff --git a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties index e7e912f14560..b3d50ee9d245 100644 --- a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties +++ b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties @@ -365,6 +365,7 @@ macro.enum=enum(...) macro.expected.type=expectedType() macro.groovy.script=groovyScript("groovy code") macro.guess.element.type.of.container=guessElementType(Container) +macro.expression.type=expressionType(Expression) macro.iterable.component.type=iterableComponentType(ArrayOrIterable) macro.iterable.variable=iterableVariable() macro.linenumber=lineNumber() @@ -421,6 +422,7 @@ livetemplate.description.surround.cdata.in.xmlorhtmlorjsp=Surround with CDATA se livetemplate.description.surround.with.callable=Surround with Callable livetemplate.description.surround.with.read.lock=Surround with ReadWriteLock.readLock livetemplate.description.surround.with.write.lock=Surround with ReadWriteLock.writeLock +livetemplate.description.surround.with.ARM=Surround with try-with-resources quickfix.add.variable.text=Initialize variable ''{0}'' quickfix.add.variable.family.name=Initialize variable inspection.i18n.quickfix.annotate.as=Annotate as @{0} diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index 75221fee1613..f7891fadfdf4 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -1194,6 +1194,7 @@ + diff --git a/resources/src/liveTemplates/surround.xml b/resources/src/liveTemplates/surround.xml index f36ed502c448..fa874bed0e9d 100644 --- a/resources/src/liveTemplates/surround.xml +++ b/resources/src/liveTemplates/surround.xml @@ -1,5 +1,6 @@ + + + + -