private static RemoveUnusedVariableUtil.RemoveMode showSideEffectsWarning(List<PsiElement> sideEffects, PsiVariable variable, Editor editor, boolean canCopeWithSideEffects) { String text; if (sideEffects.isEmpty()) { text = ""; } else { final PsiElement sideEffect = sideEffects.get(0); if (sideEffect instanceof PsiExpression) { text = PsiExpressionTrimRenderer.render((PsiExpression)sideEffect); } else { text = sideEffect.getText(); } } return showSideEffectsWarning(sideEffects, variable, editor, canCopeWithSideEffects, text, text); }
@NotNull @Override public String getElementInfo(@NotNull PsiElement e) { if(e instanceof PsiLambdaExpression) { return PsiExpressionTrimRenderer.render((PsiExpression) e); } else if(e instanceof PsiAnonymousClass) { String name = ((PsiAnonymousClass) e).getBaseClassReference().getReferenceName(); return "new " + notNullize(name, "class"); } String description = ElementDescriptionUtil.getElementDescription(e, UsageViewShortNameLocation.INSTANCE); String suffix = e instanceof PsiParameterListOwner ? "()" : null; return suffix != null ? description + suffix : description; }
@Override public String getElementPresentation(PsiElement element) { final PsiElement parent = element.getParent(); if (parent instanceof PsiFunctionalExpression) { return PsiExpressionTrimRenderer.render((PsiExpression)parent); } return super.getElementPresentation(element); }
@Override public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) { final PsiIfStatement ifStatement = PsiTreeUtil.getParentOfType(element, PsiIfStatement.class); if (ifStatement == null || ifStatement.getCondition() == null) { return false; } final PsiExpression condition = ifStatement.getCondition(); if (condition == null || !(condition instanceof PsiPolyadicExpression)) { return false; } final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)condition; final PsiType expressionType = polyadicExpression.getType(); if (expressionType == null || !PsiType.BOOLEAN.isAssignableFrom(expressionType)) { return false; } final IElementType operation = polyadicExpression.getOperationTokenType(); if (operation != JavaTokenType.OROR && operation != JavaTokenType.ANDAND) { return false; } final PsiExpression operand = findOperand(element, polyadicExpression); if (operand == null) { return false; } setText(CodeInsightBundle.message("intention.extract.if.condition.text", PsiExpressionTrimRenderer.render(operand))); return true; }
public static void selectAndPass(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile file, @NotNull final Pass<PsiElement[]> callback) { editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); if (!editor.getSelectionModel().hasSelection()) { final int offset = editor.getCaretModel().getOffset(); final List<PsiExpression> expressions = IntroduceVariableBase.collectExpressions(file, editor, offset, true); if (expressions.isEmpty()) { editor.getSelectionModel().selectLineAtCaret(); } else if (expressions.size() == 1) { callback.pass(new PsiElement[]{expressions.get(0)}); return; } else { IntroduceTargetChooser.showChooser(editor, expressions, new Pass<PsiExpression>() { @Override public void pass(PsiExpression psiExpression) { callback.pass(new PsiElement[]{psiExpression}); } }, new PsiExpressionTrimRenderer.RenderFunction()); return; } } PsiDocumentManager.getInstance(project).commitAllDocuments(); callback.pass(getElements(project, editor, file)); }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { final PsiElement psiInitializer = descriptor.getPsiElement(); if (!(psiInitializer instanceof PsiExpression)) return; if (!(psiInitializer.getParent() instanceof PsiVariable)) return; if (!FileModificationService.getInstance().prepareFileForWrite(psiInitializer.getContainingFile())) return; final PsiVariable variable = (PsiVariable)psiInitializer.getParent(); final PsiDeclarationStatement declaration = (PsiDeclarationStatement)variable.getParent(); final List<PsiElement> sideEffects = new ArrayList<PsiElement>(); boolean hasSideEffects = RemoveUnusedVariableUtil.checkSideEffects(psiInitializer, variable, sideEffects); int res = RemoveUnusedVariableUtil.DELETE_ALL; if (hasSideEffects) { hasSideEffects = PsiUtil.isStatement(psiInitializer); res = RemoveUnusedVariableFix.showSideEffectsWarning(sideEffects, variable, FileEditorManager.getInstance(project).getSelectedTextEditor(), hasSideEffects, sideEffects.get(0).getText(), variable.getTypeElement().getText() + " " + variable.getName() + ";<br>" + PsiExpressionTrimRenderer .render((PsiExpression)psiInitializer)); } try { if (res == RemoveUnusedVariableUtil.DELETE_ALL) { psiInitializer.delete(); } else if (res == RemoveUnusedVariableUtil.MAKE_STATEMENT) { final PsiElementFactory factory = JavaPsiFacade.getInstance(variable.getProject()).getElementFactory(); final PsiStatement statementFromText = factory.createStatementFromText(psiInitializer.getText() + ";", null); declaration.getParent().addAfter(statementFromText, declaration); psiInitializer.delete(); } } catch (IncorrectOperationException e) { LOG.error(e); } }
@Override public String getElementPresentation(PsiElement element) { final PsiElement parent = element.getParent(); if(parent instanceof PsiFunctionalExpression) { return PsiExpressionTrimRenderer.render((PsiExpression) parent); } return super.getElementPresentation(element); }
@Override public void expand(@NotNull PsiElement context, @NotNull final Editor editor) { List<PsiExpression> expressions = getExpressions(context, editor.getDocument(), editor.getCaretModel().getOffset()); if (expressions.isEmpty()) { PostfixTemplatesUtils.showErrorHint(context.getProject(), editor); } else if (expressions.size() == 1) { doIt(editor, expressions.get(0)); } else { IntroduceTargetChooser.showChooser( editor, expressions, new Pass<PsiExpression>() { public void pass(@NotNull final PsiExpression e) { ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { CommandProcessor.getInstance().executeCommand(e.getProject(), new Runnable() { public void run() { doIt(editor, e); } }, "Expand postfix template", PostfixLiveTemplate.POSTFIX_TEMPLATE_ID); } }); } }, new PsiExpressionTrimRenderer.RenderFunction(), "Expressions", 0, ScopeHighlighter.NATURAL_RANGER ); } }
@Override @NotNull public String getText() { return QuickFixBundle.message("change.new.operator.type.text", new PsiExpressionTrimRenderer.RenderFunction().fun(myExpression), myType.getPresentableText(), myType instanceof PsiArrayType ? "" : "()"); }
protected static void sideEffectAwareRemove(Project project, PsiExpression psiInitializer, PsiElement elementToDelete, PsiVariable variable) { if (!FileModificationService.getInstance().prepareFileForWrite(elementToDelete.getContainingFile())) return; final PsiElement declaration = variable.getParent(); final List<PsiElement> sideEffects = new ArrayList<PsiElement>(); boolean hasSideEffects = RemoveUnusedVariableUtil.checkSideEffects(psiInitializer, variable, sideEffects); RemoveUnusedVariableUtil.RemoveMode res = RemoveUnusedVariableUtil.RemoveMode.DELETE_ALL; if (hasSideEffects) { hasSideEffects = PsiUtil.isStatement(psiInitializer); res = RemoveUnusedVariableFix.showSideEffectsWarning(sideEffects, variable, FileEditorManager.getInstance(project).getSelectedTextEditor(), hasSideEffects, sideEffects.get(0).getText(), variable.getTypeElement().getText() + " " + variable.getName() + ";<br>" + PsiExpressionTrimRenderer.render(psiInitializer) ); } try { if (res == RemoveUnusedVariableUtil.RemoveMode.DELETE_ALL) { elementToDelete.delete(); } else if (res == RemoveUnusedVariableUtil.RemoveMode.MAKE_STATEMENT) { final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory(); final PsiStatement statementFromText = factory.createStatementFromText(psiInitializer.getText() + ";", null); final PsiElement parent = elementToDelete.getParent(); if (parent instanceof PsiExpressionStatement) { parent.replace(statementFromText); } else { declaration.getParent().addAfter(statementFromText, declaration); elementToDelete.delete(); } } } catch (IncorrectOperationException e) { LOG.error(e); } }
@Override public String getElementText(NavigatablePsiElement element) { return element instanceof PsiClass ? ClassPresentationUtil.getNameForClass((PsiClass)element, false) : PsiExpressionTrimRenderer.render((PsiExpression)element); }
public String getElementText(NavigatablePsiElement element) { return element instanceof PsiMethod ? myMethodCellRenderer.getElementText((PsiMethod)element) : PsiExpressionTrimRenderer.render((PsiExpression)element); }
public static void processElementToWorkOn(final Editor editor, final PsiFile file, final String refactoringName, final String helpId, final Project project, final ElementsProcessor<ElementToWorkOn> processor) { PsiLocalVariable localVar = null; PsiExpression expr = null; if (!editor.getSelectionModel().hasSelection()) { PsiElement element = TargetElementUtil.findTargetElement(editor, TargetElementUtil .ELEMENT_NAME_ACCEPTED | TargetElementUtil .REFERENCED_ELEMENT_ACCEPTED | TargetElementUtil .LOOKUP_ITEM_ACCEPTED); if (element instanceof PsiLocalVariable) { localVar = (PsiLocalVariable) element; PsiElement elementAt = file.findElementAt(editor.getCaretModel().getOffset()); if (elementAt instanceof PsiIdentifier && elementAt.getParent() instanceof PsiReferenceExpression) { expr = (PsiExpression) elementAt.getParent(); } else { final PsiReference reference = TargetElementUtil.findReference(editor); if (reference != null) { final PsiElement refElement = reference.getElement(); if (refElement instanceof PsiReferenceExpression) { expr = (PsiReferenceExpression)refElement; } } } } else { final PsiLocalVariable variable = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PsiLocalVariable.class); final int offset = editor.getCaretModel().getOffset(); final PsiElement[] statementsInRange = IntroduceVariableBase.findStatementsAtOffset(editor, file, offset); if (statementsInRange.length == 1 && IntroduceVariableBase.selectLineAtCaret(offset, statementsInRange)) { editor.getSelectionModel().selectLineAtCaret(); final ElementToWorkOn elementToWorkOn = getElementToWorkOn(editor, file, refactoringName, helpId, project, localVar, expr); if (elementToWorkOn == null || elementToWorkOn.getLocalVariable() == null && elementToWorkOn.getExpression() == null || !processor.accept(elementToWorkOn)) { editor.getSelectionModel().removeSelection(); } } if (!editor.getSelectionModel().hasSelection()){ final List<PsiExpression> expressions = IntroduceVariableBase.collectExpressions(file, editor, offset); for (Iterator<PsiExpression> iterator = expressions.iterator(); iterator.hasNext(); ) { PsiExpression expression = iterator.next(); if (!processor.accept(new ElementToWorkOn(null, expression))) { iterator.remove(); } } if (expressions.isEmpty()) { editor.getSelectionModel().selectLineAtCaret(); } else if (!IntroduceVariableBase.isChooserNeeded(expressions)) { expr = expressions.get(0); } else { final int selection = IntroduceVariableBase.preferredSelection(statementsInRange, expressions); IntroduceTargetChooser.showChooser(editor, expressions, new Pass<PsiExpression>() { @Override public void pass(final PsiExpression selectedValue) { PsiLocalVariable var = null; //replace var if selected expression == var initializer if (variable != null && variable.getInitializer() == selectedValue) { var = variable; } processor.pass(getElementToWorkOn(editor, file, refactoringName, helpId, project, var, selectedValue)); } }, new PsiExpressionTrimRenderer.RenderFunction(), "Expressions", selection, ScopeHighlighter.NATURAL_RANGER); return; } } } } processor.pass(getElementToWorkOn(editor, file, refactoringName, helpId, project, localVar, expr)); }
public static void selectAndPass(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile file, @NotNull final Pass<PsiElement[]> callback) { editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); if (!editor.getSelectionModel().hasSelection()) { final int offset = editor.getCaretModel().getOffset(); final List<PsiExpression> expressions = IntroduceVariableBase.collectExpressions(file, editor, offset, true); if (expressions.isEmpty()) { editor.getSelectionModel().selectLineAtCaret(); } else if (expressions.size() == 1) { callback.pass(new PsiElement[]{expressions.get(0)}); return; } else { IntroduceTargetChooser.showChooser(editor, expressions, new Pass<PsiExpression>() { @Override public void pass(PsiExpression psiExpression) { callback.pass(new PsiElement[]{psiExpression}); } }, new PsiExpressionTrimRenderer.RenderFunction()); return; } } int startOffset = editor.getSelectionModel().getSelectionStart(); int endOffset = editor.getSelectionModel().getSelectionEnd(); PsiDocumentManager.getInstance(project).commitAllDocuments(); PsiElement[] elements; PsiExpression expr = CodeInsightUtil.findExpressionInRange(file, startOffset, endOffset); if (expr != null) { elements = new PsiElement[]{expr}; } else { elements = CodeInsightUtil.findStatementsInRange(file, startOffset, endOffset); if (elements.length == 0) { final PsiExpression expression = IntroduceVariableBase.getSelectedExpression(project, file, startOffset, endOffset); if (expression != null && IntroduceVariableBase.getErrorMessage(expression) == null) { final PsiType originalType = RefactoringUtil.getTypeByExpressionWithExpectedType(expression); if (originalType != null) { elements = new PsiElement[]{expression}; } } } } callback.pass(elements); }
@Override @NotNull public String getText() { return JavaQuickFixBundle.message("change.new.operator.type.text", new PsiExpressionTrimRenderer.RenderFunction().fun(myExpression), myType.getPresentableText(), myType instanceof PsiArrayType ? "" : "()"); }
protected void sideEffectAwareRemove(Project project, PsiElement psiInitializer, PsiElement elementToDelete, PsiVariable variable) { if(!FileModificationService.getInstance().prepareFileForWrite(elementToDelete.getContainingFile())) { return; } final PsiElement declaration = variable.getParent(); final List<PsiElement> sideEffects = new ArrayList<PsiElement>(); boolean hasSideEffects = RemoveUnusedVariableUtil.checkSideEffects(psiInitializer, variable, sideEffects); int res = RemoveUnusedVariableUtil.DELETE_ALL; if(hasSideEffects) { hasSideEffects = PsiUtil.isStatement(psiInitializer); res = RemoveUnusedVariableFix.showSideEffectsWarning(sideEffects, variable, FileEditorManager.getInstance(project).getSelectedTextEditor (), hasSideEffects, sideEffects.get(0).getText(), variable.getTypeElement().getText() + " " + variable.getName() + ";<br>" + PsiExpressionTrimRenderer.render((PsiExpression) psiInitializer)); } try { if(res == RemoveUnusedVariableUtil.DELETE_ALL) { elementToDelete.delete(); } else if(res == RemoveUnusedVariableUtil.MAKE_STATEMENT) { final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory(); final PsiStatement statementFromText = factory.createStatementFromText(psiInitializer.getText() + ";", null); final PsiElement parent = elementToDelete.getParent(); if(parent instanceof PsiExpressionStatement) { parent.replace(statementFromText); } else { declaration.getParent().addAfter(statementFromText, declaration); elementToDelete.delete(); } } } catch(IncorrectOperationException e) { LOG.error(e); } }
@Override public String getElementText(NavigatablePsiElement element) { return element instanceof PsiClass ? ClassPresentationUtil.getNameForClass((PsiClass) element, false) : PsiExpressionTrimRenderer.render((PsiExpression) element); }
public String getElementText(NavigatablePsiElement element) { return element instanceof PsiMethod ? myMethodCellRenderer.getElementText((PsiMethod) element) : PsiExpressionTrimRenderer.render((PsiExpression) element); }
public static void selectAndPass(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile file, @NotNull final Pass<PsiElement[]> callback) { editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); if(!editor.getSelectionModel().hasSelection()) { final int offset = editor.getCaretModel().getOffset(); final List<PsiExpression> expressions = IntroduceVariableBase.collectExpressions(file, editor, offset, true); if(expressions.isEmpty()) { editor.getSelectionModel().selectLineAtCaret(); } else if(expressions.size() == 1) { callback.pass(new PsiElement[]{expressions.get(0)}); return; } else { IntroduceTargetChooser.showChooser(editor, expressions, new Pass<PsiExpression>() { @Override public void pass(PsiExpression psiExpression) { callback.pass(new PsiElement[]{psiExpression}); } }, new PsiExpressionTrimRenderer.RenderFunction()); return; } } int startOffset = editor.getSelectionModel().getSelectionStart(); int endOffset = editor.getSelectionModel().getSelectionEnd(); PsiDocumentManager.getInstance(project).commitAllDocuments(); PsiElement[] elements; PsiExpression expr = CodeInsightUtil.findExpressionInRange(file, startOffset, endOffset); if(expr != null) { elements = new PsiElement[]{expr}; } else { elements = CodeInsightUtil.findStatementsInRange(file, startOffset, endOffset); if(elements.length == 0) { final PsiExpression expression = IntroduceVariableBase.getSelectedExpression(project, file, startOffset, endOffset); if(expression != null && IntroduceVariableBase.getErrorMessage(expression) == null) { final PsiType originalType = RefactoringUtil.getTypeByExpressionWithExpectedType(expression); if(originalType != null) { elements = new PsiElement[]{expression}; } } } } callback.pass(elements); }
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file, DataContext dataContext) { final SelectionModel selectionModel = editor.getSelectionModel(); if(!selectionModel.hasSelection()) { final int offset = editor.getCaretModel().getOffset(); final PsiElement[] statementsInRange = findStatementsAtOffset(editor, file, offset); //try line selection if(statementsInRange.length == 1 && (!PsiUtil.isStatement(statementsInRange[0]) || statementsInRange[0].getTextRange().getStartOffset() > offset || statementsInRange[0].getTextRange().getEndOffset() < offset || isPreferStatements())) { selectionModel.selectLineAtCaret(); final PsiExpression expressionInRange = findExpressionInRange(project, file, selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()); if(expressionInRange == null || getErrorMessage(expressionInRange) != null) { selectionModel.removeSelection(); } } if(!selectionModel.hasSelection()) { final List<PsiExpression> expressions = collectExpressions(file, editor, offset); if(expressions.isEmpty()) { selectionModel.selectLineAtCaret(); } else if(expressions.size() == 1) { final TextRange textRange = expressions.get(0).getTextRange(); selectionModel.setSelection(textRange.getStartOffset(), textRange.getEndOffset()); } else { int selection; if(statementsInRange.length == 1 && statementsInRange[0] instanceof PsiExpressionStatement && PsiUtilCore.hasErrorElementChild(statementsInRange[0])) { selection = expressions.indexOf(((PsiExpressionStatement) statementsInRange[0]).getExpression()); } else { PsiExpression expression = expressions.get(0); if(expression instanceof PsiReferenceExpression && ((PsiReferenceExpression) expression).resolve() instanceof PsiLocalVariable) { selection = 1; } else { selection = -1; } } IntroduceTargetChooser.showChooser(editor, expressions, new Pass<PsiExpression>() { public void pass(final PsiExpression selectedValue) { invoke(project, editor, file, selectedValue.getTextRange().getStartOffset(), selectedValue.getTextRange().getEndOffset ()); } }, new PsiExpressionTrimRenderer.RenderFunction(), "Expressions", selection, ScopeHighlighter.NATURAL_RANGER); return; } } } if(invoke(project, editor, file, selectionModel.getSelectionStart(), selectionModel.getSelectionEnd()) && LookupManager.getActiveLookup (editor) == null) { selectionModel.removeSelection(); } }