mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-121693 Postfix completion for System.out.println()
IDEA-122459 Postfix completion: template for String.format
This commit is contained in:
+43
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2000-2014 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.postfix.templates;
|
||||
|
||||
import com.intellij.codeInsight.template.postfix.util.PostfixTemplatesUtils;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.psi.CommonClassNames;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.psi.PsiType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class FormatPostfixTemplate extends PostfixTemplate {
|
||||
public FormatPostfixTemplate() {
|
||||
super("format", "Creates String.format call", "String.format(expr);");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void expand(@NotNull PsiElement context, @NotNull Editor editor) {
|
||||
PostfixTemplatesUtils.createStatement(context, editor, "String.format(", ", )", -2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) {
|
||||
PsiExpression expr = getTopmostExpression(context);
|
||||
PsiType type = expr != null ? expr.getType() : null;
|
||||
return expr != null && type != null && CommonClassNames.JAVA_LANG_STRING.equals(type.getCanonicalText());
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2000-2014 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.postfix.templates;
|
||||
|
||||
import com.intellij.codeInsight.template.postfix.util.PostfixTemplatesUtils;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
abstract public class NonVoidPostfixTemplate extends PostfixTemplate {
|
||||
protected NonVoidPostfixTemplate(@NotNull String name, @NotNull String description, @NotNull String example) {
|
||||
super(name, description, example);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) {
|
||||
PsiExpression expr = getTopmostExpression(context);
|
||||
return expr != null && PostfixTemplatesUtils.isNonVoid(expr.getType());
|
||||
}
|
||||
}
|
||||
+1
-10
@@ -15,24 +15,15 @@
|
||||
*/
|
||||
package com.intellij.codeInsight.template.postfix.templates;
|
||||
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.psi.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ReturnStatementPostfixTemplate extends PostfixTemplate {
|
||||
public class ReturnStatementPostfixTemplate extends NonVoidPostfixTemplate {
|
||||
public ReturnStatementPostfixTemplate() {
|
||||
super("return", "Returns value from containing method", "return expr;");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) {
|
||||
PsiExpression expr = getTopmostExpression(context);
|
||||
if (expr == null) return false;
|
||||
PsiType type = expr.getType();
|
||||
return type != null && !PsiType.VOID.equals(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void expand(@NotNull PsiElement context, @NotNull Editor editor) {
|
||||
PsiExpression expr = getTopmostExpression(context);
|
||||
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2000-2014 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.postfix.templates;
|
||||
|
||||
import com.intellij.codeInsight.template.postfix.util.PostfixTemplatesUtils;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class SoutPostfixTemplate extends NonVoidPostfixTemplate {
|
||||
public SoutPostfixTemplate() {
|
||||
super("sout", "Creates System.out.println call", "System.out.println(expr);");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void expand(@NotNull PsiElement context, @NotNull Editor editor) {
|
||||
PostfixTemplatesUtils.createStatement(context, editor, "System.out.println(", ")");
|
||||
}
|
||||
}
|
||||
+16
-3
@@ -37,13 +37,21 @@ public abstract class PostfixTemplatesUtils {
|
||||
}
|
||||
|
||||
public static void createSimpleStatement(@NotNull PsiElement context, @NotNull Editor editor, @NotNull String text) {
|
||||
createStatement(context, editor, text + " ", "");
|
||||
}
|
||||
|
||||
public static void createStatement(@NotNull PsiElement context, @NotNull Editor editor, @NotNull String prefix, @NotNull String suffix) {
|
||||
createStatement(context, editor, prefix, suffix, 0);
|
||||
}
|
||||
|
||||
public static void createStatement(@NotNull PsiElement context, @NotNull Editor editor, @NotNull String prefix, @NotNull String suffix, int offset) {
|
||||
PsiExpression expr = PostfixTemplate.getTopmostExpression(context);
|
||||
PsiElement parent = expr != null ? expr.getParent() : null;
|
||||
assert parent instanceof PsiStatement;
|
||||
PsiElementFactory factory = JavaPsiFacade.getInstance(context.getProject()).getElementFactory();
|
||||
PsiStatement assertStatement = factory.createStatementFromText(text + " " + expr.getText() + ";", parent);
|
||||
PsiElement replace = parent.replace(assertStatement);
|
||||
editor.getCaretModel().moveToOffset(replace.getTextRange().getEndOffset());
|
||||
PsiStatement statement = factory.createStatementFromText(prefix + expr.getText() + suffix + ";", parent);
|
||||
PsiElement replace = parent.replace(statement);
|
||||
editor.getCaretModel().moveToOffset(replace.getTextRange().getEndOffset() + offset);
|
||||
}
|
||||
|
||||
@Contract("null -> false")
|
||||
@@ -71,6 +79,11 @@ public abstract class PostfixTemplatesUtils {
|
||||
return type != null && (PsiType.BOOLEAN.equals(type) || PsiType.BOOLEAN.equals(PsiPrimitiveType.getUnboxedType(type)));
|
||||
}
|
||||
|
||||
@Contract("null -> false")
|
||||
public static boolean isNonVoid(@Nullable PsiType type) {
|
||||
return type != null && !PsiType.VOID.equals(type);
|
||||
}
|
||||
|
||||
@Contract("null -> false")
|
||||
public static boolean isNumber(@Nullable PsiType type) {
|
||||
if (type == null) {
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package templates;
|
||||
|
||||
public class Foo {
|
||||
void m(boolean b, int value) {
|
||||
b.format<caret>
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package templates;
|
||||
|
||||
public class Foo {
|
||||
void m(boolean b, int value) {
|
||||
b.format <caret>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package templates;
|
||||
|
||||
public class Foo {
|
||||
void m(boolean b, int value) {
|
||||
"m()".format<caret>
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package templates;
|
||||
|
||||
public class Foo {
|
||||
void m(boolean b, int value) {
|
||||
String.format("m()", <caret>);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package templates;
|
||||
|
||||
public class Foo {
|
||||
void m(boolean b, int value) {
|
||||
b.sout<caret>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package templates;
|
||||
|
||||
public class Foo {
|
||||
void m(boolean b, int value) {
|
||||
System.out.println(b);<caret>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package templates;
|
||||
|
||||
public class Foo {
|
||||
void m(boolean b, int value) {
|
||||
m().sout<caret>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package templates;
|
||||
|
||||
public class Foo {
|
||||
void m(boolean b, int value) {
|
||||
m().sout <caret>
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2000-2014 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.postfix.templates;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class FormatPostfixTemplateTest extends PostfixTemplateTestCase {
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getSuffix() { return "format"; }
|
||||
|
||||
public void testString() { doTest(); }
|
||||
public void testNotString() { doTest(); }
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2000-2014 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.postfix.templates;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class SoutPostfixTemplateTest extends PostfixTemplateTestCase {
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getSuffix() { return "sout"; }
|
||||
|
||||
public void testSimple() { doTest(); }
|
||||
public void testVoid() { doTest(); }
|
||||
}
|
||||
@@ -23,6 +23,8 @@
|
||||
<postfixTemplate implementation="com.intellij.codeInsight.template.postfix.templates.AssertStatementPostfixTemplate"/>
|
||||
<postfixTemplate implementation="com.intellij.codeInsight.template.postfix.templates.SynchronizedStatementPostfixTemplate"/>
|
||||
<postfixTemplate implementation="com.intellij.codeInsight.template.postfix.templates.InstanceofExpressionPostfixTemplate"/>
|
||||
<postfixTemplate implementation="com.intellij.codeInsight.template.postfix.templates.SoutPostfixTemplate"/>
|
||||
<postfixTemplate implementation="com.intellij.codeInsight.template.postfix.templates.FormatPostfixTemplate"/>
|
||||
</extensions>
|
||||
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
|
||||
Reference in New Issue
Block a user