Search in sources :

Example 6 with ITypeDefinition

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

the class ActionScriptTextDocumentService method autoCompleteTypesForMXMLFromExistingTag.

/**
     * Using an existing tag, that may already have a prefix or short name,
     * populate the completion list.
     */
private void autoCompleteTypesForMXMLFromExistingTag(CompletionList result, IMXMLTagData offsetTag) {
    IMXMLDataManager mxmlDataManager = currentWorkspace.getMXMLDataManager();
    MXMLData mxmlData = (MXMLData) mxmlDataManager.get(fileSpecGetter.getFileSpecification(currentUnit.getAbsoluteFilename()));
    String tagStartShortName = offsetTag.getShortName();
    String tagPrefix = offsetTag.getPrefix();
    PrefixMap prefixMap = mxmlData.getRootTagPrefixMap();
    for (ICompilationUnit unit : compilationUnits) {
        if (unit == null) {
            continue;
        }
        Collection<IDefinition> definitions = null;
        try {
            definitions = unit.getFileScopeRequest().get().getExternallyVisibleDefinitions();
        } catch (Exception e) {
            //safe to ignore
            continue;
        }
        for (IDefinition definition : definitions) {
            if (!(definition instanceof ITypeDefinition)) {
                continue;
            }
            ITypeDefinition typeDefinition = (ITypeDefinition) definition;
            //or that the definition's base name matches the short name 
            if (tagStartShortName.length() == 0 || typeDefinition.getBaseName().startsWith(tagStartShortName)) {
                //in a namespace with that prefix
                if (tagPrefix.length() > 0) {
                    Collection<XMLName> tagNames = currentProject.getTagNamesForClass(typeDefinition.getQualifiedName());
                    for (XMLName tagName : tagNames) {
                        String tagNamespace = tagName.getXMLNamespace();
                        //not what we're using in this file
                        if (tagNamespace.equals(IMXMLLanguageConstants.NAMESPACE_MXML_2006)) {
                            //use the language namespace of the root tag instead
                            tagNamespace = mxmlData.getRootTag().getMXMLDialect().getLanguageNamespace();
                        }
                        String[] prefixes = prefixMap.getPrefixesForNamespace(tagNamespace);
                        for (String otherPrefix : prefixes) {
                            if (tagPrefix.equals(otherPrefix)) {
                                addDefinitionAutoCompleteMXML(typeDefinition, null, null, result);
                            }
                        }
                    }
                } else {
                    //no prefix yet, so complete the definition with a prefix
                    MXMLNamespace ns = getMXMLNamespaceForTypeDefinition(typeDefinition, mxmlData);
                    addDefinitionAutoCompleteMXML(typeDefinition, ns.prefix, ns.uri, result);
                }
            }
        }
    }
}
Also used : PrefixMap(org.apache.flex.compiler.common.PrefixMap) ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) ITypeDefinition(org.apache.flex.compiler.definitions.ITypeDefinition) MXMLData(org.apache.flex.compiler.internal.mxml.MXMLData) IDefinition(org.apache.flex.compiler.definitions.IDefinition) ConcurrentModificationException(java.util.ConcurrentModificationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) IMXMLDataManager(org.apache.flex.compiler.mxml.IMXMLDataManager) XMLName(org.apache.flex.compiler.common.XMLName)

Example 7 with ITypeDefinition

use of org.apache.flex.compiler.definitions.ITypeDefinition 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 8 with ITypeDefinition

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

the class ActionScriptTextDocumentService method createCodeActionsForImport.

