diff --git a/python/psi-api/src/com/jetbrains/python/psi/impl/PyPsiUtils.java b/python/psi-api/src/com/jetbrains/python/psi/impl/PyPsiUtils.java index 9588f483e782..36809880d92b 100644 --- a/python/psi-api/src/com/jetbrains/python/psi/impl/PyPsiUtils.java +++ b/python/psi-api/src/com/jetbrains/python/psi/impl/PyPsiUtils.java @@ -75,7 +75,7 @@ public class PyPsiUtils { } /** - * Find first non-whitespace sibling before given AST node. + * Finds first non-whitespace sibling before given AST node. */ @Nullable public static ASTNode getPrevNonWhitespaceSibling(@NotNull ASTNode node) { @@ -83,7 +83,7 @@ public class PyPsiUtils { } /** - * Find first sibling that is neither comment, nor whitespace before given element. + * Finds first sibling that is neither comment, nor whitespace before given element. * @param strict prohibit returning element itself */ @Nullable @@ -112,7 +112,7 @@ public class PyPsiUtils { } /** - * Find first non-whitespace sibling after given AST node. + * Finds first non-whitespace sibling after given AST node. */ @Nullable public static ASTNode getNextNonWhitespaceSibling(@NotNull ASTNode after) { @@ -120,7 +120,7 @@ public class PyPsiUtils { } /** - * Find first sibling that is neither comment, nor whitespace after given element. + * Finds first sibling that is neither comment, nor whitespace after given element. * @param strict prohibit returning element itself */ @Nullable @@ -131,6 +131,30 @@ public class PyPsiUtils { return PsiTreeUtil.skipSiblingsForward(start, PsiWhiteSpace.class, PsiComment.class); } + /** + * Finds first token after given element that doesn't consist solely of spaces and is not empty (e.g. error marker). + * @param ignoreComments ignore commentaries as well + */ + @Nullable + public static PsiElement getNextSignificantLeaf(@Nullable PsiElement element, boolean ignoreComments) { + while (element != null && StringUtil.isEmptyOrSpaces(element.getText()) || ignoreComments && element instanceof PsiComment) { + element = PsiTreeUtil.nextLeaf(element); + } + return element; + } + + /** + * Finds first token before given element that doesn't consist solely of spaces and is not empty (e.g. error marker). + * @param ignoreComments ignore commentaries as well + */ + @Nullable + public static PsiElement getPrevSignificantLeaf(@Nullable PsiElement element, boolean ignoreComments) { + while (element != null && StringUtil.isEmptyOrSpaces(element.getText()) || ignoreComments && element instanceof PsiComment) { + element = PsiTreeUtil.prevLeaf(element); + } + return element; + } + /** * Finds the closest comma looking for the next comma first and then for the preceding one. */ @@ -436,22 +460,6 @@ public class PyPsiUtils { return result; } - @Nullable - public static PsiElement getSignificantToTheRight(PsiElement element, final boolean ignoreComments) { - while (element != null && StringUtil.isEmptyOrSpaces(element.getText()) || ignoreComments && element instanceof PsiComment) { - element = PsiTreeUtil.nextLeaf(element); - } - return element; - } - - @Nullable - public static PsiElement getSignificantToTheLeft(PsiElement element, final boolean ignoreComments) { - while (element != null && StringUtil.isEmptyOrSpaces(element.getText()) || ignoreComments && element instanceof PsiComment) { - element = PsiTreeUtil.prevLeaf(element); - } - return element; - } - public static int findArgumentIndex(PyCallExpression call, PsiElement argument) { final PyExpression[] args = call.getArguments(); for (int i = 0; i < args.length; i++) { diff --git a/python/src/com/jetbrains/python/refactoring/extractmethod/PyExtractMethodHandler.java b/python/src/com/jetbrains/python/refactoring/extractmethod/PyExtractMethodHandler.java index ac92a559d076..18d4d861e162 100644 --- a/python/src/com/jetbrains/python/refactoring/extractmethod/PyExtractMethodHandler.java +++ b/python/src/com/jetbrains/python/refactoring/extractmethod/PyExtractMethodHandler.java @@ -75,8 +75,8 @@ public class PyExtractMethodHandler implements RefactoringActionHandler { } } // Pass comments and whitespaces - element1 = PyPsiUtils.getSignificantToTheRight(element1, false); - element2 = PyPsiUtils.getSignificantToTheLeft(element2, false); + element1 = PyPsiUtils.getNextSignificantLeaf(element1, false); + element2 = PyPsiUtils.getPrevSignificantLeaf(element2, false); if (element1 == null || element2 == null) { CommonRefactoringUtil.showErrorHint(project, editor, PyBundle.message("refactoring.extract.method.error.bad.selection"), @@ -158,7 +158,7 @@ public class PyExtractMethodHandler implements RefactoringActionHandler { // return elements if they are really first and last elements of statements if (element1 == PsiTreeUtil.getDeepestFirst(statement1) && - element2 == PyPsiUtils.getSignificantToTheLeft(PsiTreeUtil.getDeepestLast(statement2), !(element2 instanceof PsiComment))) { + element2 == PyPsiUtils.getPrevSignificantLeaf(PsiTreeUtil.getDeepestLast(statement2), !(element2 instanceof PsiComment))) { return Couple.of(statement1, statement2); } return null;