use of org.apache.flex.compiler.tree.as.IFileNode 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.tree.as.IFileNode in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method getCompilationUnit.
private ICompilationUnit getCompilationUnit(Path path) {
String absolutePath = path.toAbsolutePath().toString();
currentUnit = null;
currentProject = getProject();
if (currentProject == null) {
return null;
}
currentWorkspace = currentProject.getWorkspace();
//we're going to start with the files passed into the compiler
String[] files = currentProjectOptions.files;
if (files != null) {
for (int i = files.length - 1; i >= 0; i--) {
String file = files[i];
//a previous file may have created a compilation unit for the
//current file, so use that, if available
ICompilationUnit existingUnit = findCompilationUnit(file);
if (existingUnit != null) {
if (file.equals(absolutePath)) {
currentUnit = existingUnit;
}
continue;
}
IInvisibleCompilationUnit unit = currentProject.createInvisibleCompilationUnit(file, fileSpecGetter);
if (unit == null) {
System.err.println("Could not create compilation unit for file: " + file);
continue;
}
invisibleUnits.add(unit);
if (file.equals(absolutePath)) {
currentUnit = unit;
}
}
}
compilationUnits = currentProject.getCompilationUnits();
//if we didn't find the unit already, search the complete set of units
if (currentUnit == null) {
//might already be created
for (ICompilationUnit unit : compilationUnits) {
if (unit != null && unit.getAbsoluteFilename().equals(absolutePath)) {
currentUnit = unit;
break;
}
}
}
//if we still haven't found it, create it manually
if (currentUnit == null) {
//if all else fails, create the compilation unit manually
IInvisibleCompilationUnit unit = currentProject.createInvisibleCompilationUnit(absolutePath, fileSpecGetter);
if (unit == null) {
System.err.println("Could not create compilation unit for file: " + absolutePath);
return null;
}
invisibleUnits.add(unit);
currentUnit = unit;
}
//for some reason, function nodes may not always be populated, but we
//can force them to be populated
IASNode ast = null;
try {
ast = currentUnit.getSyntaxTreeRequest().get().getAST();
} catch (InterruptedException e) {
System.err.println("Interrupted while getting AST");
}
if (ast instanceof IFileNode) {
IFileNode fileNode = (IFileNode) ast;
fileNode.populateFunctionNodes();
}
return currentUnit;
}
Aggregations