/*
 * Copyright 2000-2014 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 standardDsls

import com.intellij.psi.PsiClass

contributor(context(scope:annotatedScope(ctype: "groovy.lang.Newify"))) {
  // For methods
  def memb = enclosingMember()
  if (memb) {
    // For classes
    def newifyName = "groovy.lang.Newify"
    def annotated = memb.getAnnotations(newifyName)

    def clazz = enclosingClass()
    if (clazz) {
      annotated += clazz.getAnnotations(newifyName)
    }
    for (a in annotated) {
      def refs = a.findAttributeValue("value")
      def auto  = a.findAttributeValue("auto")
      //For Python-like style
      if (refs && !place.qualifier) {
        for (c in refs.asList()) {
          def targetClass = c.bind()
          for (cons in (targetClass?.constructors ?: [])) {
            method name: ((PsiClass) targetClass).name, params: cons.paramStringVector, type: targetClass.qualName
          }
        }
      }

      // For Ruby-like style
      if (!auto || auto.eval()) {
        for (cons in psiClass?.getConstructors()) {
          method name: 'new', isStatic: true, params: cons.paramStringVector, type: psiClass?.qualName ?: "java.lang.Object"
        }
      }
    }
  }
}