use of com.intellij.psi.PsiParameter in project intellij-community by JetBrains.
the class ConstructorBodyGeneratorBase method start.
@Override
public StringBuilder start(StringBuilder buffer, @NotNull String name, @NotNull PsiParameter[] parameters) {
buffer.append("public ").append(name).append("(");
for (PsiParameter parameter : parameters) {
buffer.append(parameter.getType().getPresentableText()).append(' ').append(parameter.getName()).append(',');
}
if (parameters.length > 0) {
buffer.delete(buffer.length() - 1, buffer.length());
}
buffer.append("){\n");
return buffer;
}
use of com.intellij.psi.PsiParameter in project intellij-community by JetBrains.
the class RemoveUnusedParameterFix method invoke.
@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
final PsiParameter myParameter = (PsiParameter) startElement;
if (!FileModificationService.getInstance().prepareFileForWrite(myParameter.getContainingFile()))
return;
removeReferences(myParameter);
}
use of com.intellij.psi.PsiParameter in project intellij-community by JetBrains.
the class IntroduceParameterSettingsUI method createRemoveParamsPanel.
protected void createRemoveParamsPanel(GridBagConstraints gbConstraints, JPanel panel) {
final JCheckBox[] removeParamsCb = new JCheckBox[myParametersToRemove.length];
for (int i = 0; i < myParametersToRemove.length; i++) {
PsiParameter parameter = myParametersToRemove[i];
if (parameter == null)
continue;
final NonFocusableCheckBox cb = new NonFocusableCheckBox(RefactoringBundle.message("remove.parameter.0.no.longer.used", parameter.getName()));
removeParamsCb[i] = cb;
cb.setSelected(true);
gbConstraints.gridy++;
panel.add(cb, gbConstraints);
final int i1 = i;
cb.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
myParametersToRemoveChecked[i1] = cb.isSelected();
}
});
myParametersToRemoveChecked[i] = true;
}
updateControls(removeParamsCb);
if (myCbReplaceAllOccurences != null) {
myCbReplaceAllOccurences.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
updateControls(removeParamsCb);
}
});
}
}
use of com.intellij.psi.PsiParameter in project intellij-community by JetBrains.
the class NullParameterConstraintChecker method checkMethodParameters.
@NotNull
static PsiParameter[] checkMethodParameters(PsiMethod method) {
if (method.getBody() == null)
return PsiParameter.EMPTY_ARRAY;
final Collection<PsiParameter> nullableParameters = new SmartList<>();
final PsiParameter[] parameters = method.getParameterList().getParameters();
for (int index = 0; index < parameters.length; index++) {
PsiParameter parameter = parameters[index];
if (!(parameter.getType() instanceof PsiPrimitiveType) && !NullableNotNullManager.isNotNull(parameter) && !NullableNotNullManager.isNullable(parameter) && JavaNullMethodArgumentUtil.hasNullArgument(method, index)) {
nullableParameters.add(parameter);
}
}
if (nullableParameters.isEmpty())
return PsiParameter.EMPTY_ARRAY;
final NullParameterConstraintChecker checker = new NullParameterConstraintChecker(nullableParameters);
checker.analyzeMethod(method.getBody(), new StandardInstructionVisitor());
return checker.myPossiblyViolatedParameters.stream().filter(checker.myUsedParameters::contains).toArray(PsiParameter[]::new);
}
use of com.intellij.psi.PsiParameter in project intellij-community by JetBrains.
the class GrSetStrongTypeIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, final Editor editor) throws IncorrectOperationException {
PsiElement parent = element.getParent();
PsiElement elementToBuildTemplate;
GrVariable[] variables;
if (parent instanceof GrVariable && parent.getParent() instanceof GrVariableDeclaration) {
variables = ((GrVariableDeclaration) parent.getParent()).getVariables();
elementToBuildTemplate = parent.getParent();
} else if (parent instanceof GrVariable && parent.getParent() instanceof GrForInClause) {
variables = new GrVariable[] { (GrVariable) parent };
elementToBuildTemplate = parent.getParent().getParent();
} else if (parent instanceof GrVariableDeclaration) {
variables = ((GrVariableDeclaration) parent).getVariables();
elementToBuildTemplate = parent;
} else if (parent instanceof GrParameter && parent.getParent() instanceof GrParameterList) {
variables = new GrVariable[] { (GrVariable) parent };
elementToBuildTemplate = parent.getParent().getParent();
} else if (parent instanceof GrVariable) {
variables = new GrVariable[] { ((GrVariable) parent) };
elementToBuildTemplate = parent;
} else {
return;
}
ArrayList<TypeConstraint> types = new ArrayList<>();
if (parent.getParent() instanceof GrForInClause) {
types.add(SupertypeConstraint.create(PsiUtil.extractIteratedType((GrForInClause) parent.getParent())));
} else {
for (GrVariable variable : variables) {
GrExpression initializer = variable.getInitializerGroovy();
if (initializer != null) {
PsiType type = initializer.getType();
if (type != null) {
types.add(SupertypeConstraint.create(type));
}
}
if (variable instanceof GrParameter) {
final PsiParameter parameter = (PsiParameter) variable;
final PsiType type = getClosureParameterType(parameter);
if (type != null) {
types.add(SupertypeConstraint.create(type));
}
}
}
}
final String originalText = elementToBuildTemplate.getText();
final TypeInfo typeInfo = getOrCreateTypeElement(parent, elementToBuildTemplate);
final PsiElement replaceElement = typeInfo.elementToReplace;
TypeConstraint[] constraints = types.toArray(new TypeConstraint[types.size()]);
ChooseTypeExpression chooseTypeExpression = new ChooseTypeExpression(constraints, element.getManager(), replaceElement.getResolveScope());
TemplateBuilderImpl builder = new TemplateBuilderImpl(elementToBuildTemplate);
builder.replaceElement(replaceElement, chooseTypeExpression);
final Document document = editor.getDocument();
final RangeMarker rangeMarker = document.createRangeMarker(elementToBuildTemplate.getTextRange());
rangeMarker.setGreedyToRight(true);
rangeMarker.setGreedyToLeft(true);
final PsiElement afterPostprocess = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(elementToBuildTemplate);
final Template template = builder.buildTemplate();
TextRange range = afterPostprocess.getTextRange();
document.deleteString(range.getStartOffset(), range.getEndOffset());
TemplateManager templateManager = TemplateManager.getInstance(project);
templateManager.startTemplate(editor, template, new TemplateEditingAdapter() {
@Override
public void templateFinished(Template template, boolean brokenOff) {
if (brokenOff) {
ApplicationManager.getApplication().runWriteAction(() -> {
if (rangeMarker.isValid()) {
document.replaceString(rangeMarker.getStartOffset(), rangeMarker.getEndOffset(), originalText);
editor.getCaretModel().moveToOffset(rangeMarker.getStartOffset() + typeInfo.originalOffset);
}
});
}
}
});
}
Aggregations