[java] "Surround with try-with-resources" registered as surround template (IDEA-152348)

This commit is contained in:
Roman Shevchenko
2016-03-04 18:49:57 +01:00
parent 949b5a8e78
commit eedfac90df
3 changed files with 59 additions and 3 deletions
@@ -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;
}
}
}
@@ -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
+2 -1
View File
@@ -1023,7 +1023,6 @@
<category>Java/Strings</category>
</intentionAction>
<intentionAction>
<className>com.intellij.codeInsight.intention.impl.RemoveLiteralUnderscoresAction</className>
<category>Java/Numbers</category>
@@ -1068,6 +1067,8 @@
implementationClass="com.intellij.codeInsight.generation.surroundWith.JavaExpressionSurroundDescriptor"/>
<lang.surroundDescriptor language="JAVA"
implementationClass="com.intellij.codeInsight.generation.surroundWith.JavaStatementsSurroundDescriptor"/>
<lang.surroundDescriptor language="JAVA"
implementationClass="com.intellij.codeInsight.intention.impl.SurroundAutoCloseableAction$Template"/>
<lang.unwrapDescriptor language="JAVA" implementationClass="com.intellij.codeInsight.unwrap.JavaUnwrapDescriptor"/>