summaryrefslogtreecommitdiff
path: root/python/src/com/jetbrains/python/refactoring
diff options
context:
space:
mode:
Diffstat (limited to 'python/src/com/jetbrains/python/refactoring')
-rw-r--r--python/src/com/jetbrains/python/refactoring/PyRefactoringUtil.java2
-rw-r--r--python/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java9
-rw-r--r--python/src/com/jetbrains/python/refactoring/inline/PyInlineLocalHandler.java2
-rw-r--r--python/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java7
-rw-r--r--python/src/com/jetbrains/python/refactoring/introduce/constant/PyIntroduceConstantHandler.java6
5 files changed, 23 insertions, 3 deletions
diff --git a/python/src/com/jetbrains/python/refactoring/PyRefactoringUtil.java b/python/src/com/jetbrains/python/refactoring/PyRefactoringUtil.java
index e4259d8581bb..fa2b4cf1204b 100644
--- a/python/src/com/jetbrains/python/refactoring/PyRefactoringUtil.java
+++ b/python/src/com/jetbrains/python/refactoring/PyRefactoringUtil.java
@@ -109,7 +109,7 @@ public class PyRefactoringUtil {
final PyElementGenerator generator = PyElementGenerator.getInstance(project);
final LanguageLevel langLevel = LanguageLevel.forElement(element1);
final PyExpression expression = generator.createFromText(langLevel, PyAssignmentStatement.class, "z=" + selection).getAssignedValue();
- if (PsiUtilCore.hasErrorElementChild(expression) || !(expression instanceof PyBinaryExpression)) {
+ if (!(expression instanceof PyBinaryExpression) || PsiUtilCore.hasErrorElementChild(expression)) {
return null;
}
final String parentText = parent.getText();
diff --git a/python/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java b/python/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java
index f4cb9e6a73f3..2cebb3ec2720 100644
--- a/python/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java
+++ b/python/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java
@@ -47,6 +47,15 @@ import static com.jetbrains.python.inspections.PyStringFormatParser.parsePercent
* @author Dennis.Ushakov
*/
public class PyReplaceExpressionUtil implements PyElementTypes {
+ /**
+ * This marker is added in cases where valid selection nevertheless breaks existing expression.
+ * It can happen in cases like (here {@code <start> and <end>} represent selection boundaries):
+ * <ul>
+ * <li>Selection conflicts with operator precedence: {@code n = 1 * <start>2 + 3<end>}</li>
+ * <li>Selection conflicts with operator associativity: {@code n = 1 + <start>2 + 3<end>}</li>
+ * <li>Part of string literal is selected: {@code s = 'green <start>eggs<end> and ham'}</li>
+ * </ul>
+ */
public static final Key<Pair<PsiElement, TextRange>> SELECTION_BREAKS_AST_NODE =
new Key<Pair<PsiElement, TextRange>>("python.selection.breaks.ast.node");
diff --git a/python/src/com/jetbrains/python/refactoring/inline/PyInlineLocalHandler.java b/python/src/com/jetbrains/python/refactoring/inline/PyInlineLocalHandler.java
index a582b1728d47..575399c61ceb 100644
--- a/python/src/com/jetbrains/python/refactoring/inline/PyInlineLocalHandler.java
+++ b/python/src/com/jetbrains/python/refactoring/inline/PyInlineLocalHandler.java
@@ -134,7 +134,7 @@ public class PyInlineLocalHandler extends InlineActionHandler {
if (editor != null && !ApplicationManager.getApplication().isUnitTestMode()) {
highlightManager.addOccurrenceHighlights(editor, refsToInline, attributes, true, null);
int occurrencesCount = refsToInline.length;
- String occurencesString = RefactoringBundle.message("occurences.string", occurrencesCount);
+ String occurencesString = RefactoringBundle.message("occurrences.string", occurrencesCount);
final String promptKey = "inline.local.variable.prompt";
final String question = RefactoringBundle.message(promptKey, localName) + " " + occurencesString;
RefactoringMessageDialog dialog = new RefactoringMessageDialog(REFACTORING_NAME, question, HELP_ID, "OptionPane.questionIcon", true, project);
diff --git a/python/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java b/python/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java
index 29fc44105857..00448c8b69bd 100644
--- a/python/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java
+++ b/python/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java
@@ -69,6 +69,11 @@ import static com.jetbrains.python.inspections.PyStringFormatParser.*;
abstract public class IntroduceHandler implements RefactoringActionHandler {
protected static PsiElement findAnchor(List<PsiElement> occurrences) {
PsiElement anchor = occurrences.get(0);
+ final Pair<PsiElement, TextRange> data = anchor.getUserData(PyReplaceExpressionUtil.SELECTION_BREAKS_AST_NODE);
+ // Search anchor in the origin file, not in dummy.py, if selection breaks statement and thus element was generated
+ if (data != null && occurrences.size() == 1) {
+ return PsiTreeUtil.getParentOfType(data.getFirst(), PyStatement.class);
+ }
next:
do {
final PyStatement statement = PsiTreeUtil.getParentOfType(anchor, PyStatement.class);
@@ -192,7 +197,7 @@ abstract public class IntroduceHandler implements RefactoringActionHandler {
String text = expression.getText();
final Pair<PsiElement, TextRange> selection = expression.getUserData(PyReplaceExpressionUtil.SELECTION_BREAKS_AST_NODE);
if (selection != null) {
- text = selection.getSecond().substring(text);
+ text = selection.getSecond().substring(selection.getFirst().getText());
}
if (expression instanceof PyCallExpression) {
final PyExpression callee = ((PyCallExpression)expression).getCallee();
diff --git a/python/src/com/jetbrains/python/refactoring/introduce/constant/PyIntroduceConstantHandler.java b/python/src/com/jetbrains/python/refactoring/introduce/constant/PyIntroduceConstantHandler.java
index af9ff583e3e4..44fdee8abdf7 100644
--- a/python/src/com/jetbrains/python/refactoring/introduce/constant/PyIntroduceConstantHandler.java
+++ b/python/src/com/jetbrains/python/refactoring/introduce/constant/PyIntroduceConstantHandler.java
@@ -24,6 +24,7 @@ import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
import com.jetbrains.python.codeInsight.imports.AddImportHelper;
import com.jetbrains.python.psi.PyExpression;
import com.jetbrains.python.psi.PyFile;
+import com.jetbrains.python.psi.PyParameterList;
import com.jetbrains.python.refactoring.PyReplaceExpressionUtil;
import com.jetbrains.python.refactoring.introduce.IntroduceHandler;
import com.jetbrains.python.refactoring.introduce.IntroduceOperation;
@@ -66,6 +67,11 @@ public class PyIntroduceConstantHandler extends IntroduceHandler {
}
@Override
+ protected boolean isValidIntroduceContext(PsiElement element) {
+ return super.isValidIntroduceContext(element) || PsiTreeUtil.getParentOfType(element, PyParameterList.class) != null;
+ }
+
+ @Override
protected String getHelpId() {
return "python.reference.introduceConstant";
}