Search in sources :

Example 1 with GrScriptField

use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrScriptField in project intellij-community by JetBrains.

the class ResolveUtil method findDuplicate.

public static PsiNamedElement findDuplicate(@NotNull GrVariable variable) {
    if (isScriptField(variable)) {
        final String name = variable.getName();
        final GroovyScriptClass script = (GroovyScriptClass) ((GroovyFile) variable.getContainingFile()).getScriptClass();
        assert script != null;
        List<GrField> duplicates = ContainerUtil.filter(script.getFields(), (GrField f) -> {
            if (!(f instanceof GrScriptField))
                return false;
            if (!name.equals(f.getName()))
                return false;
            if (((GrScriptField) f).getOriginalVariable() == variable)
                return false;
            return true;
        });
        return duplicates.size() > 0 ? duplicates.get(0) : null;
    } else {
        PsiNamedElement duplicate = resolveExistingElement(variable, new DuplicateVariablesProcessor(variable), GrVariable.class);
        final PsiElement context1 = variable.getContext();
        if (duplicate == null && variable instanceof GrParameter && context1 != null) {
            final PsiElement context = context1.getContext();
            if (context instanceof GrClosableBlock || context instanceof GrMethod && !(context.getParent() instanceof GroovyFile) || context instanceof GrTryCatchStatement) {
                duplicate = resolveExistingElement(context.getParent(), new DuplicateVariablesProcessor(variable), GrVariable.class);
            }
        }
        if (duplicate instanceof GrLightParameter && "args".equals(duplicate.getName())) {
            return null;
        } else {
            return duplicate;
        }
    }
}
Also used : GrLightParameter(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightParameter) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrScriptField(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrScriptField) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Aggregations

GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)1 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)1 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)1 GrLightParameter (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightParameter)1 GrScriptField (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrScriptField)1 GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)1