use of org.apache.flex.compiler.common.PrefixMap 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);
}
use of org.apache.flex.compiler.common.PrefixMap 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);
}
}
}
}
}
Aggregations