summaryrefslogtreecommitdiff
path: root/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers
diff options
context:
space:
mode:
Diffstat (limited to 'python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers')
-rw-r--r--python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyArgumentListFixer.java34
-rw-r--r--python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyClassFixer.java33
-rw-r--r--python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyConditionalStatementPartFixer.java53
-rw-r--r--python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyExceptFixer.java40
-rw-r--r--python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyFixer.java20
-rw-r--r--python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyForPartFixer.java66
-rw-r--r--python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyFunctionFixer.java25
-rw-r--r--python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyMissingBracesFixer.java15
-rw-r--r--python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyParameterListFixer.java30
-rw-r--r--python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyParenthesizedFixer.java19
-rw-r--r--python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyStringLiteralFixer.java50
-rw-r--r--python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyUnconditionalStatementPartFixer.java28
-rw-r--r--python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyWithFixer.java74
13 files changed, 272 insertions, 215 deletions
diff --git a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyArgumentListFixer.java b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyArgumentListFixer.java
index f7d877e5bb5b..de90fa158bab 100644
--- a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyArgumentListFixer.java
+++ b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyArgumentListFixer.java
@@ -25,26 +25,30 @@ import com.jetbrains.python.psi.PyArgumentList;
import com.jetbrains.python.psi.PyClass;
import com.jetbrains.python.psi.PyDecorator;
import com.jetbrains.python.psi.PyUtil;
+import org.jetbrains.annotations.NotNull;
/**
* @author Alexey.Ivanov
*/
-public class PyArgumentListFixer implements PyFixer {
- public void apply(Editor editor, PySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
- if (psiElement instanceof PyArgumentList) {
- final PsiElement rBrace = PyUtil.getChildByFilter(psiElement, PyTokenTypes.CLOSE_BRACES, 0);
- if (psiElement.getParent() instanceof PyClass || psiElement.getParent() instanceof PyDecorator) {
- final PsiElement lBrace = PyUtil.getChildByFilter(psiElement, PyTokenTypes.OPEN_BRACES, 0);
- if (lBrace != null && rBrace == null) {
- final Document document = editor.getDocument();
- document.insertString(psiElement.getTextRange().getEndOffset(), ")");
- }
+public class PyArgumentListFixer extends PyFixer<PyArgumentList> {
+ public PyArgumentListFixer() {
+ super(PyArgumentList.class);
+ }
+
+ @Override
+ public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyArgumentList arguments) throws IncorrectOperationException {
+ final PsiElement rBrace = PyUtil.getChildByFilter(arguments, PyTokenTypes.CLOSE_BRACES, 0);
+ if (arguments.getParent() instanceof PyClass || arguments.getParent() instanceof PyDecorator) {
+ final PsiElement lBrace = PyUtil.getChildByFilter(arguments, PyTokenTypes.OPEN_BRACES, 0);
+ if (lBrace != null && rBrace == null) {
+ final Document document = editor.getDocument();
+ document.insertString(arguments.getTextRange().getEndOffset(), ")");
}
- else {
- if (rBrace == null) {
- final Document document = editor.getDocument();
- document.insertString(psiElement.getTextRange().getEndOffset(), ")");
- }
+ }
+ else {
+ if (rBrace == null) {
+ final Document document = editor.getDocument();
+ document.insertString(arguments.getTextRange().getEndOffset(), ")");
}
}
}
diff --git a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyClassFixer.java b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyClassFixer.java
index b9846e1cb894..ce51d6d46824 100644
--- a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyClassFixer.java
+++ b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyClassFixer.java
@@ -17,7 +17,6 @@ package com.jetbrains.python.codeInsight.editorActions.smartEnter.fixers;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
-import com.intellij.psi.tree.TokenSet;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PyTokenTypes;
@@ -25,6 +24,9 @@ import com.jetbrains.python.codeInsight.editorActions.smartEnter.PySmartEnterPro
import com.jetbrains.python.psi.PyArgumentList;
import com.jetbrains.python.psi.PyClass;
import com.jetbrains.python.psi.PyUtil;
+import org.jetbrains.annotations.NotNull;
+
+import static com.jetbrains.python.psi.PyUtil.sure;
/**
* Created by IntelliJ IDEA.
@@ -32,21 +34,22 @@ import com.jetbrains.python.psi.PyUtil;
* Date: 16.04.2010
* Time: 18:41:08
*/
-public class PyClassFixer implements PyFixer {
- public void apply(Editor editor, PySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
- if (psiElement instanceof PyClass) {
- final PsiElement colon = PyUtil.getChildByFilter(psiElement, TokenSet.create(PyTokenTypes.COLON), 0);
- if (colon == null) {
- final PyClass aClass = (PyClass)psiElement;
- final PyArgumentList argList = PsiTreeUtil.getChildOfType(aClass, PyArgumentList.class);
- int offset = argList.getTextRange().getEndOffset();
- String textToInsert = ":";
- if (aClass.getNameNode() == null) {
- processor.registerUnresolvedError(argList.getTextRange().getEndOffset() + 1);
- textToInsert = " :";
- }
- editor.getDocument().insertString(offset, textToInsert);
+public class PyClassFixer extends PyFixer<PyClass> {
+ public PyClassFixer() {
+ super(PyClass.class);
+ }
+
+ public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyClass pyClass) throws IncorrectOperationException {
+ final PsiElement colon = PyUtil.getFirstChildOfType(pyClass, PyTokenTypes.COLON);
+ if (colon == null) {
+ final PyArgumentList argList = PsiTreeUtil.getChildOfType(pyClass, PyArgumentList.class);
+ final int offset = sure(argList).getTextRange().getEndOffset();
+ String textToInsert = ":";
+ if (pyClass.getNameNode() == null) {
+ processor.registerUnresolvedError(argList.getTextRange().getEndOffset() + 1);
+ textToInsert = " :";
}
+ editor.getDocument().insertString(offset, textToInsert);
}
}
}
diff --git a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyConditionalStatementPartFixer.java b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyConditionalStatementPartFixer.java
index 2885c63f6345..f71c61261103 100644
--- a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyConditionalStatementPartFixer.java
+++ b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyConditionalStatementPartFixer.java
@@ -25,6 +25,9 @@ import com.jetbrains.python.codeInsight.editorActions.smartEnter.PySmartEnterPro
import com.jetbrains.python.psi.PyConditionalStatementPart;
import com.jetbrains.python.psi.PyExpression;
import com.jetbrains.python.psi.PyUtil;
+import org.jetbrains.annotations.NotNull;
+
+import static com.jetbrains.python.psi.PyUtil.sure;
/**
* Created by IntelliJ IDEA.
@@ -32,31 +35,35 @@ import com.jetbrains.python.psi.PyUtil;
* Date: 15.04.2010
* Time: 19:33:14
*/
-public class PyConditionalStatementPartFixer implements PyFixer {
- public void apply(Editor editor, PySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
- if (psiElement instanceof PyConditionalStatementPart) {
- final PyConditionalStatementPart conditionalStatementPart = (PyConditionalStatementPart)psiElement;
- final PyExpression condition = conditionalStatementPart.getCondition();
- final Document document = editor.getDocument();
- final PsiElement colon = PyUtil.getChildByFilter(conditionalStatementPart, TokenSet.create(PyTokenTypes.COLON), 0);
- if (colon == null) {
- if (condition != null) {
- final PsiElement firstNonComment = PyUtil.getFirstNonCommentAfter(condition.getNextSibling());
- if (firstNonComment != null && !":".equals(firstNonComment.getNode().getText())) {
- document.insertString(firstNonComment.getTextRange().getEndOffset(), ":");
- }
- }
- else {
- final PsiElement keywordToken = PyUtil.getChildByFilter(conditionalStatementPart,
- TokenSet.create(PyTokenTypes.IF_KEYWORD, PyTokenTypes.ELIF_KEYWORD,
- PyTokenTypes.WHILE_KEYWORD), 0);
- final int offset = keywordToken.getTextRange().getEndOffset();
- document.insertString(offset, " :");
- processor.registerUnresolvedError(offset + 1);
+public class PyConditionalStatementPartFixer extends PyFixer<PyConditionalStatementPart> {
+ public PyConditionalStatementPartFixer() {
+ super(PyConditionalStatementPart.class);
+ }
+
+ @Override
+ public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyConditionalStatementPart statementPart)
+ throws IncorrectOperationException {
+ final PyExpression condition = statementPart.getCondition();
+ final Document document = editor.getDocument();
+ final PsiElement colon = PyUtil.getFirstChildOfType(statementPart, PyTokenTypes.COLON);
+ if (colon == null) {
+ if (condition != null) {
+ final PsiElement firstNonComment = PyUtil.getFirstNonCommentAfter(condition.getNextSibling());
+ if (firstNonComment != null && !":".equals(firstNonComment.getNode().getText())) {
+ document.insertString(firstNonComment.getTextRange().getEndOffset(), ":");
}
- } else if (condition == null) {
- processor.registerUnresolvedError(colon.getTextRange().getStartOffset());
}
+ else {
+ final TokenSet keywords = TokenSet.create(PyTokenTypes.IF_KEYWORD, PyTokenTypes.ELIF_KEYWORD, PyTokenTypes.WHILE_KEYWORD);
+ final PsiElement keywordToken = PyUtil.getChildByFilter(statementPart,
+ keywords, 0);
+ final int offset = sure(keywordToken).getTextRange().getEndOffset();
+ document.insertString(offset, " :");
+ processor.registerUnresolvedError(offset + 1);
+ }
+ }
+ else if (condition == null) {
+ processor.registerUnresolvedError(colon.getTextRange().getStartOffset());
}
}
}
diff --git a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyExceptFixer.java b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyExceptFixer.java
index 8e3532cb49b3..67d5ff5c9ee5 100644
--- a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyExceptFixer.java
+++ b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyExceptFixer.java
@@ -17,13 +17,15 @@ package com.jetbrains.python.codeInsight.editorActions.smartEnter.fixers;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
-import com.intellij.psi.tree.TokenSet;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.codeInsight.editorActions.smartEnter.PySmartEnterProcessor;
import com.jetbrains.python.psi.PyExceptPart;
import com.jetbrains.python.psi.PyExpression;
import com.jetbrains.python.psi.PyUtil;
+import org.jetbrains.annotations.NotNull;
+
+import static com.jetbrains.python.psi.PyUtil.sure;
/**
* Created by IntelliJ IDEA.
@@ -31,24 +33,26 @@ import com.jetbrains.python.psi.PyUtil;
* Date: 22.04.2010
* Time: 18:13:34
*/
-public class PyExceptFixer implements PyFixer {
- public void apply(Editor editor, PySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
- if (psiElement instanceof PyExceptPart) {
- PyExceptPart exceptPart = (PyExceptPart)psiElement;
- final PsiElement colon = PyUtil.getChildByFilter(exceptPart, TokenSet.create(PyTokenTypes.COLON), 0);
- if (colon == null) {
- int offset = PyUtil.getChildByFilter(exceptPart,
- TokenSet.create(PyTokenTypes.EXCEPT_KEYWORD), 0).getTextRange().getEndOffset();
- final PyExpression exceptClass = exceptPart.getExceptClass();
- if (exceptClass != null) {
- offset = exceptClass.getTextRange().getEndOffset();
- }
- final PyExpression target = exceptPart.getTarget();
- if (target != null) {
- offset = target.getTextRange().getEndOffset();
- }
- editor.getDocument().insertString(offset, ":");
+public class PyExceptFixer extends PyFixer<PyExceptPart> {
+ public PyExceptFixer() {
+ super(PyExceptPart.class);
+ }
+
+ @Override
+ public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyExceptPart exceptPart) throws IncorrectOperationException {
+ final PsiElement colon = PyUtil.getFirstChildOfType(exceptPart, PyTokenTypes.COLON);
+ if (colon == null) {
+ final PsiElement exceptToken = PyUtil.getFirstChildOfType(exceptPart, PyTokenTypes.EXCEPT_KEYWORD);
+ int offset = sure(exceptToken).getTextRange().getEndOffset();
+ final PyExpression exceptClass = exceptPart.getExceptClass();
+ if (exceptClass != null) {
+ offset = exceptClass.getTextRange().getEndOffset();
+ }
+ final PyExpression target = exceptPart.getTarget();
+ if (target != null) {
+ offset = target.getTextRange().getEndOffset();
}
+ editor.getDocument().insertString(offset, ":");
}
}
}
diff --git a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyFixer.java b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyFixer.java
index f02831d92d36..0ebdceb20e37 100644
--- a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyFixer.java
+++ b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyFixer.java
@@ -19,6 +19,8 @@ import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.codeInsight.editorActions.smartEnter.PySmartEnterProcessor;
+import com.jetbrains.python.psi.PyElement;
+import org.jetbrains.annotations.NotNull;
/**
* Created by IntelliJ IDEA.
@@ -26,6 +28,20 @@ import com.jetbrains.python.codeInsight.editorActions.smartEnter.PySmartEnterPro
* Date: 15.04.2010
* Time: 17:10:33
*/
-public interface PyFixer {
- void apply(Editor editor, PySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException;
+public abstract class PyFixer<T extends PyElement> {
+ private final Class<T> myClass;
+
+ public PyFixer(@NotNull Class<T> aClass) {
+ myClass = aClass;
+ }
+
+ public final void apply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PsiElement element)
+ throws IncorrectOperationException {
+ if (myClass.isInstance(element)) {
+ //noinspection unchecked
+ doApply(editor, processor, (T)element);
+ }
+ }
+
+ public abstract void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull T element);
}
diff --git a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyForPartFixer.java b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyForPartFixer.java
index 1b8b438c68fd..eefe5cd1fc3e 100644
--- a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyForPartFixer.java
+++ b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyForPartFixer.java
@@ -18,12 +18,13 @@ package com.jetbrains.python.codeInsight.editorActions.smartEnter.fixers;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
-import com.intellij.psi.tree.TokenSet;
-import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.codeInsight.editorActions.smartEnter.PySmartEnterProcessor;
import com.jetbrains.python.psi.PyForPart;
import com.jetbrains.python.psi.PyUtil;
+import org.jetbrains.annotations.NotNull;
+
+import static com.jetbrains.python.psi.PyUtil.sure;
/**
* Created by IntelliJ IDEA.
@@ -31,41 +32,42 @@ import com.jetbrains.python.psi.PyUtil;
* Date: 16.04.2010
* Time: 16:03:43
*/
-public class PyForPartFixer implements PyFixer {
- public void apply(Editor editor, PySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
- if (psiElement instanceof PyForPart) {
- final PyForPart forPart = (PyForPart)psiElement;
- final PsiElement colon = PyUtil.getChildByFilter(psiElement, TokenSet.create(PyTokenTypes.COLON), 0);
- final Document document = editor.getDocument();
- final PsiElement forToken = PyUtil.getChildByFilter(forPart,
- TokenSet.create(PyTokenTypes.FOR_KEYWORD), 0);
- if (colon == null) {
- String textToInsert = ":";
- PsiElement sourceOrTarget = forPart.getSource();
- PsiElement positionToInsert = sourceOrTarget;
- if (sourceOrTarget == null) {
- sourceOrTarget = forPart.getTarget();
- final PsiElement inToken = PyUtil.getChildByFilter(forPart, TokenSet.create(PyTokenTypes.IN_KEYWORD), 0);
- if (inToken == null) {
- if (sourceOrTarget == null) {
- positionToInsert = forToken;
- textToInsert = " in :";
- processor.registerUnresolvedError(positionToInsert.getTextRange().getEndOffset() + 1);
- }
- else {
- positionToInsert = sourceOrTarget;
- textToInsert = " in :";
- processor.registerUnresolvedError(positionToInsert.getTextRange().getEndOffset() + 4);
- }
+public class PyForPartFixer extends PyFixer<PyForPart> {
+ public PyForPartFixer() {
+ super(PyForPart.class);
+ }
+
+ @Override
+ public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyForPart forPart) {
+ final PsiElement colon = PyUtil.getFirstChildOfType(forPart, PyTokenTypes.COLON);
+ final Document document = editor.getDocument();
+ final PsiElement forToken = PyUtil.getFirstChildOfType(forPart, PyTokenTypes.FOR_KEYWORD);
+ if (colon == null) {
+ String textToInsert = ":";
+ PsiElement sourceOrTarget = forPart.getSource();
+ PsiElement positionToInsert = sourceOrTarget;
+ if (sourceOrTarget == null) {
+ sourceOrTarget = forPart.getTarget();
+ final PsiElement inToken = PyUtil.getFirstChildOfType(forPart, PyTokenTypes.IN_KEYWORD);
+ if (inToken == null) {
+ if (sourceOrTarget == null) {
+ positionToInsert = sure(forToken);
+ textToInsert = " in :";
+ processor.registerUnresolvedError(positionToInsert.getTextRange().getEndOffset() + 1);
}
else {
- positionToInsert = inToken;
- textToInsert = " :";
- processor.registerUnresolvedError(positionToInsert.getTextRange().getEndOffset() + 1);
+ positionToInsert = sourceOrTarget;
+ textToInsert = " in :";
+ processor.registerUnresolvedError(positionToInsert.getTextRange().getEndOffset() + 4);
}
}
- document.insertString(positionToInsert.getTextRange().getEndOffset(), textToInsert);
+ else {
+ positionToInsert = inToken;
+ textToInsert = " :";
+ processor.registerUnresolvedError(positionToInsert.getTextRange().getEndOffset() + 1);
+ }
}
+ document.insertString(positionToInsert.getTextRange().getEndOffset(), textToInsert);
}
}
}
diff --git a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyFunctionFixer.java b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyFunctionFixer.java
index f1988e13f57a..ad959f80ccae 100644
--- a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyFunctionFixer.java
+++ b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyFunctionFixer.java
@@ -18,13 +18,13 @@ package com.jetbrains.python.codeInsight.editorActions.smartEnter.fixers;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
-import com.intellij.psi.tree.TokenSet;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.codeInsight.editorActions.smartEnter.PySmartEnterProcessor;
import com.jetbrains.python.psi.PyFunction;
import com.jetbrains.python.psi.PyParameterList;
import com.jetbrains.python.psi.PyUtil;
+import org.jetbrains.annotations.NotNull;
/**
* Created by IntelliJ IDEA.
@@ -32,16 +32,19 @@ import com.jetbrains.python.psi.PyUtil;
* Date: 16.04.2010
* Time: 16:59:07
*/
-public class PyFunctionFixer implements PyFixer {
- public void apply(Editor editor, PySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
- if (psiElement instanceof PyFunction) {
- final PsiElement colon = PyUtil.getChildByFilter(psiElement, TokenSet.create(PyTokenTypes.COLON), 0);
- if (colon == null) {
- final PyFunction function = (PyFunction)psiElement;
- final PyParameterList parameterList = function.getParameterList();
- final Document document = editor.getDocument();
- document.insertString(parameterList.getTextRange().getEndOffset(), ":");
- }
+public class PyFunctionFixer extends PyFixer<PyFunction> {
+ public PyFunctionFixer() {
+ super(PyFunction.class);
+ }
+
+ @Override
+ public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyFunction function)
+ throws IncorrectOperationException {
+ final PsiElement colon = PyUtil.getFirstChildOfType(function, PyTokenTypes.COLON);
+ if (colon == null) {
+ final PyParameterList parameterList = function.getParameterList();
+ final Document document = editor.getDocument();
+ document.insertString(parameterList.getTextRange().getEndOffset(), ":");
}
}
}
diff --git a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyMissingBracesFixer.java b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyMissingBracesFixer.java
index 0d8565d6960d..eb97d2bdb372 100644
--- a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyMissingBracesFixer.java
+++ b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyMissingBracesFixer.java
@@ -20,6 +20,7 @@ import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.codeInsight.editorActions.smartEnter.PySmartEnterProcessor;
import com.jetbrains.python.psi.*;
+import org.jetbrains.annotations.NotNull;
/**
* Created by IntelliJ IDEA.
@@ -27,10 +28,16 @@ import com.jetbrains.python.psi.*;
* Date: 15.04.2010
* Time: 17:55:46
*/
-public class PyMissingBracesFixer implements PyFixer {
- public void apply(Editor editor, PySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
+public class PyMissingBracesFixer extends PyFixer<PyElement> {
+ public PyMissingBracesFixer() {
+ super(PyElement.class);
+ }
+
+ @Override
+ public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyElement psiElement)
+ throws IncorrectOperationException {
if (psiElement instanceof PySetLiteralExpression || psiElement instanceof PyDictLiteralExpression) {
- PsiElement lastChild = PyUtil.getFirstNonCommentBefore(psiElement.getLastChild());
+ final PsiElement lastChild = PyUtil.getFirstNonCommentBefore(psiElement.getLastChild());
if (lastChild != null && !"}".equals(lastChild.getText())) {
editor.getDocument().insertString(lastChild.getTextRange().getEndOffset(), "}");
}
@@ -38,7 +45,7 @@ public class PyMissingBracesFixer implements PyFixer {
else if (psiElement instanceof PyListLiteralExpression ||
psiElement instanceof PySliceExpression ||
psiElement instanceof PySubscriptionExpression) {
- PsiElement lastChild = PyUtil.getFirstNonCommentBefore(psiElement.getLastChild());
+ final PsiElement lastChild = PyUtil.getFirstNonCommentBefore(psiElement.getLastChild());
if (lastChild != null && !"]".equals(lastChild.getText())) {
editor.getDocument().insertString(lastChild.getTextRange().getEndOffset(), "]");
}
diff --git a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyParameterListFixer.java b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyParameterListFixer.java
index 7b5970d7baf2..69a06b8b7c48 100644
--- a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyParameterListFixer.java
+++ b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyParameterListFixer.java
@@ -23,6 +23,7 @@ import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.codeInsight.editorActions.smartEnter.PySmartEnterProcessor;
import com.jetbrains.python.psi.PyParameterList;
import com.jetbrains.python.psi.PyUtil;
+import org.jetbrains.annotations.NotNull;
/**
* Created by IntelliJ IDEA.
@@ -30,19 +31,22 @@ import com.jetbrains.python.psi.PyUtil;
* Date: 16.04.2010
* Time: 17:25:46
*/
-public class PyParameterListFixer implements PyFixer {
- public void apply(Editor editor, PySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
- if (psiElement instanceof PyParameterList) {
- final PsiElement lBrace = PyUtil.getChildByFilter(psiElement, PyTokenTypes.OPEN_BRACES, 0);
- final PsiElement rBrace = PyUtil.getChildByFilter(psiElement, PyTokenTypes.CLOSE_BRACES, 0);
- if (lBrace == null || rBrace == null) {
- final Document document = editor.getDocument();
- if (lBrace == null) {
- document.insertString(psiElement.getTextRange().getStartOffset(), "(");
- }
- else {
- document.insertString(psiElement.getTextRange().getEndOffset(), ")");
- }
+public class PyParameterListFixer extends PyFixer<PyParameterList> {
+ public PyParameterListFixer() {
+ super(PyParameterList.class);
+ }
+
+ @Override
+ public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyParameterList psiElement) throws IncorrectOperationException {
+ final PsiElement lBrace = PyUtil.getChildByFilter(psiElement, PyTokenTypes.OPEN_BRACES, 0);
+ final PsiElement rBrace = PyUtil.getChildByFilter(psiElement, PyTokenTypes.CLOSE_BRACES, 0);
+ if (lBrace == null || rBrace == null) {
+ final Document document = editor.getDocument();
+ if (lBrace == null) {
+ document.insertString(psiElement.getTextRange().getStartOffset(), "(");
+ }
+ else {
+ document.insertString(psiElement.getTextRange().getEndOffset(), ")");
}
}
}
diff --git a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyParenthesizedFixer.java b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyParenthesizedFixer.java
index a055375bc040..582e034c5603 100644
--- a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyParenthesizedFixer.java
+++ b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyParenthesizedFixer.java
@@ -20,6 +20,7 @@ import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.codeInsight.editorActions.smartEnter.PySmartEnterProcessor;
import com.jetbrains.python.psi.PyParenthesizedExpression;
+import org.jetbrains.annotations.NotNull;
/**
* Created by IntelliJ IDEA.
@@ -27,13 +28,17 @@ import com.jetbrains.python.psi.PyParenthesizedExpression;
* Date: 15.04.2010
* Time: 17:42:08
*/
-public class PyParenthesizedFixer implements PyFixer {
- public void apply(final Editor editor, final PySmartEnterProcessor processor, final PsiElement psiElement) throws IncorrectOperationException {
- if (psiElement instanceof PyParenthesizedExpression) {
- final PsiElement lastChild = psiElement.getLastChild();
- if (lastChild != null && !")".equals(lastChild.getText())) {
- editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), ")");
- }
+public class PyParenthesizedFixer extends PyFixer<PyParenthesizedExpression> {
+ public PyParenthesizedFixer() {
+ super(PyParenthesizedExpression.class);
+ }
+
+ @Override
+ public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyParenthesizedExpression expression)
+ throws IncorrectOperationException {
+ final PsiElement lastChild = expression.getLastChild();
+ if (lastChild != null && !")".equals(lastChild.getText())) {
+ editor.getDocument().insertString(expression.getTextRange().getEndOffset(), ")");
}
}
}
diff --git a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyStringLiteralFixer.java b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyStringLiteralFixer.java
index 906aecd9c757..3e9925a86131 100644
--- a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyStringLiteralFixer.java
+++ b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyStringLiteralFixer.java
@@ -17,10 +17,10 @@ package com.jetbrains.python.codeInsight.editorActions.smartEnter.fixers;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.text.StringUtil;
-import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.codeInsight.editorActions.smartEnter.PySmartEnterProcessor;
import com.jetbrains.python.psi.PyStringLiteralExpression;
+import org.jetbrains.annotations.NotNull;
/**
* Created by IntelliJ IDEA.
@@ -28,31 +28,35 @@ import com.jetbrains.python.psi.PyStringLiteralExpression;
* Date: 15.04.2010
* Time: 17:17:14
*/
-public class PyStringLiteralFixer implements PyFixer {
- public void apply(Editor editor, PySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
- if (psiElement instanceof PyStringLiteralExpression) {
- final String text = psiElement.getText();
- if (StringUtil.startsWith(text, "\"\"\"")) {
- final int suffixLength = StringUtil.commonSuffixLength(text, "\"\"\"");
- if (suffixLength != 3) {
- editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), "\"\"\"".substring(suffixLength));
- }
+public class PyStringLiteralFixer extends PyFixer<PyStringLiteralExpression> {
+ public PyStringLiteralFixer() {
+ super(PyStringLiteralExpression.class);
+ }
+
+ @Override
+ public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyStringLiteralExpression psiElement)
+ throws IncorrectOperationException {
+ final String text = psiElement.getText();
+ if (StringUtil.startsWith(text, "\"\"\"")) {
+ final int suffixLength = StringUtil.commonSuffixLength(text, "\"\"\"");
+ if (suffixLength != 3) {
+ editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), "\"\"\"".substring(suffixLength));
}
- else if (StringUtil.startsWith(text, "\'\'\'")) {
- final int suffixLength = StringUtil.commonSuffixLength(text, "\'\'\'");
- if (suffixLength != 3) {
- editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), "\'\'\'".substring(suffixLength));
- }
+ }
+ else if (StringUtil.startsWith(text, "\'\'\'")) {
+ final int suffixLength = StringUtil.commonSuffixLength(text, "\'\'\'");
+ if (suffixLength != 3) {
+ editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), "\'\'\'".substring(suffixLength));
}
- else if (StringUtil.startsWith(text, "\"")) {
- if (!StringUtil.endsWith(text, "\"")) {
- editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), "\"");
- }
+ }
+ else if (StringUtil.startsWith(text, "\"")) {
+ if (!StringUtil.endsWith(text, "\"")) {
+ editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), "\"");
}
- else if (StringUtil.startsWith(text, "\'")) {
- if (!StringUtil.endsWith(text, "\'")) {
- editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), "\'");
- }
+ }
+ else if (StringUtil.startsWith(text, "\'")) {
+ if (!StringUtil.endsWith(text, "\'")) {
+ editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), "\'");
}
}
}
diff --git a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyUnconditionalStatementPartFixer.java b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyUnconditionalStatementPartFixer.java
index 57bfd39f4f85..6aace3972a1b 100644
--- a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyUnconditionalStatementPartFixer.java
+++ b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyUnconditionalStatementPartFixer.java
@@ -21,10 +21,10 @@ import com.intellij.psi.tree.TokenSet;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.codeInsight.editorActions.smartEnter.PySmartEnterProcessor;
-import com.jetbrains.python.psi.PyElsePart;
-import com.jetbrains.python.psi.PyFinallyPart;
-import com.jetbrains.python.psi.PyTryPart;
-import com.jetbrains.python.psi.PyUtil;
+import com.jetbrains.python.psi.*;
+import org.jetbrains.annotations.NotNull;
+
+import static com.jetbrains.python.psi.PyUtil.sure;
/**
* Created by IntelliJ IDEA.
@@ -32,16 +32,20 @@ import com.jetbrains.python.psi.PyUtil;
* Date: 16.04.2010
* Time: 14:25:20
*/
-public class PyUnconditionalStatementPartFixer implements PyFixer {
- public void apply(Editor editor, PySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
+public class PyUnconditionalStatementPartFixer extends PyFixer<PyElement> {
+ public PyUnconditionalStatementPartFixer() {
+ super(PyElement.class);
+ }
+
+ @Override
+ public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyElement psiElement)
+ throws IncorrectOperationException {
if (PyUtil.instanceOf(psiElement, PyElsePart.class, PyTryPart.class, PyFinallyPart.class)) {
- final PsiElement colon = PyUtil.getChildByFilter(psiElement, TokenSet.create(PyTokenTypes.COLON), 0);
+ final PsiElement colon = PyUtil.getFirstChildOfType(psiElement, PyTokenTypes.COLON);
if (colon == null) {
- final PsiElement keywordToken = PyUtil.getChildByFilter(psiElement,
- TokenSet.create(PyTokenTypes.ELSE_KEYWORD, PyTokenTypes.TRY_KEYWORD,
- PyTokenTypes.FINALLY_KEYWORD),
- 0);
- editor.getDocument().insertString(keywordToken.getTextRange().getEndOffset(), ":");
+ final TokenSet keywords = TokenSet.create(PyTokenTypes.ELSE_KEYWORD, PyTokenTypes.TRY_KEYWORD, PyTokenTypes.FINALLY_KEYWORD);
+ final PsiElement keywordToken = PyUtil.getChildByFilter(psiElement, keywords, 0);
+ editor.getDocument().insertString(sure(keywordToken).getTextRange().getEndOffset(), ":");
}
}
}
diff --git a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyWithFixer.java b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyWithFixer.java
index b6d099ef7cb5..ec236d9242f5 100644
--- a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyWithFixer.java
+++ b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/fixers/PyWithFixer.java
@@ -15,65 +15,59 @@
*/
package com.jetbrains.python.codeInsight.editorActions.smartEnter.fixers;
-import com.intellij.lang.ASTNode;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.codeInsight.editorActions.smartEnter.PySmartEnterProcessor;
-import com.jetbrains.python.psi.PyElementType;
import com.jetbrains.python.psi.PyExpression;
+import com.jetbrains.python.psi.PyUtil;
import com.jetbrains.python.psi.PyWithItem;
import com.jetbrains.python.psi.PyWithStatement;
import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
import static com.jetbrains.python.psi.PyUtil.sure;
/**
* @author Mikhail Golubev
*/
-public class PyWithFixer implements PyFixer {
- public void apply(Editor editor, PySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
- if (psiElement instanceof PyWithStatement) {
- final PyWithStatement withStatement = (PyWithStatement)psiElement;
- final PsiElement colonToken = getFirstChildOfType(psiElement, PyTokenTypes.COLON);
- final PsiElement withToken = getFirstChildOfType(withStatement, PyTokenTypes.WITH_KEYWORD);
- final Document document = editor.getDocument();
- if (colonToken == null) {
- int insertAt = sure(withToken).getTextRange().getEndOffset();
- String textToInsert = ":";
- final PyWithItem[] withItems = withStatement.getWithItems();
- final PyWithItem lastItem = withItems.length != 0 ? withItems[withItems.length - 1] : null;
- if (lastItem == null || lastItem.getExpression() == null) {
- textToInsert = " :";
- processor.registerUnresolvedError(insertAt + 1);
- }
- else {
- final PyExpression expression = lastItem.getExpression();
- insertAt = expression.getTextRange().getEndOffset();
- final PsiElement asToken = getFirstChildOfType(lastItem, PyTokenTypes.AS_KEYWORD);
- if (asToken != null) {
- insertAt = asToken.getTextRange().getEndOffset();
- final PyExpression target = lastItem.getTarget();
- if (target != null) {
- insertAt = target.getTextRange().getEndOffset();
- }
- else {
- textToInsert = " :";
- processor.registerUnresolvedError(insertAt + 1);
- }
+public class PyWithFixer extends PyFixer<PyWithStatement> {
+ public PyWithFixer() {
+ super(PyWithStatement.class);
+ }
+
+ @Override
+ public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyWithStatement withStatement) throws IncorrectOperationException {
+ final PsiElement colonToken = PyUtil.getFirstChildOfType(withStatement, PyTokenTypes.COLON);
+ final PsiElement withToken = PyUtil.getFirstChildOfType(withStatement, PyTokenTypes.WITH_KEYWORD);
+ final Document document = editor.getDocument();
+ if (colonToken == null) {
+ int insertAt = sure(withToken).getTextRange().getEndOffset();
+ String textToInsert = ":";
+ final PyWithItem[] withItems = withStatement.getWithItems();
+ final PyWithItem lastItem = withItems.length != 0 ? withItems[withItems.length - 1] : null;
+ if (lastItem == null || lastItem.getExpression() == null) {
+ textToInsert = " :";
+ processor.registerUnresolvedError(insertAt + 1);
+ }
+ else {
+ final PyExpression expression = lastItem.getExpression();
+ insertAt = expression.getTextRange().getEndOffset();
+ final PsiElement asToken = PyUtil.getFirstChildOfType(lastItem, PyTokenTypes.AS_KEYWORD);
+ if (asToken != null) {
+ insertAt = asToken.getTextRange().getEndOffset();
+ final PyExpression target = lastItem.getTarget();
+ if (target != null) {
+ insertAt = target.getTextRange().getEndOffset();
+ }
+ else {
+ textToInsert = " :";
+ processor.registerUnresolvedError(insertAt + 1);
}
}
- document.insertString(insertAt, textToInsert);
}
+ document.insertString(insertAt, textToInsert);
}
}
-
- @Nullable
- private static PsiElement getFirstChildOfType(@NotNull final PsiElement element, @NotNull PyElementType type) {
- final ASTNode child = element.getNode().findChildByType(type);
- return child != null ? child.getPsi() : null;
- }
}