private void createCodeActionsForImport(Diagnostic diagnostic, List<Command> commands) {
    String message = diagnostic.getMessage();
    int start = message.lastIndexOf(" ") + 1;
    int end = message.length() - 1;
    String typeString = message.substring(start, end);
    ArrayList<IDefinition> types = new ArrayList<>();
    for (ICompilationUnit unit : compilationUnits) {
        if (unit == null) {
            continue;
        }
        try {
            Collection<IDefinition> definitions = unit.getFileScopeRequest().get().getExternallyVisibleDefinitions();
            if (definitions == null) {
                continue;
            }
            for (IDefinition definition : definitions) {
                if (definition instanceof ITypeDefinition) {
                    ITypeDefinition typeDefinition = (ITypeDefinition) definition;
                    String baseName = typeDefinition.getBaseName();
                    if (typeDefinition.getQualifiedName().equals(baseName)) {
                        //this definition is top-level. no import required.
                        continue;
                    }
                    if (baseName.equals(typeString)) {
                        types.add(typeDefinition);
                    }
                }
            }
        } catch (Exception e) {
        //safe to ignore
        }
    }
    for (IDefinition definitionToImport : types) {
        Command command = createImportCommand(definitionToImport);
        if (command != null) {
            commands.add(command);
        }
    }
}
Also used : ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) ITypeDefinition(org.apache.flex.compiler.definitions.ITypeDefinition) Command(org.eclipse.lsp4j.Command) ArrayList(java.util.ArrayList) IDefinition(org.apache.flex.compiler.definitions.IDefinition) ConcurrentModificationException(java.util.ConcurrentModificationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 9 with ITypeDefinition

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

the class ActionScriptTextDocumentService method autoCompleteScope.

private void autoCompleteScope(IScopedNode node, boolean typesOnly, CompletionList result) {
    IScopedNode currentNode = node;
    ASScope scope = (ASScope) node.getScope();
    while (currentNode != null) {
        IASScope currentScope = currentNode.getScope();
        boolean isType = currentScope instanceof TypeScope;
        boolean staticOnly = currentNode == node && isType;
        if (currentScope instanceof TypeScope && !typesOnly) {
            TypeScope typeScope = (TypeScope) currentScope;
            addDefinitionsInTypeScopeToAutoComplete(typeScope, scope, true, true, false, null, result);
            if (!staticOnly) {
                addDefinitionsInTypeScopeToAutoCompleteActionScript(typeScope, scope, false, result);
            }
        } else {
            for (IDefinition localDefinition : currentScope.getAllLocalDefinitions()) {
                if (localDefinition.getBaseName().length() == 0) {
                    continue;
                }
                if (typesOnly && !(localDefinition instanceof ITypeDefinition)) {
                    continue;
                }
                if (!staticOnly || localDefinition.isStatic()) {
                    if (localDefinition instanceof ISetterDefinition) {
                        ISetterDefinition setter = (ISetterDefinition) localDefinition;
                        IGetterDefinition getter = setter.resolveGetter(currentProject);
                        if (getter != null) {
                            //it would add a duplicate entry
                            continue;
                        }
                    }
                    addDefinitionAutoCompleteActionScript(localDefinition, result);
                }
            }
        }
        currentNode = currentNode.getContainingScope();
    }
}
Also used : IScopedNode(org.apache.flex.compiler.tree.as.IScopedNode) IASScope(org.apache.flex.compiler.scopes.IASScope) IASScope(org.apache.flex.compiler.scopes.IASScope) ASScope(org.apache.flex.compiler.internal.scopes.ASScope) ITypeDefinition(org.apache.flex.compiler.definitions.ITypeDefinition) ISetterDefinition(org.apache.flex.compiler.definitions.ISetterDefinition) IGetterDefinition(org.apache.flex.compiler.definitions.IGetterDefinition) TypeScope(org.apache.flex.compiler.internal.scopes.TypeScope) IDefinition(org.apache.flex.compiler.definitions.IDefinition)

Example 10 with ITypeDefinition

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

the class ActionScriptTextDocumentService method getDefinitionDetail.

private String getDefinitionDetail(IDefinition definition) {
    StringBuilder detailBuilder = new StringBuilder();
    if (definition instanceof IClassDefinition) {
        IClassDefinition classDefinition = (IClassDefinition) definition;
        if (classDefinition.isDynamic()) {
            detailBuilder.append(IASKeywordConstants.DYNAMIC);
            detailBuilder.append(" ");
        }
        detailBuilder.append(IASKeywordConstants.CLASS);
        detailBuilder.append(" ");
        if (classDefinition.getPackageName().startsWith(PACKAGE_NAME_NO_IMPORT)) {
            //classes like __AS3__.vec.Vector should not include the
            //package name
            detailBuilder.append(classDefinition.getBaseName());
        } else {
            detailBuilder.append(classDefinition.getQualifiedName());
        }
        IClassDefinition baseClassDefinition = classDefinition.resolveBaseClass(currentProject);
        if (baseClassDefinition != null && !baseClassDefinition.getQualifiedName().equals(IASLanguageConstants.Object)) {
            detailBuilder.append(" ");
            detailBuilder.append(IASKeywordConstants.EXTENDS);
            detailBuilder.append(" ");
            detailBuilder.append(baseClassDefinition.getBaseName());
        }
        IInterfaceDefinition[] interfaceDefinitions = classDefinition.resolveImplementedInterfaces(currentProject);
        if (interfaceDefinitions.length > 0) {
            detailBuilder.append(" ");
            detailBuilder.append(IASKeywordConstants.IMPLEMENTS);
            detailBuilder.append(" ");
            appendInterfaceNamesToDetail(detailBuilder, interfaceDefinitions);
        }
    } else if (definition instanceof IInterfaceDefinition) {
        IInterfaceDefinition interfaceDefinition = (IInterfaceDefinition) definition;
        detailBuilder.append(IASKeywordConstants.INTERFACE);
        detailBuilder.append(" ");
        detailBuilder.append(interfaceDefinition.getQualifiedName());
        IInterfaceDefinition[] interfaceDefinitions = interfaceDefinition.resolveExtendedInterfaces(currentProject);
        if (interfaceDefinitions.length > 0) {
            detailBuilder.append(" ");
            detailBuilder.append(IASKeywordConstants.EXTENDS);
            detailBuilder.append(" ");
            appendInterfaceNamesToDetail(detailBuilder, interfaceDefinitions);
        }
    } else if (definition instanceof IVariableDefinition) {
        IVariableDefinition variableDefinition = (IVariableDefinition) definition;
        IDefinition parentDefinition = variableDefinition.getParent();
        if (parentDefinition instanceof ITypeDefinition) {
            //IVariableDefinition and IFunctionDefinition 
            if (variableDefinition instanceof IAccessorDefinition) {
                detailBuilder.append("(property) ");
            } else if (variableDefinition instanceof IConstantDefinition) {
                detailBuilder.append("(const) ");
            } else {
                detailBuilder.append("(variable) ");
            }
            detailBuilder.append(parentDefinition.getQualifiedName());
            detailBuilder.append(".");
        } else if (parentDefinition instanceof IFunctionDefinition) {
            if (variableDefinition instanceof IParameterDefinition) {
                detailBuilder.append("(parameter) ");
            } else {
                detailBuilder.append("(local ");
                if (variableDefinition instanceof IConstantDefinition) {
                    detailBuilder.append("const) ");
                } else {
                    detailBuilder.append("var) ");
                }
            }
        } else {
            if (variableDefinition instanceof IConstantDefinition) {
                detailBuilder.append(IASKeywordConstants.CONST);
            } else {
                detailBuilder.append(IASKeywordConstants.VAR);
            }
            detailBuilder.append(" ");
        }
        detailBuilder.append(variableDefinition.getBaseName());
        detailBuilder.append(":");
        detailBuilder.append(variableDefinition.getTypeAsDisplayString());
    } else if (definition instanceof IFunctionDefinition) {
        IFunctionDefinition functionDefinition = (IFunctionDefinition) definition;
        IDefinition parentDefinition = functionDefinition.getParent();
        if (parentDefinition instanceof ITypeDefinition) {
            if (functionDefinition.isConstructor()) {
                detailBuilder.append("(constructor) ");
            } else {
                detailBuilder.append("(method) ");
            }
            detailBuilder.append(parentDefinition.getQualifiedName());
            detailBuilder.append(".");
        } else if (parentDefinition instanceof IFunctionDefinition) {
            detailBuilder.append("(local function) ");
        } else {
            detailBuilder.append(IASKeywordConstants.FUNCTION);
            detailBuilder.append(" ");
        }
        detailBuilder.append(getSignatureLabel(functionDefinition));
    } else if (definition instanceof IEventDefinition) {
        IEventDefinition eventDefinition = (IEventDefinition) definition;
        detailBuilder.append("(event) ");
        detailBuilder.append("[");
        detailBuilder.append(IMetaAttributeConstants.ATTRIBUTE_EVENT);
        detailBuilder.append("(");
        detailBuilder.append(IMetaAttributeConstants.NAME_EVENT_NAME);
        detailBuilder.append("=");
        detailBuilder.append("\"");
        detailBuilder.append(eventDefinition.getBaseName());
        detailBuilder.append("\"");
        detailBuilder.append(",");
        detailBuilder.append(IMetaAttributeConstants.NAME_EVENT_TYPE);
        detailBuilder.append("=");
        detailBuilder.append("\"");
        detailBuilder.append(eventDefinition.getTypeAsDisplayString());
        detailBuilder.append("\"");
        detailBuilder.append(")");
        detailBuilder.append("]");
    } else if (definition instanceof IStyleDefinition) {
        IStyleDefinition styleDefinition = (IStyleDefinition) definition;
        detailBuilder.append("(style) ");
        detailBuilder.append("[");
        detailBuilder.append(IMetaAttributeConstants.ATTRIBUTE_STYLE);
        detailBuilder.append("(");
        detailBuilder.append(IMetaAttributeConstants.NAME_STYLE_NAME);
        detailBuilder.append("=");
        detailBuilder.append("\"");
        detailBuilder.append(styleDefinition.getBaseName());
        detailBuilder.append("\"");
        detailBuilder.append(",");
        detailBuilder.append(IMetaAttributeConstants.NAME_STYLE_TYPE);
        detailBuilder.append("=");
        detailBuilder.append("\"");
        detailBuilder.append(styleDefinition.getTypeAsDisplayString());
        detailBuilder.append("\"");
        detailBuilder.append(")");
        detailBuilder.append("]");
    }
    return detailBuilder.toString();
}
Also used : IClassDefinition(org.apache.flex.compiler.definitions.IClassDefinition) IAccessorDefinition(org.apache.flex.compiler.definitions.IAccessorDefinition) ITypeDefinition(org.apache.flex.compiler.definitions.ITypeDefinition) IStyleDefinition(org.apache.flex.compiler.definitions.IStyleDefinition) IDefinition(org.apache.flex.compiler.definitions.IDefinition) IInterfaceDefinition(org.apache.flex.compiler.definitions.IInterfaceDefinition) IFunctionDefinition(org.apache.flex.compiler.definitions.IFunctionDefinition) IVariableDefinition(org.apache.flex.compiler.definitions.IVariableDefinition) IParameterDefinition(org.apache.flex.compiler.definitions.IParameterDefinition) IEventDefinition(org.apache.flex.compiler.definitions.IEventDefinition) IConstantDefinition(org.apache.flex.compiler.definitions.IConstantDefinition)

Aggregations

ITypeDefinition (org.apache.flex.compiler.definitions.ITypeDefinition)11 IDefinition (org.apache.flex.compiler.definitions.IDefinition)10 FileNotFoundException (java.io.FileNotFoundException)4 IOException (java.io.IOException)4 ConcurrentModificationException (java.util.ConcurrentModificationException)4 IFunctionDefinition (org.apache.flex.compiler.definitions.IFunctionDefinition)4 IASScope (org.apache.flex.compiler.scopes.IASScope)4 ICompilationUnit (org.apache.flex.compiler.units.ICompilationUnit)4 IClassDefinition (org.apache.flex.compiler.definitions.IClassDefinition)3 IVariableDefinition (org.apache.flex.compiler.definitions.IVariableDefinition)3 CompletionItem (org.eclipse.lsp4j.CompletionItem)3 ArrayList (java.util.ArrayList)2 IEventDefinition (org.apache.flex.compiler.definitions.IEventDefinition)2 IInterfaceDefinition (org.apache.flex.compiler.definitions.IInterfaceDefinition)2 IPackageDefinition (org.apache.flex.compiler.definitions.IPackageDefinition)2 IParameterDefinition (org.apache.flex.compiler.definitions.IParameterDefinition)2 ASScope (org.apache.flex.compiler.internal.scopes.ASScope)2 TypeScope (org.apache.flex.compiler.internal.scopes.TypeScope)2 IExpressionNode (org.apache.flex.compiler.tree.as.IExpressionNode)2 PrefixMap (org.apache.flex.compiler.common.PrefixMap)1