Search in sources :

Example 1 with IFunctionDefinition

use of org.apache.flex.compiler.definitions.IFunctionDefinition in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method querySymbolsInScope.

private void querySymbolsInScope(String query, IASScope scope, List<SymbolInformation> result) {
    String lowerCaseQuery = query.toLowerCase();
    Collection<IDefinition> definitions = scope.getAllLocalDefinitions();
    for (IDefinition definition : definitions) {
        if (definition instanceof IPackageDefinition) {
            IPackageDefinition packageDefinition = (IPackageDefinition) definition;
            IASScope packageScope = packageDefinition.getContainedScope();
            querySymbolsInScope(query, packageScope, result);
        } else if (definition instanceof ITypeDefinition) {
            ITypeDefinition typeDefinition = (ITypeDefinition) definition;
            if (!definition.isImplicit() && typeDefinition.getQualifiedName().toLowerCase().contains(lowerCaseQuery)) {
                SymbolInformation symbol = definitionToSymbol(typeDefinition);
                result.add(symbol);
            }
            IASScope typeScope = typeDefinition.getContainedScope();
            querySymbolsInScope(query, typeScope, result);
        } else if (definition instanceof IFunctionDefinition) {
            if (definition.isImplicit()) {
                continue;
            }
            IFunctionDefinition functionDefinition = (IFunctionDefinition) definition;
            if (functionDefinition.getQualifiedName().toLowerCase().contains(lowerCaseQuery)) {
                SymbolInformation symbol = definitionToSymbol(functionDefinition);
                result.add(symbol);
            }
            IASScope functionScope = functionDefinition.getContainedScope();
            querySymbolsInScope(query, functionScope, result);
        } else if (definition instanceof IVariableDefinition) {
            if (definition.isImplicit()) {
                continue;
            }
            IVariableDefinition variableDefinition = (IVariableDefinition) definition;
            if (variableDefinition.getQualifiedName().toLowerCase().contains(lowerCaseQuery)) {
                SymbolInformation symbol = definitionToSymbol(variableDefinition);
                result.add(symbol);
            }
        }
    }
}
Also used : IFunctionDefinition(org.apache.flex.compiler.definitions.IFunctionDefinition) IASScope(org.apache.flex.compiler.scopes.IASScope) IPackageDefinition(org.apache.flex.compiler.definitions.IPackageDefinition) ITypeDefinition(org.apache.flex.compiler.definitions.ITypeDefinition) IVariableDefinition(org.apache.flex.compiler.definitions.IVariableDefinition) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) IDefinition(org.apache.flex.compiler.definitions.IDefinition)

Example 2 with IFunctionDefinition

use of org.apache.flex.compiler.definitions.IFunctionDefinition 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)

Example 3 with IFunctionDefinition

use of org.apache.flex.compiler.definitions.IFunctionDefinition in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method definitionToSymbol.

private SymbolInformation definitionToSymbol(IDefinition definition) {
    SymbolInformation symbol = new SymbolInformation();
    if (definition instanceof IClassDefinition) {
        symbol.setKind(SymbolKind.Class);
    } else if (definition instanceof IInterfaceDefinition) {
        symbol.setKind(SymbolKind.Interface);
    } else if (definition instanceof IFunctionDefinition) {
        IFunctionDefinition functionDefinition = (IFunctionDefinition) definition;
        if (functionDefinition.isConstructor()) {
            symbol.setKind(SymbolKind.Constructor);
        } else {
            symbol.setKind(SymbolKind.Function);
        }
    } else if (definition instanceof IFunctionDefinition) {
        symbol.setKind(SymbolKind.Function);
    } else if (definition instanceof IConstantDefinition) {
        symbol.setKind(SymbolKind.Constant);
    } else {
        symbol.setKind(SymbolKind.Variable);
    }
    symbol.setName(definition.getQualifiedName());
    Location location = new Location();
    String sourcePath = definition.getSourcePath();
    if (sourcePath == null) {
        //I'm not sure why getSourcePath() can sometimes return null, but
        //getContainingFilePath() seems to work as a fallback -JT
        sourcePath = definition.getContainingFilePath();
    }
    Path definitionPath = Paths.get(sourcePath);
    location.setUri(definitionPath.toUri().toString());
    Position start = new Position();
    Position end = new Position();
    //getLine() and getColumn() may include things like metadata, so it
    //makes more sense to jump to where the definition name starts
    int line = definition.getNameLine();
    int column = definition.getNameColumn();
    if (line < 0 || column < 0) {
        //this is not ideal, but MXML variable definitions may not have a
        //node associated with them, so we need to figure this out from the
        //offset instead of a pre-calculated line and column -JT
        String code = sourceByPath.get(Paths.get(sourcePath));
        offsetToLineAndCharacter(new StringReader(code), definition.getNameStart(), start);
        end.setLine(start.getLine());
        end.setCharacter(start.getCharacter());
    } else {
        start.setLine(line);
        start.setCharacter(column);
        end.setLine(line);
        end.setCharacter(column);
    }
    Range range = new Range();
    range.setStart(start);
    range.setEnd(end);
    location.setRange(range);
    symbol.setLocation(location);
    return symbol;
}
Also used : Path(java.nio.file.Path) IClassDefinition(org.apache.flex.compiler.definitions.IClassDefinition) IInterfaceDefinition(org.apache.flex.compiler.definitions.IInterfaceDefinition) IFunctionDefinition(org.apache.flex.compiler.definitions.IFunctionDefinition) Position(org.eclipse.lsp4j.Position) StringReader(java.io.StringReader) Range(org.eclipse.lsp4j.Range) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) IConstantDefinition(org.apache.flex.compiler.definitions.IConstantDefinition) Location(org.eclipse.lsp4j.Location) ISourceLocation(org.apache.flex.compiler.common.ISourceLocation)

