Search in sources :

Example 1 with FieldConflictsResolver

use of org.jetbrains.plugins.groovy.refactoring.introduce.parameter.java2groovy.FieldConflictsResolver in project intellij-community by JetBrains.

the class GrIntroduceClosureParameterProcessor method changeSignature.

private static void changeSignature(GrClosableBlock block, GrIntroduceParameterSettings settings) {
    final String name = settings.getName();
    final FieldConflictsResolver fieldConflictsResolver = new FieldConflictsResolver(name, block);
    final GrParameter[] parameters = block.getParameters();
    settings.parametersToRemove().forEachDescending(new TIntProcedure() {

        @Override
        public boolean execute(final int paramNum) {
            try {
                PsiParameter param = parameters[paramNum];
                param.delete();
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
            return true;
        }
    });
    final PsiType type = settings.getSelectedType();
    final String typeText = type == null ? null : type.getCanonicalText();
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(settings.getProject());
    GrParameter parameter = factory.createParameter(name, typeText, block);
    parameter.getModifierList().setModifierProperty(PsiModifier.FINAL, settings.declareFinal());
    final GrParameterList parameterList = block.getParameterList();
    final PsiParameter anchorParameter = GroovyIntroduceParameterUtil.getAnchorParameter(parameterList, block.isVarArgs());
    parameter = (GrParameter) parameterList.addAfter(parameter, anchorParameter);
    if (block.getArrow() == null) {
        final PsiElement arrow = block.addAfter(factory.createClosureFromText("{->}").getArrow().copy(), parameterList);
        final PsiElement child = block.getFirstChild().getNextSibling();
        if (PsiImplUtil.isWhiteSpaceOrNls(child)) {
            final String text = child.getText();
            child.delete();
            block.addAfter(factory.createLineTerminator(text), arrow);
        }
    }
    JavaCodeStyleManager.getInstance(parameter.getProject()).shortenClassReferences(parameter);
    fieldConflictsResolver.fix();
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) TIntProcedure(gnu.trove.TIntProcedure) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) FieldConflictsResolver(org.jetbrains.plugins.groovy.refactoring.introduce.parameter.java2groovy.FieldConflictsResolver) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Aggregations

IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 TIntProcedure (gnu.trove.TIntProcedure)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)1 GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)1 FieldConflictsResolver (org.jetbrains.plugins.groovy.refactoring.introduce.parameter.java2groovy.FieldConflictsResolver)1