@Override protected void updateTitle(@Nullable final PsiVariable variable, final String value) { final PsiElement declarationScope = variable != null ? ((PsiParameter) variable).getDeclarationScope() : null; if (declarationScope instanceof PsiMethod) { final PsiMethod psiMethod = (PsiMethod) declarationScope; final StringBuilder buf = new StringBuilder(); buf.append(psiMethod.getName()).append(" ("); boolean frst = true; final List<TextRange> ranges2Remove = new ArrayList<>(); TextRange addedRange = null; for (PsiParameter parameter : psiMethod.getParameterList().getParameters()) { if (frst) { frst = false; } else { buf.append(", "); } int startOffset = buf.length(); if (myMustBeFinal || myPanel.isGenerateFinal()) { buf.append("final "); } buf.append(parameter.getType().getPresentableText()) .append(" ") .append(variable == parameter ? value : parameter.getName()); int endOffset = buf.length(); if (variable == parameter) { addedRange = new TextRange(startOffset, endOffset); } else if (myPanel.isParamToRemove(parameter)) { ranges2Remove.add(new TextRange(startOffset, endOffset)); } } buf.append(")"); setPreviewText(buf.toString()); final MarkupModel markupModel = DocumentMarkupModel.forDocument(getPreviewEditor().getDocument(), myProject, true); markupModel.removeAllHighlighters(); for (TextRange textRange : ranges2Remove) { markupModel.addRangeHighlighter( textRange.getStartOffset(), textRange.getEndOffset(), 0, getTestAttributesForRemoval(), HighlighterTargetArea.EXACT_RANGE); } markupModel.addRangeHighlighter( addedRange.getStartOffset(), addedRange.getEndOffset(), 0, getTextAttributesForAdd(), HighlighterTargetArea.EXACT_RANGE); revalidate(); } }
protected void performIntroduce() { boolean isDeleteLocalVariable = false; PsiExpression parameterInitializer = myExpr; if (getLocalVariable() != null) { if (myPanel.isUseInitializer()) { parameterInitializer = getLocalVariable().getInitializer(); } isDeleteLocalVariable = myPanel.isDeleteLocalVariable(); } final TIntArrayList parametersToRemove = myPanel.getParametersToRemove(); final IntroduceParameterProcessor processor = new IntroduceParameterProcessor( myProject, myMethod, myMethodToSearchFor, parameterInitializer, myExpr, (PsiLocalVariable) getLocalVariable(), isDeleteLocalVariable, getInputName(), myPanel.isReplaceAllOccurences(), myPanel.getReplaceFieldsWithGetters(), myMustBeFinal || myPanel.isGenerateFinal(), isGenerateDelegate(), getType(), parametersToRemove); final Runnable runnable = new Runnable() { public void run() { final Runnable performRefactoring = new Runnable() { public void run() { processor.setPrepareSuccessfulSwingThreadCallback( new Runnable() { @Override public void run() {} }); processor.run(); normalizeParameterIdxAccordingToRemovedParams(parametersToRemove); final PsiParameter parameter = getParameter(); if (parameter != null) { InplaceIntroduceParameterPopup.super.saveSettings(parameter); } } }; if (ApplicationManager.getApplication().isUnitTestMode()) { performRefactoring.run(); } else { ApplicationManager.getApplication().invokeLater(performRefactoring); } } }; CommandProcessor.getInstance().executeCommand(myProject, runnable, getCommandName(), null); }
InplaceIntroduceParameterPopup( final Project project, final Editor editor, final TypeSelectorManagerImpl typeSelectorManager, final PsiExpression expr, final PsiLocalVariable localVar, final PsiMethod method, final PsiMethod methodToSearchFor, final PsiExpression[] occurrences, final TIntArrayList parametersToRemove, final boolean mustBeFinal) { super( project, editor, expr, localVar, occurrences, typeSelectorManager, IntroduceParameterHandler.REFACTORING_NAME); myMethod = method; myMethodToSearchFor = methodToSearchFor; myMustBeFinal = mustBeFinal; myWholePanel.add( getPreviewComponent(), new GridBagConstraints( 0, GridBagConstraints.RELATIVE, 2, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, JBUI.insets(0, 5), 0, 0)); myPanel = new InplaceIntroduceParameterUI( project, localVar, expr, method, parametersToRemove, typeSelectorManager, myOccurrences) { @Override protected PsiParameter getParameter() { return InplaceIntroduceParameterPopup.this.getParameter(); } @Override protected void updateControls(JCheckBox[] removeParamsCb) { super.updateControls(removeParamsCb); if (myParameterIndex < 0) return; restartInplaceIntroduceTemplate(); } protected TIntArrayList getParametersToRemove() { TIntArrayList parameters = new TIntArrayList(); if (myCbReplaceAllOccurences == null || myCbReplaceAllOccurences.isSelected()) { for (int i = 0; i < myParametersToRemove.length; i++) { if (myParametersToRemove[i] != null) { parameters.add(i); } } } return parameters; } }; myPanel.appendOccurrencesDelegate(myWholePanel); }
public void setReplaceAllOccurrences(boolean replaceAll) { myPanel.setReplaceAllOccurrences(replaceAll); }
public boolean isGenerateDelegate() { return myPanel.isGenerateDelegate(); }
@Override protected void saveSettings(@NotNull PsiVariable psiVariable) { myPanel.saveSettings(JavaRefactoringSettings.getInstance()); }
@Override public boolean isReplaceAllOccurrences() { return myPanel.isReplaceAllOccurences(); }