PY-19607 print postfix template

This commit is contained in:
Liana Bakradze
2016-06-01 02:10:10 -07:00
committed by Andrey Vlasovskikh
parent 9341fb75b6
commit c5ce062f81
12 changed files with 130 additions and 1 deletions
@@ -0,0 +1 @@
print(a)
@@ -0,0 +1 @@
<spot>a</spot>.print
@@ -0,0 +1,5 @@
<html>
<body>
Surrounds expression with print function for Python 3 or with print statement for Python 2.
</body>
</html>
@@ -34,7 +34,8 @@ public class PyPostfixTemplateProvider implements PostfixTemplateProvider {
new PyIfPostfixTemplate(),
new PyWhilePostfixTemplate(),
new PyIsNonePostfixTemplate(),
new PyIsNotNonePostfixTemplate());
new PyIsNotNonePostfixTemplate(),
new PyPrintPostfixTemplate());
}
@Override
@@ -0,0 +1,73 @@
/*
* 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.codeStyle.CodeStyleManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.refactoring.surround.surrounders.expressions.PyExpressionSurrounder;
import org.jetbrains.annotations.NotNull;
public class PyPrintPostfixTemplate extends SurroundPostfixTemplateBase {
public static final String DESCR = "print(expr)";
protected PyPrintPostfixTemplate() {
super("print", DESCR, PyPostfixUtils.PY_PSI_INFO, PyPostfixUtils.selectorTopmost());
}
@NotNull
@Override
protected Surrounder getSurrounder() {
return new PyExpressionSurrounder() {
@Override
public boolean isApplicable(@NotNull PyExpression expr) {
return true;
}
@Override
public TextRange surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull PyExpression expression)
throws IncorrectOperationException {
LanguageLevel level = LanguageLevel.forElement(expression);
String textToGenerate = level.isPy3K() ? "print(a)" : "print a";
PyStatement pyStatement = PyElementGenerator.getInstance(project).createFromText(level, PyStatement.class, textToGenerate);
if (!level.isPy3K()) {
pyStatement.getLastChild().replace(expression);
} else {
PyArgumentList argumentList = PsiTreeUtil.findChildOfType(pyStatement, PyArgumentList.class);
if (argumentList == null) {
return null;
}
argumentList.getArguments()[0].replace(expression);
}
pyStatement = (PyStatement)CodeStyleManager.getInstance(project).reformat(pyStatement);
pyStatement = (PyStatement)expression.getParent().replace(pyStatement);
return TextRange.from(pyStatement.getTextRange().getEndOffset(), 0);
}
@Override
public String getTemplateDescription() {
return DESCR;
}
};
}
}
+1
View File
@@ -0,0 +1 @@
1.print<caret>
@@ -0,0 +1 @@
print 1<caret>
+1
View File
@@ -0,0 +1 @@
1.print<caret>
@@ -0,0 +1 @@
print(1)<caret>
+1
View File
@@ -0,0 +1 @@
(1, 1).print<caret>
@@ -0,0 +1 @@
print((1, 1))<caret>
@@ -0,0 +1,42 @@
/*
* 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;
import com.jetbrains.python.psi.LanguageLevel;
import com.jetbrains.python.psi.impl.PythonLanguageLevelPusher;
public class PyPrintPostfixTemplateTest extends PyPostfixTemplateTestCase {
public void testNumber2() {
PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), LanguageLevel.PYTHON27);
doTest();
}
public void testNumber3() {
PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), LanguageLevel.PYTHON35);
doTest();
}
public void testTuple() {
PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), LanguageLevel.PYTHON35);
doTest();
}
@Override
protected String getTestDataDir() {
return "print/";
}
}