Search in sources :

Example 1 with XMLName

use of org.apache.flex.compiler.common.XMLName in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method getMXMLNamespaceForTypeDefinition.

private MXMLNamespace getMXMLNamespaceForTypeDefinition(ITypeDefinition definition, MXMLData mxmlData) {
    PrefixMap prefixMap = mxmlData.getRootTagPrefixMap();
    Collection<XMLName> tagNames = currentProject.getTagNamesForClass(definition.getQualifiedName());
    //1. try to use an existing xmlns with an uri
    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[] uriPrefixes = prefixMap.getPrefixesForNamespace(tagNamespace);
        if (uriPrefixes.length > 0) {
            return new MXMLNamespace(uriPrefixes[0], tagNamespace);
        }
    }
    //2. try to use an existing xmlns with a package name
    String packageName = definition.getPackageName();
    String packageNamespace = getPackageNameMXMLNamespaceURI(packageName);
    String[] packagePrefixes = prefixMap.getPrefixesForNamespace(packageNamespace);
    if (packagePrefixes.length > 0) {
        return new MXMLNamespace(packagePrefixes[0], packageNamespace);
    }
    //special case for the __AS3__ package
    if (packageName != null && packageName.startsWith(PACKAGE_NAME_NO_IMPORT)) {
        //anything in this package is in the language namespace
        String fxNamespace = mxmlData.getRootTag().getMXMLDialect().getLanguageNamespace();
        MXMLNamespace resultNS = getNamespaceFromURI(fxNamespace, prefixMap);
        if (resultNS != null) {
            return resultNS;
        }
    }
    String fallbackNamespace = null;
    for (XMLName tagName : tagNames) {
        //we know this type is in one or more namespaces
        //let's try to figure out a nice prefix to use
        fallbackNamespace = tagName.getXMLNamespace();
        MXMLNamespace resultNS = getNamespaceFromURI(fallbackNamespace, prefixMap);
        if (resultNS != null) {
            return resultNS;
        }
    }
    if (fallbackNamespace != null) {
        //if we couldn't find a known prefix, use a numbered one
        String prefix = getNumberedNamespacePrefix(DEFAULT_NS_PREFIX, prefixMap);
        return new MXMLNamespace(prefix, fallbackNamespace);
    }
    //4. worse case: create a new xmlns with numbered prefix and package name
    String prefix = getNumberedNamespacePrefix(DEFAULT_NS_PREFIX, prefixMap);
    return new MXMLNamespace(prefix, packageNamespace);
}
Also used : PrefixMap(org.apache.flex.compiler.common.PrefixMap) XMLName(org.apache.flex.compiler.common.XMLName)

Example 2 with XMLName

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

Aggregations

PrefixMap (org.apache.flex.compiler.common.PrefixMap)2 XMLName (org.apache.flex.compiler.common.XMLName)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1 IDefinition (org.apache.flex.compiler.definitions.IDefinition)1 ITypeDefinition (org.apache.flex.compiler.definitions.ITypeDefinition)1 MXMLData (org.apache.flex.compiler.internal.mxml.MXMLData)1 IMXMLDataManager (org.apache.flex.compiler.mxml.IMXMLDataManager)1 ICompilationUnit (org.apache.flex.compiler.units.ICompilationUnit)1