diff --git a/python/src/com/jetbrains/python/refactoring/surround/surrounders/statements/PyWithTryExceptSurrounder.java b/python/src/com/jetbrains/python/refactoring/surround/surrounders/statements/PyWithTryExceptSurrounder.java index 5cdaba21d54f..25a9f26308d3 100644 --- a/python/src/com/jetbrains/python/refactoring/surround/surrounders/statements/PyWithTryExceptSurrounder.java +++ b/python/src/com/jetbrains/python/refactoring/surround/surrounders/statements/PyWithTryExceptSurrounder.java @@ -22,7 +22,6 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; -import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; import com.jetbrains.python.PyBundle; @@ -53,9 +52,10 @@ public class PyWithTryExceptSurrounder extends PyStatementSurrounder { final PsiFile psiFile = parent.getContainingFile(); final Document document = psiFile.getViewProvider().getDocument(); - final RangeMarker rangeMarker = document.createRangeMarker(tryStatement.getTextRange()); + final TextRange range = tryStatement.getTextRange(); + assert document != null; + final RangeMarker rangeMarker = document.createRangeMarker(range); - CodeStyleManager.getInstance(project).reformat(psiFile); final PsiElement element = psiFile.findElementAt(rangeMarker.getStartOffset()); tryStatement = PsiTreeUtil.getParentOfType(element, PyTryExceptStatement.class); if (tryStatement != null) { @@ -70,7 +70,9 @@ public class PyWithTryExceptSurrounder extends PyStatementSurrounder { protected TextRange getResultRange(PyTryExceptStatement tryStatement) { final PyExceptPart part = tryStatement.getExceptParts()[0]; - return part.getStatementList().getTextRange(); + final PyStatementList list = part.getStatementList(); + assert list != null; + return list.getTextRange(); } public String getTemplateDescription() { diff --git a/python/src/com/jetbrains/python/refactoring/surround/surrounders/statements/PyWithTryFinallySurrounder.java b/python/src/com/jetbrains/python/refactoring/surround/surrounders/statements/PyWithTryFinallySurrounder.java index 54e2c27acaa9..d457e32232c2 100644 --- a/python/src/com/jetbrains/python/refactoring/surround/surrounders/statements/PyWithTryFinallySurrounder.java +++ b/python/src/com/jetbrains/python/refactoring/surround/surrounders/statements/PyWithTryFinallySurrounder.java @@ -17,6 +17,8 @@ package com.jetbrains.python.refactoring.surround.surrounders.statements; import com.intellij.codeInsight.CodeInsightBundle; import com.intellij.openapi.util.TextRange; +import com.jetbrains.python.psi.PyFinallyPart; +import com.jetbrains.python.psi.PyStatementList; import com.jetbrains.python.psi.PyTryExceptStatement; /** @@ -37,6 +39,10 @@ public class PyWithTryFinallySurrounder extends PyWithTryExceptSurrounder { @Override protected TextRange getResultRange(PyTryExceptStatement tryStatement) { - return tryStatement.getFinallyPart().getStatementList().getTextRange(); + final PyFinallyPart finallyPart = tryStatement.getFinallyPart(); + assert finallyPart != null; + final PyStatementList statementList = finallyPart.getStatementList(); + assert statementList != null; + return statementList.getTextRange(); } } diff --git a/python/testData/surround/SurroundWithTryExcept.py b/python/testData/surround/SurroundWithTryExcept.py index faee07e103c1..ee6e2cbc8b8c 100644 --- a/python/testData/surround/SurroundWithTryExcept.py +++ b/python/testData/surround/SurroundWithTryExcept.py @@ -1,2 +1,2 @@ def foo(): - print "hello" \ No newline at end of file + print "hello" \ No newline at end of file diff --git a/python/testData/surround/SurroundWithTryExcept_after.py b/python/testData/surround/SurroundWithTryExcept_after.py index 54b20bc2004d..ae244ffec0cc 100644 --- a/python/testData/surround/SurroundWithTryExcept_after.py +++ b/python/testData/surround/SurroundWithTryExcept_after.py @@ -2,4 +2,4 @@ def foo(): try: print "hello" except: - pass \ No newline at end of file + pass diff --git a/python/testSrc/com/jetbrains/python/PySurroundWithTest.java b/python/testSrc/com/jetbrains/python/PySurroundWithTest.java index b2a1292a777a..5f49581f1e71 100644 --- a/python/testSrc/com/jetbrains/python/PySurroundWithTest.java +++ b/python/testSrc/com/jetbrains/python/PySurroundWithTest.java @@ -35,7 +35,7 @@ public class PySurroundWithTest extends PyTestCase { doTest(new PyWithWhileSurrounder()); } - public void _testSurroundWithTryExcept() throws Exception { + public void testSurroundWithTryExcept() throws Exception { doTest(new PyWithTryExceptSurrounder()); }