diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/TypeIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/TypeIntention.java index e4316174294e..7ca666e00678 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/TypeIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/TypeIntention.java @@ -56,7 +56,7 @@ public abstract class TypeIntention implements IntentionAction { } @Nullable - protected static PyExpression getProblemElement(PsiElement elementAt) { + protected static PyExpression getProblemElement(@Nullable PsiElement elementAt) { PyExpression problemElement = PsiTreeUtil.getParentOfType(elementAt, PyNamedParameter.class, PyReferenceExpression.class); if (problemElement == null) return null; if (problemElement instanceof PyQualifiedExpression) { diff --git a/python/src/com/jetbrains/python/psi/PyUtil.java b/python/src/com/jetbrains/python/psi/PyUtil.java index 3e923c66fbd2..c124e541e711 100644 --- a/python/src/com/jetbrains/python/psi/PyUtil.java +++ b/python/src/com/jetbrains/python/psi/PyUtil.java @@ -9,6 +9,7 @@ import com.intellij.ide.fileTemplates.FileTemplate; import com.intellij.ide.fileTemplates.FileTemplateManager; import com.intellij.lang.ASTFactory; import com.intellij.lang.ASTNode; +import com.intellij.openapi.editor.Document; import com.intellij.openapi.extensions.Extensions; import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleUtil; @@ -1168,11 +1169,17 @@ public class PyUtil { if (element == null) { return null; } - while (caretOffset > 0 && element instanceof PsiWhiteSpace) { + int lineStartOffset = 0; + final Document document = PsiDocumentManager.getInstance(psiFile.getProject()).getDocument(psiFile); + if (document != null) { + int lineNumber = document.getLineNumber(caretOffset); + lineStartOffset = document.getLineStartOffset(lineNumber); + } + while (caretOffset >= lineStartOffset && element instanceof PsiWhiteSpace) { caretOffset--; element = psiFile.findElementAt(caretOffset); } - return element; + return element instanceof PsiWhiteSpace ? null : element; } } diff --git a/python/src/com/jetbrains/python/refactoring/changeSignature/PyChangeSignatureHandler.java b/python/src/com/jetbrains/python/refactoring/changeSignature/PyChangeSignatureHandler.java index f4396ee48e21..57732af7293f 100644 --- a/python/src/com/jetbrains/python/refactoring/changeSignature/PyChangeSignatureHandler.java +++ b/python/src/com/jetbrains/python/refactoring/changeSignature/PyChangeSignatureHandler.java @@ -37,7 +37,7 @@ public class PyChangeSignatureHandler implements ChangeSignatureHandler { @Nullable @Override - public PsiElement findTargetMember(PsiElement element) { + public PsiElement findTargetMember(@Nullable PsiElement element) { final PyCallExpression callExpression = PsiTreeUtil.getParentOfType(element, PyCallExpression.class); if (callExpression != null) { return callExpression.resolveCalleeFunction(PyResolveContext.defaultContext()); diff --git a/python/testData/refactoring/unwrap/endOfStatementNextLineUnwrap_before.py b/python/testData/refactoring/unwrap/endOfStatementNextLineUnwrap_before.py new file mode 100644 index 000000000000..93009f74cc01 --- /dev/null +++ b/python/testData/refactoring/unwrap/endOfStatementNextLineUnwrap_before.py @@ -0,0 +1,3 @@ +while True: + x = 1 + \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/refactoring/PyUnwrapperTest.java b/python/testSrc/com/jetbrains/python/refactoring/PyUnwrapperTest.java index 160d88bc0140..d35468ce7445 100644 --- a/python/testSrc/com/jetbrains/python/refactoring/PyUnwrapperTest.java +++ b/python/testSrc/com/jetbrains/python/refactoring/PyUnwrapperTest.java @@ -42,6 +42,7 @@ public class PyUnwrapperTest extends PyTestCase { public void testWithUnwrap() throws Throwable {doTest(LanguageLevel.PYTHON32);} public void testEndOfStatementUnwrap() throws Throwable {doTest();} + public void testEndOfStatementNextLineUnwrap() throws Throwable {doNegativeTest();} private void doTest() { doTest(0);