simplify UnindentingInsertHandler by not using ParentMatcher

This commit is contained in:
Dmitry Jemerov
2011-09-12 18:20:07 +02:00
parent e8eff77645
commit a4f77c545b
@@ -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<PythonLookupElement> {
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<PythonLookupEleme
int nonspace_offset = findBeginning(line_start_offset, text);
Matcher matcher = null;
Class<? extends PsiElement> 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<PythonLookupEleme
final int else_len = "else".length();
if (local_length >= 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<? extends PsiElement> 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;