mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
added while postfix template
(cherry picked from commit 24bba05)
This commit is contained in:
committed by
liana.bakradze
parent
e38d898bdc
commit
3bfae3999a
@@ -0,0 +1,3 @@
|
||||
def f(a):
|
||||
while a:
|
||||
<spot></spot>
|
||||
@@ -0,0 +1,2 @@
|
||||
def f(a):
|
||||
<spot>a</spot>.while
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Uses the expression as condition in 'while' statement.
|
||||
</body>
|
||||
</html>
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.jetbrains.python.psi.*;
|
||||
import com.jetbrains.python.refactoring.surround.surrounders.expressions.PyExpressionSurrounder;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public abstract class PyExpressionAsConditionSurrounder extends PyExpressionSurrounder {
|
||||
|
||||
abstract protected String getTextToGenerate();
|
||||
|
||||
@Nullable
|
||||
abstract protected PyExpression getCondition(PyStatement statement);
|
||||
|
||||
@Nullable
|
||||
abstract protected PyStatementListContainer getStatementListContainer(PyStatement statement);
|
||||
|
||||
@Override
|
||||
public TextRange surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull PyExpression expression)
|
||||
throws IncorrectOperationException {
|
||||
TextRange currentCaretPosition = TextRange.from(editor.getCaretModel().getOffset(), 0);
|
||||
PyStatement statement = PyElementGenerator.getInstance(project).
|
||||
createFromText(LanguageLevel.getDefault(), PyStatement.class, getTextToGenerate());
|
||||
final PyExpression condition = getCondition(statement);
|
||||
if (condition == null) {
|
||||
return currentCaretPosition;
|
||||
}
|
||||
condition.replace(expression);
|
||||
statement = (PyStatement)CodeStyleManager.getInstance(project).reformat(statement);
|
||||
statement = (PyStatement)expression.getParent().replace(statement);
|
||||
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
|
||||
PyStatementListContainer statementListContainer = getStatementListContainer(statement);
|
||||
if (statementListContainer == null) {
|
||||
return currentCaretPosition;
|
||||
}
|
||||
PyStatementList statementList = statementListContainer.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 boolean isApplicable(@NotNull PyExpression expr) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -17,14 +17,10 @@ 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.codeStyle.CodeStyleManager;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.jetbrains.python.psi.*;
|
||||
import com.jetbrains.python.refactoring.surround.surrounders.expressions.PyExpressionSurrounder;
|
||||
import com.jetbrains.python.psi.PyExpression;
|
||||
import com.jetbrains.python.psi.PyIfStatement;
|
||||
import com.jetbrains.python.psi.PyStatement;
|
||||
import com.jetbrains.python.psi.PyStatementListContainer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class PyIfPostfixTemplate extends SurroundPostfixTemplateBase {
|
||||
@@ -41,30 +37,28 @@ public class PyIfPostfixTemplate extends SurroundPostfixTemplateBase {
|
||||
return new PyIfSurrounder();
|
||||
}
|
||||
|
||||
private static class PyIfSurrounder extends PyExpressionSurrounder {
|
||||
|
||||
private static class PyIfSurrounder extends PyExpressionAsConditionSurrounder {
|
||||
|
||||
@Override
|
||||
public boolean isApplicable(@NotNull PyExpression expr) {
|
||||
return true;
|
||||
protected String getTextToGenerate() {
|
||||
return "if a:\n pass";
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextRange surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull PyExpression expression)
|
||||
throws IncorrectOperationException {
|
||||
String text = "if a:\n pass";
|
||||
PyIfStatement ifStatement = PyElementGenerator.getInstance(project).
|
||||
createFromText(LanguageLevel.getDefault(), PyIfStatement.class, text);
|
||||
final PyExpression condition = ifStatement.getIfPart().getCondition();
|
||||
if (condition != null) {
|
||||
condition.replace(expression);
|
||||
protected PyExpression getCondition(PyStatement statement) {
|
||||
if (statement instanceof PyIfStatement) {
|
||||
return ((PyIfStatement)statement).getIfPart().getCondition();
|
||||
}
|
||||
ifStatement = (PyIfStatement)CodeStyleManager.getInstance(project).reformat(ifStatement);
|
||||
ifStatement = (PyIfStatement)expression.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);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PyStatementListContainer getStatementListContainer(PyStatement statement) {
|
||||
if (statement instanceof PyIfStatement) {
|
||||
return ((PyIfStatement)statement).getIfPart();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -31,7 +31,8 @@ public class PyPostfixTemplateProvider implements PostfixTemplateProvider {
|
||||
return ContainerUtil.<PostfixTemplate>newHashSet(new PyNotPostfixTemplate(),
|
||||
new PyParenthesizedExpressionPostfixTemplate(),
|
||||
new PyReturnPostfixTemplate(),
|
||||
new PyIfPostfixTemplate());
|
||||
new PyIfPostfixTemplate(),
|
||||
new PyWhilePostfixTemplate());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.jetbrains.python.psi.PyExpression;
|
||||
import com.jetbrains.python.psi.PyStatement;
|
||||
import com.jetbrains.python.psi.PyStatementListContainer;
|
||||
import com.jetbrains.python.psi.PyWhileStatement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class PyWhilePostfixTemplate extends SurroundPostfixTemplateBase {
|
||||
|
||||
public static final String TEMPLATE_DESCRIPTION = "while expr";
|
||||
|
||||
public PyWhilePostfixTemplate() {
|
||||
super("while", TEMPLATE_DESCRIPTION, PyPostfixUtils.PY_PSI_INFO, PyPostfixUtils.selectorTopmost());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Surrounder getSurrounder() {
|
||||
return new PyWhileSurrounder();
|
||||
}
|
||||
|
||||
private static class PyWhileSurrounder extends PyExpressionAsConditionSurrounder {
|
||||
|
||||
@Override
|
||||
protected String getTextToGenerate() {
|
||||
return "while a:\n pass";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PyExpression getCondition(PyStatement statement) {
|
||||
if (statement instanceof PyWhileStatement) {
|
||||
return ((PyWhileStatement)statement).getWhilePart().getCondition();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PyStatementListContainer getStatementListContainer(PyStatement statement) {
|
||||
if (statement instanceof PyWhileStatement) {
|
||||
return ((PyWhileStatement)statement).getWhilePart();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTemplateDescription() {
|
||||
return TEMPLATE_DESCRIPTION;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
def f(a, b, c):
|
||||
(2*(a + b)) * c.while<caret>
|
||||
@@ -0,0 +1,3 @@
|
||||
def f(a, b, c):
|
||||
while (2 * (a + b)) * c:
|
||||
<caret>
|
||||
@@ -0,0 +1,2 @@
|
||||
def f(a):
|
||||
a.while<caret>
|
||||
@@ -0,0 +1,3 @@
|
||||
def f(a):
|
||||
while a:
|
||||
<caret>
|
||||
@@ -0,0 +1 @@
|
||||
a = 1.while<caret>
|
||||
@@ -0,0 +1 @@
|
||||
a = 1.while <caret>
|
||||
@@ -0,0 +1 @@
|
||||
True.while<caret>
|
||||
@@ -0,0 +1,2 @@
|
||||
while 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 PyWhilePostfixTemplateTest extends PyPostfixTemplateTestCase{
|
||||
|
||||
public void testTopLevel() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testFunction() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testNonApplicable() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testComplexExpression() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTestDataDir() {
|
||||
return "while/";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user