<commit_before>/*
 * Copyright 2000-2007 JetBrains s.r.o.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jetbrains.plugins.groovy.lang.resolve.processors;

import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField;
import org.jetbrains.plugins.groovy.lang.psi.impl.GroovyResolveResultImpl;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.arithmetic.TypesUtil;
import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;

import java.util.*;

/**
 * @author ven
 * Resolves methods from call expression or function application.
 */
public class MethodResolverProcessor extends ResolverProcessor {
  @Nullable
  PsiType[] myArgumentTypes;

  private Set<GroovyResolveResult> myInapplicableCandidates = new LinkedHashSet<GroovyResolveResult>();
  private boolean myIsConstructor;

  public MethodResolverProcessor(String name, GroovyPsiElement place, boolean forCompletion, boolean isConstructor, PsiType[] argumentTypes) {
    super(name, EnumSet.of(ResolveKind.METHOD, ResolveKind.PROPERTY), place, forCompletion, PsiType.EMPTY_ARRAY);
    myIsConstructor = isConstructor;
    myArgumentTypes = argumentTypes;
  }

  public MethodResolverProcessor(String name, GroovyPsiElement place, boolean forCompletion, boolean isConstructor) {
    this(name, place, forCompletion, isConstructor, PsiUtil.getArgumentTypes(place, isConstructor));
  }

  public boolean execute(PsiElement element, PsiSubstitutor substitutor) {
    if (element instanceof PsiMethod) {
      PsiMethod method = (PsiMethod) element;
      if (method.isConstructor() != myIsConstructor) return true;

      if (!isAccessible((PsiNamedElement) element)) return true;

      if (myForCompletion || PsiUtil.isApplicable(myArgumentTypes, method)) {
        myCandidates.add(new GroovyResolveResultImpl(method, true, myImportStatementContext, substitutor));
      }
      else {
        myInapplicableCandidates.add(new GroovyResolveResultImpl(method, true, myImportStatementContext, substitutor));
      }

      return true;
    } else if (element instanceof PsiVariable) {
      if (element instanceof GrField && ((GrField)element).isProperty() ||
          ((PsiVariable) element).getType().equalsToText("groovy.lang.Closure")) {
        return super.execute(element, substitutor);
      }
    }

    return true;
  }

  public GroovyResolveResult[] getCandidates() {
    if (myCandidates.size() > 0) {
      if (myForCompletion) {
        return myCandidates.toArray(new GroovyResolveResult[myInapplicableCandidates.size()]);
      }<commit_msg>(no message)<commit_after>/*
 * Copyright 2000-2007 JetBrains s.r.o.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jetbrains.plugins.groovy.lang.resolve.processors;

import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField;
import org.jetbrains.plugins.groovy.lang.psi.impl.GroovyResolveResultImpl;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.arithmetic.TypesUtil;
import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;

import java.util.*;

/**
 * @author ven
 * Resolves methods from call expression or function application.
 */
public class MethodResolverProcessor extends ResolverProcessor {
  @Nullable
  PsiType[] myArgumentTypes;

  private Set<GroovyResolveResult> myInapplicableCandidates = new LinkedHashSet<GroovyResolveResult>();
  private boolean myIsConstructor;

  public MethodResolverProcessor(String name, GroovyPsiElement place, boolean forCompletion, boolean isConstructor, PsiType[] argumentTypes) {
    super(name, EnumSet.of(ResolveKind.METHOD, ResolveKind.PROPERTY), place, forCompletion, PsiType.EMPTY_ARRAY);
    myIsConstructor = isConstructor;
    myArgumentTypes = argumentTypes;
  }

  public MethodResolverProcessor(String name, GroovyPsiElement place, boolean forCompletion, boolean isConstructor) {
    this(name, place, forCompletion, isConstructor, PsiUtil.getArgumentTypes(place, isConstructor));
  }

  public boolean execute(PsiElement element, PsiSubstitutor substitutor) {
    if (element instanceof PsiMethod) {
      PsiMethod method = (PsiMethod) element;
      if (method.isConstructor() != myIsConstructor) return true;

      if (!isAccessible((PsiNamedElement) element)) return true;

      if (myForCompletion || PsiUtil.isApplicable(myArgumentTypes, method)) {
        myCandidates.add(new GroovyResolveResultImpl(method, true, myImportStatementContext, substitutor));
      }
      else {
        myInapplicableCandidates.add(new GroovyResolveResultImpl(method, true, myImportStatementContext, substitutor));
      }

      return true;
    } else if (element instanceof PsiVariable) {
      if (element instanceof GrField && ((GrField)element).isProperty() ||
          ((PsiVariable) element).getType().equalsToText("groovy.lang.Closure")) {
        return super.execute(element, substitutor);
      }
    }

    return true;
  }

  public GroovyResolveResult[] getCandidates() {
    if (myCandidates.size() > 0) {
      if (myForCompletion ||
          myArgumentTypes == null) { //should not filter
        return myCandidates.toArray(new GroovyResolveResult[myInapplicableCandidates.size()]);
      }
{'fraction_non_alphanumeric': 0.09563658099222953, 'fraction_numerical': 0.0038852361028093247, 'mean_word_length': 4.619647355163728, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6653085', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1834, 'n_words': 478}