public PyAssignmentStatement createDeclaration(IntroduceOperation operation) { final Project project = operation.getProject(); final PyExpression initializer = operation.getInitializer(); InitializerTextBuilder builder = new InitializerTextBuilder(); initializer.accept(builder); String assignmentText = operation.getName() + " = " + builder.result(); PsiElement anchor = operation.isReplaceAll() ? findAnchor(operation.getOccurrences()) : PsiTreeUtil.getParentOfType(initializer, PyStatement.class); return createDeclaration(project, assignmentText, anchor); }
protected void performInplaceIntroduce(IntroduceOperation operation) { final PsiElement statement = performRefactoring(operation); if (statement instanceof PyAssignmentStatement) { PyTargetExpression target = (PyTargetExpression) ((PyAssignmentStatement) statement).getTargets()[0]; final List<PsiElement> occurrences = operation.getOccurrences(); final PsiElement occurrence = findOccurrenceUnderCaret(occurrences, operation.getEditor()); PsiElement elementForCaret = occurrence != null ? occurrence : target; operation .getEditor() .getCaretModel() .moveToOffset(elementForCaret.getTextRange().getStartOffset()); final InplaceVariableIntroducer<PsiElement> introducer = new PyInplaceVariableIntroducer(target, operation, occurrences); introducer.performInplaceRefactoring( new LinkedHashSet<String>(operation.getSuggestedNames())); } }
protected void performActionOnElementOccurrences(final IntroduceOperation operation) { final Editor editor = operation.getEditor(); if (editor.getSettings().isVariableInplaceRenameEnabled()) { ensureName(operation); if (operation.isReplaceAll() != null) { performInplaceIntroduce(operation); } else { OccurrencesChooser.simpleChooser(editor) .showChooser( operation.getElement(), operation.getOccurrences(), new Pass<OccurrencesChooser.ReplaceChoice>() { @Override public void pass(OccurrencesChooser.ReplaceChoice replaceChoice) { operation.setReplaceAll(replaceChoice == OccurrencesChooser.ReplaceChoice.ALL); performInplaceIntroduce(operation); } }); } } else { performIntroduceWithDialog(operation); } }
private void performActionOnElement(IntroduceOperation operation) { if (!checkEnabled(operation)) { return; } final PsiElement element = operation.getElement(); final PsiElement parent = element.getParent(); final PyExpression initializer = parent instanceof PyAssignmentStatement ? ((PyAssignmentStatement) parent).getAssignedValue() : (PyExpression) element; operation.setInitializer(initializer); if (initializer != null) { operation.setOccurrences(getOccurrences(element, initializer)); operation.setSuggestedNames(getSuggestedNames(initializer)); } if (operation.getOccurrences().size() == 0) { operation.setReplaceAll(false); } performActionOnElementOccurrences(operation); }