PY-18522 Detection of empty parent block takes comments into account

This commit is contained in:
Mikhail Golubev
2016-04-05 17:04:19 +03:00
parent 5744c4792c
commit e3c9f28909
5 changed files with 33 additions and 9 deletions
@@ -23,10 +23,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.CharFilter;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiErrorElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.psi.tree.IElementType;
@@ -184,11 +181,11 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor {
}
@Nullable
private static PyStatementList findEmptyStatementListNearby(@NotNull PsiElement ws) {
PyStatementList statementList = ObjectUtils.chooseNotNull(as(ws.getNextSibling(), PyStatementList.class),
as(ws.getPrevSibling(), PyStatementList.class));
private static PyStatementList findEmptyStatementListNearby(@NotNull PsiElement whitespace) {
PyStatementList statementList = ObjectUtils.chooseNotNull(as(whitespace.getNextSibling(), PyStatementList.class),
as(whitespace.getPrevSibling(), PyStatementList.class));
if (statementList == null) {
final PsiElement prevLeaf = PsiTreeUtil.prevLeaf(ws, false);
final PsiElement prevLeaf = getPrevNonCommentLeaf(whitespace);
if (prevLeaf instanceof PsiErrorElement) {
statementList = as(prevLeaf.getParent(), PyStatementList.class);
}
@@ -198,7 +195,7 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor {
@Nullable
private static PyStatementListContainer getDeepestPossibleParentBlock(@NotNull PsiElement whitespace) {
final PsiElement prevLeaf = PsiTreeUtil.prevVisibleLeaf(whitespace);
final PsiElement prevLeaf = getPrevNonCommentLeaf(whitespace);
return PsiTreeUtil.getParentOfType(prevLeaf, PyStatementListContainer.class);
}
@@ -214,6 +211,15 @@ public class PythonCopyPasteProcessor implements CopyPastePreProcessor {
return false;
}
@Nullable
private static PsiElement getPrevNonCommentLeaf(@NotNull PsiElement element) {
PsiElement anchor = PsiTreeUtil.prevLeaf(element);
while (anchor instanceof PsiComment || anchor instanceof PsiWhiteSpace) {
anchor = PsiTreeUtil.prevLeaf(anchor, false);
}
return anchor;
}
private static boolean inStatementList(@NotNull final PsiFile file, int caretOffset) {
final PsiElement element = file.findElementAt(caretOffset);
return PsiTreeUtil.getParentOfType(element, PyStatementListContainer.class) != null;
@@ -0,0 +1,6 @@
def func():
try:
pass
except:
# comment
x = 42
@@ -0,0 +1,6 @@
def func():
try:
pass
except:
# comment
<caret>
@@ -0,0 +1 @@
<selection>x = 42</selection>
@@ -402,6 +402,11 @@ public class PyCopyPasteTest extends PyTestCase {
doTest();
}
// PY-18522
public void testEmptyParentBlockWithCommentInside() {
doTest();
}
// PY-19064
public void testAmbiguousParentBlockSmallestIndent() {
doTest();