From 2e89ac43f1a99798e0919006bdd300a8c07d66eb Mon Sep 17 00:00:00 2001 From: Oleg Shpynov Date: Wed, 27 Jan 2010 11:44:06 +0300 Subject: [PATCH] Fix bundle properties --- python/src/com/jetbrains/python/PyBundle.properties | 8 ++++++++ .../codeFragment/PyCodeFragmentBuilder.java | 12 +++++++++++- .../codeInsight/codeFragment/PyCodeFragmentUtil.java | 8 +++----- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/python/src/com/jetbrains/python/PyBundle.properties b/python/src/com/jetbrains/python/PyBundle.properties index a176eabbaba7..b4a88f29e171 100644 --- a/python/src/com/jetbrains/python/PyBundle.properties +++ b/python/src/com/jetbrains/python/PyBundle.properties @@ -132,6 +132,13 @@ refactoring.push.down.error.cannot.perform.refactoring.using.selected.elements=C refactoring.push.down.error.cannot.perform.refactoring.not.inside.class=Cannot perform pull member down: not inside the class +# extract method +refactoring.extract.method.error.cannot.perform.refactoring.when.class.declaration.inside=Cannot perform refactoring with class declaration inside code block +refactoring.extract.method.error.cannot.perform.refactoring.when.function.declaration.inside=Cannot perform refactoring with function declaration inside code block +refactoring.extract.method.error.cannot.perform.refactoring.no.corresponding.loop.for.break=No corresponding loop for break statement inside code fragment +refactoring.extract.method.error.cannot.perform.refactoring.no.corresponding.loop.for.continue=No corresponding loop for continue statement inside code fragment +refactoring.extract.method.error.cannot.perform.refactoring.when.execution.flow.is.interrupted=Cannot perform refactoring when execution flow is interrupted + ### Annotators ### ANN.deleting.none=deleting None ANN.assign.to.none=assignment to None @@ -233,3 +240,4 @@ runcfg.labels.interpreter_options=Interpreter &options: runcfg.labels.working_directory=&Working directory: runcfg.captions.script_parameters_dialog=Enter script parameters runcfg.captions.interpreter_options_dialog=Enter interpreter options + diff --git a/python/src/com/jetbrains/python/codeInsight/codeFragment/PyCodeFragmentBuilder.java b/python/src/com/jetbrains/python/codeInsight/codeFragment/PyCodeFragmentBuilder.java index ee37a0c21938..0d058239e765 100644 --- a/python/src/com/jetbrains/python/codeInsight/codeFragment/PyCodeFragmentBuilder.java +++ b/python/src/com/jetbrains/python/codeInsight/codeFragment/PyCodeFragmentBuilder.java @@ -4,6 +4,8 @@ import com.intellij.codeInsight.codeFragment.CodeFragmentUtil; import com.intellij.codeInsight.codeFragment.Position; import com.intellij.psi.PsiElement; import com.intellij.psi.ResolveResult; +import com.intellij.psi.util.PsiTreeUtil; +import com.jetbrains.python.codeInsight.controlflow.ScopeOwner; import com.jetbrains.python.psi.*; import java.util.*; @@ -17,10 +19,12 @@ public class PyCodeFragmentBuilder extends PyRecursiveElementVisitor { final Set inElements = new HashSet(); final Set outElements = new HashSet(); + private final ScopeOwner myOwner; private final int startOffset; private final int endOffset; - public PyCodeFragmentBuilder(int start, int end) { + public PyCodeFragmentBuilder(final ScopeOwner owner, final int start, final int end) { + myOwner = owner; startOffset = start; endOffset = end; } @@ -44,6 +48,9 @@ public class PyCodeFragmentBuilder extends PyRecursiveElementVisitor { if (position == Position.INSIDE) { for (ResolveResult result : element.multiResolve(false)) { final PsiElement declaration = result.getElement(); + if (declaration == null || !PsiTreeUtil.isAncestor(myOwner, declaration, false)){ + continue; + } final Position pos = CodeFragmentUtil.getPosition(declaration, startOffset, endOffset); // If declaration is before add it to input if (pos == Position.BEFORE) { @@ -61,6 +68,9 @@ public class PyCodeFragmentBuilder extends PyRecursiveElementVisitor { } for (ResolveResult result : element.multiResolve(false)) { final PsiElement declaration = result.getElement(); + if (declaration == null || !PsiTreeUtil.isAncestor(myOwner, declaration, false)){ + continue; + } final Position pos = CodeFragmentUtil.getPosition(declaration, startOffset, endOffset); // If declaration is inside if (pos == Position.INSIDE) { diff --git a/python/src/com/jetbrains/python/codeInsight/codeFragment/PyCodeFragmentUtil.java b/python/src/com/jetbrains/python/codeInsight/codeFragment/PyCodeFragmentUtil.java index 1197f5f1e9cd..da2689a6ec58 100644 --- a/python/src/com/jetbrains/python/codeInsight/codeFragment/PyCodeFragmentUtil.java +++ b/python/src/com/jetbrains/python/codeInsight/codeFragment/PyCodeFragmentUtil.java @@ -41,7 +41,7 @@ public class PyCodeFragmentUtil { @Override public void visitPyFunction(final PyFunction node) { if (CodeFragmentUtil.getPosition(node, start, end) == Position.INSIDE){ - throw new CannotCreateCodeFragmentException(PyBundle.message("refactoring.extract.method.error.cannot.perform.refactoring.when.class.declaration.inside")); + throw new CannotCreateCodeFragmentException(PyBundle.message("refactoring.extract.method.error.cannot.perform.refactoring.when.function.declaration.inside")); } } }); @@ -104,10 +104,8 @@ public class PyCodeFragmentUtil { } // Building code fragment - final PyCodeFragmentBuilder builder = new PyCodeFragmentBuilder(start, end); + final PyCodeFragmentBuilder builder = new PyCodeFragmentBuilder(owner, start, end); owner.acceptChildren(builder); return new CodeFragment(builder.inElements, builder.outElements, returnInstructionInside); } - - -} +} \ No newline at end of file