Search in sources :

Example 1 with Node

use of com.google.javascript.rhino.Node in project ow by vtst.

the class ClosureTextHover method getHoverHTML.

/* (non-Javadoc)
   * @see net.vtst.ow.eclipse.js.closure.editor.hover.AbstractTextHover#getHoverHTML(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
   */
@Override
protected String getHoverHTML(ITextViewer viewer, IRegion region) {
    CompilableJSUnit unit = getJSUnit();
    if (unit == null)
        return null;
    CompilerRun run = getCompilerRun(unit);
    if (run == null)
        return null;
    Node node = run.getNode(unit, region.getOffset());
    if (node == null)
        return null;
    JSElementInfo info = getElementInfo(run, node);
    if (info == null)
        return null;
    return info.getHTMLStringForHover();
}
Also used : Node(com.google.javascript.rhino.Node) JSElementInfo(net.vtst.ow.eclipse.js.closure.editor.JSElementInfo) CompilerRun(net.vtst.ow.closure.compiler.compile.CompilerRun) CompilableJSUnit(net.vtst.ow.closure.compiler.compile.CompilableJSUnit)

Example 2 with Node

use of com.google.javascript.rhino.Node in project ow by vtst.

the class JSElementInfo method isNamespace.

// **************************************************************************
// Computing properties of the element
private boolean isNamespace(Node node) {
    // First, check whether the node's parent or its grand parent is tagged
    // as a name space.
    Node parent = node.getParent();
    if (parent == null)
        return false;
    if (parent.getBooleanProp(Node.IS_NAMESPACE))
        return true;
    Node parent2 = parent.getParent();
    if (parent2 == null)
        return false;
    if (parent2.getBooleanProp(Node.IS_NAMESPACE))
        return true;
    // Special case for name spaces which are defined by var foo = {} or var foo = foo || {}.
    if (parent2.getType() != Token.SCRIPT || parent.getType() != Token.VAR || node.getType() != Token.NAME)
        return false;
    boolean hasValidNode = false;
    for (Node child : node.children()) {
        int type = child.getType();
        if (type == Token.OBJECTLIT && !child.hasChildren()) {
            hasValidNode = true;
        } else if (type == Token.OR) {
            for (Node child2 : child.children()) {
                if (child2.getType() == Token.OBJECTLIT && !child2.hasChildren()) {
                    hasValidNode = true;
                }
            }
        } else {
            return false;
        }
    }
    return hasValidNode;
}
Also used : Node(com.google.javascript.rhino.Node)

Example 3 with Node

use of com.google.javascript.rhino.Node in project ow by vtst.

the class JSElementInfo method writeFunctionInfo.

/**
   * @param fnNode A node for a function for which to generate a type annotation
   */
private void writeFunctionInfo(Node fnNode) {
    if (fnNode == null)
        return;
    Preconditions.checkState(fnNode.getType() == Token.FUNCTION);
    JSDocInfo fnDocInfo = NodeUtil.getFunctionJSDocInfo(fnNode);
    JSType type = fnNode.getJSType();
    if (type == null || type.isUnknownType()) {
        return;
    }
    FunctionType funType = type.toMaybeFunctionType();
    //     NAME param2
    if (fnNode != null) {
        openSection(messages.getString("jsdoc_parameters"));
        Node paramNode = NodeUtil.getFunctionParameters(fnNode).getFirstChild();
        // Param types
        for (Node n : funType.getParameters()) {
            // Bail out if the paramNode is not there.
            if (paramNode == null) {
                break;
            }
            openItem();
            writeParameter(paramNode, n, fnDocInfo.getDescriptionForParameter(paramNode.getString()));
            closeItem();
            paramNode = paramNode.getNext();
        }
        closeSection();
    }
    // Return type
    JSType retType = funType.getReturnType();
    if (retType != null && !retType.isUnknownType() && !retType.isEmptyType()) {
        openSectionAndItem(messages.getString("jsdoc_return"));
        writeType(retType);
        writeTypeDescription(fnDocInfo.getReturnDescription());
        closeSectionAndItem();
    }
    // Constructor/interface
    if (funType.isConstructor() || funType.isInterface()) {
        FunctionType superConstructor = funType.getSuperClassConstructor();
        if (superConstructor != null) {
            ObjectType superInstance = funType.getSuperClassConstructor().getInstanceType();
            if (!superInstance.toString().equals("Object")) {
                openSectionAndItem(messages.getString("jsdoc_extends"));
                buf.append(superInstance.toString());
                closeSectionAndItem();
            }
        }
        if (funType.isInterface()) {
            for (ObjectType interfaceType : funType.getExtendedInterfaces()) {
                openSectionAndItem(messages.getString("jsdoc_extends"));
                buf.append(interfaceType.toString());
                closeSectionAndItem();
            }
        }
        // Avoid duplicates, add implemented type to a set first
        Set<String> interfaces = Sets.newTreeSet();
        for (ObjectType interfaze : funType.getImplementedInterfaces()) {
            interfaces.add(interfaze.toString());
        }
        if (!interfaces.isEmpty()) {
            openSectionAndItem(messages.getString("jsdoc_implements"));
            boolean first = true;
            for (String interfaze : interfaces) {
                if (first)
                    first = false;
                else
                    buf.append("<p>");
                buf.append(interfaze.toString());
            }
            closeSectionAndItem();
        }
    }
}
Also used : ObjectType(com.google.javascript.rhino.jstype.ObjectType) JSType(com.google.javascript.rhino.jstype.JSType) FunctionType(com.google.javascript.rhino.jstype.FunctionType) Node(com.google.javascript.rhino.Node) JSDocInfo(com.google.javascript.rhino.JSDocInfo)