Example 4 with IFunctionDefinition

use of org.apache.flex.compiler.definitions.IFunctionDefinition in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method scopeToSymbols.

private void scopeToSymbols(IASScope scope, List<SymbolInformation> result) {
    Collection<IDefinition> definitions = scope.getAllLocalDefinitions();
    for (IDefinition definition : definitions) {
        if (definition instanceof IPackageDefinition) {
            IPackageDefinition packageDefinition = (IPackageDefinition) definition;
            IASScope packageScope = packageDefinition.getContainedScope();
            scopeToSymbols(packageScope, result);
        } else if (definition instanceof ITypeDefinition) {
            ITypeDefinition typeDefinition = (ITypeDefinition) definition;
            if (!definition.isImplicit()) {
                SymbolInformation typeSymbol = definitionToSymbol(typeDefinition);
                result.add(typeSymbol);
            }
            IASScope typeScope = typeDefinition.getContainedScope();
            scopeToSymbols(typeScope, result);
        } else if (definition instanceof IFunctionDefinition || definition instanceof IVariableDefinition) {
            if (definition.isImplicit()) {
                continue;
            }
            SymbolInformation localSymbol = definitionToSymbol(definition);
            result.add(localSymbol);
        }
    }
}
Also used : IFunctionDefinition(org.apache.flex.compiler.definitions.IFunctionDefinition) IASScope(org.apache.flex.compiler.scopes.IASScope) IPackageDefinition(org.apache.flex.compiler.definitions.IPackageDefinition) ITypeDefinition(org.apache.flex.compiler.definitions.ITypeDefinition) IVariableDefinition(org.apache.flex.compiler.definitions.IVariableDefinition) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) IDefinition(org.apache.flex.compiler.definitions.IDefinition)

Example 5 with IFunctionDefinition

use of org.apache.flex.compiler.definitions.IFunctionDefinition in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method addDefinitionsInTypeScopeToAutoComplete.

