Search in sources :

Example 1 with IMXMLInstanceNode

use of org.apache.flex.compiler.tree.mxml.IMXMLInstanceNode in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method getEmbeddedActionScriptNodeInMXMLTag.

private IASNode getEmbeddedActionScriptNodeInMXMLTag(IMXMLTagData tag, int offset, TextDocumentPositionParams position) {
    IMXMLTagAttributeData attributeData = getMXMLTagAttributeWithValueAtOffset(tag, currentOffset);
    if (attributeData != null) {
        //some attributes can have ActionScript completion, such as
        //events and properties with data binding
        IClassDefinition tagDefinition = (IClassDefinition) currentProject.resolveXMLNameToDefinition(tag.getXMLName(), tag.getMXMLDialect());
        IDefinition attributeDefinition = currentProject.resolveSpecifier(tagDefinition, attributeData.getShortName());
        if (attributeDefinition instanceof IEventDefinition) {
            IMXMLInstanceNode mxmlNode = (IMXMLInstanceNode) getOffsetNode(position);
            IMXMLEventSpecifierNode eventNode = mxmlNode.getEventSpecifierNode(attributeData.getShortName());
            for (IASNode asNode : eventNode.getASNodes()) {
                IASNode containingNode = getContainingNodeIncludingStart(asNode, currentOffset);
                if (containingNode != null) {
                    return containingNode;
                }
            }
            return eventNode;
        } else {
            IASNode offsetNode = getOffsetNode(position);
            if (offsetNode instanceof IMXMLInstanceNode) {
                IMXMLInstanceNode instanceNode = (IMXMLInstanceNode) offsetNode;
                IMXMLPropertySpecifierNode propertyNode = instanceNode.getPropertySpecifierNode(attributeData.getShortName());
                if (propertyNode != null) {
                    for (int i = 0, count = propertyNode.getChildCount(); i < count; i++) {
                        IMXMLNode propertyChild = (IMXMLNode) propertyNode.getChild(i);
                        if (propertyChild instanceof IMXMLConcatenatedDataBindingNode) {
                            IMXMLConcatenatedDataBindingNode dataBinding = (IMXMLConcatenatedDataBindingNode) propertyChild;
                            for (int j = 0, childCount = dataBinding.getChildCount(); j < childCount; j++) {
                                IASNode dataBindingChild = dataBinding.getChild(i);
                                if (dataBindingChild.contains(currentOffset) && dataBindingChild instanceof IMXMLSingleDataBindingNode) {
                                    //we'll parse this in a moment, as if it were
                                    //a direct child of the property specifier
                                    propertyChild = (IMXMLSingleDataBindingNode) dataBindingChild;
                                    break;
                                }
                            }
                        }
                        if (propertyChild instanceof IMXMLSingleDataBindingNode) {
                            IMXMLSingleDataBindingNode dataBinding = (IMXMLSingleDataBindingNode) propertyChild;
                            IASNode containingNode = dataBinding.getExpressionNode().getContainingNode(currentOffset);
                        //we found the correct expression, but due to a bug
                        //in the compiler its line and column positions
                        //will be wrong. the resulting completion is too
                        //quirky, so this feature will be completed later.
                        //return containingNode;
                        }
                    }
                }
            }
        //nothing possible for this attribute
        }
    }
    return null;
}
Also used : IMXMLConcatenatedDataBindingNode(org.apache.flex.compiler.tree.mxml.IMXMLConcatenatedDataBindingNode) IMXMLEventSpecifierNode(org.apache.flex.compiler.tree.mxml.IMXMLEventSpecifierNode) IClassDefinition(org.apache.flex.compiler.definitions.IClassDefinition) IMXMLInstanceNode(org.apache.flex.compiler.tree.mxml.IMXMLInstanceNode) IASNode(org.apache.flex.compiler.tree.as.IASNode) IMXMLSingleDataBindingNode(org.apache.flex.compiler.tree.mxml.IMXMLSingleDataBindingNode) IMXMLPropertySpecifierNode(org.apache.flex.compiler.tree.mxml.IMXMLPropertySpecifierNode) IEventDefinition(org.apache.flex.compiler.definitions.IEventDefinition) IDefinition(org.apache.flex.compiler.definitions.IDefinition) IMXMLNode(org.apache.flex.compiler.tree.mxml.IMXMLNode) IMXMLTagAttributeData(org.apache.flex.compiler.mxml.IMXMLTagAttributeData)