Example 4 with Node

use of com.google.javascript.rhino.Node in project ow by vtst.

the class JSElementInfo method makeFromPropertyOrNull.

/**
   * Creates a new {@code JSElementInfo} from a property.
   * @param run  The compiler run (to be used to retrieve further information).
   * @param type  The type the property may belong to.
   * @param propertyName  The name of the property.  It may exist or not.
   * @return  A new {@code JSElementInfo}, or null.
   */
public static JSElementInfo makeFromPropertyOrNull(CompilerRun run, JSType type, String propertyName) {
    if (type instanceof ObjectType) {
        ObjectType objectType = (ObjectType) type;
        if (objectType.hasProperty(propertyName)) {
            Node propertyNode = objectType.getPropertyNode(propertyName);
            JSType propertyType = objectType.getPropertyType(propertyName);
            if (propertyNode != null && propertyType != null) {
                return new JSElementInfo(run, propertyNode, propertyType, getJSDocInfoOfProperty(objectType, propertyName), true, false);
            }
        }
    }
    return null;
}
Also used : ObjectType(com.google.javascript.rhino.jstype.ObjectType) JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node)

Example 5 with Node

use of com.google.javascript.rhino.Node in project ow by vtst.

the class JSElementInfo method buildHTMLString.

/**
   * Build the HTML string for the proposal info.
   */
private void buildHTMLString() {
    if (isNamespace) {
        Node namespaceNode = run.getNamespaceProvider(node.getQualifiedName());
        if (namespaceNode != null)
            docInfo = namespaceNode.getJSDocInfo();
    }
    buf = new StringBuffer();
    messages = OwJsClosurePlugin.getDefault().getMessages();
    if (docInfo != null) {
        HTMLPrinter.insertPageProlog(buf, 0, "");
        String qualifiedName = node.getQualifiedName();
        if (qualifiedName != null) {
            buf.append("<b>");
            buf.append(qualifiedName);
            buf.append("</b><p><hr><p>");
        }
        if (docInfo.hasFileOverview()) {
            writeFileOverview();
        } else {
            writeBlockDescription();
            writeTypeInfo();
            writeOtherInfo();
        }
        HTMLPrinter.addPageEpilog(buf);
    } else {
        Node functionNode = getFunctionNodeOfFunctionParameterNode(node);
        if (functionNode != null) {
            writeFunctionParameterInfo(functionNode, node.getString());
        }
    }
    htmlString = buf.toString();
    buf = null;
}
Also used : Node(com.google.javascript.rhino.Node)

Aggregations

Node (com.google.javascript.rhino.Node)9 JSType (com.google.javascript.rhino.jstype.JSType)3 FunctionType (com.google.javascript.rhino.jstype.FunctionType)2 ObjectType (com.google.javascript.rhino.jstype.ObjectType)2 CompilerPass (com.google.javascript.jscomp.CompilerPass)1 HotSwapCompilerPass (com.google.javascript.jscomp.HotSwapCompilerPass)1 JSDocInfo (com.google.javascript.rhino.JSDocInfo)1 HashSet (java.util.HashSet)1 CompilableJSUnit (net.vtst.ow.closure.compiler.compile.CompilableJSUnit)1 CompilerRun (net.vtst.ow.closure.compiler.compile.CompilerRun)1 JSElementInfo (net.vtst.ow.eclipse.js.closure.editor.JSElementInfo)1