Search in sources :

Example 1 with IIdentifierNode

use of org.apache.flex.compiler.tree.as.IIdentifierNode in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method actionScriptRenameWithNode.

private CompletableFuture<WorkspaceEdit> actionScriptRenameWithNode(RenameParams params, IASNode offsetNode) {
    if (offsetNode == null) {
        //we couldn't find a node at the specified location
        return CompletableFuture.completedFuture(new WorkspaceEdit(new HashMap<>()));
    }
    IDefinition definition = null;
    if (offsetNode instanceof IDefinitionNode) {
        IDefinitionNode definitionNode = (IDefinitionNode) offsetNode;
        IExpressionNode expressionNode = definitionNode.getNameExpressionNode();
        definition = expressionNode.resolve(currentProject);
    } else if (offsetNode instanceof IIdentifierNode) {
        IIdentifierNode identifierNode = (IIdentifierNode) offsetNode;
        definition = identifierNode.resolve(currentProject);
    }
    if (definition == null) {
        if (languageClient != null) {
            MessageParams message = new MessageParams();
            message.setType(MessageType.Info);
            message.setMessage("You cannot rename this element.");
            languageClient.showMessage(message);
        }
        return CompletableFuture.completedFuture(new WorkspaceEdit(new HashMap<>()));
    }
    WorkspaceEdit result = renameDefinition(definition, params.getNewName());
    return CompletableFuture.completedFuture(result);
}
Also used : HashMap(java.util.HashMap) MessageParams(org.eclipse.lsp4j.MessageParams) IIdentifierNode(org.apache.flex.compiler.tree.as.IIdentifierNode) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) IDefinitionNode(org.apache.flex.compiler.tree.as.IDefinitionNode) IDefinition(org.apache.flex.compiler.definitions.IDefinition) IExpressionNode(org.apache.flex.compiler.tree.as.IExpressionNode)

Example 2 with IIdentifierNode

use of org.apache.flex.compiler.tree.as.IIdentifierNode in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method memberAccessToPackageName.

private String memberAccessToPackageName(IMemberAccessExpressionNode memberAccess) {
    String result = null;
    IExpressionNode rightOperand = memberAccess.getRightOperandNode();
    if (!(rightOperand instanceof IIdentifierNode)) {
        return null;
    }
    IExpressionNode leftOperand = memberAccess.getLeftOperandNode();
    if (leftOperand instanceof IMemberAccessExpressionNode) {
        result = memberAccessToPackageName((IMemberAccessExpressionNode) leftOperand);
    } else if (leftOperand instanceof IIdentifierNode) {
        IIdentifierNode identifierNode = (IIdentifierNode) leftOperand;
        result = identifierNode.getName();
    } else {
        return null;
    }
    IIdentifierNode identifierNode = (IIdentifierNode) rightOperand;
    return result + "." + identifierNode.getName();
}
Also used : IIdentifierNode(org.apache.flex.compiler.tree.as.IIdentifierNode) IExpressionNode(org.apache.flex.compiler.tree.as.IExpressionNode) IMemberAccessExpressionNode(org.apache.flex.compiler.tree.as.IMemberAccessExpressionNode)

Example 3 with IIdentifierNode

use of org.apache.flex.compiler.tree.as.IIdentifierNode in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method findIdentifiers.

private void findIdentifiers(IASNode node, IDefinition definition, List<IIdentifierNode> result) {
    if (node.isTerminal()) {
        if (node instanceof IIdentifierNode) {
            IIdentifierNode identifierNode = (IIdentifierNode) node;
            if (identifierNode.resolve(currentProject) == definition) {
                result.add(identifierNode);
            }
        }
        return;
    }
    for (int i = 0, count = node.getChildCount(); i < count; i++) {
        IASNode childNode = node.getChild(i);
        findIdentifiers(childNode, definition, result);
    }
}
Also used : IASNode(org.apache.flex.compiler.tree.as.IASNode) IIdentifierNode(org.apache.flex.compiler.tree.as.IIdentifierNode)

Example 4 with IIdentifierNode

use of org.apache.flex.compiler.tree.as.IIdentifierNode in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method actionScriptHoverWithNode.

private CompletableFuture<Hover> actionScriptHoverWithNode(TextDocumentPositionParams position, IASNode offsetNode) {
    IDefinition definition = null;
    if (offsetNode == null) {
        //we couldn't find a node at the specified location
        return CompletableFuture.completedFuture(new Hover(Collections.emptyList(), null));
    }
    //any hover information for it.
    if (definition == null && offsetNode instanceof IIdentifierNode && !(offsetNode instanceof INamespaceDecorationNode)) {
        IIdentifierNode identifierNode = (IIdentifierNode) offsetNode;
        definition = identifierNode.resolve(currentUnit.getProject());
    }
    if (definition == null) {
        return CompletableFuture.completedFuture(new Hover(Collections.emptyList(), null));
    }
    Hover result = new Hover();
    String detail = getDefinitionDetail(definition);
    List<String> contents = new ArrayList<>();
    contents.add(MARKDOWN_CODE_BLOCK_NEXTGENAS_START + detail + MARKDOWN_CODE_BLOCK_END);
    result.setContents(contents);
    return CompletableFuture.completedFuture(result);
}
Also used : INamespaceDecorationNode(org.apache.flex.compiler.tree.as.INamespaceDecorationNode) Hover(org.eclipse.lsp4j.Hover) ArrayList(java.util.ArrayList) IIdentifierNode(org.apache.flex.compiler.tree.as.IIdentifierNode) IDefinition(org.apache.flex.compiler.definitions.IDefinition)

Example 5 with IIdentifierNode

use of org.apache.flex.compiler.tree.as.IIdentifierNode 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

IIdentifierNode (org.apache.flex.compiler.tree.as.IIdentifierNode)10 IDefinition (org.apache.flex.compiler.definitions.IDefinition)7 ArrayList (java.util.ArrayList)6 IASNode (org.apache.flex.compiler.tree.as.IASNode)5 ISourceLocation (org.apache.flex.compiler.common.ISourceLocation)4 IExpressionNode (org.apache.flex.compiler.tree.as.IExpressionNode)4 IMXMLTagData (org.apache.flex.compiler.mxml.IMXMLTagData)3 Location (org.eclipse.lsp4j.Location)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 ConcurrentModificationException (java.util.ConcurrentModificationException)2 HashMap (java.util.HashMap)2 MXMLData (org.apache.flex.compiler.internal.mxml.MXMLData)2 SWCCompilationUnit (org.apache.flex.compiler.internal.units.SWCCompilationUnit)2 IMXMLDataManager (org.apache.flex.compiler.mxml.IMXMLDataManager)2 IFunctionCallNode (org.apache.flex.compiler.tree.as.IFunctionCallNode)2 IMemberAccessExpressionNode (org.apache.flex.compiler.tree.as.IMemberAccessExpressionNode)2 ICompilationUnit (org.apache.flex.compiler.units.ICompilationUnit)2 CompletionList (org.eclipse.lsp4j.CompletionList)2 URI (java.net.URI)1