Example 2 with IMXMLInstanceNode

use of org.apache.flex.compiler.tree.mxml.IMXMLInstanceNode in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method signatureHelp.

/**
     * Displays a function's parameters, including which one is currently
     * active. Called automatically by VSCode any time that the user types "(",
     * so be sure to check that a function call is actually happening at the
     * current position.
     */
@Override
public CompletableFuture<SignatureHelp> signatureHelp(TextDocumentPositionParams position) {
    String textDocumentUri = position.getTextDocument().getUri();
    if (!textDocumentUri.endsWith(AS_EXTENSION) && !textDocumentUri.endsWith(MXML_EXTENSION)) {
        //we couldn't find a node at the specified location
        return CompletableFuture.completedFuture(new SignatureHelp(Collections.emptyList(), -1, -1));
    }
    IASNode offsetNode = null;
    IMXMLTagData offsetTag = getOffsetMXMLTag(position);
    if (offsetTag != null) {
        IMXMLTagAttributeData attributeData = getMXMLTagAttributeWithValueAtOffset(offsetTag, currentOffset);
        if (attributeData != null) {
            //some attributes can have ActionScript completion, such as
            //events and properties with data binding
            IClassDefinition tagDefinition = (IClassDefinition) currentProject.resolveXMLNameToDefinition(offsetTag.getXMLName(), offsetTag.getMXMLDialect());
            IDefinition attributeDefinition = currentProject.resolveSpecifier(tagDefinition, attributeData.getShortName());
            if (attributeDefinition instanceof IEventDefinition) {
                IMXMLInstanceNode mxmlNode = (IMXMLInstanceNode) getOffsetNode(position);
                IMXMLEventSpecifierNode eventNode = mxmlNode.getEventSpecifierNode(attributeData.getShortName());
                for (IASNode asNode : eventNode.getASNodes()) {
                    IASNode containingNode = getContainingNodeIncludingStart(asNode, currentOffset);
                    if (containingNode != null) {
                        offsetNode = containingNode;
                    }
                }
                if (offsetNode == null) {
                    offsetNode = eventNode;
                }
            }
        }
    }
    if (offsetNode == null) {
        offsetNode = getOffsetNode(position);
    }
    if (offsetNode == null) {
        //we couldn't find a node at the specified location
        return CompletableFuture.completedFuture(new SignatureHelp(Collections.emptyList(), -1, -1));
    }
    IFunctionCallNode functionCallNode = getAncestorFunctionCallNode(offsetNode);
    IFunctionDefinition functionDefinition = null;
    if (functionCallNode != null) {
        IExpressionNode nameNode = functionCallNode.getNameNode();
        IDefinition definition = nameNode.resolve(currentUnit.getProject());
        if (definition instanceof IFunctionDefinition) {
            functionDefinition = (IFunctionDefinition) definition;
        } else if (definition instanceof IClassDefinition) {
            IClassDefinition classDefinition = (IClassDefinition) definition;
            functionDefinition = classDefinition.getConstructor();
        } else if (nameNode instanceof IIdentifierNode) {
            //special case for super()
            IIdentifierNode identifierNode = (IIdentifierNode) nameNode;
            if (identifierNode.getName().equals(IASKeywordConstants.SUPER)) {
                ITypeDefinition typeDefinition = nameNode.resolveType(currentProject);
                if (typeDefinition instanceof IClassDefinition) {
                    IClassDefinition classDefinition = (IClassDefinition) typeDefinition;
                    functionDefinition = classDefinitionToConstructor(classDefinition);
                }
            }
        }
    }
    if (functionDefinition != null) {
        SignatureHelp result = new SignatureHelp();
        List<SignatureInformation> signatures = new ArrayList<>();
        SignatureInformation signatureInfo = new SignatureInformation();
        signatureInfo.setLabel(getSignatureLabel(functionDefinition));
        List<ParameterInformation> parameters = new ArrayList<>();
        for (IParameterDefinition param : functionDefinition.getParameters()) {
            ParameterInformation paramInfo = new ParameterInformation();
            paramInfo.setLabel(param.getBaseName());
            parameters.add(paramInfo);
        }
        signatureInfo.setParameters(parameters);
        signatures.add(signatureInfo);
        result.setSignatures(signatures);
        result.setActiveSignature(0);
        int index = getFunctionCallNodeArgumentIndex(functionCallNode, offsetNode);
        IParameterDefinition[] params = functionDefinition.getParameters();
        int paramCount = params.length;
        if (paramCount > 0 && index >= paramCount) {
            if (index >= paramCount) {
                IParameterDefinition lastParam = params[paramCount - 1];
                if (lastParam.isRest()) {
                    //functions with rest parameters may accept any
                    //number of arguments, so continue to make the rest
                    //parameter active
                    index = paramCount - 1;
                } else {
                    //if there's no rest parameter, and we're beyond the
                    //final parameter, none should be active
                    index = -1;
                }
            }
        }
        if (index != -1) {
            result.setActiveParameter(index);
        }
        return CompletableFuture.completedFuture(result);
    }
    return CompletableFuture.completedFuture(new SignatureHelp(Collections.emptyList(), -1, -1));
}
Also used : IMXMLEventSpecifierNode(org.apache.flex.compiler.tree.mxml.IMXMLEventSpecifierNode) IClassDefinition(org.apache.flex.compiler.definitions.IClassDefinition) IMXMLInstanceNode(org.apache.flex.compiler.tree.mxml.IMXMLInstanceNode) ITypeDefinition(org.apache.flex.compiler.definitions.ITypeDefinition) ArrayList(java.util.ArrayList) IIdentifierNode(org.apache.flex.compiler.tree.as.IIdentifierNode) SignatureHelp(org.eclipse.lsp4j.SignatureHelp) IMXMLTagData(org.apache.flex.compiler.mxml.IMXMLTagData) IFunctionCallNode(org.apache.flex.compiler.tree.as.IFunctionCallNode) ParameterInformation(org.eclipse.lsp4j.ParameterInformation) IDefinition(org.apache.flex.compiler.definitions.IDefinition) IFunctionDefinition(org.apache.flex.compiler.definitions.IFunctionDefinition) SignatureInformation(org.eclipse.lsp4j.SignatureInformation) IASNode(org.apache.flex.compiler.tree.as.IASNode) IParameterDefinition(org.apache.flex.compiler.definitions.IParameterDefinition) IEventDefinition(org.apache.flex.compiler.definitions.IEventDefinition) IExpressionNode(org.apache.flex.compiler.tree.as.IExpressionNode) IMXMLTagAttributeData(org.apache.flex.compiler.mxml.IMXMLTagAttributeData)

