diff --git a/python/src/com/jetbrains/python/refactoring/unwrap/PyUnwrapDescriptor.java b/python/src/com/jetbrains/python/refactoring/unwrap/PyUnwrapDescriptor.java index 926fa9cfe744..44c2a260c5fe 100644 --- a/python/src/com/jetbrains/python/refactoring/unwrap/PyUnwrapDescriptor.java +++ b/python/src/com/jetbrains/python/refactoring/unwrap/PyUnwrapDescriptor.java @@ -2,6 +2,12 @@ package com.jetbrains.python.refactoring.unwrap; import com.intellij.codeInsight.unwrap.UnwrapDescriptorBase; import com.intellij.codeInsight.unwrap.Unwrapper; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.SelectionModel; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.jetbrains.python.psi.PyUtil; +import org.jetbrains.annotations.Nullable; /** * user : ktisha @@ -21,4 +27,19 @@ public class PyUnwrapDescriptor extends UnwrapDescriptorBase{ new PyWithUnwrapper() }; } + + @Override + @Nullable + protected PsiElement findTargetElement(Editor editor, PsiFile file) { + int offset = editor.getCaretModel().getOffset(); + PsiElement endElement = PyUtil.findNonWhitespaceAtOffset(file, offset); + SelectionModel selectionModel = editor.getSelectionModel(); + if (selectionModel.hasSelection() && selectionModel.getSelectionStart() < offset) { + PsiElement startElement = PyUtil.findNonWhitespaceAtOffset(file, selectionModel.getSelectionStart()); + if (startElement != null && startElement != endElement && startElement.getTextRange().getEndOffset() == offset) { + return startElement; + } + } + return endElement; + } } diff --git a/python/testData/refactoring/unwrap/endOfStatementUnwrap_after.py b/python/testData/refactoring/unwrap/endOfStatementUnwrap_after.py new file mode 100644 index 000000000000..7d4290a117a4 --- /dev/null +++ b/python/testData/refactoring/unwrap/endOfStatementUnwrap_after.py @@ -0,0 +1 @@ +x = 1 diff --git a/python/testData/refactoring/unwrap/endOfStatementUnwrap_before.py b/python/testData/refactoring/unwrap/endOfStatementUnwrap_before.py new file mode 100644 index 000000000000..09a36679149a --- /dev/null +++ b/python/testData/refactoring/unwrap/endOfStatementUnwrap_before.py @@ -0,0 +1,2 @@ +while True: + x = 1 diff --git a/python/testSrc/com/jetbrains/python/refactoring/PyUnwrapperTest.java b/python/testSrc/com/jetbrains/python/refactoring/PyUnwrapperTest.java index fba356e2c829..160d88bc0140 100644 --- a/python/testSrc/com/jetbrains/python/refactoring/PyUnwrapperTest.java +++ b/python/testSrc/com/jetbrains/python/refactoring/PyUnwrapperTest.java @@ -41,6 +41,8 @@ public class PyUnwrapperTest extends PyTestCase { public void testWithUnwrap() throws Throwable {doTest(LanguageLevel.PYTHON32);} + public void testEndOfStatementUnwrap() throws Throwable {doTest();} + private void doTest() { doTest(0); }