private void addDefinitionsInTypeScopeToAutoComplete(TypeScope typeScope, ASScope otherScope, boolean isStatic, boolean includeSuperStatics, boolean forMXML, String prefix, CompletionList result) {
    IMetaTag[] excludeMetaTags = typeScope.getDefinition().getMetaTagsByName(IMetaAttributeConstants.ATTRIBUTE_EXCLUDE);
    ArrayList<IDefinition> memberAccessDefinitions = new ArrayList<>();
    Set<INamespaceDefinition> namespaceSet = otherScope.getNamespaceSet(currentProject);
    if (typeScope.getContainingDefinition() instanceof IInterfaceDefinition) {
        //interfaces have a special namespace that isn't actually the same
        //as public, but should be treated the same way
        IInterfaceDefinition interfaceDefinition = (IInterfaceDefinition) typeScope.getContainingDefinition();
        collectInterfaceNamespaces(interfaceDefinition, namespaceSet);
    }
    IClassDefinition otherContainingClass = otherScope.getContainingClass();
    if (otherContainingClass != null) {
        IClassDefinition classDefinition = typeScope.getContainingClass();
        if (classDefinition != null) {
            boolean isSuperClass = Arrays.asList(otherContainingClass.resolveAncestry(currentProject)).contains(classDefinition);
            if (isSuperClass) {
                //namespaces from the super classes
                do {
                    namespaceSet.add(classDefinition.getProtectedNamespaceReference());
                    classDefinition = classDefinition.resolveBaseClass(currentProject);
                } while (classDefinition instanceof IClassDefinition);
            }
        }
    }
    typeScope.getAllPropertiesForMemberAccess(currentProject, memberAccessDefinitions, namespaceSet);
    for (IDefinition localDefinition : memberAccessDefinitions) {
        if (localDefinition.isOverride()) {
            //overrides would add unnecessary duplicates to the list
            continue;
        }
        if (excludeMetaTags != null && excludeMetaTags.length > 0) {
            boolean exclude = false;
            for (IMetaTag excludeMetaTag : excludeMetaTags) {
                String excludeName = excludeMetaTag.getAttributeValue(IMetaAttributeConstants.NAME_EXCLUDE_NAME);
                if (excludeName.equals(localDefinition.getBaseName())) {
                    exclude = true;
                    break;
                }
            }
            if (exclude) {
                continue;
            }
        }
        //there are some things that we need to skip in MXML
        if (forMXML) {
            if (localDefinition instanceof IGetterDefinition) {
                //no getters because we can only set
                continue;
            } else if (localDefinition instanceof IFunctionDefinition && !(localDefinition instanceof ISetterDefinition)) {
                //no calling functions, unless they're setters
                continue;
            }
        } else //actionscript
        {
            if (localDefinition instanceof ISetterDefinition) {
                ISetterDefinition setter = (ISetterDefinition) localDefinition;
                IGetterDefinition getter = setter.resolveGetter(currentProject);
                if (getter != null) {
                    //would add a duplicate entry
                    continue;
                }
            }
        }
        if (isStatic) {
            if (!localDefinition.isStatic()) {
                //static, skip it
                continue;
            }
            if (!includeSuperStatics && localDefinition.getParent() != typeScope.getContainingDefinition()) {
                //aren't available with member access
                continue;
            }
        }
        if (!isStatic && localDefinition.isStatic()) {
            //skip it!
            continue;
        }
        if (forMXML) {
            addDefinitionAutoCompleteMXML(localDefinition, prefix, null, result);
        } else //actionscript
        {
            addDefinitionAutoCompleteActionScript(localDefinition, result);
        }
    }
}
Also used : IClassDefinition(org.apache.flex.compiler.definitions.IClassDefinition) ArrayList(java.util.ArrayList) ISetterDefinition(org.apache.flex.compiler.definitions.ISetterDefinition) IDefinition(org.apache.flex.compiler.definitions.IDefinition) IInterfaceDefinition(org.apache.flex.compiler.definitions.IInterfaceDefinition) IMetaTag(org.apache.flex.compiler.definitions.metadata.IMetaTag) IFunctionDefinition(org.apache.flex.compiler.definitions.IFunctionDefinition) INamespaceDefinition(org.apache.flex.compiler.definitions.INamespaceDefinition) IGetterDefinition(org.apache.flex.compiler.definitions.IGetterDefinition)

Aggregations

IFunctionDefinition (org.apache.flex.compiler.definitions.IFunctionDefinition)7 IDefinition (org.apache.flex.compiler.definitions.IDefinition)6 IClassDefinition (org.apache.flex.compiler.definitions.IClassDefinition)4 ITypeDefinition (org.apache.flex.compiler.definitions.ITypeDefinition)4 IInterfaceDefinition (org.apache.flex.compiler.definitions.IInterfaceDefinition)3 IVariableDefinition (org.apache.flex.compiler.definitions.IVariableDefinition)3 IASScope (org.apache.flex.compiler.scopes.IASScope)3 SymbolInformation (org.eclipse.lsp4j.SymbolInformation)3 ArrayList (java.util.ArrayList)2 IConstantDefinition (org.apache.flex.compiler.definitions.IConstantDefinition)2 IEventDefinition (org.apache.flex.compiler.definitions.IEventDefinition)2 IPackageDefinition (org.apache.flex.compiler.definitions.IPackageDefinition)2 IParameterDefinition (org.apache.flex.compiler.definitions.IParameterDefinition)2 StringReader (java.io.StringReader)1 Path (java.nio.file.Path)1 ISourceLocation (org.apache.flex.compiler.common.ISourceLocation)1 IAccessorDefinition (org.apache.flex.compiler.definitions.IAccessorDefinition)1 IGetterDefinition (org.apache.flex.compiler.definitions.IGetterDefinition)1 INamespaceDefinition (org.apache.flex.compiler.definitions.INamespaceDefinition)1 ISetterDefinition (org.apache.flex.compiler.definitions.ISetterDefinition)1