Aggregations

IClassDefinition (org.apache.flex.compiler.definitions.IClassDefinition)2 IDefinition (org.apache.flex.compiler.definitions.IDefinition)2 IEventDefinition (org.apache.flex.compiler.definitions.IEventDefinition)2 IMXMLTagAttributeData (org.apache.flex.compiler.mxml.IMXMLTagAttributeData)2 IASNode (org.apache.flex.compiler.tree.as.IASNode)2 IMXMLEventSpecifierNode (org.apache.flex.compiler.tree.mxml.IMXMLEventSpecifierNode)2 IMXMLInstanceNode (org.apache.flex.compiler.tree.mxml.IMXMLInstanceNode)2 ArrayList (java.util.ArrayList)1 IFunctionDefinition (org.apache.flex.compiler.definitions.IFunctionDefinition)1 IParameterDefinition (org.apache.flex.compiler.definitions.IParameterDefinition)1 ITypeDefinition (org.apache.flex.compiler.definitions.ITypeDefinition)1 IMXMLTagData (org.apache.flex.compiler.mxml.IMXMLTagData)1 IExpressionNode (org.apache.flex.compiler.tree.as.IExpressionNode)1 IFunctionCallNode (org.apache.flex.compiler.tree.as.IFunctionCallNode)1 IIdentifierNode (org.apache.flex.compiler.tree.as.IIdentifierNode)1 IMXMLConcatenatedDataBindingNode (org.apache.flex.compiler.tree.mxml.IMXMLConcatenatedDataBindingNode)1 IMXMLNode (org.apache.flex.compiler.tree.mxml.IMXMLNode)1 IMXMLPropertySpecifierNode (org.apache.flex.compiler.tree.mxml.IMXMLPropertySpecifierNode)1 IMXMLSingleDataBindingNode (org.apache.flex.compiler.tree.mxml.IMXMLSingleDataBindingNode)1 ParameterInformation (org.eclipse.lsp4j.ParameterInformation)1