use of org.apache.flex.compiler.internal.mxml.MXMLData in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method getOffsetMXMLTag.
private IMXMLTagData getOffsetMXMLTag(TextDocumentIdentifier textDocument, Position position) {
namespaceStartIndex = -1;
namespaceEndIndex = -1;
String uri = textDocument.getUri();
if (!uri.endsWith(MXML_EXTENSION)) {
// don't try to parse ActionScript files as MXML
return null;
}
currentOffset = -1;
importStartIndex = -1;
importEndIndex = -1;
Path path = LanguageServerUtils.getPathFromLanguageServerURI(uri);
if (path == null) {
return null;
}
if (!isInWorkspaceOrSourcePath(path)) {
//the path must be in the workspace or source-path
return null;
}
String code;
if (sourceByPath.containsKey(path)) {
code = sourceByPath.get(path);
} else {
System.err.println("Could not find source " + path.toAbsolutePath().toString());
System.err.println(sourceByPath.keySet().size());
return null;
}
//need to ensure that the compilation unit exists, even though we don't
//use it directly
ICompilationUnit unit = getCompilationUnit(path);
if (unit == null) {
//should have been logged already
return null;
}
IMXMLDataManager mxmlDataManager = currentWorkspace.getMXMLDataManager();
MXMLData mxmlData = (MXMLData) mxmlDataManager.get(fileSpecGetter.getFileSpecification(path.toAbsolutePath().toString()));
if (mxmlData == null) {
return null;
}
currentOffset = lineAndCharacterToOffset(new StringReader(code), position.getLine(), position.getCharacter());
if (currentOffset == -1) {
System.err.println("Could not find code at position " + position.getLine() + ":" + position.getCharacter() + " in file " + path.toAbsolutePath().toString());
return null;
}
//calculate the location for automatically generated xmlns tags
IMXMLTagData rootTag = mxmlData.getRootTag();
IMXMLTagAttributeData[] attributeDatas = rootTag.getAttributeDatas();
for (IMXMLTagAttributeData attributeData : attributeDatas) {
if (!attributeData.getName().startsWith("xmlns")) {
if (namespaceStartIndex == -1) {
namespaceStartIndex = attributeData.getStart();
namespaceEndIndex = namespaceStartIndex;
}
break;
}
int start = attributeData.getAbsoluteStart();
int end = attributeData.getValueEnd() + 1;
if (brokenMXMLValueEnd) {
end--;
}
if (namespaceStartIndex == -1 || namespaceStartIndex > start) {
namespaceStartIndex = start;
}
if (namespaceEndIndex == -1 || namespaceEndIndex < end) {
namespaceEndIndex = end;
}
}
IMXMLUnitData unitData = mxmlData.findContainmentReferenceUnit(currentOffset);
IMXMLUnitData currentUnitData = unitData;
while (currentUnitData != null) {
if (currentUnitData instanceof IMXMLTagData) {
IMXMLTagData tagData = (IMXMLTagData) currentUnitData;
if (tagData.getXMLName().equals(tagData.getMXMLDialect().resolveScript()) && unitData instanceof IMXMLTextData) {
IMXMLTextData textUnitData = (IMXMLTextData) unitData;
if (textUnitData.getTextType() == IMXMLTextData.TextType.CDATA) {
importStartIndex = textUnitData.getCompilableTextStart();
importEndIndex = textUnitData.getCompilableTextEnd();
}
}
return tagData;
}
currentUnitData = currentUnitData.getParentUnitData();
}
return null;
}
use of org.apache.flex.compiler.internal.mxml.MXMLData in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method renameDefinition.
private WorkspaceEdit renameDefinition(IDefinition definition, String newName) {
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 new WorkspaceEdit(new HashMap<>());
}
WorkspaceEdit result = new WorkspaceEdit();
Map<String, List<TextEdit>> changes = new HashMap<>();
result.setChanges(changes);
if (definition.getContainingFilePath().endsWith(SWC_EXTENSION)) {
if (languageClient != null) {
MessageParams message = new MessageParams();
message.setType(MessageType.Info);
message.setMessage("You cannot rename an element defined in a SWC file.");
languageClient.showMessage(message);
}
return result;
}
if (definition instanceof IPackageDefinition) {
if (languageClient != null) {
MessageParams message = new MessageParams();
message.setType(MessageType.Info);
message.setMessage("You cannot rename a package.");
languageClient.showMessage(message);
}
return result;
}
IDefinition parentDefinition = definition.getParent();
if (parentDefinition != null && parentDefinition instanceof IPackageDefinition) {
if (languageClient != null) {
MessageParams message = new MessageParams();
message.setType(MessageType.Info);
message.setMessage("You cannot rename this element.");
languageClient.showMessage(message);
}
return result;
}
for (ICompilationUnit compilationUnit : compilationUnits) {
if (compilationUnit == null || compilationUnit instanceof SWCCompilationUnit) {
continue;
}
ArrayList<TextEdit> textEdits = new ArrayList<>();
if (compilationUnit.getAbsoluteFilename().endsWith(MXML_EXTENSION)) {
IMXMLDataManager mxmlDataManager = currentWorkspace.getMXMLDataManager();
MXMLData mxmlData = (MXMLData) mxmlDataManager.get(fileSpecGetter.getFileSpecification(compilationUnit.getAbsoluteFilename()));
IMXMLTagData rootTag = mxmlData.getRootTag();
if (rootTag != null) {
ArrayList<ISourceLocation> units = new ArrayList<>();
findMXMLUnits(mxmlData.getRootTag(), definition, units);
for (ISourceLocation otherUnit : units) {
TextEdit textEdit = new TextEdit();
textEdit.setNewText(newName);
Range range = LanguageServerUtils.getRangeFromSourceLocation(otherUnit);
if (range == null) {
continue;
}
textEdit.setRange(range);
textEdits.add(textEdit);
}
}
}
IASNode ast = null;
try {
ast = compilationUnit.getSyntaxTreeRequest().get().getAST();
} catch (Exception e) {
//safe to ignore
}
if (ast != null) {
ArrayList<IIdentifierNode> identifiers = new ArrayList<>();
findIdentifiers(ast, definition, identifiers);
for (IIdentifierNode identifierNode : identifiers) {
TextEdit textEdit = new TextEdit();
textEdit.setNewText(newName);
Range range = LanguageServerUtils.getRangeFromSourceLocation(identifierNode);
if (range == null) {
continue;
}
textEdit.setRange(range);
textEdits.add(textEdit);
}
}
if (textEdits.size() == 0) {
continue;
}
URI uri = Paths.get(compilationUnit.getAbsoluteFilename()).toUri();
changes.put(uri.toString(), textEdits);
}
return result;
}
use of org.apache.flex.compiler.internal.mxml.MXMLData in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method addMXMLTypeDefinitionAutoComplete.
private void addMXMLTypeDefinitionAutoComplete(ITypeDefinition definition, CompletionList result) {
IMXMLDataManager mxmlDataManager = currentWorkspace.getMXMLDataManager();
MXMLData mxmlData = (MXMLData) mxmlDataManager.get(fileSpecGetter.getFileSpecification(currentUnit.getAbsoluteFilename()));
MXMLNamespace discoveredNS = getMXMLNamespaceForTypeDefinition(definition, mxmlData);
addDefinitionAutoCompleteMXML(definition, discoveredNS.prefix, discoveredNS.uri, result);
}
use of org.apache.flex.compiler.internal.mxml.MXMLData in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method referencesForDefinition.
private void referencesForDefinition(IDefinition definition, List<Location> result) {
for (ICompilationUnit compilationUnit : compilationUnits) {
if (compilationUnit == null || compilationUnit instanceof SWCCompilationUnit) {
continue;
}
if (compilationUnit.getAbsoluteFilename().endsWith(MXML_EXTENSION)) {
IMXMLDataManager mxmlDataManager = currentWorkspace.getMXMLDataManager();
MXMLData mxmlData = (MXMLData) mxmlDataManager.get(fileSpecGetter.getFileSpecification(compilationUnit.getAbsoluteFilename()));
IMXMLTagData rootTag = mxmlData.getRootTag();
if (rootTag != null) {
ArrayList<ISourceLocation> units = new ArrayList<>();
findMXMLUnits(mxmlData.getRootTag(), definition, units);
for (ISourceLocation otherUnit : units) {
Location location = LanguageServerUtils.getLocationFromSourceLocation(otherUnit);
if (location == null) {
continue;
}
result.add(location);
}
}
}
IASNode ast;
try {
ast = compilationUnit.getSyntaxTreeRequest().get().getAST();
} catch (Exception e) {
continue;
}
ArrayList<IIdentifierNode> identifiers = new ArrayList<>();
findIdentifiers(ast, definition, identifiers);
for (IIdentifierNode otherNode : identifiers) {
Location location = LanguageServerUtils.getLocationFromSourceLocation(otherNode);
if (location == null) {
continue;
}
result.add(location);
}
}
}
use of org.apache.flex.compiler.internal.mxml.MXMLData 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