mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
if postfix template added
(cherry picked from commit 30e3fe9)
This commit is contained in:
committed by
liana.bakradze
parent
f3de4b7f1f
commit
6029cf55b1
@@ -0,0 +1,3 @@
|
||||
def f(a):
|
||||
if a:
|
||||
<spot></spot>
|
||||
@@ -0,0 +1,2 @@
|
||||
def f(a):
|
||||
<spot>a</spot>.if
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Uses the expression as condition in 'if' statement.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2000-2016 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.jetbrains.python.codeInsight.postfix;
|
||||
|
||||
import com.intellij.codeInsight.template.postfix.templates.SurroundPostfixTemplateBase;
|
||||
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.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.jetbrains.python.psi.*;
|
||||
import com.jetbrains.python.refactoring.surround.surrounders.statements.PyStatementSurrounder;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class PyIfPostfixTemplate extends SurroundPostfixTemplateBase {
|
||||
|
||||
public static final String TEMPLATE_DESCRIPTION = "if expr";
|
||||
|
||||
public PyIfPostfixTemplate() {
|
||||
super("if", TEMPLATE_DESCRIPTION, PyPostfixUtils.PY_PSI_INFO, PyPostfixUtils.selectorTopmost());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Surrounder getSurrounder() {
|
||||
return new PyIfSurrounder();
|
||||
}
|
||||
|
||||
private static class PyIfSurrounder extends PyStatementSurrounder {
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected TextRange surroundStatement(@NotNull Project project, @NotNull final Editor editor, @NotNull PsiElement[] elements)
|
||||
throws IncorrectOperationException {
|
||||
String text = "if a:\n pass";
|
||||
PyIfStatement ifStatement = PyElementGenerator.getInstance(project).
|
||||
createFromText(LanguageLevel.getDefault(), PyIfStatement.class, text);
|
||||
final PsiElement element = elements[0];
|
||||
final PyExpression condition = ifStatement.getIfPart().getCondition();
|
||||
if (condition != null) {
|
||||
condition.replace(element);
|
||||
}
|
||||
ifStatement = (PyIfStatement)CodeStyleManager.getInstance(project).reformat(ifStatement);
|
||||
ifStatement = (PyIfStatement)element.getParent().replace(ifStatement);
|
||||
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
|
||||
PyStatementList statementList = ifStatement.getIfPart().getStatementList();
|
||||
PyStatement[] statements = statementList.getStatements();
|
||||
final TextRange range = statements[0].getTextRange();
|
||||
editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
|
||||
return TextRange.from(range.getStartOffset(), 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTemplateDescription() {
|
||||
return TEMPLATE_DESCRIPTION;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,8 @@ public class PyPostfixTemplateProvider implements PostfixTemplateProvider {
|
||||
public Set<PostfixTemplate> getTemplates() {
|
||||
return ContainerUtil.<PostfixTemplate>newHashSet(new PyNotPostfixTemplate(),
|
||||
new PyParenthesizedExpressionPostfixTemplate(),
|
||||
new PyReturnPostfixTemplate());
|
||||
new PyReturnPostfixTemplate(),
|
||||
new PyIfPostfixTemplate());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
def f(a, b, c):
|
||||
(a + b) * c.if<caret>
|
||||
@@ -0,0 +1,3 @@
|
||||
def f(a, b, c):
|
||||
if (a + b) * c:
|
||||
<caret>
|
||||
@@ -0,0 +1,2 @@
|
||||
def f(a):
|
||||
a.if<caret>
|
||||
@@ -0,0 +1,3 @@
|
||||
def f(a):
|
||||
if a:
|
||||
<caret>
|
||||
@@ -0,0 +1 @@
|
||||
a = 1.if<caret>
|
||||
@@ -0,0 +1 @@
|
||||
a = 1.if <caret>
|
||||
@@ -0,0 +1 @@
|
||||
True.if<caret>
|
||||
@@ -0,0 +1,2 @@
|
||||
if True:
|
||||
<caret>
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2000-2016 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.jetbrains.python.postfix;
|
||||
|
||||
public class PyIfPostfixTemplateTest extends PyPostfixTemplateTestCase {
|
||||
|
||||
public void testTopLevel() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testFunction() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testNonApplicable() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testComplexExpression() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTestDataDir() {
|
||||
return "if/";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user