mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
java postfix templates: new castvar template IDEA-191025
This commit is contained in:
+83
@@ -0,0 +1,83 @@
|
||||
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.codeInsight.template.postfix.templates;
|
||||
|
||||
import com.intellij.codeInsight.guess.GuessManager;
|
||||
import com.intellij.codeInsight.lookup.LookupElement;
|
||||
import com.intellij.codeInsight.lookup.PsiTypeLookupItem;
|
||||
import com.intellij.codeInsight.template.*;
|
||||
import com.intellij.codeInsight.template.impl.MacroCallNode;
|
||||
import com.intellij.codeInsight.template.macro.SuggestVariableNameMacro;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiType;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils.IS_NON_VOID;
|
||||
import static com.intellij.codeInsight.template.postfix.util.JavaPostfixTemplatesUtils.selectorTopmost;
|
||||
|
||||
public class CastVarPostfixTemplate extends StringBasedPostfixTemplate {
|
||||
private static final String TYPE_VAR = "typeVar";
|
||||
private static final String VAR_NAME = "varName";
|
||||
|
||||
public CastVarPostfixTemplate() {
|
||||
super("castvar", "T name = (T)expr", selectorTopmost(IS_NON_VOID));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getTemplateString(@NotNull PsiElement element) {
|
||||
PsiFile file = element.getContainingFile();
|
||||
boolean isFinal = JavaCodeStyleSettings.getInstance(file).GENERATE_FINAL_LOCALS;
|
||||
|
||||
return (isFinal ? "final " : "") + "$" + TYPE_VAR + "$ $" + VAR_NAME + "$ = ($" + TYPE_VAR + "$)$expr$;$END$";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVariables(@NotNull Template template, @NotNull PsiElement element) {
|
||||
super.setVariables(template, element);
|
||||
if (element instanceof PsiExpression) {
|
||||
PsiType[] types = GuessManager.getInstance(element.getProject()).guessTypeToCast((PsiExpression)element);
|
||||
fill(template, types, element);
|
||||
}
|
||||
else {
|
||||
template.addVariable(TYPE_VAR, null, null, true, false);
|
||||
}
|
||||
|
||||
MacroCallNode nameMacro = new MacroCallNode(new SuggestVariableNameMacro());
|
||||
template.addVariable(VAR_NAME, nameMacro, nameMacro, true);
|
||||
}
|
||||
|
||||
private static void fill(@NotNull Template template, @NotNull PsiType[] suggestedTypes, @NotNull PsiElement context) {
|
||||
Set<LookupElement> itemSet = new LinkedHashSet<>();
|
||||
for (PsiType type : suggestedTypes) {
|
||||
itemSet.add(PsiTypeLookupItem.createLookupItem(type, null));
|
||||
}
|
||||
final LookupElement[] lookupItems = itemSet.toArray(LookupElement.EMPTY_ARRAY);
|
||||
final Result result = suggestedTypes.length > 0 ? new PsiTypeResult(suggestedTypes[0], context.getProject()) : null;
|
||||
|
||||
Expression expr = new Expression() {
|
||||
@Override
|
||||
public LookupElement[] calculateLookupItems(ExpressionContext context) {
|
||||
return lookupItems.length > 1 ? lookupItems : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result calculateResult(ExpressionContext context) {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result calculateQuickResult(ExpressionContext context) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
template.addVariable(TYPE_VAR, expr, expr, true);
|
||||
}
|
||||
}
|
||||
+1
@@ -48,6 +48,7 @@ public class JavaPostfixTemplateProvider implements PostfixTemplateProvider {
|
||||
new ArgumentPostfixTemplate(this),
|
||||
|
||||
new CastExpressionPostfixTemplate(),
|
||||
new CastVarPostfixTemplate(),
|
||||
new ElseStatementPostfixTemplate(),
|
||||
new IfStatementPostfixTemplate(),
|
||||
new InstanceofExpressionPostfixTemplate(),
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
void m(Object o) {
|
||||
if (o instanceof String) {
|
||||
String o = (String)o;<spot></spot>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
void m(Object o) {
|
||||
if (o instanceof String) {
|
||||
<spot>o</spot>$key
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
|
||||
Creates a new variable for the expression with type casting.
|
||||
</body>
|
||||
</html>
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
public class Foo {
|
||||
void m(Object o) {
|
||||
if (o instanceof Boolean) {
|
||||
o.castvar<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
public class Foo {
|
||||
void m(Object o) {
|
||||
if (o instanceof Boolean) {
|
||||
final Boolean aBoolean = (Boolean) o;
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
public class Foo {
|
||||
void m(Object o) {
|
||||
if (o instanceof Boolean) {
|
||||
o.castvar<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
public class Foo {
|
||||
void m(Object o) {
|
||||
if (o instanceof Boolean) {
|
||||
Boolean aBoolean = (Boolean) o;
|
||||
}
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.java.codeInsight.template.postfix.templates;
|
||||
|
||||
import com.intellij.application.options.CodeStyle;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class CastVarPostfixTemplateTest extends PostfixTemplateTestCase {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getSuffix() {
|
||||
return "castvar";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
try {
|
||||
CodeStyle.dropTemporarySettings(myFixture.getProject());
|
||||
}
|
||||
finally {
|
||||
super.tearDown();
|
||||
}
|
||||
}
|
||||
|
||||
public void testSingleExpression() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testFinalSingleExpression() {
|
||||
CodeStyleSettings codeStyleSettings = CodeStyle.getSettings(myFixture.getProject());
|
||||
CodeStyleSettings clone = codeStyleSettings.clone();
|
||||
CodeStyle.setTemporarySettings(myFixture.getProject(), clone);
|
||||
|
||||
JavaCodeStyleSettings customSettings = clone.getCustomSettings(JavaCodeStyleSettings.class);
|
||||
customSettings.GENERATE_FINAL_LOCALS = true;
|
||||
|
||||
doTest();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user