use of org.apache.flex.compiler.units.ICompilationUnit in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method mxmlReferences.
private CompletableFuture<List<? extends Location>> mxmlReferences(ReferenceParams params, IMXMLTagData offsetTag) {
IDefinition definition = getDefinitionForMXMLNameAtOffset(offsetTag, currentOffset);
if (definition != null) {
if (isInsideTagPrefix(offsetTag, currentOffset)) {
//ignore the tag's prefix
return CompletableFuture.completedFuture(Collections.emptyList());
}
ArrayList<Location> result = new ArrayList<>();
referencesForDefinition(definition, result);
return CompletableFuture.completedFuture(result);
}
//finally, check if we're looking for references to a tag's id
IMXMLTagAttributeData attributeData = getMXMLTagAttributeWithValueAtOffset(offsetTag, currentOffset);
if (attributeData == null || !attributeData.getName().equals(IMXMLLanguageConstants.ATTRIBUTE_ID)) {
//definition referenced at the current position.
return CompletableFuture.completedFuture(Collections.emptyList());
}
Path path = LanguageServerUtils.getPathFromLanguageServerURI(params.getTextDocument().getUri());
if (path == null) {
//this probably shouldn't happen, but check just to be safe
return CompletableFuture.completedFuture(Collections.emptyList());
}
ICompilationUnit unit = getCompilationUnit(path);
Collection<IDefinition> definitions = null;
try {
definitions = unit.getFileScopeRequest().get().getExternallyVisibleDefinitions();
} catch (Exception e) {
//safe to ignore
}
if (definitions == null || definitions.size() == 0) {
return CompletableFuture.completedFuture(Collections.emptyList());
}
IClassDefinition classDefinition = null;
for (IDefinition currentDefinition : definitions) {
if (currentDefinition instanceof IClassDefinition) {
classDefinition = (IClassDefinition) currentDefinition;
break;
}
}
if (classDefinition == null) {
//this probably shouldn't happen, but check just to be safe
return CompletableFuture.completedFuture(Collections.emptyList());
}
IASScope scope = classDefinition.getContainedScope();
for (IDefinition currentDefinition : scope.getAllLocalDefinitions()) {
if (currentDefinition.getBaseName().equals(attributeData.getRawValue())) {
definition = currentDefinition;
break;
}
}
if (definition == null) {
//definition referenced at the current position.
return CompletableFuture.completedFuture(Collections.emptyList());
}
ArrayList<Location> result = new ArrayList<>();
referencesForDefinition(definition, result);
return CompletableFuture.completedFuture(result);
}
use of org.apache.flex.compiler.units.ICompilationUnit in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method getOffsetNode.
private IASNode getOffsetNode(TextDocumentIdentifier textDocument, Position position) {
currentOffset = -1;
if (!textDocument.getUri().endsWith(MXML_EXTENSION)) {
//if we're in an <fx:Script> element, these will have been
//previously calculated, so don't clear them
importStartIndex = -1;
importEndIndex = -1;
}
Path path = LanguageServerUtils.getPathFromLanguageServerURI(textDocument.getUri());
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;
}
ICompilationUnit unit = getCompilationUnit(path);
if (unit == null) {
//should have been logged already
return null;
}
IASNode ast = null;
try {
ast = unit.getSyntaxTreeRequest().get().getAST();
} catch (InterruptedException e) {
System.err.println("Interrupted while getting AST: " + path.toAbsolutePath().toString());
return null;
}
if (ast == null) {
//we couldn't find the root node for this file
System.err.println("Could not find AST: " + path.toAbsolutePath().toString());
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;
}
IASNode offsetNode = getContainingNodeIncludingStart(ast, currentOffset);
if (offsetNode != null) {
//if we have an offset node, try to find where imports may be added
IPackageNode packageNode = (IPackageNode) offsetNode.getAncestorOfType(IPackageNode.class);
if (packageNode == null) {
IFileNode fileNode = (IFileNode) offsetNode.getAncestorOfType(IFileNode.class);
if (fileNode != null) {
boolean foundPackage = false;
for (int i = 0; i < fileNode.getChildCount(); i++) {
IASNode childNode = fileNode.getChild(i);
if (foundPackage) {
//this is the node following the package
importStartIndex = childNode.getAbsoluteStart();
break;
}
if (childNode instanceof IPackageNode) {
//use the start of the the next node after the
//package as the place where the import can be added
foundPackage = true;
}
}
}
} else {
importEndIndex = packageNode.getAbsoluteEnd();
}
}
return offsetNode;
}
use of org.apache.flex.compiler.units.ICompilationUnit in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method autoCompleteImport.
private void autoCompleteImport(String importName, CompletionList result) {
List<CompletionItem> items = result.getItems();
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) {
String qualifiedName = definition.getQualifiedName();
if (qualifiedName.equals(definition.getBaseName())) {
//this definition is top-level. no import required.
continue;
}
if (qualifiedName.startsWith(importName)) {
int index = importName.lastIndexOf(".");
if (index != -1) {
qualifiedName = qualifiedName.substring(index + 1);
}
index = qualifiedName.indexOf(".");
if (index > 0) {
qualifiedName = qualifiedName.substring(0, index);
}
CompletionItem item = new CompletionItem();
item.setLabel(qualifiedName);
if (definition.getBaseName().equals(qualifiedName)) {
ITypeDefinition typeDefinition = (ITypeDefinition) definition;
if (typeDefinition instanceof IInterfaceDefinition) {
item.setKind(CompletionItemKind.Interface);
} else if (typeDefinition instanceof IClassDefinition) {
item.setKind(CompletionItemKind.Class);
} else {
item.setKind(CompletionItemKind.Text);
}
} else {
item.setKind(CompletionItemKind.Text);
}
if (!items.contains(item)) {
items.add(item);
}
}
}
}
}
}
use of org.apache.flex.compiler.units.ICompilationUnit 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.units.ICompilationUnit in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method autoCompleteDefinitions.
private void autoCompleteDefinitions(CompletionList result, boolean forMXML, boolean typesOnly, String packageName, IDefinition definitionToSkip) {
String skipQualifiedName = null;
if (definitionToSkip != null) {
skipQualifiedName = definitionToSkip.getQualifiedName();
}
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) {
boolean isType = definition instanceof ITypeDefinition;
if (!typesOnly || isType) {
if (packageName == null || definition.getPackageName().equals(packageName)) {
if (skipQualifiedName != null && skipQualifiedName.equals(definition.getQualifiedName())) {
continue;
}
if (isType) {
IMetaTag excludeClassMetaTag = definition.getMetaTagByName(IMetaAttributeConstants.ATTRIBUTE_EXCLUDECLASS);
if (excludeClassMetaTag != null) {
//skip types with [ExcludeClass] metadata
continue;
}
}
if (forMXML && isType) {
ITypeDefinition typeDefinition = (ITypeDefinition) definition;
addMXMLTypeDefinitionAutoComplete(typeDefinition, result);
} else {
addDefinitionAutoCompleteActionScript(definition, result);
}
}
}
}
}
if (packageName == null || packageName.equals("")) {
CompletionItem item = new CompletionItem();
item.setKind(CompletionItemKind.Class);
item.setLabel(IASKeywordConstants.VOID);
result.getItems().add(item);
}
}
Aggregations