From eedfac90dfaf2f715d331ce19ceb61f5b1af4b30 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Fri, 4 Mar 2016 18:49:42 +0100 Subject: [PATCH] [java] "Surround with try-with-resources" registered as surround template (IDEA-152348) --- .../impl/SurroundAutoCloseableAction.java | 58 ++++++++++++++++++- .../src/messages/CodeInsightBundle.properties | 1 + resources/src/META-INF/IdeaPlugin.xml | 3 +- 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/SurroundAutoCloseableAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/SurroundAutoCloseableAction.java index 542119679ec6..84f20fcef158 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/SurroundAutoCloseableAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/SurroundAutoCloseableAction.java @@ -16,6 +16,7 @@ package com.intellij.codeInsight.intention.impl; import com.intellij.codeInsight.CodeInsightBundle; +import com.intellij.codeInsight.CodeInsightUtil; import com.intellij.codeInsight.CodeInsightUtilCore; import com.intellij.codeInsight.FileModificationService; import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction; @@ -23,8 +24,11 @@ import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.codeInsight.lookup.LookupElementBuilder; import com.intellij.codeInsight.template.*; import com.intellij.lang.java.JavaLanguage; +import com.intellij.lang.surroundWith.SurroundDescriptor; +import com.intellij.lang.surroundWith.Surrounder; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.TextRange; import com.intellij.pom.java.LanguageLevel; import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleManager; @@ -40,6 +44,7 @@ import com.intellij.util.IncorrectOperationException; import com.intellij.util.ObjectUtils; import com.intellij.util.SmartList; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.stream.Stream; @@ -63,7 +68,7 @@ public class SurroundAutoCloseableAction extends PsiElementBaseIntentionAction { } } - return type != null && InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_LANG_AUTO_CLOSEABLE); + return type != null && rightType(type); } @Override @@ -126,6 +131,10 @@ public class SurroundAutoCloseableAction extends PsiElementBaseIntentionAction { return null; } + private static boolean rightType(PsiType type) { + return InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_LANG_AUTO_CLOSEABLE); + } + private static void processVariable(Project project, Editor editor, PsiLocalVariable variable) { PsiExpression initializer = ObjectUtils.assertNotNull(variable.getInitializer()); PsiElement declaration = variable.getParent(); @@ -243,7 +252,7 @@ public class SurroundAutoCloseableAction extends PsiElementBaseIntentionAction { type = initializer.getType(); String[] names = IntroduceVariableBase.getSuggestedName(type, initializer).names; PsiType[] types = Stream.of(new TypeSelectorManagerImpl(project, type, initializer, PsiExpression.EMPTY_ARRAY).getTypesForAll()) - .filter(t -> InheritanceUtil.isInheritor(t, CommonClassNames.JAVA_LANG_AUTO_CLOSEABLE)) + .filter(SurroundAutoCloseableAction::rightType) .toArray(PsiType[]::new); TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(var); builder.replaceElement(id, new NamesExpression(names)); @@ -287,4 +296,49 @@ public class SurroundAutoCloseableAction extends PsiElementBaseIntentionAction { return Stream.of(myNames).map(LookupElementBuilder::create).toArray(LookupElement[]::new); } } + + public static class Template implements SurroundDescriptor, Surrounder { + private Surrounder[] mySurrounders = {this}; + + @NotNull + @Override + public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) { + PsiExpression expr = CodeInsightUtil.findExpressionInRange(file, startOffset, endOffset); + if (expr == null) { + expr = findExpression(file.findElementAt(endOffset)); + } + return expr != null && rightType(expr.getType()) ? new PsiElement[]{expr} : PsiElement.EMPTY_ARRAY; + } + + @NotNull + @Override + public Surrounder[] getSurrounders() { + return mySurrounders; + } + + @Override + public boolean isExclusive() { + return false; + } + + @Override + public String getTemplateDescription() { + return CodeInsightBundle.message("intention.surround.with.ARM.block.template"); + } + + @Override + public boolean isApplicable(@NotNull PsiElement[] elements) { + return true; + } + + @Nullable + @Override + public TextRange surroundElements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement[] elements) { + if (elements.length == 1 && elements[0] instanceof PsiExpression) { + processExpression(project, editor, (PsiExpression)elements[0]); + } + + return null; + } + } } \ No newline at end of file diff --git a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties index 01eece58bb2a..e4e70c99fc4b 100644 --- a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties +++ b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties @@ -193,6 +193,7 @@ intention.replace.concatenation.with.formatted.output.text=Replace '+' with 'jav intention.color.chooser.dialog=Choose Color intention.convert.to.basic.latin=Convert to Basic Latin intention.surround.resource.with.ARM.block=Surround with try-with-resources block +intention.surround.with.ARM.block.template=try-with-resources dialog.create.field.from.parameter.title=Create Field dialog.create.field.from.parameter.already.exists.text=Use existing field ''{0}''? dialog.create.field.from.parameter.already.exists.title=Field Already Exists diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index 12dc68c04b4a..1818da28023f 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -1023,7 +1023,6 @@ Java/Strings - com.intellij.codeInsight.intention.impl.RemoveLiteralUnderscoresAction Java/Numbers @@ -1068,6 +1067,8 @@ implementationClass="com.intellij.codeInsight.generation.surroundWith.JavaExpressionSurroundDescriptor"/> +