Package org.angularjs.codeInsight.attributes

Source Code of org.angularjs.codeInsight.attributes.AngularAttributesRegistry

package org.angularjs.codeInsight.attributes;

import com.intellij.lang.javascript.index.JSNamedElementProxy;
import com.intellij.openapi.project.Project;
import com.intellij.psi.xml.XmlAttribute;
import org.angularjs.codeInsight.DirectiveUtil;
import org.angularjs.index.AngularControllerIndex;
import org.angularjs.index.AngularDirectivesDocIndex;
import org.angularjs.index.AngularIndexUtil;
import org.angularjs.index.AngularModuleIndex;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* @author Dennis.Ushakov
*/
public class AngularAttributesRegistry {
  static AngularAttributeDescriptor createDescriptor(@Nullable final Project project,
                                                     @NotNull String directiveName) {
    if ("ng-controller".equals(directiveName)) {
      return new AngularAttributeDescriptor(project, directiveName, AngularControllerIndex.INDEX_ID);
    }
    if ("ng-app".equals(directiveName)) {
      return new AngularAttributeDescriptor(project, directiveName, AngularModuleIndex.INDEX_ID);
    }
    return new AngularAttributeDescriptor(project, directiveName, null);
  }

  public static boolean isAngularExpressionAttribute(XmlAttribute parent) {
    final String attributeName = DirectiveUtil.normalizeAttributeName(parent.getName());
    final JSNamedElementProxy directive = AngularIndexUtil.resolve(parent.getProject(), AngularDirectivesDocIndex.INDEX_ID, attributeName);
    if (directive != null) {
      final String restrict = directive.getIndexItem().getTypeString();
      final String param = restrict.split(";", -1)[2];
      return param.endsWith("expression") || param.startsWith("string");
    }
    return false;
  }

  public static boolean isJSONAttribute(XmlAttribute parent) {
    final String value = parent.getValue();
    if (value == null || !value.startsWith("{")) return false;

    final String attributeName = DirectiveUtil.normalizeAttributeName(parent.getName());
    final JSNamedElementProxy directive = AngularIndexUtil.resolve(parent.getProject(), AngularDirectivesDocIndex.INDEX_ID, attributeName);
    if (directive != null) {
      final String restrict = directive.getIndexItem().getTypeString();
      final String type = restrict.split(";", -1)[2];
      return type.contains("object literal") || type.equals("mixed");
    }
    return false;
  }
}
TOP

Related Classes of org.angularjs.codeInsight.attributes.AngularAttributesRegistry

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.