diff --git a/python/src/com/jetbrains/python/codeInsight/UnindentingInsertHandler.java b/python/src/com/jetbrains/python/codeInsight/UnindentingInsertHandler.java index 39ff94156e82..27cf78af99c8 100644 --- a/python/src/com/jetbrains/python/codeInsight/UnindentingInsertHandler.java +++ b/python/src/com/jetbrains/python/codeInsight/UnindentingInsertHandler.java @@ -9,13 +9,10 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; +import com.intellij.psi.util.PsiTreeUtil; import com.jetbrains.python.codeInsight.completion.PythonLookupElement; import com.jetbrains.python.psi.PyStatementWithElse; import com.jetbrains.python.psi.PyTryExceptStatement; -import com.jetbrains.python.psi.patterns.Matcher; -import com.jetbrains.python.psi.patterns.ParentMatcher; - -import java.util.List; /** * Adjusts indentation after a final part keyword is inserted, e.g. an "else:". @@ -23,10 +20,6 @@ import java.util.List; * Date: Mar 2, 2010 6:48:40 PM */ public class UnindentingInsertHandler implements InsertHandler { - - private final static ParentMatcher TRY_MATCHER = new ParentMatcher(PyTryExceptStatement.class); - private final static ParentMatcher ELSE_MATCHER = new ParentMatcher(PyStatementWithElse.class); - public final static UnindentingInsertHandler INSTANCE = new UnindentingInsertHandler(); private UnindentingInsertHandler() { @@ -54,7 +47,7 @@ public class UnindentingInsertHandler implements InsertHandler parentClass = null; int last_offset = nonspace_offset + "finally".length(); // the longest of all if (last_offset > offset) last_offset = offset; @@ -64,32 +57,31 @@ public class UnindentingInsertHandler implements InsertHandler= else_len) { if ((piece.startsWith("else") || piece.startsWith("elif")) && (else_len == piece.length() || piece.charAt(else_len) < 'a' || piece.charAt(else_len) > 'z')) { - matcher = ELSE_MATCHER; + parentClass = PyStatementWithElse.class; } } final int except_len = "except".length(); if (local_length >= except_len) { if (piece.startsWith("except") && (except_len == piece.length() || piece.charAt(except_len) < 'a' || piece.charAt(except_len) > 'z')) { - matcher = TRY_MATCHER; + parentClass = PyTryExceptStatement.class; } } final int finally_len = "finally".length(); if (local_length >= finally_len) { if (piece.startsWith("finally") && (finally_len == piece.length() || piece.charAt(finally_len) < 'a' || piece.charAt(finally_len) > 'z')) { - matcher = TRY_MATCHER; + parentClass = PyTryExceptStatement.class; } } } - if (matcher == null) return false; // failed + if (parentClass == null) return false; // failed PsiDocumentManager.getInstance(project).commitDocument(document); // reparse PsiElement token = file.findElementAt(offset-2); // -1 is our ':'; -2 is even safer. - List result = matcher.search(token); - if (result != null && result.size() > 0) { - PsiElement outer = result.get(0); + PsiElement outer = PsiTreeUtil.getParentOfType(token, parentClass); + if (outer != null) { int outer_offset = outer.getTextOffset(); int outer_indent = outer_offset - document.getLineStartOffset(document.getLineNumber(outer_offset)); assert outer_indent >= 0;